]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/engine/DictionaryBuilder.java
Unit tests working again after refactoring!!!
[DictionaryPC.git] / src / com / hughes / android / dictionary / engine / DictionaryBuilder.java
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package com.hughes.android.dictionary.engine;
16
17 import java.io.File;
18 import java.io.IOException;
19 import java.io.PrintStream;
20 import java.io.RandomAccessFile;
21 import java.nio.charset.Charset;
22 import java.util.ArrayList;
23 import java.util.LinkedHashSet;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.regex.Pattern;
28
29 import javax.xml.parsers.ParserConfigurationException;
30
31 import org.xml.sax.SAXException;
32
33 import com.hughes.android.dictionary.parser.DictFileParser;
34 import com.hughes.android.dictionary.parser.Parser;
35 import com.hughes.android.dictionary.parser.wiktionary.EnForeignParser;
36 import com.hughes.android.dictionary.parser.wiktionary.EnToTranslationParser;
37 import com.hughes.util.Args;
38 import com.hughes.util.FileUtil;
39
40 public class DictionaryBuilder {
41   
42   public final Dictionary dictionary;
43   public final List<IndexBuilder> indexBuilders = new ArrayList<IndexBuilder>();
44   
45   public DictionaryBuilder(final String dictInfoString, final Language lang0, final Language lang1, final String normalizerRules1, final String normalizerRules2, final Set<String> lang1Stoplist, final Set<String> lang2Stoplist) {
46     dictionary = new Dictionary(dictInfoString);
47     indexBuilders.add(new IndexBuilder(this, lang0.getIsoCode(), lang0.getIsoCode() + "->" + lang1.getIsoCode(), lang0, normalizerRules1, lang1Stoplist, false));
48     indexBuilders.add(new IndexBuilder(this, lang1.getIsoCode(), lang1.getIsoCode() + "->" + lang0.getIsoCode(), lang1, normalizerRules2, lang2Stoplist, true));
49   }
50   
51   void build() {
52     for (final IndexBuilder indexBuilder : indexBuilders) {
53       indexBuilder.build();
54       dictionary.indices.add(indexBuilder.index);
55     }
56   }
57   
58   public static void main(final String[] args) throws IOException, ParserConfigurationException, SAXException {
59     System.out.println("Running with arguments:");
60     for (final String arg : args) {
61       System.out.println(arg);
62     }
63     
64     final Map<String,String> keyValueArgs = Args.keyValueArgs(args);
65     
66     if (!keyValueArgs.containsKey("lang1") || !keyValueArgs.containsKey("lang2")) {
67       fatalError("--lang1= and --lang2= must both be specified.");
68     }
69     final Language lang1 = Language.lookup(keyValueArgs.remove("lang1"));
70     final Language lang2 = Language.lookup(keyValueArgs.remove("lang2"));
71
72     final Set<String> lang1Stoplist = new LinkedHashSet<String>();
73     final Set<String> lang2Stoplist = new LinkedHashSet<String>();
74     final String lang1StoplistFile = keyValueArgs.remove("lang1Stoplist");
75     final String lang2StoplistFile = keyValueArgs.remove("lang2Stoplist");
76     if (lang1StoplistFile != null) {
77       lang1Stoplist.addAll(FileUtil.readLines(new File(lang1StoplistFile)));
78     }
79     if (lang2StoplistFile != null) {
80       lang2Stoplist.addAll(FileUtil.readLines(new File(lang2StoplistFile)));
81     }
82
83     String normalizerRules1 = keyValueArgs.remove("normalizerRules1");
84     String normalizerRules2 = keyValueArgs.remove("normalizerRules2");
85     if (normalizerRules1 == null) {
86       normalizerRules1 = lang1.getDefaultNormalizerRules();
87     }
88     if (normalizerRules2 == null) {
89       normalizerRules2 = lang2.getDefaultNormalizerRules();
90     }
91     
92     final String dictOutFilename = keyValueArgs.remove("dictOut");
93     if (dictOutFilename == null) {
94       fatalError("--dictOut= must be specified.");
95     }
96     
97     String dictInfo = keyValueArgs.remove("dictInfo");
98     if (dictInfo == null) {
99       fatalError("--dictInfo= must be specified.");
100     }
101     if (dictInfo.startsWith("@")) {
102       dictInfo = FileUtil.readToString(new File(dictInfo.substring(1)));
103     }
104     
105     final String printFile = keyValueArgs.remove("print");
106     
107     System.out.println("lang1=" + lang1);
108     System.out.println("lang2=" + lang2);
109     System.out.println("normalizerRules1=" + normalizerRules1);
110     System.out.println("normalizerRules2=" + normalizerRules2);
111     System.out.println("dictInfo=" + dictInfo);
112     System.out.println("dictOut=" + dictOutFilename);    
113     
114     final DictionaryBuilder dictionaryBuilder = new DictionaryBuilder(dictInfo, lang1, lang2, normalizerRules1, normalizerRules2, lang1Stoplist, lang2Stoplist);
115     
116     for (int i = 0; i < 100; ++i) {
117       final String prefix = "input" + i;
118       if (keyValueArgs.containsKey(prefix)) {
119         final File file = new File(keyValueArgs.remove(prefix));
120         System.out.println("Processing: " + file);
121         String charsetName = keyValueArgs.remove(prefix + "Charset");
122         if (charsetName == null) {
123           charsetName = "UTF8";
124         }
125         final Charset charset = Charset.forName(charsetName);
126         String inputName = keyValueArgs.remove(prefix + "Name");
127         if (inputName == null) {
128           fatalError("Must specify human readable name for: " + prefix + "Name");
129         }
130         String pageLimitString = keyValueArgs.remove(prefix + "PageLimit");
131         if (pageLimitString == null) {
132           pageLimitString = "-1";
133         }
134         final int pageLimit = Integer.parseInt(pageLimitString);
135
136         final EntrySource entrySource = new EntrySource(dictionaryBuilder.dictionary.sources.size(), inputName, 0);
137         System.out.println("");
138         
139         String inputFormat = keyValueArgs.remove(prefix + "Format");
140         if ("tab_separated".equals(inputFormat)) {
141           final boolean flipColumns = "true".equals(keyValueArgs.remove(prefix + "FlipColumns"));
142           new DictFileParser(charset, flipColumns, DictFileParser.TAB, null, dictionaryBuilder, dictionaryBuilder.indexBuilders.toArray(new IndexBuilder[0]), null).parse(file, entrySource, pageLimit);
143         } else if ("chemnitz".equals(inputFormat)) {
144           final boolean flipColumns = "true".equals(keyValueArgs.remove(prefix + "FlipColumns"));
145           new DictFileParser(charset, flipColumns, DictFileParser.DOUBLE_COLON, DictFileParser.PIPE, dictionaryBuilder, dictionaryBuilder.indexBuilders.toArray(new IndexBuilder[0]), null).parse(file, entrySource, pageLimit);
146         } else if ("enwiktionary".equals(inputFormat)) {
147           final String type = keyValueArgs.remove(prefix + "WiktionaryType");
148           final Pattern langPattern = Pattern.compile(keyValueArgs.remove(prefix + "LangPattern"), Pattern.CASE_INSENSITIVE);
149           final Pattern langCodePattern = Pattern.compile(keyValueArgs.remove(prefix + "LangCodePattern"));
150           final int enIndex = Integer.parseInt(keyValueArgs.remove(prefix + "EnIndex")) - 1;
151             
152           if (enIndex < 0 || enIndex >= 2) {
153             fatalError("Must be 1 or 2: " + prefix + "EnIndex");
154           }
155           final Parser parser;
156           if ("EnToTranslation".equals(type)) {
157             parser = new EnToTranslationParser(dictionaryBuilder.indexBuilders.get(enIndex), dictionaryBuilder.indexBuilders.get(1-enIndex),
158                 langPattern, langCodePattern, enIndex != 0);
159           } else if ("EnForeign".equals(type)) {
160             parser = new EnForeignParser(dictionaryBuilder.indexBuilders.get(enIndex), dictionaryBuilder.indexBuilders.get(1-enIndex),
161                 langPattern, langCodePattern, enIndex != 0);
162           } else {
163             fatalError("Invalid WiktionaryType (use EnToTranslation or EnForeign): " + type);
164             return;
165           }
166           parser.parse(file, entrySource, pageLimit);
167         } else {
168           fatalError("Invalid or missing input format: " + inputFormat);
169         }
170         
171         dictionaryBuilder.dictionary.sources.add(entrySource);
172         System.out.println("Done: " + file + "\n\n");
173       }
174     }
175    
176     dictionaryBuilder.build();
177     
178     if (printFile != null) {
179       final PrintStream out = new PrintStream(new File(printFile));
180       dictionaryBuilder.dictionary.print(out);
181       out.close();
182     }
183     
184     System.out.println("Writing dictionary to: " + dictOutFilename);
185     final RandomAccessFile dictOut = new RandomAccessFile(dictOutFilename, "rw");
186     dictOut.setLength(0);
187     dictionaryBuilder.dictionary.write(dictOut);
188     dictOut.close();
189     
190     if (!keyValueArgs.isEmpty()) {
191       System.err.println("WARNING: couldn't parse arguments: " + keyValueArgs);
192       System.exit(1);
193     }
194   
195   }
196   
197   private static void fatalError(String string) {
198     System.err.println(string);
199     
200     
201     System.exit(1);
202   }
203   
204 }