]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/engine/DictionaryBuilderTest.java
Build fr_de dictionary from enwiktionary, yeah!
[DictionaryPC.git] / src / com / hughes / android / dictionary / engine / DictionaryBuilderTest.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.FileNotFoundException;
19 import java.io.IOException;
20 import java.io.PrintStream;
21 import java.io.RandomAccessFile;
22
23 import com.hughes.android.dictionary.parser.wiktionary.EnTranslationToTranslationParser;
24 import com.hughes.util.FileUtil;
25
26 import junit.framework.TestCase;
27
28 public class DictionaryBuilderTest extends TestCase {
29   
30   public static final String TEST_INPUTS = "testdata/inputs/";
31   public static final String WIKISPLIT = "data/inputs/wikiSplit/en/";
32   public static final String STOPLISTS = "data/inputs/stoplists/";
33   public static final String GOLDENS = "testdata/goldens/";
34
35   public static final String TEST_OUTPUTS = "testdata/outputs/";
36
37   
38   public void testWiktionary_en_de2fr() throws Exception {
39     wiktionaryTestWithEnTrans2Trans("wiktionary.de_fr.quickdic", "DE", "FR");
40   }
41
42   public void wiktionaryTestWithEnTrans2Trans(final String name, final String lang1,
43       final String lang2) throws Exception {
44     final File result = new File(TEST_OUTPUTS + name);
45     System.out.println("Writing to: " + result);
46     DictionaryBuilder.main(new String[] {
47         "--dictOut=" + result.getAbsolutePath(),
48         "--lang1=" + lang1,
49         "--lang2=" + lang2,
50         "--lang1Stoplist=" + STOPLISTS + "empty.txt",
51         "--lang2Stoplist=" + STOPLISTS + "empty.txt",
52         "--dictInfo=SomeWikiDataTrans2Trans",
53
54         "--input4=" + WIKISPLIT + "EN.data",
55         "--input4Name=" + name,
56         "--input4Format=" + EnTranslationToTranslationParser.NAME,
57         "--input4LangPattern1=" + lang1,
58         "--input4LangPattern2=" + lang2,
59         "--input4PageLimit=1000",
60
61         "--print=" + result.getPath() + ".text",
62     });
63     
64     checkGolden(name, result); 
65   }
66
67   
68   public void testWiktionary_IT_EN() throws Exception {
69     wiktionaryTestWithLangToEn("wiktionary.it_en.quickdic", "IT", "it.txt",
70         "EN.data", "enwiktionary.english", "Italian", "it");
71   }
72
73   public void testWiktionary_ZH_EN() throws Exception {
74     wiktionaryTestWithLangToEn("wiktionary.zh_en.quickdic", "ZH", "empty.txt",
75         // These missing "e" prevents a complete match, forcing the name to be printed
76         "EN.data", "enwiktionary.english", "Chinese|Mandarin|Cantones", "zh");
77   }
78
79   public void testWiktionary_DE_EN() throws Exception {
80     wiktionaryTestWithLangToEn("wiktionary.de_en.quickdic", "DE", "de.txt",
81         "EN.data", "enwiktionary.english", "German", "it");
82   }
83
84   public void testWiktionary_IT_IT() throws Exception {
85     wiktionaryTestWithLangToEn("wiktionary.it_it.quickdic", "IT", "it.txt",
86         "IT.data", "enwiktionary.italian", "Italian", "it");
87   }
88
89   // French
90   public void testWiktionary_FR_FR() throws Exception {
91     wiktionaryTestWithLangToEn("wiktionary.fr_fr.quickdic", "FR", "fr.txt",
92         "FR.data", "enwiktionary.french", "French", "fr");
93   }
94
95   
96   // Arabic
97   public void testWiktionary_AR_AR() throws Exception {
98     wiktionaryTestWithLangToEn("wiktionary.ar_ar.quickdic", "AR", "empty.txt",
99         "AR.data", "enwiktionary.arabic", "Arabic", "ar");
100   }
101
102   // Chinese
103   public void testWiktionary_ZH_ZH() throws Exception {
104     wiktionaryTestWithLangToEn("wiktionary.zh_zh.quickdic", "ZH", "empty.txt",
105         // These missing "e" prevents a complete match, forcing the name to be printed.
106         "ZH.data", "enwiktionary.chinese", "Chinese|Mandarin|Cantones", "zh");
107   }
108
109   // German
110   public void testWiktionary_DE_DE() throws Exception {
111     wiktionaryTestWithLangToEn("wiktionary.de_de.quickdic", "DE", "de.txt",
112         "DE.data", "enwiktionary.german", "German", "it");
113   }
114
115   // Thai
116   public void testWiktionary_TH_TH() throws Exception {
117     wiktionaryTestWithLangToEn("wiktionary.th_th.quickdic", "TH", "empty.txt",
118         // These missing "e" prevents a complete match, forcing the name to be printed.
119         "TH.data", "enwiktionary.thai", "Thai", "th");
120   }
121
122   public void wiktionaryTestWithLangToEn(final String name, final String lang1,
123       final String stoplist, final String data, final String dictName,
124       final String langPattern, final String langCode) throws Exception {
125     final File result = new File(TEST_OUTPUTS + name);
126     System.out.println("Writing to: " + result);
127     final String type = data.equals("EN.data") ? "EnToTranslation" : "EnForeign";
128     DictionaryBuilder.main(new String[] {
129         "--dictOut=" + result.getAbsolutePath(),
130         "--lang1=" + lang1,
131         "--lang2=EN",
132         "--lang1Stoplist=" + STOPLISTS + stoplist,
133         "--lang2Stoplist=" + STOPLISTS + "en.txt",
134         "--dictInfo=SomeWikiData",
135
136         "--input4=" + WIKISPLIT + data,
137         "--input4Name=" + dictName,
138         "--input4Format=enwiktionary",
139         "--input4WiktionaryType=" + type,
140         "--input4LangPattern=" + langPattern,
141         "--input4LangCodePattern=" + langCode,
142         "--input4EnIndex=2",
143         "--input4PageLimit=1000",
144
145         "--print=" + result.getPath() + ".text",
146     });
147     
148     checkGolden(name, result); 
149   }
150
151   public void testGermanCombined() throws Exception {
152     final String name = "de-en.quickdic";
153     final File result = new File(TEST_OUTPUTS + name);
154     System.out.println("Writing to: " + result);
155     DictionaryBuilder.main(new String[] {
156         "--dictOut=" + result.getAbsolutePath(),
157         "--lang1=DE",
158         "--lang2=EN",
159         "--dictInfo=@" + TEST_INPUTS + "de-en_dictInfo.txt",
160
161         "--input1=" + TEST_INPUTS + "de-en_chemnitz_100",
162         "--input1Name=chemnitz",
163         "--input1Charset=UTF8",
164         "--input1Format=chemnitz",
165
166         "--input2=" + TEST_INPUTS + "de-en_dictcc_simulated",
167         "--input2Name=dictcc",
168         "--input2Charset=UTF8",
169         "--input2Format=tab_separated",
170
171         "--print=" + result.getPath() + ".text",
172     });
173     
174     checkGolden(name, result); 
175   }
176
177   private void checkGolden(final String dictName, final File dictFile)
178       throws IOException, FileNotFoundException {
179     // Check it once:
180     assertFilesEqual(GOLDENS + dictName + ".text", dictFile.getPath() + ".text");
181
182     // Check it again.
183     final Dictionary dict = new Dictionary(new RandomAccessFile(dictFile.getAbsolutePath(), "r"));
184     final PrintStream out = new PrintStream(new File(dictFile.getPath() + ".text"));
185     dict.print(out);
186     out.close();
187     assertFilesEqual(GOLDENS + dictName + ".text", dictFile.getPath() + ".text");
188   }
189
190
191   void assertFilesEqual(final String expected, final String actual) throws IOException {
192     final String expectedString = FileUtil.readToString(new File(expected));
193     final String actualString = FileUtil.readToString(new File(actual));
194     assertEquals(expectedString, actualString);
195   }
196
197   
198 }