upstream version 1.2.2
[debian/freetts] / com / sun / speech / freetts / en / PauseGenerator.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.Utterance;
15 import com.sun.speech.freetts.FeatureSet;
16 import com.sun.speech.freetts.Item;
17 import com.sun.speech.freetts.Relation;
18 import com.sun.speech.freetts.PathExtractorImpl;
19 import com.sun.speech.freetts.PathExtractor;
20 import com.sun.speech.freetts.ProcessException;
21 import com.sun.speech.freetts.Voice;
22 import java.io.BufferedInputStream;
23 import java.io.InputStream;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.util.List;
27 import java.util.LinkedList;
28 import java.util.Iterator;
29
30
31 /**
32  * Annotates an utterance with pause information.
33  */
34 public class PauseGenerator implements UtteranceProcessor {
35     private final static PathExtractor segmentPath = new
36       PathExtractorImpl("R:SylStructure.daughtern.daughtern.R:Segment", false);
37     private final static PathExtractor puncPath =
38       new PathExtractorImpl("R:Token.parent.punc", true);
39
40     /**
41      * Constructs a PauseGenerator
42      */
43      public PauseGenerator() {
44      }
45
46     /**
47      * Annotates an utterance with pause information.
48      *
49      * @param  utterance  the utterance to process
50      *
51      * @throws ProcessException if an error occurs while
52      *         processing of the utterance
53      */
54     public void processUtterance(Utterance utterance) throws ProcessException {
55         String silence = utterance.getVoice().getFeatures().
56                 getString(Voice.FEATURE_SILENCE);
57
58         Item phraseHead = utterance.getRelation(Relation.PHRASE).getHead();
59
60         // If there are not any phrases at all, then just skip
61         // the whole thing.
62
63         if (phraseHead == null) {
64             return;
65         }
66
67         // insert initial silence
68         Relation segment = utterance.getRelation(Relation.SEGMENT);
69         Item s = segment.getHead();
70         if (s == null) {
71             s = segment.appendItem(null);
72         } else {
73            s = s.prependItem(null);
74         }
75         s.getFeatures().setString("name", silence);
76
77         for (Item phrase = phraseHead;
78                     phrase != null;
79                     phrase = phrase.getNext()) {
80             Item word = phrase.getLastDaughter();
81             while (word != null) {
82                 Item seg = segmentPath.findItem(word);
83             // was this an explicit change or a lost bug fix
84             //if (seg != null && !"".equals(puncPath.findFeature(word))) {
85                 if (seg != null) {
86                     Item pause = seg.appendItem(null);
87                     pause.getFeatures().setString("name", silence);
88                     break;
89                 }
90                 word = word.getPrevious();
91             }
92         }
93     }
94
95     /**
96      * Returns the string representation of the object
97      *
98      * @return the string representation of the object
99      */
100     public String toString() {
101         return "PauseGenerator";
102     }
103 }