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