1 // Copyright 2011 Google Inc. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 package com.hughes.android.dictionary.engine;
18 import java.io.FileNotFoundException;
19 import java.io.IOException;
20 import java.io.PrintStream;
21 import java.io.RandomAccessFile;
23 import com.hughes.android.dictionary.parser.wiktionary.EnTranslationToTranslationParser;
24 import com.hughes.android.dictionary.parser.wiktionary.WholeSectionToHtmlParser;
25 import com.hughes.util.FileUtil;
27 import junit.framework.TestCase;
29 public class DictionaryBuilderTest extends TestCase {
31 public static final String TEST_INPUTS = "testdata/inputs/";
32 public static final String WIKISPLIT_EN = "data/inputs/wikiSplit/en/";
33 public static final String STOPLISTS = "data/inputs/stoplists/";
34 public static final String GOLDENS = "testdata/goldens/";
36 public static final String TEST_OUTPUTS = "testdata/outputs/";
38 public void doTestCustomDict(final String name, final String lang1,
39 final String lang2, final String inputFile) throws Exception {
40 final File result = new File(TEST_OUTPUTS + name);
41 System.out.println("Writing to: " + result);
42 DictionaryBuilder.main(new String[] {
43 "--dictOut=" + result.getAbsolutePath(),
46 "--lang1Stoplist=" + STOPLISTS + "empty.txt",
47 "--lang2Stoplist=" + STOPLISTS + "empty.txt",
50 "--input1=testdata/inputs/" + inputFile,
51 "--input1Name=my_input_" + name,
52 "--input1Charset=ISO-8859-1",
53 "--input1Format=tab_separated",
55 "--print=" + result.getPath() + ".text",
58 checkGolden(name, result);
61 public void test_FR_NL() throws Exception {
62 doTestCustomDict("QuickDic-FR-NL.quickdic", "FR", "NL", "QuickDic-FR-NL.txt");
65 public void testWiktionary_en_de2fr() throws Exception {
66 wiktionaryTestWithEnTrans2Trans("wiktionary.de_fr.quickdic", "DE", "FR");
69 public void wiktionaryTestWithEnTrans2Trans(final String name, final String lang1,
70 final String lang2) throws Exception {
71 final File result = new File(TEST_OUTPUTS + name);
72 System.out.println("Writing to: " + result);
73 DictionaryBuilder.main(new String[] {
74 "--dictOut=" + result.getAbsolutePath(),
77 "--lang1Stoplist=" + STOPLISTS + "empty.txt",
78 "--lang2Stoplist=" + STOPLISTS + "empty.txt",
79 "--dictInfo=SomeWikiDataTrans2Trans",
81 "--input4=" + WIKISPLIT_EN + "EN.data",
82 "--input4Name=" + name,
83 "--input4Format=" + EnTranslationToTranslationParser.NAME,
84 "--input4LangPattern1=" + lang1,
85 "--input4LangPattern2=" + lang2,
86 "--input4PageLimit=1000",
88 "--print=" + result.getPath() + ".text",
91 checkGolden(name, result);
94 public void testWiktionary_WholeSection_DE() throws Exception {
95 wiktionaryTestWithWholeSectionToHtml("wiktionary.WholeSection.DE.quickdic", "DE");
98 public void testWiktionary_WholeSection_EN() throws Exception {
99 wiktionaryTestWithWholeSectionToHtml("wiktionary.WholeSection.EN.quickdic", "EN");
102 public void testWiktionary_WholeSection_IT() throws Exception {
103 wiktionaryTestWithWholeSectionToHtml("wiktionary.WholeSection.IT.quickdic", "IT");
106 public void wiktionaryTestWithWholeSectionToHtml(final String name, final String langCode) throws Exception {
107 final File result = new File(TEST_OUTPUTS + name);
108 System.out.println("Writing to: " + result);
109 DictionaryBuilder.main(new String[] {
110 "--dictOut=" + result.getAbsolutePath(),
111 "--lang1=" + langCode,
113 "--lang1Stoplist=" + STOPLISTS + "empty.txt",
114 "--lang2Stoplist=" + STOPLISTS + "empty.txt",
115 "--dictInfo=SomeWikiDataWholeSection",
117 "--input4=" + WIKISPLIT_EN + langCode + ".data",
118 "--input4Name=" + name,
119 "--input4Format=" + WholeSectionToHtmlParser.NAME,
120 "--input4WiktionaryLang=EN",
121 "--input4SkipLang=" + langCode,
122 "--input4TitleIndex=" + "1",
123 "--input4PageLimit=100",
125 "--print=" + result.getPath() + ".text",
127 checkGolden(name, result);
131 public void testWiktionary_IT_EN() throws Exception {
132 wiktionaryTestWithLangToEn("wiktionary.it_en.quickdic", "IT", "it.txt",
133 "EN.data", "enwiktionary.english", "Italian", "it");
136 public void testWiktionary_ZH_EN() throws Exception {
137 wiktionaryTestWithLangToEn("wiktionary.zh_en.quickdic", "ZH", "empty.txt",
138 // These missing "e" prevents a complete match, forcing the name to be printed
139 "EN.data", "enwiktionary.english", "Chinese|Mandarin|Cantones", "zh");
142 public void testWiktionary_DE_EN() throws Exception {
143 wiktionaryTestWithLangToEn("wiktionary.de_en.quickdic", "DE", "de.txt",
144 "EN.data", "enwiktionary.english", "German", "it");
147 public void testWiktionary_IT_IT() throws Exception {
148 wiktionaryTestWithLangToEn("wiktionary.it_it.quickdic", "IT", "it.txt",
149 "IT.data", "enwiktionary.italian", "Italian", "it");
153 public void testWiktionary_FR_FR() throws Exception {
154 wiktionaryTestWithLangToEn("wiktionary.fr_fr.quickdic", "FR", "fr.txt",
155 "FR.data", "enwiktionary.french", "French", "fr");
160 public void testWiktionary_AR_AR() throws Exception {
161 wiktionaryTestWithLangToEn("wiktionary.ar_ar.quickdic", "AR", "empty.txt",
162 "AR.data", "enwiktionary.arabic", "Arabic", "ar");
166 public void testWiktionary_ZH_ZH() throws Exception {
167 wiktionaryTestWithLangToEn("wiktionary.zh_zh.quickdic", "ZH", "empty.txt",
168 // These missing "e" prevents a complete match, forcing the name to be printed.
169 "ZH.data", "enwiktionary.chinese", "Chinese|Mandarin|Cantones", "zh");
173 public void testWiktionary_DE_DE() throws Exception {
174 wiktionaryTestWithLangToEn("wiktionary.de_de.quickdic", "DE", "de.txt",
175 "DE.data", "enwiktionary.german", "German", "it");
179 public void testWiktionary_TH_TH() throws Exception {
180 wiktionaryTestWithLangToEn("wiktionary.th_th.quickdic", "TH", "empty.txt",
181 // These missing "e" prevents a complete match, forcing the name to be printed.
182 "TH.data", "enwiktionary.thai", "Thai", "th");
185 public void wiktionaryTestWithLangToEn(final String name, final String lang1,
186 final String stoplist, final String data, final String dictName,
187 final String langPattern, final String langCode) throws Exception {
188 final File result = new File(TEST_OUTPUTS + name);
189 System.out.println("Writing to: " + result);
190 final String type = data.equals("EN.data") ? "EnToTranslation" : "EnForeign";
191 DictionaryBuilder.main(new String[] {
192 "--dictOut=" + result.getAbsolutePath(),
195 "--lang1Stoplist=" + STOPLISTS + stoplist,
196 "--lang2Stoplist=" + STOPLISTS + "en.txt",
197 "--dictInfo=SomeWikiData",
199 "--input4=" + WIKISPLIT_EN + data,
200 "--input4Name=" + dictName,
201 "--input4Format=enwiktionary",
202 "--input4WiktionaryType=" + type,
203 "--input4LangPattern=" + langPattern,
204 "--input4LangCodePattern=" + langCode,
206 "--input4PageLimit=1000",
208 "--print=" + result.getPath() + ".text",
211 checkGolden(name, result);
214 public void testGermanCombined() throws Exception {
215 final String name = "de-en.quickdic";
216 final File result = new File(TEST_OUTPUTS + name);
217 System.out.println("Writing to: " + result);
218 DictionaryBuilder.main(new String[] {
219 "--dictOut=" + result.getAbsolutePath(),
222 "--dictInfo=@" + TEST_INPUTS + "de-en_dictInfo.txt",
224 "--input1=" + TEST_INPUTS + "de-en_chemnitz_100",
225 "--input1Name=chemnitz",
226 "--input1Charset=UTF8",
227 "--input1Format=chemnitz",
229 "--input2=" + TEST_INPUTS + "de-en_dictcc_simulated",
230 "--input2Name=dictcc",
231 "--input2Charset=UTF8",
232 "--input2Format=tab_separated",
234 "--print=" + result.getPath() + ".text",
237 checkGolden(name, result);
240 private void checkGolden(final String dictName, final File dictFile)
241 throws IOException, FileNotFoundException {
243 assertFilesEqual(GOLDENS + dictName + ".text", dictFile.getPath() + ".text");
246 final Dictionary dict = new Dictionary(new RandomAccessFile(dictFile.getAbsolutePath(), "r"));
247 final PrintStream out = new PrintStream(new File(dictFile.getPath() + ".text"));
250 assertFilesEqual(GOLDENS + dictName + ".text", dictFile.getPath() + ".text");
254 void assertFilesEqual(final String expected, final String actual) throws IOException {
255 final String expectedString = FileUtil.readToString(new File(expected));
256 final String actualString = FileUtil.readToString(new File(actual));
257 assertEquals(expectedString, actualString);