upstream version 1.2.2
[debian/freetts] / com / sun / speech / freetts / en / PostLexicalAnalyzer.java
1 /**
2  * Portions Copyright 2001 Sun Microsystems, Inc.
3  * Portions Copyright 1999-2001 Language Technologies Institute, 
4  * Carnegie Mellon University.
5  * All Rights Reserved.  Use is subject to license terms.
6  * 
7  * See the file "license.terms" for information on usage and
8  * redistribution of this file, and for a DISCLAIMER OF ALL 
9  * WARRANTIES.
10  */
11 package com.sun.speech.freetts.en;
12
13 import com.sun.speech.freetts.UtteranceProcessor;
14 import com.sun.speech.freetts.Voice;
15 import com.sun.speech.freetts.Utterance;
16 import com.sun.speech.freetts.Item;
17 import com.sun.speech.freetts.Relation;
18 import com.sun.speech.freetts.ProcessException;
19 import com.sun.speech.freetts.PathExtractorImpl;
20 import com.sun.speech.freetts.PathExtractor;
21
22
23 /**
24  * Annotates the utterance with post lexical information.
25  */
26 public class PostLexicalAnalyzer implements UtteranceProcessor {
27     private static final PathExtractor wordPath =
28         new PathExtractorImpl("R:SylStructure.parent.parent.name", true);
29     private static final PathExtractor P_PH_VC =
30         new PathExtractorImpl("p.ph_vc", true);
31     private static final PathExtractor N_PH_VC =
32         new PathExtractorImpl("n.ph_vc", true);
33
34     /**
35      * Constructs a PostLexicalAnalyzer
36      */
37      public PostLexicalAnalyzer () {
38      }
39
40     /**
41      * Performs the post lexical processing.
42      *
43      * @param  utterance  the utterance to process
44      *
45      * @throws ProcessException if an error occurs while
46      *         processing of the utterance
47      */
48     public void processUtterance(Utterance utterance) throws ProcessException {
49         fixApostrophe(utterance);
50         fixTheIy(utterance);
51     }
52
53     /**
54      * Fixes apostrophe s segments.
55      *
56      * @param utterance the utterance to fix
57      */
58     private void fixApostrophe(Utterance utterance) {
59         Voice voice = utterance.getVoice();
60         for (Item item = utterance.getRelation(Relation.SEGMENT).getHead();
61                 item != null;
62                 item = item.getNext()) {
63             String word = wordPath.findFeature(item).toString();
64
65             if (word.equals("'s")) {
66
67                 String pname = item.getPrevious().toString();
68
69                 if (("fa".indexOf(
70                             voice.getPhoneFeature(pname,"ctype")) != -1) &&
71                     ("dbg".indexOf(
72                             voice.getPhoneFeature(pname, "cplace")) == -1)) {
73                     prependSchwa(item);
74                 } else  if (voice.getPhoneFeature(pname, "cvox").equals("-")) {
75                     item.getFeatures().setString("name", "s");
76                 }
77             } else if (word.equals("'ve") ||
78                        word.equals("'ll") || word.equals("'d")) {
79                 if ("-".equals(P_PH_VC.findFeature(item))) {
80                     prependSchwa(item);
81                 }
82             }
83         }
84     }
85
86
87     /**
88      * Prepends a schwa to the given item
89      * 
90      * @param item the item to prepend the schwa to.
91      */
92     private static void prependSchwa(Item item) {
93         Item schwa = item.prependItem(null);
94         schwa.getFeatures().setString("name", "ax");
95         item.getItemAs(
96             Relation.SYLLABLE_STRUCTURE).prependItem(schwa);
97     }
98
99
100     /**
101      * Changes the pronunciation of "the" from 'd ax'  to 'd iy' if
102      * the following word starts with a vowel. "The every" is a good
103      * example.
104      *
105      * @param  utterance  the utterance to process
106      */
107     private void fixTheIy(Utterance utterance) {
108         Voice voice = utterance.getVoice();
109         for (Item item = utterance.getRelation(Relation.SEGMENT).getHead();
110                 item != null; item = item.getNext()) {
111
112             if ("ax".equals(item.toString())) {
113                 String word = wordPath.findFeature(item).toString();
114                 if ("the".equals(word) &&
115                         ("+".equals(N_PH_VC.findFeature(item)))) {
116                     item.getFeatures().setString("name", "iy");
117                 }
118             }
119         }
120     }
121
122     /**
123      * Returns the string representation of the object
124      *
125      * @return the string representation of the object
126      */
127     public String toString() {
128         return "PostLexicalAnalyzer";
129     }
130 }
131