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; 012 013import java.io.BufferedReader; 014import java.io.IOException; 015import java.io.InputStreamReader; 016import java.net.URL; 017import java.util.Map; 018import java.util.StringTokenizer; 019 020/** 021 * Maintains set of PhoneDuration instances read in from a file. The 022 * format of the file is as follows: 023 * 024 * <pre> 025 * phone mean stddev 026 * phone mean stddev 027 * phone mean stddev 028 * ... 029 * </pre> 030 * 031 * Where <code>phone</code> is the phone name, <code>mean</code> is 032 * a <code>float</code> representing the mean duration of the phone 033 * (typically in seconds), and <code>stddev</code> is a 034 * <code>float</code> representing the standard deviation from the 035 * mean. 036 */ 037public class PhoneDurationsImpl implements PhoneDurations { 038 /** 039 * The set of PhoneDuration instances indexed by phone. 040 */ 041 private Map<String, PhoneDuration> phoneDurations; 042 043 /** 044 * Creates a new PhoneDurationsImpl by reading from the given URL. 045 * 046 * @param url the input source 047 * 048 * @throws IOException if an error occurs 049 */ 050 public PhoneDurationsImpl(URL url) throws IOException { 051 BufferedReader reader; 052 String line; 053 054 phoneDurations = new java.util.HashMap<String, PhoneDuration>(); 055 reader = new BufferedReader(new 056 InputStreamReader(url.openStream())); 057 line = reader.readLine(); 058 while (line != null) { 059 if (!line.startsWith("***")) { 060 parseAndAdd(line); 061 } 062 line = reader.readLine(); 063 } 064 reader.close(); 065 } 066 067 /** 068 * Creates a word from the given input line and adds it to the 069 * map. 070 * 071 * @param line the input line 072 */ 073 private void parseAndAdd(String line) { 074 StringTokenizer tokenizer = new StringTokenizer(line," "); 075 String phone = tokenizer.nextToken(); 076 float mean = Float.parseFloat(tokenizer.nextToken()); 077 float stddev = Float.parseFloat(tokenizer.nextToken()); 078 phoneDurations.put(phone, new PhoneDuration(mean,stddev)); 079 } 080 081 /** 082 * Gets the <code>PhoneDuration</code> for the given phone. If no 083 * duration is applicable, returns <code>null</code>. 084 * 085 * @param phone the phone 086 * 087 * @return the <code>PhoneDuration</code> for <code>phone</code> 088 */ 089 public PhoneDuration getPhoneDuration(String phone) { 090 return phoneDurations.get(phone); 091 } 092}