001/**
002 * Portions Copyright 2001 Sun Microsystems, Inc.
003 * Portions Copyright 1999-2001 Language Technologies Institute, 
004 * Carnegie Mellon University.
005 * All Rights Reserved.  Use is subject to license terms.
006 * 
007 * See the file "license.terms" for information on usage and
008 * redistribution of this file, and for a DISCLAIMER OF ALL 
009 * WARRANTIES.
010 */
011package com.sun.speech.freetts.en.us;
012
013import com.sun.speech.freetts.FeatureSet;
014import com.sun.speech.freetts.FeatureSetImpl;
015import com.sun.speech.freetts.Item;
016import com.sun.speech.freetts.Relation;
017import com.sun.speech.freetts.Utterance;
018
019/**
020 * Helper class to add words and breaks into a Relation object.
021 */
022public class WordRelation {
023
024    private Relation relation;
025    private TokenToWords tokenToWords;
026
027
028    private WordRelation(Relation parentRelation, TokenToWords tokenToWords) {
029        this.relation = parentRelation;
030        this.tokenToWords = tokenToWords;
031    }
032
033
034    /**
035     * Creates a WordRelation object with the given utterance and 
036     * TokenToWords.
037     *
038     * @param utterance the Utterance from which to create a Relation
039     * @param tokenToWords the TokenToWords object to use
040     *
041     * @return a WordRelation object
042     */
043    public static WordRelation createWordRelation(Utterance utterance,
044                                                  TokenToWords tokenToWords) {
045        Relation relation = utterance.createRelation(Relation.WORD);
046        return new WordRelation(relation, tokenToWords);
047    }
048
049
050    /**
051     * Adds a break as a feature to the last item in the list.
052     */
053    public void addBreak() {
054        Item wordItem = (Item) relation.getTail();
055        if (wordItem != null) {
056            FeatureSet featureSet = wordItem.getFeatures();
057            featureSet.setString("break", "1");
058        }
059    }
060
061
062    /**
063     * Adds a word as an Item to this WordRelation object.
064     *
065     * @param word the word to add
066     */
067    public void addWord(String word) {
068        Item tokenItem = tokenToWords.getTokenItem();
069        Item wordItem = tokenItem.createDaughter();
070        FeatureSet featureSet = wordItem.getFeatures();
071        featureSet.setString("name", word);
072        relation.appendItem(wordItem);
073    }
074
075
076    /**
077     * Sets the last Item in this WordRelation to the given word.
078     *
079     * @param word the word to set
080     */
081    public void setLastWord(String word) {
082        Item lastItem = relation.getTail();
083        FeatureSet featureSet = lastItem.getFeatures();
084        featureSet.setString("name", word);
085    }
086
087
088    /**
089     * Returns the last item in this WordRelation.
090     *
091     * @return the last item
092     */
093    public Item getTail() {
094        return relation.getTail();
095    }
096}