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 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);
51 final RowBase row = enIndex.rows.get(4);
52 assertEquals("-ical", row.getRawText(false));
54 final Index itIndex = dict.indices.get(1);
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());
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);
72 assertEquals("DE", deIndex.shortName);
73 assertEquals("DE->EN", deIndex.longName);
75 assertEquals(2, dict.sources.size());
76 assertEquals("chemnitz", dict.sources.get(0).name);
77 assertEquals("dictcc", dict.sources.get(1).name);
79 assertEquals("dictcc", dict.pairEntries.get(0).entrySource.name);
80 assertEquals("chemnitz", dict.pairEntries.get(1).entrySource.name);
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);
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(
94 assertEquals("Looked up: " + indexEntry.token, indexEntry.token.toLowerCase(), searchResult.token.toLowerCase());
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)));
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)));
108 assertSearchResult("Zweckorientiertheit", "zählen", deIndex.findInsertionPoint("ZZZZZ", new AtomicBoolean(false)));
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)));
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)));
118 assertSearchResult("überprüfe", "überprüfe", deIndex.findInsertionPoint("ueberpBLEH", new AtomicBoolean(false)));
119 assertSearchResult("überprüfe", "überprüfe", deIndex.findInsertionPoint("überprBLEH", new AtomicBoolean(false)));
121 assertSearchResult("überprüfen", "überprüfe", deIndex.findInsertionPoint("überprüfeBLEH", new AtomicBoolean(false)));
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());
130 private void assertSearchResult(final String insertionPoint, final String longestPrefix,
131 final IndexEntry actual) {
132 assertEquals(insertionPoint, actual.token);
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);
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);
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;
151 assertEquals(lastTokenRow, row.getTokenRow(true));
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;
159 // This will break if the Row cache isn't big enough.
160 assertEquals(lastTokenRow, row.getTokenRow(false));
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);
171 assertSearchResult("Höschen", "Hos", deIndex.findInsertionPoint("Hos", new AtomicBoolean(false)));
172 assertSearchResult("Höschen", "hos", deIndex.findInsertionPoint("hos", new AtomicBoolean(false)));
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);
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);
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);
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@"));
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());
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());
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)"));
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);
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);
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);
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);
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);
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());
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());
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"));
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"));
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);
295 final Random random = new Random(10);
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....
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);
314 final IndexEntry entry = thIndex.findInsertionPoint("ดี", new AtomicBoolean(false));
315 assertEquals("di", entry.token);
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);
325 IndexEntry entry = nlIndex.findInsertionPoint("Xhosa", new AtomicBoolean(false));
326 assertEquals("Xhosa", entry.token);
328 entry = nlIndex.findInsertionPoint("Zyne", new AtomicBoolean(false));
329 assertEquals("Zyne", entry.token);