1 // Copyright 2011 Google Inc. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 package com.hughes.android.dictionary.engine;
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;
24 import junit.framework.TestCase;
26 import com.hughes.android.dictionary.engine.Index.IndexEntry;
27 import com.hughes.util.CollectionUtil;
30 public class DictionaryTest extends TestCase {
32 static final String TEST_OUTPUTS = com.hughes.android.dictionary.engine.DictionaryBuilderTest.TEST_OUTPUTS;
33 public static final String OUTPUTS = "data/outputs/";
36 protected void setUp() {
37 while (!TransliteratorManager.init(null)) {
40 } catch (InterruptedException e) {
46 public void testURLFormatting() {
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);
54 final RowBase row = enIndex.rows.get(4);
55 assertEquals("-ical", row.getRawText(false));
57 final Index itIndex = dict.indices.get(1);
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());
68 final IndexEntry searchResult = itIndex.findInsertionPoint("azzurro", new AtomicBoolean(
70 HtmlEntry htmlEntry = searchResult.htmlEntries.get(0);
71 System.out.println("azzurro:\n" + htmlEntry.getHtml());
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);
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);
91 public void testDeEnWiktionary() throws IOException {
92 final RandomAccessFile raf = new RandomAccessFile(OUTPUTS + "DE-EN.quickdic", "r");
93 final Dictionary dict = new Dictionary(raf);
95 final Index deIndex = dict.indices.get(0);
98 final IndexEntry searchResult = deIndex.findInsertionPoint("rot", new AtomicBoolean(
100 HtmlEntry htmlEntry = searchResult.htmlEntries.get(0);
101 System.out.println("rot:\n" + htmlEntry.getHtml());
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);
112 assertEquals("DE", deIndex.shortName);
113 assertEquals("DE->EN", deIndex.longName);
115 assertEquals(2, dict.sources.size());
116 assertEquals("chemnitz", dict.sources.get(0).name);
117 assertEquals("dictcc", dict.sources.get(1).name);
119 assertEquals("dictcc", dict.pairEntries.get(0).entrySource.name);
120 assertEquals("chemnitz", dict.pairEntries.get(1).entrySource.name);
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);
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(
134 assertEquals("Looked up: " + indexEntry.token, indexEntry.token.toLowerCase(), searchResult.token.toLowerCase());
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)));
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)));
148 assertSearchResult("Zweckorientiertheit", "zählen", deIndex.findInsertionPoint("ZZZZZ", new AtomicBoolean(false)));
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)));
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)));
158 assertSearchResult("überprüfe", "überprüfe", deIndex.findInsertionPoint("ueberpBLEH", new AtomicBoolean(false)));
159 assertSearchResult("überprüfe", "überprüfe", deIndex.findInsertionPoint("überprBLEH", new AtomicBoolean(false)));
161 assertSearchResult("überprüfen", "überprüfe", deIndex.findInsertionPoint("überprüfeBLEH", new AtomicBoolean(false)));
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());
170 private void assertSearchResult(final String insertionPoint, final String longestPrefix,
171 final IndexEntry actual) {
172 assertEquals(insertionPoint, actual.token);
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);
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);
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;
191 assertEquals(lastTokenRow, row.getTokenRow(true));
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;
199 // This will break if the Row cache isn't big enough.
200 assertEquals(lastTokenRow, row.getTokenRow(false));
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);
211 assertSearchResult("Höschen", "Hos", deIndex.findInsertionPoint("Hos", new AtomicBoolean(false)));
212 assertSearchResult("Höschen", "hos", deIndex.findInsertionPoint("hos", new AtomicBoolean(false)));
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);
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);
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);
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@"));
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);
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@"));
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());
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());
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)"));
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);
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);
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);
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);
306 public void testMultiSearchBigAF() throws IOException {
307 final RandomAccessFile raf = new RandomAccessFile(OUTPUTS + "AF-EN.quickdic", "r");
308 final Dictionary dict = new Dictionary(raf);
309 final Index enIndex = dict.indices.get(1);
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("vark\tpig (someone who overeats or eats rapidly) (noun)", rows.get(0).toString());
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("vark\tpig (someone who overeats or eats rapidly) (noun)", rows.get(0).toString());
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("vark\tpig (someone who overeats or eats rapidly) (noun)"));
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("vark\tpig (someone who overeats or eats rapidly) (noun)"));
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);
349 final Random random = new Random(10);
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....
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);
368 final IndexEntry entry = thIndex.findInsertionPoint("ดี", new AtomicBoolean(false));
369 assertEquals("di", entry.token);
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);
379 IndexEntry entry = nlIndex.findInsertionPoint("Xhosa", new AtomicBoolean(false));
380 assertEquals("Xhosa", entry.token);
382 entry = nlIndex.findInsertionPoint("Zyne", new AtomicBoolean(false));
383 assertEquals("Zyne", entry.token);