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