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.List;
25 import java.util.regex.Pattern;
27 import javax.xml.parsers.ParserConfigurationException;
29 import org.xml.sax.SAXException;
31 import com.hughes.android.dictionary.parser.DictFileParser;
32 import com.hughes.android.dictionary.parser.EnWiktionaryXmlParser;
33 import com.hughes.util.Args;
34 import com.hughes.util.FileUtil;
42 --dictInfo=@dictInfo.txt
44 --input0=/Users/thadh/personal/quickDic/de-en-chemnitz.txt
47 --input0Format=chemnitz
49 --input1=/Users/thadh/personal/quickDic/dewiktionary-20100326-pages-articles.xml
50 --input1Name=wiktionary
51 --input1Format=wiktionary
53 --input2=/Users/thadh/personal/quickDic/de-en-dictcc.txt
55 --input2Charset=Cp1252
59 public class DictionaryBuilder {
61 public final Dictionary dictionary;
62 public final List<IndexBuilder> indexBuilders = new ArrayList<IndexBuilder>();
64 public DictionaryBuilder(final String dictInfo, final Language lang0, final Language lang1, final String normalizerRules1, final String normalizerRules2) {
65 dictionary = new Dictionary(dictInfo);
66 indexBuilders.add(new IndexBuilder(this, lang0.getSymbol(), lang0.getSymbol() + "->" + lang1.getSymbol(), lang0, normalizerRules1, false));
67 indexBuilders.add(new IndexBuilder(this, lang1.getSymbol(), lang1.getSymbol() + "->" + lang0.getSymbol(), lang1, normalizerRules2, true));
71 for (final IndexBuilder indexBuilder : indexBuilders) {
73 dictionary.indices.add(indexBuilder.index);
77 public static void main(final String[] args) throws IOException, ParserConfigurationException, SAXException {
78 final Map<String,String> keyValueArgs = Args.keyValueArgs(args);
80 final Language lang1 = Language.lookup(keyValueArgs.remove("lang1"));
81 final Language lang2 = Language.lookup(keyValueArgs.remove("lang2"));
82 if (lang1 == null || lang2 == null) {
83 fatalError("--lang1= and --lang2= must both be specified.");
86 String normalizerRules1 = keyValueArgs.remove("normalizerRules1");
87 String normalizerRules2 = keyValueArgs.remove("normalizerRules2");
88 if (normalizerRules1 == null) {
89 normalizerRules1 = lang1.getDefaultNormalizerRules();
91 if (normalizerRules2 == null) {
92 normalizerRules2 = lang2.getDefaultNormalizerRules();
95 final String dictOutFilename = keyValueArgs.remove("dictOut");
96 if (dictOutFilename == null) {
97 fatalError("--dictOut= must be specified.");
100 String dictInfo = keyValueArgs.remove("dictInfo");
101 if (dictInfo == null) {
102 fatalError("--dictInfo= must be specified.");
104 if (dictInfo.startsWith("@")) {
105 dictInfo = FileUtil.readToString(new File(dictInfo.substring(1)));
108 final String printFile = keyValueArgs.remove("print");
110 System.out.println("lang1=" + lang1);
111 System.out.println("lang2=" + lang2);
112 System.out.println("normalizerRules1=" + normalizerRules1);
113 System.out.println("normalizerRules2=" + normalizerRules2);
114 System.out.println("dictInfo=" + dictInfo);
115 System.out.println("dictOut=" + dictOutFilename);
117 final DictionaryBuilder dictionaryBuilder = new DictionaryBuilder(dictInfo, lang1, lang2, normalizerRules1, normalizerRules2);
119 for (int i = 0; i < 100; ++i) {
120 final String prefix = "input" + i;
121 if (keyValueArgs.containsKey(prefix)) {
122 final File file = new File(keyValueArgs.remove(prefix));
123 System.out.println("Processing: " + file);
124 String charsetName = keyValueArgs.remove(prefix + "Charset");
125 if (charsetName == null) {
126 charsetName = "UTF8";
128 final Charset charset = Charset.forName(charsetName);
129 String inputName = keyValueArgs.remove(prefix + "Name");
130 if (inputName == null) {
131 fatalError("Must specify human readable name for: " + prefix + "Name");
134 final EntrySource entrySource = new EntrySource(dictionaryBuilder.dictionary.sources.size(), inputName, dictionaryBuilder.dictionary.pairEntries.size());
135 System.out.println("");
137 String inputFormat = keyValueArgs.remove(prefix + "Format");
138 if ("dictcc".equals(inputFormat)) {
139 new DictFileParser(charset, false, DictFileParser.TAB, null, dictionaryBuilder, dictionaryBuilder.indexBuilders.toArray(new IndexBuilder[0]), null).parseFile(file);
140 } else if ("chemnitz".equals(inputFormat)) {
141 new DictFileParser(charset, false, DictFileParser.DOUBLE_COLON, DictFileParser.PIPE, dictionaryBuilder, dictionaryBuilder.indexBuilders.toArray(new IndexBuilder[0]), null).parseFile(file);
142 } else if ("enwiktionary".equals(inputFormat)) {
143 final Pattern langPattern = Pattern.compile(keyValueArgs.remove(prefix + "LangPattern"));
144 final Pattern langCodePattern = Pattern.compile(keyValueArgs.remove(prefix + "LangCodePattern"));
145 final int enIndex = Integer.parseInt(keyValueArgs.remove(prefix + "EnIndex")) - 1;
146 String pageLimit = keyValueArgs.remove(prefix + "PageLimit");
147 if (pageLimit == null) {
151 if (enIndex < 0 || enIndex >= 2) {
152 fatalError("Must be 1 or 2: " + prefix + "EnIndex");
154 new EnWiktionaryXmlParser(dictionaryBuilder.indexBuilders.get(enIndex), dictionaryBuilder.indexBuilders.get(1-enIndex),
155 langPattern, langCodePattern, enIndex != 0).parse(file, Integer.parseInt(pageLimit));
157 fatalError("Invalid or missing input format: " + inputFormat);
160 dictionaryBuilder.dictionary.sources.add(entrySource);
161 System.out.println("Done: " + file + "\n\n");
165 dictionaryBuilder.build();
167 if (printFile != null) {
168 final PrintStream out = new PrintStream(new File(printFile));
169 dictionaryBuilder.dictionary.print(out);
173 System.out.println("Writing dictionary to: " + dictOutFilename);
174 final RandomAccessFile dictOut = new RandomAccessFile(dictOutFilename, "rw");
175 dictOut.setLength(0);
176 dictionaryBuilder.dictionary.write(dictOut);
179 if (!keyValueArgs.isEmpty()) {
180 System.err.println("WARNING: couldn't parse arguments: " + keyValueArgs);
186 private static void fatalError(String string) {
187 System.err.println(string);