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