]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/engine/DictionaryBuilderMain.java
cbf4dc335d6e885cab906b5250432da8c5789007
[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.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.LinkedHashMap;
20 import java.util.LinkedHashSet;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Set;
24
25 import junit.framework.TestCase;
26
27 import com.hughes.android.dictionary.parser.wiktionary.EnTranslationToTranslationParser;
28 import com.hughes.android.dictionary.parser.wiktionary.WholeSectionToHtmlParser;
29 import com.hughes.android.dictionary.parser.wiktionary.WiktionaryLangs;
30
31 public class DictionaryBuilderMain extends TestCase {
32   
33   static final String INPUTS = "data/inputs/";
34   static final String STOPLISTS = "data/inputs/stoplists/";
35   static final String OUTPUTS = "data/outputs/";  
36
37   
38   static final Map<String,String>  isoToDedication = new LinkedHashMap<String, String>();
39   static {
40   isoToDedication.put("AF", "Afrikaans dictionary dedicated to Heiko and Mariëtte Horn.");
41   isoToDedication.put("HR", "Croatian dictionary dedicated to Ines Viskic and Miro Kresonja.");
42   isoToDedication.put("NL", "Dutch dictionary dedicated to Mike LeBeau.");
43   // German handled in file.
44   isoToDedication.put("EL", "Greek dictionary dedicated to Noah Egge.");
45   isoToDedication.put("IT", "Italian dictionary dedicated to Carolina Tropini, my favorite stardust in the whole universe!  Ti amo!");
46   isoToDedication.put("KO", "Korean dictionary dedicated to Ande Elwood--fall fashion und Fernsehturms!");
47   isoToDedication.put("PT", "Portuguese dictionary dedicated to Carlos Melo, one Tough Mudder.");
48   isoToDedication.put("RO", "Romanian dictionary dedicated to Radu Teodorescu.");
49   isoToDedication.put("RU", "Russian dictionary dedicated to Maxim Aronin--best friend always!.");
50   isoToDedication.put("SR", "Serbian dictionary dedicated to Filip Crnogorac--thanks for the honey.");
51   isoToDedication.put("ES", "Spanish dictionary made especially for Carolina Tropini! <3 XoXoXXXXX!");
52   isoToDedication.put("SV", "Swedish dictionary dedicated to Kajsa Palmblad--björn kramar!");
53   }
54   private static String getDedication(String iso) {
55     return isoToDedication.containsKey(iso) ? "\n\n" + isoToDedication.get(iso) : "";
56   }
57   
58   static final Map<String,String>  isoToStoplist = new LinkedHashMap<String, String>();
59   static {
60   isoToStoplist.put("DE", "de.txt");
61   isoToStoplist.put("EN", "en.txt");
62   isoToStoplist.put("ES", "es.txt");
63   isoToStoplist.put("IT", "it.txt");
64   isoToStoplist.put("FR", "fr.txt");
65   }
66   private static String getStoplist(String iso) {
67     return isoToStoplist.containsKey(iso) ? isoToStoplist.get(iso) : "empty.txt";
68   }
69   
70   static List<String> getMainArgs(final String[] pair) {
71     final List<String> result = new ArrayList<String>();
72     
73     final String lang1 = pair[0];
74     final String lang2 = pair[1];
75     
76     final String dictFile = String.format("%s/%s-%s.quickdic", 
77         OUTPUTS, lang1, lang2);
78     
79     result.add(String.format("--dictOut=%s", dictFile));
80     result.add(String.format("--lang1Stoplist=%s", STOPLISTS + getStoplist(lang1)));
81     result.add(String.format("--lang2Stoplist=%s", STOPLISTS + getStoplist(lang2)));
82
83     int i = 2;
84     
85     // Deal with the pairs where one is English.
86     if (Arrays.asList(pair).contains("EN")) {
87       final String foreignIso = pair[0].equals("EN") ? pair[1] : pair[0];
88       
89       
90       String foreignRegex = WiktionaryLangs.isoCodeToEnWikiName.get(foreignIso);
91       if (foreignIso.equals("ZH")) {
92         // HACK: The missing "e" prevents a full match, causing "Cantonese" to be appended to the entries.
93         foreignRegex = "Chinese|Mandarin|Cantones";
94       }
95       
96       final int enIndex;
97       if (foreignIso.equals("DE")) {
98         // German-English is a special case since it was the first ever QuickDic!
99         result.add(String.format("--lang1=%s", "DE"));
100         result.add(String.format("--lang2=%s", "EN"));
101         result.add("--dictInfo=@" + INPUTS + "de-en_chemnitz_enwiktionary.info");
102
103         enIndex = 2;
104       } else {
105         result.add(String.format("--lang1=%s", "EN"));
106         result.add(String.format("--lang2=%s",  foreignIso));
107         result.add(String.format("--dictInfo=(EN)Wikitionary-based EN-%s dictionary.%s", foreignIso, getDedication(foreignIso)));
108         enIndex = 1;
109       }
110       
111       result.add(String.format("--input%d=%s/wikiSplit/en/%s.data", i, INPUTS, foreignIso));
112       result.add(String.format("--input%dName=ENWiktionary.%s", i, foreignIso)) ;
113       result.add(String.format("--input%dFormat=enwiktionary", i));
114       result.add(String.format("--input%dWiktionaryType=EnForeign", i));
115       result.add(String.format("--input%dLangPattern=%s", i, foreignRegex));
116       result.add(String.format("--input%dLangCodePattern=%s", i, foreignIso.toLowerCase()));
117       result.add(String.format("--input%dEnIndex=%d", i, enIndex));
118       ++i;
119
120       result.add(String.format("--input%d=%swikiSplit/en/EN.data", i, INPUTS));
121       result.add(String.format("--input%dName=enwiktionary.english", i));
122       result.add(String.format("--input%dFormat=enwiktionary", i));
123       result.add(String.format("--input%dWiktionaryType=EnToTranslation", i));
124       result.add(String.format("--input%dLangPattern=%s", i, foreignRegex));
125       result.add(String.format("--input%dLangCodePattern=%s", i, foreignIso.toLowerCase()));
126       result.add(String.format("--input%dEnIndex=%d", i, enIndex));
127       ++i;
128       
129       if (foreignIso.equals("DE")) {
130         result.add(String.format("--input%d=%sde-en_chemnitz.txt", i, INPUTS));
131         result.add(String.format("--input%dName=chemnitz", i));
132         result.add(String.format("--input%dCharset=UTF8", i));
133         result.add(String.format("--input%dFormat=chemnitz", i));
134         ++i;
135       }
136       
137       result.add(String.format("--input%d=%s/wikiSplit/en/%s.data", i, INPUTS, foreignIso));
138       result.add(String.format("--input%dName=%s", i, "ENWiktionary.WholeSections.%s", foreignIso));
139       result.add(String.format("--input%dFormat=%s", i, WholeSectionToHtmlParser.NAME));
140       result.add(String.format("--input%dTitleIndex=%d", i, 3 - enIndex));
141       ++i;
142
143     } else {
144       // Pairs without English.
145       result.add(String.format("--lang1=%s", lang1));
146       result.add(String.format("--lang2=%s", lang2));
147       
148       result.add(String.format("--input%d=%swikiSplit/en/EN.data", i, INPUTS));
149       result.add(String.format("--input%dName=BETA!enwiktionary.%s-%s", i, lang1, lang2));
150       result.add(String.format("--input%dFormat=%s", i, EnTranslationToTranslationParser.NAME));
151       result.add(String.format("--input%dLangPattern1=%s", i, lang1));
152       result.add(String.format("--input%dLangPattern2=%s", i, lang2));
153       ++i;
154     }
155     return result;
156   }
157
158   public static void main(final String[] args) throws Exception {
159     
160     final List<String[]> allPairs = new ArrayList<String[]>();
161     
162     // Build the non EN ones.
163     final String[][] nonEnPairs = new String[][] {
164         
165         {"AR", "DE" },
166         {"AR", "ES" },
167         {"AR", "FR" },
168         {"AR", "HE" },
169         {"AR", "IT" },
170         {"AR", "JA" },
171         {"AR", "RU" },
172         {"AR", "TR" },  // Turkish
173         {"AR", "ZH" },
174         
175         {"DE", "AR" },
176         {"DE", "FR" },
177         {"DE", "CA" },  // Catalan
178         {"DE", "CS" },  // Czech
179         {"DE", "EO" },  // Esperanto
180         {"DE", "ES" },
181         {"DE", "FR" },
182         {"DE", "HE" },
183         {"DE", "HU" },  // Hungarian
184         {"DE", "IT" },
185         {"DE", "JA" },
186         {"DE", "LA" },  // Latin
187         {"DE", "PL" },  // Polish
188         {"DE", "RU" },
189         {"DE", "SV" },  // Swedish
190         {"DE", "TR" },  // Turkish
191         {"DE", "ZH" },
192         {"DE", "TA" },  // Tamil
193
194         
195         {"FR", "BG" },  // Bulgarian
196         {"FR", "CS" },  // Czech
197         {"FR", "DE" },
198         {"FR", "ES" },
199         {"FR", "IT" },
200         {"FR", "JA" },
201         {"FR", "LA" },
202         {"FR", "NL" },  // Dutch
203         {"FR", "RU" },
204         {"FR", "TR" },  // Turkish
205         {"FR", "ZH" },
206
207         {"IT", "DE" },
208         {"IT", "EL" },  // Greek
209         {"IT", "ES" },
210         {"IT", "FR" },
211         {"IT", "HU" },
212         {"IT", "JA" },
213         {"IT", "LA" },  // Latin
214         {"IT", "LV" },  // Latvian
215         {"IT", "NL" },
216         {"IT", "PL" },
217         {"IT", "RU" },
218         {"IT", "SV" },
219         {"IT", "TR" },  // Turkish
220         {"IT", "ZH" },
221
222         {"JA", "ZH" },
223         {"JA", "AR" },
224         {"JA", "KO" },
225
226         {"ZH", "AR" },
227         {"ZH", "DE" },
228         {"ZH", "ES" },
229         {"ZH", "FR" },
230         {"ZH", "IT" },
231         {"ZH", "KO" },
232
233         
234         {"NO", "SV" },
235         {"NO", "FI" },
236         {"FI", "SV" },
237         
238         {"PL", "FR" },  // Polish
239         {"PL", "RU" },  // Polish
240         {"PL", "HU" },  // Polish
241         {"PL", "ES" },  // Polish
242         
243         {"TR", "EL" },  // Turkish, Greek
244     };
245     allPairs.addAll(Arrays.asList(nonEnPairs));
246     
247     // Add all the EN-XX pairs.
248     for (final String isoCode : WiktionaryLangs.isoCodeToEnWikiName.keySet()) {
249       if (isoCode.equals("EN") || isoCode.equals("DE")) {
250         continue;
251       }
252       allPairs.add(new String[] {"EN", isoCode});
253     }
254     allPairs.add(new String[] {"EN", "DE"});
255     
256         
257     final Set<List<String>> done = new LinkedHashSet<List<String>>();
258     for (final String[] pair : allPairs) {
259       Arrays.sort(pair);
260       final List<String> pairList = Arrays.asList(pair);
261       if (done.contains(pairList)) {
262         continue;
263       }
264       done.add(pairList);
265       
266       if (!pairList.contains("IT") || !pairList.contains("EN")) {
267         continue;
268       }
269       
270       DictionaryBuilder.main(getMainArgs(pair).toArray(new String[0]));
271     }
272     
273   }    
274 }