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.util.FileUtil;
26 import junit.framework.TestCase;
28 public class DictionaryBuilderTest extends TestCase {
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/";
35 public static final String TEST_OUTPUTS = "testdata/outputs/";
38 public void testWiktionary_en_de2fr() throws Exception {
39 wiktionaryTestWithEnTrans2Trans("wiktionary.de_fr.quickdic", "DE", "FR");
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(),
50 "--lang1Stoplist=" + STOPLISTS + "empty.txt",
51 "--lang2Stoplist=" + STOPLISTS + "empty.txt",
52 "--dictInfo=SomeWikiDataTrans2Trans",
54 "--input4=" + WIKISPLIT + "EN.data",
55 "--input4Name=" + name,
56 "--input4Format=" + EnTranslationToTranslationParser.NAME,
57 "--input4LangPattern1=" + lang1,
58 "--input4LangPattern2=" + lang2,
59 "--input4PageLimit=1000",
61 "--print=" + result.getPath() + ".text",
64 checkGolden(name, result);
68 public void testWiktionary_IT_EN() throws Exception {
69 wiktionaryTestWithLangToEn("wiktionary.it_en.quickdic", "IT", "it.txt",
70 "EN.data", "enwiktionary.english", "Italian", "it");
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");
79 public void testWiktionary_DE_EN() throws Exception {
80 wiktionaryTestWithLangToEn("wiktionary.de_en.quickdic", "DE", "de.txt",
81 "EN.data", "enwiktionary.english", "German", "it");
84 public void testWiktionary_IT_IT() throws Exception {
85 wiktionaryTestWithLangToEn("wiktionary.it_it.quickdic", "IT", "it.txt",
86 "IT.data", "enwiktionary.italian", "Italian", "it");
90 public void testWiktionary_FR_FR() throws Exception {
91 wiktionaryTestWithLangToEn("wiktionary.fr_fr.quickdic", "FR", "fr.txt",
92 "FR.data", "enwiktionary.french", "French", "fr");
97 public void testWiktionary_AR_AR() throws Exception {
98 wiktionaryTestWithLangToEn("wiktionary.ar_ar.quickdic", "AR", "empty.txt",
99 "AR.data", "enwiktionary.arabic", "Arabic", "ar");
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");
110 public void testWiktionary_DE_DE() throws Exception {
111 wiktionaryTestWithLangToEn("wiktionary.de_de.quickdic", "DE", "de.txt",
112 "DE.data", "enwiktionary.german", "German", "it");
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");
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(),
132 "--lang1Stoplist=" + STOPLISTS + stoplist,
133 "--lang2Stoplist=" + STOPLISTS + "en.txt",
134 "--dictInfo=SomeWikiData",
136 "--input4=" + WIKISPLIT + data,
137 "--input4Name=" + dictName,
138 "--input4Format=enwiktionary",
139 "--input4WiktionaryType=" + type,
140 "--input4LangPattern=" + langPattern,
141 "--input4LangCodePattern=" + langCode,
143 "--input4PageLimit=1000",
145 "--print=" + result.getPath() + ".text",
148 checkGolden(name, result);
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(),
159 "--dictInfo=@" + TEST_INPUTS + "de-en_dictInfo.txt",
161 "--input1=" + TEST_INPUTS + "de-en_chemnitz_100",
162 "--input1Name=chemnitz",
163 "--input1Charset=UTF8",
164 "--input1Format=chemnitz",
166 "--input2=" + TEST_INPUTS + "de-en_dictcc_simulated",
167 "--input2Name=dictcc",
168 "--input2Charset=UTF8",
169 "--input2Format=tab_separated",
171 "--print=" + result.getPath() + ".text",
174 checkGolden(name, result);
177 private void checkGolden(final String dictName, final File dictFile)
178 throws IOException, FileNotFoundException {
180 assertFilesEqual(GOLDENS + dictName + ".text", dictFile.getPath() + ".text");
183 final Dictionary dict = new Dictionary(new RandomAccessFile(dictFile.getAbsolutePath(), "r"));
184 final PrintStream out = new PrintStream(new File(dictFile.getPath() + ".text"));
187 assertFilesEqual(GOLDENS + dictName + ".text", dictFile.getPath() + ".text");
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);