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