1 // Copyright 2011 Google Inc. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 package com.hughes.android.dictionary.engine;
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;
27 import java.util.regex.Pattern;
29 import javax.xml.parsers.ParserConfigurationException;
31 import org.xml.sax.SAXException;
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.android.dictionary.parser.wiktionary.EnTranslationToTranslationParser;
38 import com.hughes.android.dictionary.parser.wiktionary.WholeSectionToHtmlParser;
39 import com.hughes.util.Args;
40 import com.hughes.util.FileUtil;
42 public class DictionaryBuilder {
44 public final Dictionary dictionary;
45 public final List<IndexBuilder> indexBuilders = new ArrayList<IndexBuilder>();
47 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) {
48 dictionary = new Dictionary(dictInfoString);
49 indexBuilders.add(new IndexBuilder(this, lang0.getIsoCode(), lang0.getIsoCode() + "->" + lang1.getIsoCode(), lang0, normalizerRules1, lang1Stoplist, false));
50 indexBuilders.add(new IndexBuilder(this, lang1.getIsoCode(), lang1.getIsoCode() + "->" + lang0.getIsoCode(), lang1, normalizerRules2, lang2Stoplist, true));
54 for (final IndexBuilder indexBuilder : indexBuilders) {
56 dictionary.indices.add(indexBuilder.index);
60 public static void main(final String[] args) throws IOException, ParserConfigurationException, SAXException {
61 System.out.println("Running with arguments:");
62 for (final String arg : args) {
63 System.out.println(arg);
66 final Map<String,String> keyValueArgs = Args.keyValueArgs(args);
68 if (!keyValueArgs.containsKey("lang1") || !keyValueArgs.containsKey("lang2")) {
69 fatalError("--lang1= and --lang2= must both be specified.");
71 final Language lang1 = Language.lookup(keyValueArgs.remove("lang1"));
72 final Language lang2 = Language.lookup(keyValueArgs.remove("lang2"));
74 final Set<String> lang1Stoplist = new LinkedHashSet<String>();
75 final Set<String> lang2Stoplist = new LinkedHashSet<String>();
76 final String lang1StoplistFile = keyValueArgs.remove("lang1Stoplist");
77 final String lang2StoplistFile = keyValueArgs.remove("lang2Stoplist");
78 if (lang1StoplistFile != null) {
79 lang1Stoplist.addAll(FileUtil.readLines(new File(lang1StoplistFile)));
81 if (lang2StoplistFile != null) {
82 lang2Stoplist.addAll(FileUtil.readLines(new File(lang2StoplistFile)));
85 String normalizerRules1 = keyValueArgs.remove("normalizerRules1");
86 String normalizerRules2 = keyValueArgs.remove("normalizerRules2");
87 if (normalizerRules1 == null) {
88 normalizerRules1 = lang1.getDefaultNormalizerRules();
90 if (normalizerRules2 == null) {
91 normalizerRules2 = lang2.getDefaultNormalizerRules();
94 final String dictOutFilename = keyValueArgs.remove("dictOut");
95 if (dictOutFilename == null) {
96 fatalError("--dictOut= must be specified.");
99 String dictInfo = keyValueArgs.remove("dictInfo");
100 if (dictInfo == null) {
101 fatalError("--dictInfo= must be specified.");
103 if (dictInfo.startsWith("@")) {
104 dictInfo = FileUtil.readToString(new File(dictInfo.substring(1)));
107 final String printFile = keyValueArgs.remove("print");
109 System.out.println("lang1=" + lang1);
110 System.out.println("lang2=" + lang2);
111 System.out.println("normalizerRules1=" + normalizerRules1);
112 System.out.println("normalizerRules2=" + normalizerRules2);
113 System.out.println("dictInfo=" + dictInfo);
114 System.out.println("dictOut=" + dictOutFilename);
116 final DictionaryBuilder dictionaryBuilder = new DictionaryBuilder(dictInfo, lang1, lang2, normalizerRules1, normalizerRules2, lang1Stoplist, lang2Stoplist);
118 for (int i = 0; i < 100; ++i) {
119 final String prefix = "input" + i;
120 if (keyValueArgs.containsKey(prefix)) {
121 final File file = new File(keyValueArgs.remove(prefix));
122 System.out.println("Processing: " + file);
123 String charsetName = keyValueArgs.remove(prefix + "Charset");
124 if (charsetName == null) {
125 charsetName = "UTF8";
127 final Charset charset = Charset.forName(charsetName);
128 String inputName = keyValueArgs.remove(prefix + "Name");
129 if (inputName == null) {
130 fatalError("Must specify human readable name for: " + prefix + "Name");
132 String pageLimitString = keyValueArgs.remove(prefix + "PageLimit");
133 if (pageLimitString == null) {
134 pageLimitString = "-1";
136 final int pageLimit = Integer.parseInt(pageLimitString);
138 final EntrySource entrySource = new EntrySource(dictionaryBuilder.dictionary.sources.size(), inputName, 0);
139 System.out.println("");
141 String inputFormat = keyValueArgs.remove(prefix + "Format");
142 if ("tab_separated".equals(inputFormat)) {
143 final boolean flipColumns = "true".equals(keyValueArgs.remove(prefix + "FlipColumns"));
144 new DictFileParser(charset, flipColumns, DictFileParser.TAB, null, dictionaryBuilder, dictionaryBuilder.indexBuilders.toArray(new IndexBuilder[0]), null).parse(file, entrySource, pageLimit);
145 } else if ("chemnitz".equals(inputFormat)) {
146 final boolean flipColumns = "true".equals(keyValueArgs.remove(prefix + "FlipColumns"));
147 new DictFileParser(charset, flipColumns, DictFileParser.DOUBLE_COLON, DictFileParser.PIPE, dictionaryBuilder, dictionaryBuilder.indexBuilders.toArray(new IndexBuilder[0]), null).parse(file, entrySource, pageLimit);
148 } else if ("enwiktionary".equals(inputFormat)) {
149 final String type = keyValueArgs.remove(prefix + "WiktionaryType");
150 final Pattern langPattern = Pattern.compile(keyValueArgs.remove(prefix + "LangPattern"), Pattern.CASE_INSENSITIVE);
151 final Pattern langCodePattern = Pattern.compile(keyValueArgs.remove(prefix + "LangCodePattern"));
152 final int enIndex = Integer.parseInt(keyValueArgs.remove(prefix + "EnIndex")) - 1;
154 if (enIndex < 0 || enIndex >= 2) {
155 fatalError("Must be 1 or 2: " + prefix + "EnIndex");
158 if ("EnToTranslation".equals(type)) {
159 parser = new EnToTranslationParser(dictionaryBuilder.indexBuilders.get(enIndex), dictionaryBuilder.indexBuilders.get(1-enIndex),
160 langPattern, langCodePattern, enIndex != 0);
161 } else if ("EnForeign".equals(type)) {
162 parser = new EnForeignParser(dictionaryBuilder.indexBuilders.get(enIndex), dictionaryBuilder.indexBuilders.get(1-enIndex),
163 langPattern, langCodePattern, enIndex != 0);
165 fatalError("Invalid WiktionaryType (use EnToTranslation or EnForeign): " + type);
168 parser.parse(file, entrySource, pageLimit);
169 } else if (EnTranslationToTranslationParser.NAME.equals(inputFormat)) {
170 final String code1 = keyValueArgs.remove(prefix + "LangPattern1");
171 final String code2 = keyValueArgs.remove(prefix + "LangPattern2");
172 if (code1 == null || code2 == null) {
173 fatalError("Must specify LangPattern1 and LangPattern2.");
176 final Pattern codePattern1 = Pattern.compile(code1, Pattern.CASE_INSENSITIVE);
177 final Pattern codePattern2 = Pattern.compile(code2, Pattern.CASE_INSENSITIVE);
178 new EnTranslationToTranslationParser(dictionaryBuilder.indexBuilders, new Pattern[] {codePattern1, codePattern2}).parse(file, entrySource, pageLimit);
179 } else if (WholeSectionToHtmlParser.NAME.equals(inputFormat)) {
180 final int titleIndex = Integer.parseInt(keyValueArgs.remove(prefix + "TitleIndex")) - 1;
181 final String wiktionaryLang = keyValueArgs.remove(prefix + "WiktionaryLang");
182 new WholeSectionToHtmlParser(dictionaryBuilder.indexBuilders.get(titleIndex), wiktionaryLang).parse(file, entrySource, pageLimit);
184 fatalError("Invalid or missing input format: " + inputFormat);
187 dictionaryBuilder.dictionary.sources.add(entrySource);
188 System.out.println("Done: " + file + "\n\n");
192 dictionaryBuilder.build();
194 if (printFile != null) {
195 final PrintStream out = new PrintStream(new File(printFile));
196 dictionaryBuilder.dictionary.print(out);
200 System.out.println("Writing dictionary to: " + dictOutFilename);
201 final RandomAccessFile dictOut = new RandomAccessFile(dictOutFilename, "rw");
202 dictOut.setLength(0);
203 dictionaryBuilder.dictionary.write(dictOut);
206 if (!keyValueArgs.isEmpty()) {
207 System.err.println("WARNING: couldn't parse arguments: " + keyValueArgs);
213 private static void fatalError(String string) {
214 System.err.println(string);