upstream version 1.2.2
[debian/freetts] / de / dfki / lt / freetts / de / GermanLexicon.java
1 /**
2  * Portions Copyright 2004 DFKI GmbH.
3  * Portions Copyright 2001 Sun Microsystems, Inc.
4  * Portions Copyright 1999-2001 Language Technologies Institute, 
5  * Carnegie Mellon University.
6  * All Rights Reserved.  Use is subject to license terms.
7  * 
8  * See the file "license.terms" for information on usage and
9  * redistribution of this file, and for a DISCLAIMER OF ALL 
10  * WARRANTIES.
11  */
12 package de.dfki.lt.freetts.de;
13
14 import java.io.IOException;
15 import java.net.URL;
16 import java.util.List;
17
18 import com.sun.speech.freetts.VoiceManager;
19 import com.sun.speech.freetts.lexicon.LexiconImpl;
20 import com.sun.speech.freetts.util.BulkTimer;
21
22 /**
23  * Provides a CMU lexicon-specific implementation of a Lexicon that is
24  * stored in a text file.
25  */
26 public class GermanLexicon extends LexiconImpl {
27     
28
29     /**
30      * Creates a GermanLexicon based upon the given compiled and addenda
31      * DBs and the given letter to sound rules
32      *
33      * @param compiledURL the compiled database is loaded from here
34      * @param addendaURL the database addenda is loaded from here
35      * @param letterToSoundURL the letter to sound rules are loaded
36      *          from here
37      * @param binary if <code>true</code> the input data are loaded as
38      *          binary ; otherwise if <code>false</code> the input
39      *          data are loaded as text.
40      *
41      */
42     public GermanLexicon(URL compiledURL,
43                        URL addendaURL,
44                        URL  letterToSoundURL,
45                        boolean binary) {
46         setLexiconParameters(compiledURL, addendaURL, letterToSoundURL, binary);
47     }
48
49     /**
50      * Creates the default CMU Lexicon which is a binary lexicon
51      */
52     public GermanLexicon() {
53         this("germanlex");
54     }
55
56     /**
57      * Creates the CMU Lexicon which is a binary lexicon
58      *
59      * @param basename the basename for the lexicon.
60      */
61     public GermanLexicon(String basename) {
62         this(basename, true);
63     }
64
65     public GermanLexicon(String basename, boolean useBinaryIO) {
66         java.net.URLClassLoader classLoader =
67                 VoiceManager.getVoiceClassLoader();
68         String type = (useBinaryIO ? "bin" : "txt");
69
70         URL letterToSoundURL = classLoader.getResource(
71                 "de/dfki/lt/freetts/de/" + basename + "_lts." + type);
72         URL compiledURL = classLoader.getResource(
73                 "de/dfki/lt/freetts/de/" + basename
74                 + "_compiled." + type);
75         URL addendaURL = classLoader.getResource(
76                 "de/dfki/lt/freetts/de/" + basename
77                 + "_addenda." + type);
78
79         /* Just another try with possibly a different class loader
80          * if the above didn't work.
81          */
82         if (letterToSoundURL == null) {
83             Class cls = GermanLexicon.class;
84             letterToSoundURL = cls.getResource(basename + "_lts." + type);
85             compiledURL = cls.getResource(basename + "_compiled." + type);
86             addendaURL = cls.getResource(basename + "_addenda." + type);
87             if (letterToSoundURL == null) {
88                 System.err.println(
89                     "GermanLexicon: Oh no!  Couldn't find lexicon data!");
90             }
91         }
92         
93         setLexiconParameters(compiledURL, addendaURL,
94                 letterToSoundURL, useBinaryIO);
95     }
96     
97     /**
98      * Get the GermanLexicon.
99      *
100      * @param useBinaryIO if true use binary IO to load DB
101      *
102      * @throws IOException if problems occurred while reading the data
103      */ 
104     static public GermanLexicon getInstance( boolean useBinaryIO) 
105                                                 throws IOException {
106         return getInstance("dummylex", useBinaryIO);
107     }
108
109     /**
110      * Get the GermanLexicon.
111      *
112      * @param useBinaryIO if true use binary IO to load DB
113      *
114      * @throws IOException if problems occurred while reading the data
115      */ 
116     static public GermanLexicon getInstance( String basename, boolean useBinaryIO) 
117                                                 throws IOException {
118         GermanLexicon lexicon = new GermanLexicon(basename, useBinaryIO);
119         lexicon.load();
120         return lexicon;
121     }
122
123     /**
124      * Determines if the currentPhone represents a new syllable
125      * boundary.
126      *
127      * @param syllablePhones the phones in the current syllable so far
128      * @param wordPhones the phones for the whole word
129      * @param currentWordPhone the word phone in question
130      *
131      * @return <code>true</code> if the word phone in question is on a
132      *     syllable boundary; otherwise <code>false</code>.
133      */
134     public boolean isSyllableBoundary(List syllablePhones,
135                                       String[] wordPhones,
136                                       int currentWordPhone) {
137         return false;
138     }
139
140     /**
141      * Provides test code for the GermanLexicon.
142      * <br><b>Usage:</b><br>
143      * <pre>
144      *  de.dfki.lt.freetts.de.GermanLexicon [options]
145      *
146      * Where options is any combination of:
147      *
148      * -src path
149      * -dest path
150      * -generate_binary [base_name]
151      * -compare
152      * -showtimes
153      *
154      * </pre>
155      */
156     public static void main(String[] args) {
157         LexiconImpl lex, lex2;
158         boolean showTimes = false;
159         String srcPath = ".";
160         String destPath = ".";
161         String baseName = "germanlex";
162
163         try {
164             if (args.length > 0) {
165                 BulkTimer.LOAD.start();
166                 for (int i = 0 ; i < args.length; i++) {
167                     if (args[i].equals("-src")) {
168                         srcPath = args[++i];
169                     } else if (args[i].equals("-dest")) {
170                         destPath = args[++i];
171                     } else if (args[i].equals("-name")
172                                && i < args.length - 1) {
173                         baseName = args[++i];
174                     } else if (args[i].equals("-generate_binary")) {
175
176                          System.out.println("Loading " + baseName);
177                          String path = "file:" + srcPath + "/" + baseName;
178                          lex = new GermanLexicon(
179                              new URL(path + "_compiled.txt"),
180                              new URL(path + "_addenda.txt"),
181                              new URL(path + "_lts.txt"),
182                              false);
183                          BulkTimer.LOAD.start("load_text");
184                          lex.load();
185                          BulkTimer.LOAD.stop("load_text");
186
187                          System.out.println("Dumping " + baseName);
188                          BulkTimer.LOAD.start("dump_text");
189                          lex.dumpBinary(destPath + "/" + baseName);
190                          BulkTimer.LOAD.stop("dump_text");
191
192                     } else if (args[i].equals("-compare")) {
193
194                         BulkTimer.LOAD.start("load_text");
195                         lex = GermanLexicon.getInstance(baseName, false);
196                         BulkTimer.LOAD.stop("load_text");
197
198                         BulkTimer.LOAD.start("load_binary");
199                         lex2 = GermanLexicon.getInstance(baseName, true);
200                         BulkTimer.LOAD.stop("load_binary");
201
202                         BulkTimer.LOAD.start("compare");
203                         lex.compare(lex2);
204                         BulkTimer.LOAD.stop("compare");
205                     } else if (args[i].equals("-showtimes")) {
206                         showTimes = true;
207                     } else {
208                         System.out.println("Unknown option " + args[i]);
209                     }
210                 }
211                 BulkTimer.LOAD.stop();
212                 if (showTimes) {
213                     BulkTimer.LOAD.show("GermanLexicon loading and dumping");
214                 }
215             } else {
216                 System.out.println("Options: ");
217                 System.out.println("    -src path");
218                 System.out.println("    -dest path");
219                 System.out.println("    -compare");
220                 System.out.println("    -generate_binary");
221                 System.out.println("    -showtimes");
222             }
223         } catch (IOException ioe) {
224             System.err.println(ioe);
225         }
226     }
227 }