upstream version 1.2.2
[debian/freetts] / com / sun / speech / freetts / PartOfSpeechImpl.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>PartOfSpeech</code> that reads the info
24  * from a file.  The format of the file is as follows:
25  *
26  * <pre>
27  * word pos
28  * word pos
29  * word pos
30  * ...
31  * </pre>
32  *
33  * Where <code>word</code> is the word and <code>pos</code> is the
34  * part of speech for the word.  The part of speech is implementation
35  * dependent.
36  */
37 public class PartOfSpeechImpl implements PartOfSpeech {
38     /**
39      * Used for informational purposes if there's a bad line in the
40      * file.
41      */ 
42     private int lineCount = 0;
43
44     /**
45      * A map from words to their part of speech.
46      */
47     private Map partOfSpeechMap;
48
49     /**
50      * Default part of speech.
51      */
52     private String defaultPartOfSpeech;
53
54     /**
55      * Creates a new PartOfSpeechImpl by reading from the given URL.
56      *
57      * @param url the input source
58      * @param defaultPartOfSpeech the default part of speech
59      *
60      * @throws IOException if an error occurs
61      */ 
62     public PartOfSpeechImpl(URL url, String defaultPartOfSpeech) 
63         throws IOException {
64         
65         BufferedReader reader;
66         String line;
67
68         partOfSpeechMap = new HashMap();
69         this.defaultPartOfSpeech = defaultPartOfSpeech;
70         reader = new BufferedReader(new
71                 InputStreamReader(url.openStream()));
72         line = reader.readLine();
73         lineCount++;
74         while (line != null) {
75             if (!line.startsWith("***")) {
76                 parseAndAdd(line);
77             }
78             line = reader.readLine();
79         }
80         reader.close();
81     }
82     
83     /**
84      * Creates a word from the given input line and adds it to the map.
85      *
86      * @param line the input line
87      */
88     private void parseAndAdd(String line) {
89         StringTokenizer tokenizer = new StringTokenizer(line," ");
90         try {
91             String word = tokenizer.nextToken();
92             String pos = tokenizer.nextToken();        
93             partOfSpeechMap.put(word, pos);
94         } catch (NoSuchElementException nse) {
95             System.err.println("part of speech data in bad format at line " 
96             + lineCount);
97         }
98     }
99
100     /**
101      * Returns a description of the part of speech given a word.
102      * If the given word cannot be found, the part of speech will be the
103      * <code>defaultPartOfSpeech</code> parameter passed to the constructor.
104      *
105      * @param word the word to classify
106      *
107      * @return an implementation dependent part of speech for the word
108      */
109     public String getPartOfSpeech(String word) {
110         String pos = (String) partOfSpeechMap.get(word);
111         if (pos == null) {
112             pos = defaultPartOfSpeech;
113         }
114         return pos;
115     }
116 }