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