]> gitweb.fperrin.net Git - DictionaryPC.git/blobdiff - src/com/hughes/android/dictionary/engine/DictionaryBuilder.java
Wiktionary upgrade!
[DictionaryPC.git] / src / com / hughes / android / dictionary / engine / DictionaryBuilder.java
index 888d5c87b5ccddf93194bafcbbdfebebf8efcf45..519663774bc9242c5708d498a725f4f1bda0a672 100644 (file)
@@ -20,6 +20,7 @@ import java.io.PrintStream;
 import java.io.RandomAccessFile;
 import java.nio.charset.Charset;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
@@ -31,7 +32,7 @@ import javax.xml.parsers.ParserConfigurationException;
 import org.xml.sax.SAXException;
 
 import com.hughes.android.dictionary.parser.DictFileParser;
-import com.hughes.android.dictionary.parser.EnWiktionaryXmlParser;
+import com.hughes.android.dictionary.parser.enwiktionary.EnWiktionaryXmlParser;
 import com.hughes.util.Args;
 import com.hughes.util.FileUtil;
 
@@ -43,8 +44,8 @@ public class DictionaryBuilder {
   
   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) {
     dictionary = new Dictionary(dictInfo);
-    indexBuilders.add(new IndexBuilder(this, lang0.getSymbol(), lang0.getSymbol() + "->" + lang1.getSymbol(), lang0, normalizerRules1, lang1Stoplist, false));
-    indexBuilders.add(new IndexBuilder(this, lang1.getSymbol(), lang1.getSymbol() + "->" + lang0.getSymbol(), lang1, normalizerRules2, lang2Stoplist, true));
+    indexBuilders.add(new IndexBuilder(this, lang0.getIsoCode(), lang0.getIsoCode() + "->" + lang1.getIsoCode(), lang0, normalizerRules1, lang1Stoplist, false));
+    indexBuilders.add(new IndexBuilder(this, lang1.getIsoCode(), lang1.getIsoCode() + "->" + lang0.getIsoCode(), lang1, normalizerRules2, lang2Stoplist, true));
   }
   
   void build() {
@@ -55,13 +56,18 @@ public class DictionaryBuilder {
   }
   
   public static void main(final String[] args) throws IOException, ParserConfigurationException, SAXException {
+    System.out.println("Running with arguments:");
+    for (final String arg : args) {
+      System.out.println(arg);
+    }
+    
     final Map<String,String> keyValueArgs = Args.keyValueArgs(args);
     
-    final Language lang1 = Language.lookup(keyValueArgs.remove("lang1"));
-    final Language lang2 = Language.lookup(keyValueArgs.remove("lang2"));
-    if (lang1 == null || lang2 == null) {
+    if (!keyValueArgs.containsKey("lang1") || !keyValueArgs.containsKey("lang2")) {
       fatalError("--lang1= and --lang2= must both be specified.");
     }
+    final Language lang1 = Language.lookup(keyValueArgs.remove("lang1"));
+    final Language lang2 = Language.lookup(keyValueArgs.remove("lang2"));
 
     final Set<String> lang1Stoplist = new LinkedHashSet<String>();
     final Set<String> lang2Stoplist = new LinkedHashSet<String>();
@@ -122,14 +128,16 @@ public class DictionaryBuilder {
           fatalError("Must specify human readable name for: " + prefix + "Name");
         }
 
-        final EntrySource entrySource = new EntrySource(dictionaryBuilder.dictionary.sources.size(), inputName, dictionaryBuilder.dictionary.pairEntries.size());
+        final EntrySource entrySource = new EntrySource(dictionaryBuilder.dictionary.sources.size(), inputName);
         System.out.println("");
         
         String inputFormat = keyValueArgs.remove(prefix + "Format");
-        if ("dictcc".equals(inputFormat)) {
-          new DictFileParser(charset, false, DictFileParser.TAB, null, dictionaryBuilder, dictionaryBuilder.indexBuilders.toArray(new IndexBuilder[0]), null).parseFile(file);
+        if ("tab_separated".equals(inputFormat)) {
+          final boolean flipColumns = "true".equals(keyValueArgs.remove(prefix + "FlipColumns"));
+          new DictFileParser(charset, flipColumns, DictFileParser.TAB, null, dictionaryBuilder, dictionaryBuilder.indexBuilders.toArray(new IndexBuilder[0]), null).parseFile(file, entrySource);
         } else if ("chemnitz".equals(inputFormat)) {
-          new DictFileParser(charset, false, DictFileParser.DOUBLE_COLON, DictFileParser.PIPE, dictionaryBuilder, dictionaryBuilder.indexBuilders.toArray(new IndexBuilder[0]), null).parseFile(file);
+          final boolean flipColumns = "true".equals(keyValueArgs.remove(prefix + "FlipColumns"));
+          new DictFileParser(charset, flipColumns, DictFileParser.DOUBLE_COLON, DictFileParser.PIPE, dictionaryBuilder, dictionaryBuilder.indexBuilders.toArray(new IndexBuilder[0]), null).parseFile(file, entrySource);
         } else if ("enwiktionary".equals(inputFormat)) {
           final Pattern langPattern = Pattern.compile(keyValueArgs.remove(prefix + "LangPattern"), Pattern.CASE_INSENSITIVE);
           final Pattern langCodePattern = Pattern.compile(keyValueArgs.remove(prefix + "LangCodePattern"));
@@ -143,7 +151,7 @@ public class DictionaryBuilder {
             fatalError("Must be 1 or 2: " + prefix + "EnIndex");
           }
           new EnWiktionaryXmlParser(dictionaryBuilder.indexBuilders.get(enIndex), dictionaryBuilder.indexBuilders.get(1-enIndex),
-              langPattern, langCodePattern, enIndex != 0).parse(file, Integer.parseInt(pageLimit));
+              langPattern, langCodePattern, enIndex != 0).parse(file, entrySource, Integer.parseInt(pageLimit));
         } else {
           fatalError("Invalid or missing input format: " + inputFormat);
         }
@@ -176,6 +184,8 @@ public class DictionaryBuilder {
   
   private static void fatalError(String string) {
     System.err.println(string);
+    
+    
     System.exit(1);
   }