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;
012
013import com.sun.speech.freetts.UtteranceProcessor;
014import com.sun.speech.freetts.Utterance;
015import com.sun.speech.freetts.FeatureSet;
016import com.sun.speech.freetts.Item;
017import com.sun.speech.freetts.Relation;
018import com.sun.speech.freetts.PathExtractorImpl;
019import com.sun.speech.freetts.PathExtractor;
020import com.sun.speech.freetts.ProcessException;
021import com.sun.speech.freetts.Voice;
022import java.io.BufferedInputStream;
023import java.io.InputStream;
024import java.io.FileInputStream;
025import java.io.IOException;
026import java.util.List;
027import java.util.LinkedList;
028import java.util.Iterator;
029
030
031/**
032 * Annotates an utterance with pause information.
033 */
034public class PauseGenerator implements UtteranceProcessor {
035    private final static PathExtractor segmentPath = new
036      PathExtractorImpl("R:SylStructure.daughtern.daughtern.R:Segment", false);
037    private final static PathExtractor puncPath =
038      new PathExtractorImpl("R:Token.parent.punc", true);
039
040    /**
041     * Constructs a PauseGenerator
042     */
043     public PauseGenerator() {
044     }
045
046    /**
047     * Annotates an utterance with pause information.
048     *
049     * @param  utterance  the utterance to process
050     *
051     * @throws ProcessException if an error occurs while
052     *         processing of the utterance
053     */
054    public void processUtterance(Utterance utterance) throws ProcessException {
055        String silence = utterance.getVoice().getFeatures().
056                getString(Voice.FEATURE_SILENCE);
057
058        Item phraseHead = utterance.getRelation(Relation.PHRASE).getHead();
059
060        // If there are not any phrases at all, then just skip
061        // the whole thing.
062
063        if (phraseHead == null) {
064            return;
065        }
066
067        // insert initial silence
068        Relation segment = utterance.getRelation(Relation.SEGMENT);
069        Item s = segment.getHead();
070        if (s == null) {
071            s = segment.appendItem(null);
072        } else {
073           s = s.prependItem(null);
074        }
075        s.getFeatures().setString("name", silence);
076
077        for (Item phrase = phraseHead;
078                    phrase != null;
079                    phrase = phrase.getNext()) {
080            Item word = phrase.getLastDaughter();
081            while (word != null) {
082                Item seg = segmentPath.findItem(word);
083            // was this an explicit change or a lost bug fix
084            //if (seg != null && !"".equals(puncPath.findFeature(word))) {
085                if (seg != null) {
086                    Item pause = seg.appendItem(null);
087                    pause.getFeatures().setString("name", silence);
088                    break;
089                }
090                word = word.getPrevious();
091            }
092        }
093    }
094
095    /**
096     * Returns the string representation of the object
097     *
098     * @return the string representation of the object
099     */
100    public String toString() {
101        return "PauseGenerator";
102    }
103}