]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/engine/DictionaryTest.java
Split ZH into yue and cmn, fixed German heading.
[DictionaryPC.git] / src / com / hughes / android / dictionary / engine / DictionaryTest.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.IOException;
18 import java.io.RandomAccessFile;
19 import java.util.Arrays;
20 import java.util.List;
21 import java.util.Random;
22 import java.util.concurrent.atomic.AtomicBoolean;
23
24 import junit.framework.TestCase;
25
26 import com.hughes.android.dictionary.engine.Index.IndexEntry;
27 import com.hughes.util.CollectionUtil;
28
29
30 public class DictionaryTest extends TestCase {
31   
32   static final String TEST_OUTPUTS = com.hughes.android.dictionary.engine.DictionaryBuilderTest.TEST_OUTPUTS;
33   public static final String OUTPUTS = "data/outputs/";
34
35   @Override
36   protected void setUp() {
37     while (!TransliteratorManager.init(null)) {
38       try {
39         Thread.sleep(10);
40       } catch (InterruptedException e) {
41         e.printStackTrace();
42       }
43     }
44   }
45   
46   public void testURLFormatting() {
47   }
48
49   public void testEnItWiktionary() throws IOException {
50     final RandomAccessFile raf = new RandomAccessFile(OUTPUTS + "EN-IT.quickdic", "r");
51     final Dictionary dict = new Dictionary(raf);
52     final Index enIndex = dict.indices.get(0);
53     
54     final RowBase row = enIndex.rows.get(4);
55     assertEquals("-ical", row.getRawText(false));
56     
57     final Index itIndex = dict.indices.get(1);
58     {
59     final List<RowBase> rows = itIndex.multiWordSearch("come mai", Arrays.asList("come", "mai"), new AtomicBoolean(false));
60     System.out.println(CollectionUtil.join(rows, "\n  "));
61     assertTrue(rows.toString(), rows.size() > 0);
62     assertTrue(rows.get(0).toString().startsWith("come mai@"));
63     assertTrue(rows.get(0) instanceof TokenRow);
64     assertTrue(!((TokenRow)rows.get(0)).getIndexEntry().htmlEntries.isEmpty());
65     }
66
67     {
68         final IndexEntry searchResult = itIndex.findInsertionPoint("azzurro", new AtomicBoolean(
69                 false));
70         HtmlEntry htmlEntry = searchResult.htmlEntries.get(0);
71         System.out.println("azzurro:\n" + htmlEntry.getHtml());
72     }
73
74     raf.close();
75   }
76
77   public void testFr() throws IOException {
78       final RandomAccessFile raf = new RandomAccessFile(OUTPUTS + "FR.quickdic", "r");
79       final Dictionary dict = new Dictionary(raf);
80       final Index frIndex = dict.indices.get(0);
81       
82       // Now they're all cached, we shouldn't have to search.
83       for (final IndexEntry indexEntry : frIndex.sortedIndexEntries) {
84           System.out.println(indexEntry.token);
85       }
86
87       raf.close();
88   }
89
90   
91   public void testDeEnWiktionary() throws IOException {
92       final RandomAccessFile raf = new RandomAccessFile(OUTPUTS + "DE-EN.quickdic", "r");
93       final Dictionary dict = new Dictionary(raf);
94             
95       final Index deIndex = dict.indices.get(0);
96
97       {
98           final IndexEntry searchResult = deIndex.findInsertionPoint("rot", new AtomicBoolean(
99                   false));
100           HtmlEntry htmlEntry = searchResult.htmlEntries.get(0);
101           System.out.println("rot:\n" + htmlEntry.getHtml());
102       }
103
104       raf.close();
105     }
106
107   public void testGermanMetadata() throws IOException {
108     final RandomAccessFile raf = new RandomAccessFile(TEST_OUTPUTS + "de-en.quickdic", "r");
109     final Dictionary dict = new Dictionary(raf);
110     final Index deIndex = dict.indices.get(0);
111     
112     assertEquals("DE", deIndex.shortName);
113     assertEquals("DE->EN", deIndex.longName);
114     
115     assertEquals(2, dict.sources.size());
116     assertEquals("chemnitz", dict.sources.get(0).name);
117     assertEquals("dictcc", dict.sources.get(1).name);
118     
119     assertEquals("dictcc", dict.pairEntries.get(0).entrySource.name);
120     assertEquals("chemnitz", dict.pairEntries.get(1).entrySource.name);
121     
122     raf.close();
123   }
124   
125   public void testGermanIndex() throws IOException {
126     final RandomAccessFile raf = new RandomAccessFile(TEST_OUTPUTS + "de-en.quickdic", "r");
127     final Dictionary dict = new Dictionary(raf);
128     final Index deIndex = dict.indices.get(0);
129     
130     for (final Index.IndexEntry indexEntry : deIndex.sortedIndexEntries) {
131       System.out.println("testing: " + indexEntry.token);
132       final IndexEntry searchResult = deIndex.findInsertionPoint(indexEntry.token, new AtomicBoolean(
133           false));
134       assertEquals("Looked up: " + indexEntry.token, indexEntry.token.toLowerCase(), searchResult.token.toLowerCase());
135     }
136
137     // TODO: maybe if user types capitalization, use it.
138     assertSearchResult("aaac", "aaac", deIndex.findInsertionPoint("aaac", new AtomicBoolean(false)));
139     assertSearchResult("aaac", "aaac", deIndex.findInsertionPoint("AAAC", new AtomicBoolean(false)));
140     assertSearchResult("aaac", "aaac", deIndex.findInsertionPoint("AAAc", new AtomicBoolean(false)));
141     assertSearchResult("aaac", "aaac", deIndex.findInsertionPoint("aAac", new AtomicBoolean(false)));
142
143     // Before the beginning.
144     assertSearchResult("40", "40" /* special case */, deIndex.findInsertionPoint("", new AtomicBoolean(false)));
145     assertSearchResult("40", "40" /* special case */, deIndex.findInsertionPoint("__", new AtomicBoolean(false)));
146     
147     // After the end.
148     assertSearchResult("Zweckorientiertheit", "zählen", deIndex.findInsertionPoint("ZZZZZ", new AtomicBoolean(false)));
149
150     assertSearchResult("ab", "aaac", deIndex.findInsertionPoint("aaaca", new AtomicBoolean(false)));
151     assertSearchResult("machen", "machen", deIndex.findInsertionPoint("m", new AtomicBoolean(false)));
152     assertSearchResult("machen", "machen", deIndex.findInsertionPoint("macdddd", new AtomicBoolean(false)));
153
154
155     assertSearchResult("überprüfe", "überprüfe", deIndex.findInsertionPoint("ueberprüfe", new AtomicBoolean(false)));
156     assertSearchResult("überprüfe", "überprüfe", deIndex.findInsertionPoint("ueberpruefe", new AtomicBoolean(false)));
157
158     assertSearchResult("überprüfe", "überprüfe", deIndex.findInsertionPoint("ueberpBLEH", new AtomicBoolean(false)));
159     assertSearchResult("überprüfe", "überprüfe", deIndex.findInsertionPoint("überprBLEH", new AtomicBoolean(false)));
160
161     assertSearchResult("überprüfen", "überprüfe", deIndex.findInsertionPoint("überprüfeBLEH", new AtomicBoolean(false)));
162
163     // Check that search in lowercase works.
164     assertSearchResult("Alibi", "Alibi", deIndex.findInsertionPoint("alib", new AtomicBoolean(false)));
165     System.out.println(deIndex.findInsertionPoint("alib", new AtomicBoolean(false)).toString());
166     
167     raf.close();
168   }
169   
170   private void assertSearchResult(final String insertionPoint, final String longestPrefix,
171       final IndexEntry actual) {
172     assertEquals(insertionPoint, actual.token);
173   }
174
175   public void testGermanTokenRows() throws IOException {
176     final RandomAccessFile raf = new RandomAccessFile(TEST_OUTPUTS + "de-en.quickdic", "r");
177     final Dictionary dict = new Dictionary(raf);
178     final Index deIndex = dict.indices.get(0);
179     
180     // Pre-cache a few of these, just to make sure that's working.
181     for (int i = 0; i < deIndex.rows.size(); i += 7) {
182       deIndex.rows.get(i).getTokenRow(true);
183     }
184     
185     // Do the exhaustive searching.
186     TokenRow lastTokenRow = null;
187     for (final RowBase row : deIndex.rows) {
188       if (row instanceof TokenRow) {
189         lastTokenRow = (TokenRow) row;
190       }
191       assertEquals(lastTokenRow, row.getTokenRow(true));
192     }
193
194     // Now they're all cached, we shouldn't have to search.
195     for (final RowBase row : deIndex.rows) {
196       if (row instanceof TokenRow) {
197         lastTokenRow = (TokenRow) row;
198       }
199       // This will break if the Row cache isn't big enough.
200       assertEquals(lastTokenRow, row.getTokenRow(false));
201     }
202     
203     raf.close();
204   }
205   
206   public void testChemnitz() throws IOException {
207     final RandomAccessFile raf = new RandomAccessFile(TEST_OUTPUTS + "de-en.quickdic", "r");
208     final Dictionary dict = new Dictionary(raf);
209     final Index deIndex = dict.indices.get(0);
210     
211     assertSearchResult("Höschen", "Hos", deIndex.findInsertionPoint("Hos", new AtomicBoolean(false)));
212     assertSearchResult("Höschen", "hos", deIndex.findInsertionPoint("hos", new AtomicBoolean(false)));
213     
214     raf.close();
215   }
216
217   public void testMultiSearch() throws IOException {
218     final RandomAccessFile raf = new RandomAccessFile(TEST_OUTPUTS + "de-en.quickdic", "r");
219     final Dictionary dict = new Dictionary(raf);
220     final Index deIndex = dict.indices.get(0);
221
222     {
223     final List<RowBase> rows = deIndex.multiWordSearch("aaa aaab", Arrays.asList("aaa", "aaab"), new AtomicBoolean(false));
224     System.out.println(CollectionUtil.join(rows, "\n  "));
225     assertTrue(rows.toString(), rows.size() > 0);
226     }
227     
228     raf.close();
229   }
230
231   public void testMultiSearchBig() throws IOException {
232     final RandomAccessFile raf = new RandomAccessFile(OUTPUTS + "DE-EN.quickdic", "r");
233     final Dictionary dict = new Dictionary(raf);
234     final Index enIndex = dict.indices.get(1);
235
236     {
237     final List<RowBase> rows = enIndex.multiWordSearch("train station", Arrays.asList("train", "station"), new AtomicBoolean(false));
238     System.out.println(CollectionUtil.join(rows, "\n  "));
239     assertTrue(rows.toString(), rows.size() > 0);
240     assertTrue(rows.get(0).toString().startsWith("train station@"));
241     }
242
243     {
244     final List<RowBase> rows = enIndex.multiWordSearch("a train station", Arrays.asList("a", "train", "station"), new AtomicBoolean(false));
245     System.out.println(CollectionUtil.join(rows, "\n  "));
246     assertTrue(rows.toString(), rows.size() > 0);
247     assertEquals("Bahnhofsuhr {{de-noun|g=f|plural=Bahnhofsuhren}}\tstation clock (at a train station)", rows.get(0).toString());
248     }
249
250     {
251     final List<RowBase> rows = enIndex.multiWordSearch("a station", Arrays.asList("a", "station"), new AtomicBoolean(false));
252     System.out.println(CollectionUtil.join(rows, "\n  "));
253     assertTrue(rows.toString(), rows.size() > 0);
254     assertEquals("Abfahrthalle {en-noun}\tDeparture room of a station.", rows.get(0).toString());
255     }
256
257     {
258     // Should print: Giving up, too many words with prefix: p
259     final List<RowBase> rows = enIndex.multiWordSearch("p eat", Arrays.asList("p", "eat"), new AtomicBoolean(false));
260     System.out.println(CollectionUtil.join(rows, "\n  "));
261     assertTrue(rows.toString(), rows.size() > 0);
262     assertTrue(rows.toString().contains("verschlingen; verputzen\tto dispatch (eat)"));
263     }
264
265     {
266     // Should print: Giving up, too many words with prefix: p
267     final List<RowBase> rows = enIndex.multiWordSearch("p p", Arrays.asList("p", "p"), new AtomicBoolean(false));
268     assertTrue(rows.size() >= 1000);
269     }
270
271     {
272     // Should print: Giving up, too many words with prefix: a
273     final List<RowBase> rows = enIndex.multiWordSearch("a a", Arrays.asList("a", "a"), new AtomicBoolean(false));
274     assertTrue(rows.size() >= 1000);
275     }
276
277     {
278     // Should print: Giving up, too many words with prefix: a
279     final List<RowBase> rows = enIndex.multiWordSearch("b ba", Arrays.asList("b", "ba"), new AtomicBoolean(false));
280     assertTrue(rows.size() >= 1000);
281     }
282
283     {
284     // Should print: Giving up, too many words with prefix: a
285     final List<RowBase> rows = enIndex.multiWordSearch("b ba", Arrays.asList("b", "ba"), new AtomicBoolean(false));
286     assertTrue(rows.size() >= 1000);
287     }
288
289     raf.close();
290   }
291
292   public void testMultiSearchBigAF() throws IOException {
293     final RandomAccessFile raf = new RandomAccessFile(OUTPUTS + "EN-AF.quickdic", "r");
294     final Dictionary dict = new Dictionary(raf);
295     final Index enIndex = dict.indices.get(0);
296
297     {
298     final List<RowBase> rows = enIndex.multiWordSearch("pig eats", Arrays.asList("pig", "eats"), new AtomicBoolean(false));
299     System.out.println(CollectionUtil.join(rows, "\n  "));
300     assertTrue(rows.toString(), rows.size() > 0);
301     assertEquals("pig (someone who overeats or eats rapidly) (noun)\tvark", rows.get(0).toString());
302     }
303
304     {
305     final List<RowBase> rows = enIndex.multiWordSearch("pig eat", Arrays.asList("pig", "eat"), new AtomicBoolean(false));
306     System.out.println(CollectionUtil.join(rows, "\n  "));
307     assertTrue(rows.toString(), rows.size() > 0);
308     assertEquals("pig (someone who overeats or eats rapidly) (noun)\tvark", rows.get(0).toString());
309     }
310
311     {
312     final List<RowBase> rows = enIndex.multiWordSearch("pi ea", Arrays.asList("pi", "ea"), new AtomicBoolean(false));
313     System.out.println(CollectionUtil.join(rows, "\n  "));
314     assertTrue(rows.toString(), rows.size() > 0);
315     assertTrue(rows.toString().contains("pig (someone who overeats or eats rapidly) (noun)\tvark"));
316     }
317
318     {
319     final List<RowBase> rows = enIndex.multiWordSearch("p eat", Arrays.asList("p", "eat"), new AtomicBoolean(false));
320     System.out.println(CollectionUtil.join(rows, "\n  "));
321     assertTrue(rows.toString(), rows.size() > 0);
322     assertTrue(rows.toString().contains("pig (someone who overeats or eats rapidly) (noun)\tvark"));
323     }
324
325     
326     raf.close();
327   }
328
329
330   public void testExactSearch() throws IOException {
331     final RandomAccessFile raf = new RandomAccessFile(OUTPUTS + "EN-cmn.quickdic", "r");
332     final Dictionary dict = new Dictionary(raf);
333     final Index cmnIndex = dict.indices.get(1);
334
335     final Random random = new Random(10);
336     
337     for (int i = 0; i < 1000; ++i) {
338       final int ii = random.nextInt(cmnIndex.sortedIndexEntries.size());
339       final IndexEntry indexEntry = cmnIndex.sortedIndexEntries.get(ii);
340       final IndexEntry found = cmnIndex.findExact(indexEntry.token);
341       assertNotNull(found);
342       assertEquals(indexEntry.token, found.token);
343       assertEquals(indexEntry, found);  // Test of caching....
344     }
345     
346     raf.close();
347   }
348
349   public void testThai() throws IOException {
350     final RandomAccessFile raf = new RandomAccessFile(OUTPUTS + "EN-TH.quickdic", "r");
351     final Dictionary dict = new Dictionary(raf);
352     final Index thIndex = dict.indices.get(1);
353
354     final IndexEntry entry = thIndex.findInsertionPoint("ดี", new AtomicBoolean(false));
355     assertEquals("di", entry.token);
356     
357     raf.close();
358   }
359
360   public void testNorwegian() throws IOException {
361       final RandomAccessFile raf = new RandomAccessFile(OUTPUTS + "EN-NL.quickdic", "r");
362       final Dictionary dict = new Dictionary(raf);
363       final Index nlIndex = dict.indices.get(1);
364
365       IndexEntry entry = nlIndex.findInsertionPoint("Xhosa", new AtomicBoolean(false));
366       assertEquals("Xhosa", entry.token);
367
368       entry = nlIndex.findInsertionPoint("Zyne", new AtomicBoolean(false));
369       assertEquals("Zyne", entry.token);
370
371       raf.close();
372   }
373
374 }