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 = "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/";
39 public void testWiktionary_en_de2fr() throws Exception {
40 wiktionaryTestWithEnTrans2Trans("wiktionary.de_fr.quickdic", "DE", "FR");
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(),
51 "--lang1Stoplist=" + STOPLISTS + "empty.txt",
52 "--lang2Stoplist=" + STOPLISTS + "empty.txt",
53 "--dictInfo=SomeWikiDataTrans2Trans",
55 "--input4=" + WIKISPLIT + "EN.data",
56 "--input4Name=" + name,
57 "--input4Format=" + EnTranslationToTranslationParser.NAME,
58 "--input4LangPattern1=" + lang1,
59 "--input4LangPattern2=" + lang2,
60 "--input4PageLimit=1000",
62 "--print=" + result.getPath() + ".text",
65 checkGolden(name, result);
68 public void testWiktionary_WholeSection_DE() throws Exception {
69 wiktionaryTestWithWholeSectionToHtml("wiktionary.WholeSection.DE.quickdic", "DE");
72 public void testWiktionary_WholeSection_EN() throws Exception {
73 wiktionaryTestWithWholeSectionToHtml("wiktionary.WholeSection.EN.quickdic", "EN");
76 public void testWiktionary_WholeSection_IT() throws Exception {
77 wiktionaryTestWithWholeSectionToHtml("wiktionary.WholeSection.IT.quickdic", "IT");
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,
87 "--lang1Stoplist=" + STOPLISTS + "empty.txt",
88 "--lang2Stoplist=" + STOPLISTS + "empty.txt",
89 "--dictInfo=SomeWikiDataWholeSection",
91 "--input4=" + WIKISPLIT + langCode + ".data",
92 "--input4Name=" + name,
93 "--input4Format=" + WholeSectionToHtmlParser.NAME,
94 "--input4TitleIndex=" + "1",
95 "--input4PageLimit=100",
97 "--print=" + result.getPath() + ".text",
99 checkGolden(name, result);
103 public void testWiktionary_IT_EN() throws Exception {
104 wiktionaryTestWithLangToEn("wiktionary.it_en.quickdic", "IT", "it.txt",
105 "EN.data", "enwiktionary.english", "Italian", "it");
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");
114 public void testWiktionary_DE_EN() throws Exception {
115 wiktionaryTestWithLangToEn("wiktionary.de_en.quickdic", "DE", "de.txt",
116 "EN.data", "enwiktionary.english", "German", "it");
119 public void testWiktionary_IT_IT() throws Exception {
120 wiktionaryTestWithLangToEn("wiktionary.it_it.quickdic", "IT", "it.txt",
121 "IT.data", "enwiktionary.italian", "Italian", "it");
125 public void testWiktionary_FR_FR() throws Exception {
126 wiktionaryTestWithLangToEn("wiktionary.fr_fr.quickdic", "FR", "fr.txt",
127 "FR.data", "enwiktionary.french", "French", "fr");
132 public void testWiktionary_AR_AR() throws Exception {
133 wiktionaryTestWithLangToEn("wiktionary.ar_ar.quickdic", "AR", "empty.txt",
134 "AR.data", "enwiktionary.arabic", "Arabic", "ar");
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");
145 public void testWiktionary_DE_DE() throws Exception {
146 wiktionaryTestWithLangToEn("wiktionary.de_de.quickdic", "DE", "de.txt",
147 "DE.data", "enwiktionary.german", "German", "it");
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");
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(),
167 "--lang1Stoplist=" + STOPLISTS + stoplist,
168 "--lang2Stoplist=" + STOPLISTS + "en.txt",
169 "--dictInfo=SomeWikiData",
171 "--input4=" + WIKISPLIT + data,
172 "--input4Name=" + dictName,
173 "--input4Format=enwiktionary",
174 "--input4WiktionaryType=" + type,
175 "--input4LangPattern=" + langPattern,
176 "--input4LangCodePattern=" + langCode,
178 "--input4PageLimit=1000",
180 "--print=" + result.getPath() + ".text",
183 checkGolden(name, result);
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(),
194 "--dictInfo=@" + TEST_INPUTS + "de-en_dictInfo.txt",
196 "--input1=" + TEST_INPUTS + "de-en_chemnitz_100",
197 "--input1Name=chemnitz",
198 "--input1Charset=UTF8",
199 "--input1Format=chemnitz",
201 "--input2=" + TEST_INPUTS + "de-en_dictcc_simulated",
202 "--input2Name=dictcc",
203 "--input2Charset=UTF8",
204 "--input2Format=tab_separated",
206 "--print=" + result.getPath() + ".text",
209 checkGolden(name, result);
212 private void checkGolden(final String dictName, final File dictFile)
213 throws IOException, FileNotFoundException {
215 assertFilesEqual(GOLDENS + dictName + ".text", dictFile.getPath() + ".text");
218 final Dictionary dict = new Dictionary(new RandomAccessFile(dictFile.getAbsolutePath(), "r"));
219 final PrintStream out = new PrintStream(new File(dictFile.getPath() + ".text"));
222 assertFilesEqual(GOLDENS + dictName + ".text", dictFile.getPath() + ".text");
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);