]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/engine/DictionaryTest.java
Skip Italian references.
[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 testMultiSearchIt() throws IOException {
232       final RandomAccessFile raf = new RandomAccessFile(OUTPUTS + "IT.quickdic", "r");
233       final Dictionary dict = new Dictionary(raf);
234       final Index index = dict.indices.get(0);
235
236       {
237       final List<RowBase> rows = index.multiWordSearch("fare centro", 
238               Arrays.asList("fare", "centro"), new AtomicBoolean(false));
239       System.out.println(CollectionUtil.join(rows, "\n  "));
240       assertTrue(rows.toString(), rows.size() > 0);
241       assertTrue(rows.get(0).toString().startsWith("fare centro@"));
242       }
243   }
244
245   public void testMultiSearchDeBig() throws IOException {
246     final RandomAccessFile raf = new RandomAccessFile(OUTPUTS + "DE-EN.quickdic", "r");
247     final Dictionary dict = new Dictionary(raf);
248     final Index enIndex = dict.indices.get(1);
249
250     {
251     final List<RowBase> rows = enIndex.multiWordSearch("train station", Arrays.asList("train", "station"), new AtomicBoolean(false));
252     System.out.println(CollectionUtil.join(rows, "\n  "));
253     assertTrue(rows.toString(), rows.size() > 0);
254     assertTrue(rows.get(0).toString().startsWith("train station@"));
255     }
256
257     {
258     final List<RowBase> rows = enIndex.multiWordSearch("a train station", Arrays.asList("a", "train", "station"), new AtomicBoolean(false));
259     System.out.println(CollectionUtil.join(rows, "\n  "));
260     assertTrue(rows.toString(), rows.size() > 0);
261     assertEquals("Bahnhofsuhr {{de-noun|g=f|plural=Bahnhofsuhren}}\tstation clock (at a train station)", rows.get(0).toString());
262     }
263
264     {
265     final List<RowBase> rows = enIndex.multiWordSearch("a station", Arrays.asList("a", "station"), new AtomicBoolean(false));
266     System.out.println(CollectionUtil.join(rows, "\n  "));
267     assertTrue(rows.toString(), rows.size() > 0);
268     assertEquals("Abfahrthalle {en-noun}\tDeparture room of a station.", rows.get(0).toString());
269     }
270
271     {
272     // Should print: Giving up, too many words with prefix: p
273     final List<RowBase> rows = enIndex.multiWordSearch("p eat", Arrays.asList("p", "eat"), new AtomicBoolean(false));
274     System.out.println(CollectionUtil.join(rows, "\n  "));
275     assertTrue(rows.toString(), rows.size() > 0);
276     assertTrue(rows.toString().contains("verschlingen; verputzen\tto dispatch (eat)"));
277     }
278
279     {
280     // Should print: Giving up, too many words with prefix: p
281     final List<RowBase> rows = enIndex.multiWordSearch("p p", Arrays.asList("p", "p"), new AtomicBoolean(false));
282     assertTrue(rows.size() >= 1000);
283     }
284
285     {
286     // Should print: Giving up, too many words with prefix: a
287     final List<RowBase> rows = enIndex.multiWordSearch("a a", Arrays.asList("a", "a"), new AtomicBoolean(false));
288     assertTrue(rows.size() >= 1000);
289     }
290
291     {
292     // Should print: Giving up, too many words with prefix: a
293     final List<RowBase> rows = enIndex.multiWordSearch("b ba", Arrays.asList("b", "ba"), new AtomicBoolean(false));
294     assertTrue(rows.size() >= 1000);
295     }
296
297     {
298     // Should print: Giving up, too many words with prefix: a
299     final List<RowBase> rows = enIndex.multiWordSearch("b ba", Arrays.asList("b", "ba"), new AtomicBoolean(false));
300     assertTrue(rows.size() >= 1000);
301     }
302
303     raf.close();
304   }
305
306   public void testMultiSearchBigAF() throws IOException {
307     final RandomAccessFile raf = new RandomAccessFile(OUTPUTS + "EN-AF.quickdic", "r");
308     final Dictionary dict = new Dictionary(raf);
309     final Index enIndex = dict.indices.get(0);
310
311     {
312     final List<RowBase> rows = enIndex.multiWordSearch("pig eats", Arrays.asList("pig", "eats"), new AtomicBoolean(false));
313     System.out.println(CollectionUtil.join(rows, "\n  "));
314     assertTrue(rows.toString(), rows.size() > 0);
315     assertEquals("pig (someone who overeats or eats rapidly) (noun)\tvark", rows.get(0).toString());
316     }
317
318     {
319     final List<RowBase> rows = enIndex.multiWordSearch("pig eat", Arrays.asList("pig", "eat"), new AtomicBoolean(false));
320     System.out.println(CollectionUtil.join(rows, "\n  "));
321     assertTrue(rows.toString(), rows.size() > 0);
322     assertEquals("pig (someone who overeats or eats rapidly) (noun)\tvark", rows.get(0).toString());
323     }
324
325     {
326     final List<RowBase> rows = enIndex.multiWordSearch("pi ea", Arrays.asList("pi", "ea"), new AtomicBoolean(false));
327     System.out.println(CollectionUtil.join(rows, "\n  "));
328     assertTrue(rows.toString(), rows.size() > 0);
329     assertTrue(rows.toString().contains("pig (someone who overeats or eats rapidly) (noun)\tvark"));
330     }
331
332     {
333     final List<RowBase> rows = enIndex.multiWordSearch("p eat", Arrays.asList("p", "eat"), new AtomicBoolean(false));
334     System.out.println(CollectionUtil.join(rows, "\n  "));
335     assertTrue(rows.toString(), rows.size() > 0);
336     assertTrue(rows.toString().contains("pig (someone who overeats or eats rapidly) (noun)\tvark"));
337     }
338
339     
340     raf.close();
341   }
342
343
344   public void testExactSearch() throws IOException {
345     final RandomAccessFile raf = new RandomAccessFile(OUTPUTS + "EN-cmn.quickdic", "r");
346     final Dictionary dict = new Dictionary(raf);
347     final Index cmnIndex = dict.indices.get(1);
348
349     final Random random = new Random(10);
350     
351     for (int i = 0; i < 1000; ++i) {
352       final int ii = random.nextInt(cmnIndex.sortedIndexEntries.size());
353       final IndexEntry indexEntry = cmnIndex.sortedIndexEntries.get(ii);
354       final IndexEntry found = cmnIndex.findExact(indexEntry.token);
355       assertNotNull(found);
356       assertEquals(indexEntry.token, found.token);
357       assertEquals(indexEntry, found);  // Test of caching....
358     }
359     
360     raf.close();
361   }
362
363   public void testThai() throws IOException {
364     final RandomAccessFile raf = new RandomAccessFile(OUTPUTS + "EN-TH.quickdic", "r");
365     final Dictionary dict = new Dictionary(raf);
366     final Index thIndex = dict.indices.get(1);
367
368     final IndexEntry entry = thIndex.findInsertionPoint("ดี", new AtomicBoolean(false));
369     assertEquals("di", entry.token);
370     
371     raf.close();
372   }
373
374   public void testNorwegian() throws IOException {
375       final RandomAccessFile raf = new RandomAccessFile(OUTPUTS + "EN-NL.quickdic", "r");
376       final Dictionary dict = new Dictionary(raf);
377       final Index nlIndex = dict.indices.get(1);
378
379       IndexEntry entry = nlIndex.findInsertionPoint("Xhosa", new AtomicBoolean(false));
380       assertEquals("Xhosa", entry.token);
381
382       entry = nlIndex.findInsertionPoint("Zyne", new AtomicBoolean(false));
383       assertEquals("Zyne", entry.token);
384
385       raf.close();
386   }
387
388 }