upstream version 1.2.2
[debian/freetts] / com / sun / speech / freetts / en / us / WordRelation.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.us;
12
13 import com.sun.speech.freetts.FeatureSet;
14 import com.sun.speech.freetts.FeatureSetImpl;
15 import com.sun.speech.freetts.Item;
16 import com.sun.speech.freetts.Relation;
17 import com.sun.speech.freetts.Utterance;
18
19 /**
20  * Helper class to add words and breaks into a Relation object.
21  */
22 public class WordRelation {
23
24     private Relation relation;
25     private TokenToWords tokenToWords;
26
27
28     private WordRelation(Relation parentRelation, TokenToWords tokenToWords) {
29         this.relation = parentRelation;
30         this.tokenToWords = tokenToWords;
31     }
32
33
34     /**
35      * Creates a WordRelation object with the given utterance and 
36      * TokenToWords.
37      *
38      * @param utterance the Utterance from which to create a Relation
39      * @param tokenToWords the TokenToWords object to use
40      *
41      * @return a WordRelation object
42      */
43     public static WordRelation createWordRelation(Utterance utterance,
44                                                   TokenToWords tokenToWords) {
45         Relation relation = utterance.createRelation(Relation.WORD);
46         return new WordRelation(relation, tokenToWords);
47     }
48
49
50     /**
51      * Adds a break as a feature to the last item in the list.
52      */
53     public void addBreak() {
54         Item wordItem = (Item) relation.getTail();
55         if (wordItem != null) {
56             FeatureSet featureSet = wordItem.getFeatures();
57             featureSet.setString("break", "1");
58         }
59     }
60
61
62     /**
63      * Adds a word as an Item to this WordRelation object.
64      *
65      * @param word the word to add
66      */
67     public void addWord(String word) {
68         Item tokenItem = tokenToWords.getTokenItem();
69         Item wordItem = tokenItem.createDaughter();
70         FeatureSet featureSet = wordItem.getFeatures();
71         featureSet.setString("name", word);
72         relation.appendItem(wordItem);
73     }
74
75
76     /**
77      * Sets the last Item in this WordRelation to the given word.
78      *
79      * @param word the word to set
80      */
81     public void setLastWord(String word) {
82         Item lastItem = relation.getTail();
83         FeatureSet featureSet = lastItem.getFeatures();
84         featureSet.setString("name", word);
85     }
86
87
88     /**
89      * Returns the last item in this WordRelation.
90      *
91      * @return the last item
92      */
93     public Item getTail() {
94         return relation.getTail();
95     }
96 }