001/**
002 * Portions Copyright 2004 DFKI GmbH.
003 * Portions Copyright 2001 Sun Microsystems, Inc.
004 * Portions Copyright 1999-2001 Language Technologies Institute, 
005 * Carnegie Mellon University.
006 * All Rights Reserved.  Use is subject to license terms.
007 * 
008 * See the file "license.terms" for information on usage and
009 * redistribution of this file, and for a DISCLAIMER OF ALL 
010 * WARRANTIES.
011 */
012package de.dfki.lt.freetts.de;
013
014import java.io.IOException;
015import java.net.URL;
016import java.util.List;
017
018import com.sun.speech.freetts.VoiceManager;
019import com.sun.speech.freetts.lexicon.LexiconImpl;
020import com.sun.speech.freetts.util.BulkTimer;
021
022/**
023 * Provides a CMU lexicon-specific implementation of a Lexicon that is
024 * stored in a text file.
025 */
026public class GermanLexicon extends LexiconImpl {
027    
028
029    /**
030     * Creates a GermanLexicon based upon the given compiled and addenda
031     * DBs and the given letter to sound rules
032     *
033     * @param compiledURL the compiled database is loaded from here
034     * @param addendaURL the database addenda is loaded from here
035     * @param letterToSoundURL the letter to sound rules are loaded
036     *          from here
037     * @param binary if <code>true</code> the input data are loaded as
038     *          binary ; otherwise if <code>false</code> the input
039     *          data are loaded as text.
040     *
041     */
042    public GermanLexicon(URL compiledURL,
043                       URL addendaURL,
044                       URL  letterToSoundURL,
045                       boolean binary) {
046        setLexiconParameters(compiledURL, addendaURL, letterToSoundURL, binary);
047    }
048
049    /**
050     * Creates the default CMU Lexicon which is a binary lexicon
051     */
052    public GermanLexicon() {
053        this("germanlex");
054    }
055
056    /**
057     * Creates the CMU Lexicon which is a binary lexicon
058     *
059     * @param basename the basename for the lexicon.
060     */
061    public GermanLexicon(String basename) {
062        this(basename, true);
063    }
064
065    public GermanLexicon(String basename, boolean useBinaryIO) {
066        java.net.URLClassLoader classLoader =
067                VoiceManager.getVoiceClassLoader();
068        String type = (useBinaryIO ? "bin" : "txt");
069
070        URL letterToSoundURL = classLoader.getResource(
071                "de/dfki/lt/freetts/de/" + basename + "_lts." + type);
072        URL compiledURL = classLoader.getResource(
073                "de/dfki/lt/freetts/de/" + basename
074                + "_compiled." + type);
075        URL addendaURL = classLoader.getResource(
076                "de/dfki/lt/freetts/de/" + basename
077                + "_addenda." + type);
078
079        /* Just another try with possibly a different class loader
080         * if the above didn't work.
081         */
082        if (letterToSoundURL == null) {
083            Class cls = GermanLexicon.class;
084            letterToSoundURL = cls.getResource(basename + "_lts." + type);
085            compiledURL = cls.getResource(basename + "_compiled." + type);
086            addendaURL = cls.getResource(basename + "_addenda." + type);
087            if (letterToSoundURL == null) {
088                System.err.println(
089                    "GermanLexicon: Oh no!  Couldn't find lexicon data!");
090            }
091        }
092        
093        setLexiconParameters(compiledURL, addendaURL,
094                letterToSoundURL, useBinaryIO);
095    }
096    
097    /**
098     * Get the GermanLexicon.
099     *
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}