upstream version 1.2.2
[debian/freetts] / com / sun / speech / freetts / PhoneSetImpl.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;
12
13 import java.io.BufferedReader;
14 import java.io.IOException;
15 import java.io.InputStreamReader;
16 import java.net.URL;
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.NoSuchElementException;
20 import java.util.StringTokenizer;
21
22 /**
23  * Implementation of a <code>PhoneSet</code> that reads the info from
24  * a file.  The format of the file is as follows:
25  *
26  * <pre>
27  * phone feature value
28  * phone feature value
29  * phone feature value
30  * ...
31  * </pre>
32  *
33  * Where <code>phone</code> is the phone name, <code>feature</code> is
34  * the phone feature such as "vc," "vlng," "vheight," and so on, and
35  * "value" is the value of the feature.  There can be multiple lines
36  * for the same phone to describe various features of that phone.
37  */
38 public class PhoneSetImpl implements PhoneSet {
39     /**
40      * Used for informational purposes if there's a bad line in the
41      * file.
42      */ 
43     private int lineCount = 0;
44
45     /**
46      * The set of phone features indexed by phone.
47      */    
48     private Map phonesetMap;
49
50     /**
51      * Create a new <code>PhoneSetImpl</code> by reading from the
52      * given URL.
53      *
54      * @param url the input source
55      *
56      * @throws IOException if an error occurs
57      */ 
58     public PhoneSetImpl(URL url) throws IOException {
59         BufferedReader reader;
60         String line;
61
62         phonesetMap = new HashMap();
63         reader = new BufferedReader(new
64                 InputStreamReader(url.openStream()));
65         line = reader.readLine();
66         lineCount++;
67         while (line != null) {
68             if (!line.startsWith("***")) {
69                 parseAndAdd(line);
70             }
71             line = reader.readLine();
72         }
73         reader.close();
74     }
75     
76     /**
77      * Creates a word from the given input line and add it to the map.
78      *
79      * @param line the input line
80      */
81     private void parseAndAdd(String line) {
82         StringTokenizer tokenizer = new StringTokenizer(line," ");
83         try {
84             String phoneme = tokenizer.nextToken();
85             String feature = tokenizer.nextToken();        
86             String value = tokenizer.nextToken();        
87             phonesetMap.put(getKey(phoneme, feature), value);
88         } catch (NoSuchElementException nse) {
89             throw new Error("part of speech data in bad format at line " 
90             + lineCount);
91         }
92     }
93
94     /**
95      * Given a phoneme and a feature, returns the key that
96      * will obtain the value.
97      *
98      * @param phoneme the phoneme
99      * @param feature the name of the feature
100      *
101      * @return the key used to obtain the value
102      */
103     private String getKey(String phoneme, String feature) {
104         return phoneme + feature;
105     }
106
107     /**
108      * Given a phoneme and a feature name, returns the feature.
109      *
110      * @param phone the phoneme of interest
111      * @param featureName the name of the feature of interest
112      *
113      * @return the feature with the given name
114      */
115     public String getPhoneFeature(String phone, String featureName) {
116         return (String) phonesetMap.get(getKey(phone, featureName));
117     }
118 }