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