upstream version 1.2.2
[debian/freetts] / com / sun / speech / freetts / cart / Phraser.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.cart;
12
13 import java.util.logging.Level;
14 import java.util.logging.Logger;
15
16 import com.sun.speech.freetts.Item;
17 import com.sun.speech.freetts.ProcessException;
18 import com.sun.speech.freetts.Relation;
19 import com.sun.speech.freetts.Utterance;
20 import com.sun.speech.freetts.UtteranceProcessor;
21
22 /**
23  * Creates a <code>Relation.PHRASE</code> relation, grouping
24  * <code>Relation.WORD</code> relations by breaks.
25  *
26  * @see Relation#PHRASE
27  * @see Relation#WORD
28  */
29 public class Phraser implements UtteranceProcessor {
30     /** Logger instance. */
31     private static final Logger LOGGER =
32         Logger.getLogger(UtteranceProcessor.class.getName());
33
34     /**
35      * The CART used for this Phrasing UtteranceProcessor.  It is
36      * passed into the constructor.
37      */
38     protected final CART cart;
39     
40     /**
41      * Creates a new Phrasing UtteranceProcessor with the given
42      * CART.  The phrasing CART is expected to return "BB" values
43      * for big breaks.
44      *
45      * @param cart a phrasing CART
46      */
47     public Phraser(CART cart) {
48         this.cart = cart;
49     }
50     
51     /**
52      * Creates a <code>Relation.PHRASE</code> relation, grouping
53      * <code>Relation.WORD</code> relations by breaks.
54      * Depends upon a phrasing CART that returns strings containing
55      * "BB" for big breaks.
56      *
57      * @param  utterance  the utterance to process
58      *
59      * @throws ProcessException if a problem is encountered during the
60      *         processing of the utterance
61      */
62     public void processUtterance(Utterance utterance) throws ProcessException {
63         Relation relation = utterance.createRelation(Relation.PHRASE);
64         Item p = null;
65         for (Item w = utterance.getRelation(Relation.WORD).getHead();
66                         w != null; w = w.getNext()) {
67             if (p == null) {
68                 p = relation.appendItem();
69                 p.getFeatures().setString("name","BB");
70             }
71             p.addDaughter(w);
72             String results = (String) cart.interpret(w);
73             
74             if (LOGGER.isLoggable(Level.FINER)) {
75                 LOGGER.finer("word: " + w + ", results: " + results);
76             }
77             if (results.equals("BB")) {
78                 p = null;
79             }
80         }
81     }
82
83     // inherited from Object
84     public String toString() {
85         return "CARTPhraser";
86     }
87 }