]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/engine/DictionaryBuilder.java
Apache license.
[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.List;
24 import java.util.Map;
25 import java.util.regex.Pattern;
26
27 import javax.xml.parsers.ParserConfigurationException;
28
29 import org.xml.sax.SAXException;
30
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;
35
36 /*
37
38 --maxEntries=100
39 --dictOut=de-en.dict
40 --lang1=DE
41 --lang2=EN
42 --dictInfo=@dictInfo.txt
43
44 --input0=/Users/thadh/personal/quickDic/de-en-chemnitz.txt
45 --input0Name=chemnitz
46 --input0Charset=UTF8
47 --input0Format=chemnitz
48
49 --input1=/Users/thadh/personal/quickDic/dewiktionary-20100326-pages-articles.xml
50 --input1Name=wiktionary
51 --input1Format=wiktionary
52
53 --input2=/Users/thadh/personal/quickDic/de-en-dictcc.txt
54 --input2Name=dictcc
55 --input2Charset=Cp1252
56 --input2Format=dictcc
57  */
58
59 public class DictionaryBuilder {
60   
61   public final Dictionary dictionary;
62   public final List<IndexBuilder> indexBuilders = new ArrayList<IndexBuilder>();
63   
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));
68   }
69   
70   void build() {
71     for (final IndexBuilder indexBuilder : indexBuilders) {
72       indexBuilder.build();
73       dictionary.indices.add(indexBuilder.index);
74     }
75   }
76   
77   public static void main(final String[] args) throws IOException, ParserConfigurationException, SAXException {
78     final Map<String,String> keyValueArgs = Args.keyValueArgs(args);
79     
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.");
84     }
85     
86     String normalizerRules1 = keyValueArgs.remove("normalizerRules1");
87     String normalizerRules2 = keyValueArgs.remove("normalizerRules2");
88     if (normalizerRules1 == null) {
89       normalizerRules1 = lang1.getDefaultNormalizerRules();
90     }
91     if (normalizerRules2 == null) {
92       normalizerRules2 = lang2.getDefaultNormalizerRules();
93     }
94     
95     final String dictOutFilename = keyValueArgs.remove("dictOut");
96     if (dictOutFilename == null) {
97       fatalError("--dictOut= must be specified.");
98     }
99     
100     String dictInfo = keyValueArgs.remove("dictInfo");
101     if (dictInfo == null) {
102       fatalError("--dictInfo= must be specified.");
103     }
104     if (dictInfo.startsWith("@")) {
105       dictInfo = FileUtil.readToString(new File(dictInfo.substring(1)));
106     }
107     
108     final String printFile = keyValueArgs.remove("print");
109     
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);    
116     
117     final DictionaryBuilder dictionaryBuilder = new DictionaryBuilder(dictInfo, lang1, lang2, normalizerRules1, normalizerRules2);
118     
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";
127         }
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");
132         }
133
134         final EntrySource entrySource = new EntrySource(dictionaryBuilder.dictionary.sources.size(), inputName, dictionaryBuilder.dictionary.pairEntries.size());
135         System.out.println("");
136         
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) {
148             pageLimit = "-1";
149           }
150             
151           if (enIndex < 0 || enIndex >= 2) {
152             fatalError("Must be 1 or 2: " + prefix + "EnIndex");
153           }
154           new EnWiktionaryXmlParser(dictionaryBuilder.indexBuilders.get(enIndex), dictionaryBuilder.indexBuilders.get(1-enIndex),
155               langPattern, langCodePattern, enIndex != 0).parse(file, Integer.parseInt(pageLimit));
156         } else {
157           fatalError("Invalid or missing input format: " + inputFormat);
158         }
159         
160         dictionaryBuilder.dictionary.sources.add(entrySource);
161         System.out.println("Done: " + file + "\n\n");
162       }
163     }
164    
165     dictionaryBuilder.build();
166     
167     if (printFile != null) {
168       final PrintStream out = new PrintStream(new File(printFile));
169       dictionaryBuilder.dictionary.print(out);
170       out.close();
171     }
172     
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);
177     dictOut.close();
178     
179     if (!keyValueArgs.isEmpty()) {
180       System.err.println("WARNING: couldn't parse arguments: " + keyValueArgs);
181       System.exit(1);
182     }
183   
184   }
185   
186   private static void fatalError(String string) {
187     System.err.println(string);
188     System.exit(1);
189   }
190   
191 }