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.enwiktionary.EnWiktionaryXmlParser;
35 import com.hughes.util.Args;
36 import com.hughes.util.FileUtil;
39 public class DictionaryBuilder {
41 public final Dictionary dictionary;
42 public final List<IndexBuilder> indexBuilders = new ArrayList<IndexBuilder>();
44 public DictionaryBuilder(final String dictInfo, final Language lang0, final Language lang1, final String normalizerRules1, final String normalizerRules2, final Set<String> lang1Stoplist, final Set<String> lang2Stoplist) {
45 dictionary = new Dictionary(dictInfo);
46 indexBuilders.add(new IndexBuilder(this, lang0.getIsoCode(), lang0.getIsoCode() + "->" + lang1.getIsoCode(), lang0, normalizerRules1, lang1Stoplist, false));
47 indexBuilders.add(new IndexBuilder(this, lang1.getIsoCode(), lang1.getIsoCode() + "->" + lang0.getIsoCode(), lang1, normalizerRules2, lang2Stoplist, true));
51 for (final IndexBuilder indexBuilder : indexBuilders) {
53 dictionary.indices.add(indexBuilder.index);
57 public static void main(final String[] args) throws IOException, ParserConfigurationException, SAXException {
58 final Map<String,String> keyValueArgs = Args.keyValueArgs(args);
60 final Language lang1 = Language.lookup(keyValueArgs.remove("lang1"));
61 final Language lang2 = Language.lookup(keyValueArgs.remove("lang2"));
62 if (lang1 == null || lang2 == null) {
63 fatalError("--lang1= and --lang2= must both be specified.");
66 final Set<String> lang1Stoplist = new LinkedHashSet<String>();
67 final Set<String> lang2Stoplist = new LinkedHashSet<String>();
68 final String lang1StoplistFile = keyValueArgs.remove("lang1Stoplist");
69 final String lang2StoplistFile = keyValueArgs.remove("lang2Stoplist");
70 if (lang1StoplistFile != null) {
71 lang1Stoplist.addAll(FileUtil.readLines(new File(lang1StoplistFile)));
73 if (lang2StoplistFile != null) {
74 lang2Stoplist.addAll(FileUtil.readLines(new File(lang2StoplistFile)));
77 String normalizerRules1 = keyValueArgs.remove("normalizerRules1");
78 String normalizerRules2 = keyValueArgs.remove("normalizerRules2");
79 if (normalizerRules1 == null) {
80 normalizerRules1 = lang1.getDefaultNormalizerRules();
82 if (normalizerRules2 == null) {
83 normalizerRules2 = lang2.getDefaultNormalizerRules();
86 final String dictOutFilename = keyValueArgs.remove("dictOut");
87 if (dictOutFilename == null) {
88 fatalError("--dictOut= must be specified.");
91 String dictInfo = keyValueArgs.remove("dictInfo");
92 if (dictInfo == null) {
93 fatalError("--dictInfo= must be specified.");
95 if (dictInfo.startsWith("@")) {
96 dictInfo = FileUtil.readToString(new File(dictInfo.substring(1)));
99 final String printFile = keyValueArgs.remove("print");
101 System.out.println("lang1=" + lang1);
102 System.out.println("lang2=" + lang2);
103 System.out.println("normalizerRules1=" + normalizerRules1);
104 System.out.println("normalizerRules2=" + normalizerRules2);
105 System.out.println("dictInfo=" + dictInfo);
106 System.out.println("dictOut=" + dictOutFilename);
108 final DictionaryBuilder dictionaryBuilder = new DictionaryBuilder(dictInfo, lang1, lang2, normalizerRules1, normalizerRules2, lang1Stoplist, lang2Stoplist);
110 for (int i = 0; i < 100; ++i) {
111 final String prefix = "input" + i;
112 if (keyValueArgs.containsKey(prefix)) {
113 final File file = new File(keyValueArgs.remove(prefix));
114 System.out.println("Processing: " + file);
115 String charsetName = keyValueArgs.remove(prefix + "Charset");
116 if (charsetName == null) {
117 charsetName = "UTF8";
119 final Charset charset = Charset.forName(charsetName);
120 String inputName = keyValueArgs.remove(prefix + "Name");
121 if (inputName == null) {
122 fatalError("Must specify human readable name for: " + prefix + "Name");
125 final EntrySource entrySource = new EntrySource(dictionaryBuilder.dictionary.sources.size(), inputName, dictionaryBuilder.dictionary.pairEntries.size());
126 System.out.println("");
128 String inputFormat = keyValueArgs.remove(prefix + "Format");
129 if ("dictcc".equals(inputFormat)) {
130 new DictFileParser(charset, false, DictFileParser.TAB, null, dictionaryBuilder, dictionaryBuilder.indexBuilders.toArray(new IndexBuilder[0]), null).parseFile(file);
131 } else if ("chemnitz".equals(inputFormat)) {
132 new DictFileParser(charset, false, DictFileParser.DOUBLE_COLON, DictFileParser.PIPE, dictionaryBuilder, dictionaryBuilder.indexBuilders.toArray(new IndexBuilder[0]), null).parseFile(file);
133 } else if ("enwiktionary".equals(inputFormat)) {
134 final Pattern langPattern = Pattern.compile(keyValueArgs.remove(prefix + "LangPattern"), Pattern.CASE_INSENSITIVE);
135 final Pattern langCodePattern = Pattern.compile(keyValueArgs.remove(prefix + "LangCodePattern"));
136 final int enIndex = Integer.parseInt(keyValueArgs.remove(prefix + "EnIndex")) - 1;
137 String pageLimit = keyValueArgs.remove(prefix + "PageLimit");
138 if (pageLimit == null) {
142 if (enIndex < 0 || enIndex >= 2) {
143 fatalError("Must be 1 or 2: " + prefix + "EnIndex");
145 new EnWiktionaryXmlParser(dictionaryBuilder.indexBuilders.get(enIndex), dictionaryBuilder.indexBuilders.get(1-enIndex),
146 langPattern, langCodePattern, enIndex != 0).parse(file, Integer.parseInt(pageLimit));
148 fatalError("Invalid or missing input format: " + inputFormat);
151 dictionaryBuilder.dictionary.sources.add(entrySource);
152 System.out.println("Done: " + file + "\n\n");
156 dictionaryBuilder.build();
158 if (printFile != null) {
159 final PrintStream out = new PrintStream(new File(printFile));
160 dictionaryBuilder.dictionary.print(out);
164 System.out.println("Writing dictionary to: " + dictOutFilename);
165 final RandomAccessFile dictOut = new RandomAccessFile(dictOutFilename, "rw");
166 dictOut.setLength(0);
167 dictionaryBuilder.dictionary.write(dictOut);
170 if (!keyValueArgs.isEmpty()) {
171 System.err.println("WARNING: couldn't parse arguments: " + keyValueArgs);
177 private static void fatalError(String string) {
178 System.err.println(string);