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.
7 * See the file "license.terms" for information on usage and
8 * redistribution of this file, and for a DISCLAIMER OF ALL
11 package com.sun.speech.freetts;
13 import java.io.BufferedReader;
14 import java.io.IOException;
15 import java.io.InputStreamReader;
17 import java.util.HashMap;
19 import java.util.NoSuchElementException;
20 import java.util.StringTokenizer;
23 * Implementation of a <code>PhoneSet</code> that reads the info from
24 * a file. The format of the file is as follows:
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.
38 public class PhoneSetImpl implements PhoneSet {
40 * Used for informational purposes if there's a bad line in the
43 private int lineCount = 0;
46 * The set of phone features indexed by phone.
48 private Map phonesetMap;
51 * Create a new <code>PhoneSetImpl</code> by reading from the
54 * @param url the input source
56 * @throws IOException if an error occurs
58 public PhoneSetImpl(URL url) throws IOException {
59 BufferedReader reader;
62 phonesetMap = new HashMap();
63 reader = new BufferedReader(new
64 InputStreamReader(url.openStream()));
65 line = reader.readLine();
67 while (line != null) {
68 if (!line.startsWith("***")) {
71 line = reader.readLine();
77 * Creates a word from the given input line and add it to the map.
79 * @param line the input line
81 private void parseAndAdd(String line) {
82 StringTokenizer tokenizer = new StringTokenizer(line," ");
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 "
95 * Given a phoneme and a feature, returns the key that
96 * will obtain the value.
98 * @param phoneme the phoneme
99 * @param feature the name of the feature
101 * @return the key used to obtain the value
103 private String getKey(String phoneme, String feature) {
104 return phoneme + feature;
108 * Given a phoneme and a feature name, returns the feature.
110 * @param phone the phoneme of interest
111 * @param featureName the name of the feature of interest
113 * @return the feature with the given name
115 public String getPhoneFeature(String phone, String featureName) {
116 return (String) phonesetMap.get(getKey(phone, featureName));