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 testDeEnWiktionary() throws IOException {
78 final RandomAccessFile raf = new RandomAccessFile(OUTPUTS + "DE-EN.quickdic", "r");
79 final Dictionary dict = new Dictionary(raf);
81 final Index deIndex = dict.indices.get(0);
84 final IndexEntry searchResult = deIndex.findInsertionPoint("rot", new AtomicBoolean(
86 HtmlEntry htmlEntry = searchResult.htmlEntries.get(0);
87 System.out.println("rot:\n" + htmlEntry.getHtml());
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);
98 assertEquals("DE", deIndex.shortName);
99 assertEquals("DE->EN", deIndex.longName);
101 assertEquals(2, dict.sources.size());
102 assertEquals("chemnitz", dict.sources.get(0).name);
103 assertEquals("dictcc", dict.sources.get(1).name);
105 assertEquals("dictcc", dict.pairEntries.get(0).entrySource.name);
106 assertEquals("chemnitz", dict.pairEntries.get(1).entrySource.name);
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);
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(
120 assertEquals("Looked up: " + indexEntry.token, indexEntry.token.toLowerCase(), searchResult.token.toLowerCase());
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)));
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)));
134 assertSearchResult("Zweckorientiertheit", "zählen", deIndex.findInsertionPoint("ZZZZZ", new AtomicBoolean(false)));
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)));
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)));
144 assertSearchResult("überprüfe", "überprüfe", deIndex.findInsertionPoint("ueberpBLEH", new AtomicBoolean(false)));
145 assertSearchResult("überprüfe", "überprüfe", deIndex.findInsertionPoint("überprBLEH", new AtomicBoolean(false)));
147 assertSearchResult("überprüfen", "überprüfe", deIndex.findInsertionPoint("überprüfeBLEH", new AtomicBoolean(false)));
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());
156 private void assertSearchResult(final String insertionPoint, final String longestPrefix,
157 final IndexEntry actual) {
158 assertEquals(insertionPoint, actual.token);
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);
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);
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;
177 assertEquals(lastTokenRow, row.getTokenRow(true));
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;
185 // This will break if the Row cache isn't big enough.
186 assertEquals(lastTokenRow, row.getTokenRow(false));
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);
197 assertSearchResult("Höschen", "Hos", deIndex.findInsertionPoint("Hos", new AtomicBoolean(false)));
198 assertSearchResult("Höschen", "hos", deIndex.findInsertionPoint("hos", new AtomicBoolean(false)));
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);
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);
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);
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@"));
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());
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());
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)"));
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);
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);
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);
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);
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);
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());
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());
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"));
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"));
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);
321 final Random random = new Random(10);
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....
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);
340 final IndexEntry entry = thIndex.findInsertionPoint("ดี", new AtomicBoolean(false));
341 assertEquals("di", entry.token);
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);
351 IndexEntry entry = nlIndex.findInsertionPoint("Xhosa", new AtomicBoolean(false));
352 assertEquals("Xhosa", entry.token);
354 entry = nlIndex.findInsertionPoint("Zyne", new AtomicBoolean(false));
355 assertEquals("Zyne", entry.token);