]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/engine/DictionaryBuilderMain.java
Add your own dictionary
[DictionaryPC.git] / src / com / hughes / android / dictionary / engine / DictionaryBuilderMain.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.PrintWriter;
20 import java.io.RandomAccessFile;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.LinkedHashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import junit.framework.TestCase;
28
29 public class DictionaryBuilderMain extends TestCase {
30   
31   static final String INPUTS = "data/inputs/";
32   static final String STOPLISTS = "data/inputs/stoplists/";
33   static final String OUTPUTS = "data/outputs/";
34     
35   public static void main(final String[] args) throws Exception {
36     
37     final Map<String,String> isoToWikiName = new LinkedHashMap<String, String>(Language.isoCodeToWikiName);
38     isoToWikiName.remove("EN");
39     isoToWikiName.remove("DE");
40
41     final Map<String,String>  isoToDedication = new LinkedHashMap<String, String>();
42     isoToDedication.put("AF", "Afrikaans dictionary dedicated to Heiko and Mariëtte Horn.");
43     isoToDedication.put("HR", "Croation dictionary dedicated to Ines Viskic and Miro Kresonja.");
44     isoToDedication.put("NL", "Dutch dictionary dedicated to Mike LeBeau.");
45     // German handled in file.
46     isoToDedication.put("EL", "Greek dictionary dedicated to Noah Egge.");
47     isoToDedication.put("IT", "Italian dictionary dedicated to Carolina Tropini, my favorite stardust in the whole universe!  Ti amo!");
48     isoToDedication.put("KO", "Korean dictionary dedicated to Ande Elwood--fall fashion und Fernsehturms!");
49     isoToDedication.put("PT", "Portuguese dictionary dedicated to Carlos Melo, one Tough Mudder.");
50     isoToDedication.put("RO", "Romanian dictionary dedicated to Radu Teodorescu.");
51     isoToDedication.put("RU", "Russian dictionary dedicated to Maxim Aronin--best friend always!.");
52     isoToDedication.put("SR", "Serbian dictionary dedicated to Filip Crnogorac--thanks for the honey.");
53     isoToDedication.put("ES", "Spanish dictionary made especially for Carolina Tropini! <3 XoXoXXXXX!");
54     isoToDedication.put("SV", "Swedish dictionary dedicated to Kajsa Palmblad--björn kramar!");
55
56     final Map<String,String>  isoToStoplist = new LinkedHashMap<String, String>();
57     isoToStoplist.put("DE", "de.txt");
58     isoToStoplist.put("EN", "en.txt");
59     isoToStoplist.put("ES", "es.txt");
60     isoToStoplist.put("IT", "it.txt");
61     isoToStoplist.put("FR", "fr.txt");
62
63     final Map<String,String>  isoToRegex = new LinkedHashMap<String, String>();
64     // HACK: The missing "e" prevents a full match, causing "Cantonese" to be appended to the entries.
65     isoToRegex.put("ZH", "Chinese|Mandarin|Cantones");
66     
67 //    isoToWikiName.keySet().retainAll(Arrays.asList("UK", "HR", "FI"));
68     //isoToWikiName.clear();
69     boolean go = true;
70     for (final String foreignIso : isoToWikiName.keySet()) {
71       if (foreignIso.equals("SK")) {
72         go = true;
73       }
74       if (!go) {
75         continue;
76       }
77
78         final String dictFile = String.format(OUTPUTS + "/EN-%s_enwiktionary.quickdic", foreignIso);
79         System.out.println("building dictFile: " + dictFile);
80         
81         if (!isoToStoplist.containsKey(foreignIso)) {
82           isoToStoplist.put(foreignIso, "empty.txt");
83         }
84         if (!isoToDedication.containsKey(foreignIso)) {
85           isoToDedication.put(foreignIso, "");
86         }
87         if (!isoToRegex.containsKey(foreignIso)) {
88           isoToRegex.put(foreignIso, isoToWikiName.get(foreignIso));
89         }
90   
91         DictionaryBuilder.main(new String[] {
92             String.format("--dictOut=%s", dictFile),
93             String.format("--lang1=EN"),
94             String.format("--lang2=%s", foreignIso),
95             String.format("--lang1Stoplist=%s", STOPLISTS + isoToStoplist.get("EN")),
96             String.format("--lang2Stoplist=%s", STOPLISTS + isoToStoplist.get(foreignIso)),
97             String.format("--dictInfo=(EN)Wikitionary-based EN-%s dictionary.  %s", foreignIso, isoToDedication.get(foreignIso)),
98
99             "--input2=" + INPUTS + "enWikiSplit/" + foreignIso + ".data",
100             "--input2Name=enwiktionary." + foreignIso,
101             "--input2Format=enwiktionary",
102             "--input2LangPattern=" + isoToRegex.get(foreignIso),
103             "--input2LangCodePattern=" + foreignIso.toLowerCase(),
104             "--input2EnIndex=1",
105
106             "--input3=" + INPUTS + "enWikiSplit/EN.data",
107             "--input3Name=enwiktionary.english",
108             "--input3Format=enwiktionary",
109             "--input3LangPattern=" + isoToRegex.get(foreignIso),
110             "--input3LangCodePattern=" + foreignIso.toLowerCase(),
111             "--input3EnIndex=1",
112
113         });
114         
115         // Print the entries for diffing.
116         printToText(dictFile);
117
118     }  // foreignIso
119
120     final String dictFile = OUTPUTS + "DE-EN_chemnitz_enwiktionary.quickdic"; 
121     DictionaryBuilder.main(new String[] {
122         "--dictOut=" + dictFile,
123         "--lang1=DE",
124         "--lang2=EN",
125         String.format("--lang1Stoplist=%s", STOPLISTS + "de.txt"),
126         String.format("--lang2Stoplist=%s", STOPLISTS + "en.txt"),
127         "--dictInfo=@" + INPUTS + "de-en_chemnitz_enwiktionary.info",
128
129         "--input4=" + INPUTS + "de-en_chemnitz.txt",
130         "--input4Name=chemnitz",
131         "--input4Charset=UTF8",
132         "--input4Format=chemnitz",
133         
134         "--input2=" + INPUTS + "enWikiSplit/DE.data",
135         "--input2Name=enwiktionary.DE",
136         "--input2Format=enwiktionary",
137         "--input2LangPattern=German",
138         "--input2LangCodePattern=de",
139         "--input2EnIndex=2",
140
141         "--input3=" + INPUTS + "enWikiSplit/EN.data",
142         "--input3Name=enwiktionary.english",
143         "--input3Format=enwiktionary",
144         "--input3LangPattern=German",
145         "--input3LangCodePattern=de",
146         "--input3EnIndex=2",
147     });
148     printToText(dictFile);
149     
150   }
151   
152   static void printToText(final String dictFile) throws IOException {
153     final RandomAccessFile raf = new RandomAccessFile(new File(dictFile), "r");
154     final Dictionary dict = new Dictionary(raf);
155     final PrintWriter textOut = new PrintWriter(new File(dictFile + ".text"));
156     final List<PairEntry> sorted = new ArrayList<PairEntry>(dict.pairEntries);
157     Collections.sort(sorted);
158     for (final PairEntry pairEntry : sorted) {
159       textOut.println(pairEntry.getRawText(false));
160     }
161     textOut.close();
162     raf.close();
163   }
164   
165 }