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.cart;
012
013import java.util.logging.Level;
014import java.util.logging.Logger;
015
016import com.sun.speech.freetts.Item;
017import com.sun.speech.freetts.ProcessException;
018import com.sun.speech.freetts.Relation;
019import com.sun.speech.freetts.Utterance;
020import com.sun.speech.freetts.UtteranceProcessor;
021
022/**
023 * Creates a <code>Relation.PHRASE</code> relation, grouping
024 * <code>Relation.WORD</code> relations by breaks.
025 *
026 * @see Relation#PHRASE
027 * @see Relation#WORD
028 */
029public class Phraser implements UtteranceProcessor {
030    /** Logger instance. */
031    private static final Logger LOGGER =
032        Logger.getLogger(UtteranceProcessor.class.getName());
033
034    /**
035     * The CART used for this Phrasing UtteranceProcessor.  It is
036     * passed into the constructor.
037     */
038    protected final CART cart;
039    
040    /**
041     * Creates a new Phrasing UtteranceProcessor with the given
042     * CART.  The phrasing CART is expected to return "BB" values
043     * for big breaks.
044     *
045     * @param cart a phrasing CART
046     */
047    public Phraser(CART cart) {
048        this.cart = cart;
049    }
050    
051    /**
052     * Creates a <code>Relation.PHRASE</code> relation, grouping
053     * <code>Relation.WORD</code> relations by breaks.
054     * Depends upon a phrasing CART that returns strings containing
055     * "BB" for big breaks.
056     *
057     * @param  utterance  the utterance to process
058     *
059     * @throws ProcessException if a problem is encountered during the
060     *         processing of the utterance
061     */
062    public void processUtterance(Utterance utterance) throws ProcessException {
063        Relation relation = utterance.createRelation(Relation.PHRASE);
064        Item p = null;
065        for (Item w = utterance.getRelation(Relation.WORD).getHead();
066                        w != null; w = w.getNext()) {
067            if (p == null) {
068                p = relation.appendItem();
069                p.getFeatures().setString("name","BB");
070            }
071            p.addDaughter(w);
072            String results = (String) cart.interpret(w);
073            
074            if (LOGGER.isLoggable(Level.FINER)) {
075                LOGGER.finer("word: " + w + ", results: " + results);
076            }
077            if (results.equals("BB")) {
078                p = null;
079            }
080        }
081    }
082
083    // inherited from Object
084    public String toString() {
085        return "CARTPhraser";
086    }
087}