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