]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/engine/DictionaryBuilder.java
Rename enwiktionary package to wiktionary.
[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.wiktionary.EnWiktionaryXmlParser;
35 import com.hughes.util.Args;
36 import com.hughes.util.FileUtil;
37
38
39 public class DictionaryBuilder {
40   
41   public final Dictionary dictionary;
42   public final List<IndexBuilder> indexBuilders = new ArrayList<IndexBuilder>();
43   
44   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) {
45     dictionary = new Dictionary(dictInfoString);
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));
48   }
49   
50   void build() {
51     for (final IndexBuilder indexBuilder : indexBuilders) {
52       indexBuilder.build();
53       dictionary.indices.add(indexBuilder.index);
54     }
55   }
56   
57   public static void main(final String[] args) throws IOException, ParserConfigurationException, SAXException {
58     System.out.println("Running with arguments:");
59     for (final String arg : args) {
60       System.out.println(arg);
61     }
62     
63     final Map<String,String> keyValueArgs = Args.keyValueArgs(args);
64     
65     if (!keyValueArgs.containsKey("lang1") || !keyValueArgs.containsKey("lang2")) {
66       fatalError("--lang1= and --lang2= must both be specified.");
67     }
68     final Language lang1 = Language.lookup(keyValueArgs.remove("lang1"));
69     final Language lang2 = Language.lookup(keyValueArgs.remove("lang2"));
70
71     final Set<String> lang1Stoplist = new LinkedHashSet<String>();
72     final Set<String> lang2Stoplist = new LinkedHashSet<String>();
73     final String lang1StoplistFile = keyValueArgs.remove("lang1Stoplist");
74     final String lang2StoplistFile = keyValueArgs.remove("lang2Stoplist");
75     if (lang1StoplistFile != null) {
76       lang1Stoplist.addAll(FileUtil.readLines(new File(lang1StoplistFile)));
77     }
78     if (lang2StoplistFile != null) {
79       lang2Stoplist.addAll(FileUtil.readLines(new File(lang2StoplistFile)));
80     }
81
82     String normalizerRules1 = keyValueArgs.remove("normalizerRules1");
83     String normalizerRules2 = keyValueArgs.remove("normalizerRules2");
84     if (normalizerRules1 == null) {
85       normalizerRules1 = lang1.getDefaultNormalizerRules();
86     }
87     if (normalizerRules2 == null) {
88       normalizerRules2 = lang2.getDefaultNormalizerRules();
89     }
90     
91     final String dictOutFilename = keyValueArgs.remove("dictOut");
92     if (dictOutFilename == null) {
93       fatalError("--dictOut= must be specified.");
94     }
95     
96     String dictInfo = keyValueArgs.remove("dictInfo");
97     if (dictInfo == null) {
98       fatalError("--dictInfo= must be specified.");
99     }
100     if (dictInfo.startsWith("@")) {
101       dictInfo = FileUtil.readToString(new File(dictInfo.substring(1)));
102     }
103     
104     final String printFile = keyValueArgs.remove("print");
105     
106     System.out.println("lang1=" + lang1);
107     System.out.println("lang2=" + lang2);
108     System.out.println("normalizerRules1=" + normalizerRules1);
109     System.out.println("normalizerRules2=" + normalizerRules2);
110     System.out.println("dictInfo=" + dictInfo);
111     System.out.println("dictOut=" + dictOutFilename);    
112     
113     final DictionaryBuilder dictionaryBuilder = new DictionaryBuilder(dictInfo, lang1, lang2, normalizerRules1, normalizerRules2, lang1Stoplist, lang2Stoplist);
114     
115     for (int i = 0; i < 100; ++i) {
116       final String prefix = "input" + i;
117       if (keyValueArgs.containsKey(prefix)) {
118         final File file = new File(keyValueArgs.remove(prefix));
119         System.out.println("Processing: " + file);
120         String charsetName = keyValueArgs.remove(prefix + "Charset");
121         if (charsetName == null) {
122           charsetName = "UTF8";
123         }
124         final Charset charset = Charset.forName(charsetName);
125         String inputName = keyValueArgs.remove(prefix + "Name");
126         if (inputName == null) {
127           fatalError("Must specify human readable name for: " + prefix + "Name");
128         }
129
130         final EntrySource entrySource = new EntrySource(dictionaryBuilder.dictionary.sources.size(), inputName, 0);
131         System.out.println("");
132         
133         String inputFormat = keyValueArgs.remove(prefix + "Format");
134         if ("tab_separated".equals(inputFormat)) {
135           final boolean flipColumns = "true".equals(keyValueArgs.remove(prefix + "FlipColumns"));
136           new DictFileParser(charset, flipColumns, DictFileParser.TAB, null, dictionaryBuilder, dictionaryBuilder.indexBuilders.toArray(new IndexBuilder[0]), null).parseFile(file, entrySource);
137         } else if ("chemnitz".equals(inputFormat)) {
138           final boolean flipColumns = "true".equals(keyValueArgs.remove(prefix + "FlipColumns"));
139           new DictFileParser(charset, flipColumns, DictFileParser.DOUBLE_COLON, DictFileParser.PIPE, dictionaryBuilder, dictionaryBuilder.indexBuilders.toArray(new IndexBuilder[0]), null).parseFile(file, entrySource);
140         } else if ("enwiktionary".equals(inputFormat)) {
141           final Pattern langPattern = Pattern.compile(keyValueArgs.remove(prefix + "LangPattern"), Pattern.CASE_INSENSITIVE);
142           final Pattern langCodePattern = Pattern.compile(keyValueArgs.remove(prefix + "LangCodePattern"));
143           final int enIndex = Integer.parseInt(keyValueArgs.remove(prefix + "EnIndex")) - 1;
144           String pageLimit = keyValueArgs.remove(prefix + "PageLimit");
145           if (pageLimit == null) {
146             pageLimit = "-1";
147           }
148             
149           if (enIndex < 0 || enIndex >= 2) {
150             fatalError("Must be 1 or 2: " + prefix + "EnIndex");
151           }
152           new EnWiktionaryXmlParser(dictionaryBuilder.indexBuilders.get(enIndex), dictionaryBuilder.indexBuilders.get(1-enIndex),
153               langPattern, langCodePattern, enIndex != 0).parse(file, entrySource, Integer.parseInt(pageLimit));
154         } else {
155           fatalError("Invalid or missing input format: " + inputFormat);
156         }
157         
158         dictionaryBuilder.dictionary.sources.add(entrySource);
159         System.out.println("Done: " + file + "\n\n");
160       }
161     }
162    
163     dictionaryBuilder.build();
164     
165     if (printFile != null) {
166       final PrintStream out = new PrintStream(new File(printFile));
167       dictionaryBuilder.dictionary.print(out);
168       out.close();
169     }
170     
171     System.out.println("Writing dictionary to: " + dictOutFilename);
172     final RandomAccessFile dictOut = new RandomAccessFile(dictOutFilename, "rw");
173     dictOut.setLength(0);
174     dictionaryBuilder.dictionary.write(dictOut);
175     dictOut.close();
176     
177     if (!keyValueArgs.isEmpty()) {
178       System.err.println("WARNING: couldn't parse arguments: " + keyValueArgs);
179       System.exit(1);
180     }
181   
182   }
183   
184   private static void fatalError(String string) {
185     System.err.println(string);
186     
187     
188     System.exit(1);
189   }
190   
191 }