upstream version 1.2.2
[debian/freetts] / com / sun / speech / freetts / PhoneDurationsImpl.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.StringTokenizer;
19
20 /**
21  * Maintains set of PhoneDuration instances read in from a file.  The
22  * format of the file is as follows:
23  *
24  * <pre>
25  * phone mean stddev
26  * phone mean stddev
27  * phone mean stddev
28  * ...
29  * </pre>
30  *
31  * Where <code>phone</code> is the phone name, <code>mean</code> is
32  * a <code>float</code> representing the mean duration of the phone
33  * (typically in seconds), and <code>stddev</code> is a
34  * <code>float</code> representing the standard deviation from the
35  * mean.
36  */
37 public class PhoneDurationsImpl implements PhoneDurations {
38     /**
39      * The set of PhoneDuration instances indexed by phone.
40      */
41     private HashMap phoneDurations;
42     
43     /**
44      * Creates a new PhoneDurationsImpl by reading from the given URL.
45      *
46      * @param url the input source
47      *
48      * @throws IOException if an error occurs
49      */ 
50     public PhoneDurationsImpl(URL url) throws IOException {
51         BufferedReader reader;
52         String line;
53
54         phoneDurations = new HashMap();
55         reader = new BufferedReader(new
56                 InputStreamReader(url.openStream()));
57         line = reader.readLine();
58         while (line != null) {
59             if (!line.startsWith("***")) {
60                 parseAndAdd(line);
61             }
62             line = reader.readLine();
63         }
64         reader.close();
65     }
66     
67     /**
68      * Creates a word from the given input line and adds it to the
69      * map.
70      *
71      * @param line the input line
72      */
73     private void parseAndAdd(String line) {
74         StringTokenizer tokenizer = new StringTokenizer(line," ");
75         String phone = tokenizer.nextToken();
76         float mean = Float.parseFloat(tokenizer.nextToken());        
77         float stddev = Float.parseFloat(tokenizer.nextToken());        
78         phoneDurations.put(phone, new PhoneDuration(mean,stddev));
79     }
80
81     /**
82      * Gets the <code>PhoneDuration</code> for the given phone.  If no
83      * duration is applicable, returns <code>null</code>.
84      *
85      * @param phone the phone
86      *
87      * @return the <code>PhoneDuration</code> for <code>phone</code>
88      */
89     public PhoneDuration getPhoneDuration(String phone) {
90         return (PhoneDuration) phoneDurations.get(phone);
91     }
92 }