]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/engine/Index.java
Some lint fixes.
[Dictionary.git] / src / com / hughes / android / dictionary / engine / Index.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 com.hughes.android.dictionary.DictionaryInfo;
18 import com.hughes.android.dictionary.DictionaryInfo.IndexInfo;
19 import com.hughes.android.dictionary.engine.RowBase.RowKey;
20 import com.hughes.util.CachingList;
21 import com.hughes.util.StringUtil;
22 import com.hughes.util.TransformingList;
23 import com.hughes.util.raf.RAFList;
24 import com.hughes.util.raf.RAFSerializable;
25 import com.hughes.util.raf.RAFSerializer;
26 import com.hughes.util.raf.SerializableSerializer;
27 import com.hughes.util.raf.UniformRAFList;
28 import com.ibm.icu.text.Transliterator;
29
30 import java.io.DataInput;
31 import java.io.DataOutput;
32 import java.io.IOException;
33 import java.io.PrintStream;
34 import java.io.RandomAccessFile;
35 import java.nio.channels.FileChannel;
36 import java.util.AbstractList;
37 import java.util.ArrayList;
38 import java.util.Collection;
39 import java.util.Collections;
40 import java.util.Comparator;
41 import java.util.EnumMap;
42 import java.util.HashSet;
43 import java.util.HashMap;
44 import java.util.List;
45 import java.util.Map;
46 import java.util.Set;
47 import java.util.concurrent.atomic.AtomicBoolean;
48 import java.util.regex.Pattern;
49
50 public final class Index implements RAFSerializable<Index> {
51
52     private static final int CACHE_SIZE = 5000;
53
54     public final Dictionary dict;
55
56     public final String shortName; // Typically the ISO code for the language.
57     public final String longName;
58
59     // persisted: tells how the entries are sorted.
60     public final Language sortLanguage;
61     public final String normalizerRules;
62
63     // Built from the two above.
64     private Transliterator normalizer;
65
66     // persisted
67     public final List<IndexEntry> sortedIndexEntries;
68
69     // persisted.
70     public final Set<String> stoplist;
71
72     // One big list!
73     // Various sub-types.
74     // persisted
75     public final List<RowBase> rows;
76     public final boolean swapPairEntries;
77
78     // Version 2:
79     @SuppressWarnings("WeakerAccess")
80     public int mainTokenCount = -1;
81
82     // --------------------------------------------------------------------------
83
84     public Index(final Dictionary dict, final String shortName, final String longName,
85                  final Language sortLanguage, final String normalizerRules,
86                  final boolean swapPairEntries, final Set<String> stoplist) {
87         this.dict = dict;
88         this.shortName = shortName;
89         this.longName = longName;
90         this.sortLanguage = sortLanguage;
91         this.normalizerRules = normalizerRules;
92         this.swapPairEntries = swapPairEntries;
93         sortedIndexEntries = new ArrayList<>();
94         this.stoplist = stoplist;
95         rows = new ArrayList<>();
96
97         normalizer = null;
98     }
99
100     /**
101      * Deferred initialization because it can be slow.
102      */
103     @SuppressWarnings("WeakerAccess")
104     public synchronized Transliterator normalizer() {
105         if (normalizer == null) {
106             normalizer = TransliteratorManager.get(normalizerRules);
107         }
108         return normalizer;
109     }
110
111     /**
112      * Note that using this comparator probably involves doing too many text
113      * normalizations.
114      */
115     @SuppressWarnings("WeakerAccess")
116     public NormalizeComparator getSortComparator() {
117         return new NormalizeComparator(normalizer(), sortLanguage.getCollator(), dict.dictFileVersion);
118     }
119
120     public Index(final Dictionary dict, final FileChannel inp, final DataInput raf) throws IOException {
121         this.dict = dict;
122         shortName = raf.readUTF();
123         longName = raf.readUTF();
124         final String languageCode = raf.readUTF();
125         sortLanguage = Language.lookup(languageCode);
126         normalizerRules = raf.readUTF();
127         swapPairEntries = raf.readBoolean();
128         if (dict.dictFileVersion >= 2) {
129             mainTokenCount = raf.readInt();
130         }
131         sortedIndexEntries = CachingList.create(
132                                  RAFList.create(inp, new IndexEntrySerializer(dict.dictFileVersion == 6 ? inp : null), inp.position(),
133                                                 dict.dictFileVersion, dict.dictInfo + " idx " + languageCode + ": "), CACHE_SIZE, true);
134         if (dict.dictFileVersion >= 7) {
135             int count = StringUtil.readVarInt(raf);
136             stoplist = new HashSet<>(count);
137             for (int i = 0; i < count; ++i) {
138                 stoplist.add(raf.readUTF());
139             }
140         } else if (dict.dictFileVersion >= 4) {
141             stoplist = new SerializableSerializer<Set<String>>().read(raf);
142         } else {
143             stoplist = Collections.emptySet();
144         }
145         rows = CachingList.create(
146                    UniformRAFList.create(inp, new RowBase.Serializer(this), inp.position()),
147                    CACHE_SIZE, true);
148     }
149
150     @Override
151     public void write(final DataOutput out) throws IOException {
152         RandomAccessFile raf = (RandomAccessFile)out;
153         raf.writeUTF(shortName);
154         raf.writeUTF(longName);
155         raf.writeUTF(sortLanguage.getIsoCode());
156         raf.writeUTF(normalizerRules);
157         raf.writeBoolean(swapPairEntries);
158         raf.writeInt(mainTokenCount);
159         RAFList.write(raf, sortedIndexEntries, new IndexEntrySerializer(null), 32, true);
160         StringUtil.writeVarInt(raf, stoplist.size());
161         for (String i : stoplist) {
162             raf.writeUTF(i);
163         }
164         UniformRAFList.write(raf, rows, new RowBase.Serializer(this), 3 /* bytes per entry */);
165     }
166
167     public void print(final PrintStream out) {
168         for (final RowBase row : rows) {
169             row.print(out);
170         }
171     }
172
173     private final class IndexEntrySerializer implements RAFSerializer<IndexEntry> {
174         private final FileChannel ch;
175
176         IndexEntrySerializer(FileChannel ch) {
177             this.ch = ch;
178         }
179
180         @Override
181         public IndexEntry read(DataInput raf) throws IOException {
182             return new IndexEntry(Index.this, ch, raf);
183         }
184
185         @Override
186         public void write(DataOutput raf, IndexEntry t) throws IOException {
187             t.write(raf);
188         }
189     }
190
191     public static final class IndexEntry implements RAFSerializable<Index.IndexEntry> {
192         public final String token;
193         private final String normalizedToken;
194         public final int startRow;
195         final int numRows; // doesn't count the token row!
196         public List<HtmlEntry> htmlEntries;
197
198         public IndexEntry(final Index index, final String token, final String normalizedToken,
199                           final int startRow, final int numRows) {
200             assert token.equals(token.trim());
201             assert token.length() > 0;
202             this.token = token;
203             this.normalizedToken = normalizedToken;
204             this.startRow = startRow;
205             this.numRows = numRows;
206             this.htmlEntries = new ArrayList<>();
207         }
208
209         IndexEntry(final Index index, final FileChannel ch, final DataInput raf) throws IOException {
210             token = raf.readUTF();
211             if (index.dict.dictFileVersion >= 7) {
212                 startRow = StringUtil.readVarInt(raf);
213                 numRows = StringUtil.readVarInt(raf);
214             } else {
215                 startRow = raf.readInt();
216                 numRows = raf.readInt();
217             }
218             final boolean hasNormalizedForm = raf.readBoolean();
219             normalizedToken = hasNormalizedForm ? raf.readUTF() : token;
220             if (index.dict.dictFileVersion >= 7) {
221                 int size = StringUtil.readVarInt(raf);
222                 if (size == 0) {
223                     this.htmlEntries = Collections.emptyList();
224                 } else {
225                     final int[] htmlEntryIndices = new int[size];
226                     for (int i = 0; i < size; ++i) {
227                         htmlEntryIndices[i] = StringUtil.readVarInt(raf);
228                     }
229                     this.htmlEntries = new AbstractList<HtmlEntry>() {
230                         @Override
231                         public HtmlEntry get(int i) {
232                             return index.dict.htmlEntries.get(htmlEntryIndices[i]);
233                         }
234                         @Override
235                         public int size() {
236                             return htmlEntryIndices.length;
237                         }
238                     };
239                 }
240             } else if (index.dict.dictFileVersion >= 6) {
241                 this.htmlEntries = CachingList.create(
242                                        RAFList.create(ch, index.dict.htmlEntryIndexSerializer,
243                                                       ch.position(), index.dict.dictFileVersion,
244                                                       index.dict.dictInfo + " htmlEntries: "), 1, false);
245             } else {
246                 this.htmlEntries = Collections.emptyList();
247             }
248         }
249
250         public void write(DataOutput raf) throws IOException {
251             raf.writeUTF(token);
252             StringUtil.writeVarInt(raf, startRow);
253             StringUtil.writeVarInt(raf, numRows);
254             final boolean hasNormalizedForm = !token.equals(normalizedToken);
255             raf.writeBoolean(hasNormalizedForm);
256             if (hasNormalizedForm) {
257                 raf.writeUTF(normalizedToken);
258             }
259             StringUtil.writeVarInt(raf, htmlEntries.size());
260             for (HtmlEntry e : htmlEntries)
261                 StringUtil.writeVarInt(raf, e.index());
262         }
263
264         public String toString() {
265             return String.format("%s@%d(%d)", token, startRow, numRows);
266         }
267
268         String normalizedToken() {
269             return normalizedToken;
270         }
271     }
272
273     private static final TransformingList.Transformer<IndexEntry, String> INDEX_ENTRY_TO_TOKEN = new TransformingList.Transformer<IndexEntry, String>() {
274         @Override
275         public String transform(IndexEntry t1) {
276             return t1.token;
277         }
278     };
279
280     public IndexEntry findExact(final String exactToken) {
281         final int result = Collections.binarySearch(
282                                TransformingList.create(sortedIndexEntries, INDEX_ENTRY_TO_TOKEN), exactToken,
283                                getSortComparator());
284         if (result >= 0) {
285             return sortedIndexEntries.get(result);
286         }
287         return null;
288     }
289
290     public IndexEntry findInsertionPoint(String token, final AtomicBoolean interrupted) {
291         final int index = findInsertionPointIndex(token, interrupted);
292         return index != -1 ? sortedIndexEntries.get(index) : null;
293     }
294
295     private int compareIdx(String token, final Comparator<Object> sortCollator, int idx) {
296         final IndexEntry entry = sortedIndexEntries.get(idx);
297         return NormalizeComparator.compareWithoutDash(token, entry.normalizedToken(), sortCollator, dict.dictFileVersion);
298     }
299
300     private int findMatchLen(final Comparator<Object> sortCollator, String a, String b) {
301         int start = 0;
302         int end = Math.min(a.length(), b.length());
303         while (start < end)
304         {
305             int mid = (start + end + 1) / 2;
306             if (sortCollator.compare(a.substring(0, mid), b.substring(0, mid)) == 0)
307                 start = mid;
308             else
309                 end = mid - 1;
310         }
311         return start;
312     }
313
314     private int findInsertionPointIndex(String token, final AtomicBoolean interrupted) {
315         token = normalizeToken(token);
316
317         int start = 0;
318         int end = sortedIndexEntries.size();
319
320         final Comparator<Object> sortCollator = sortLanguage.getCollator();
321         while (start < end) {
322             final int mid = (start + end) / 2;
323             if (interrupted.get()) {
324                 return -1;
325             }
326             final IndexEntry midEntry = sortedIndexEntries.get(mid);
327
328             int comp = NormalizeComparator.compareWithoutDash(token, midEntry.normalizedToken(), sortCollator, dict.dictFileVersion);
329             if (comp == 0)
330                 comp = sortCollator.compare(token, midEntry.normalizedToken());
331             if (comp == 0) {
332                 return windBackCase(token, mid, interrupted);
333             } else if (comp < 0) {
334                 // System.out.println("Upper bound: " + midEntry + ", norm=" +
335                 // midEntry.normalizedToken() + ", mid=" + mid);
336
337                 // Hack for robustness if sort order is broken
338                 if (mid + 2 < end &&
339                     compareIdx(token, sortCollator, mid + 1) > 0 &&
340                     compareIdx(token, sortCollator, mid + 2) > 0) {
341                     start = mid;
342                 } else {
343                     end = mid;
344                 }
345             } else {
346                 // System.out.println("Lower bound: " + midEntry + ", norm=" +
347                 // midEntry.normalizedToken() + ", mid=" + mid);
348
349                 // Hack for robustness if sort order is broken
350                 if (mid - 2 >= start &&
351                     compareIdx(token, sortCollator, mid - 1) < 0 &&
352                     compareIdx(token, sortCollator, mid - 2) < 0) {
353                     end = mid + 1;
354                 } else {
355                     start = mid + 1;
356                 }
357             }
358         }
359
360         // if the word before is the better match, move
361         // our result to it
362         if (start > 0 && start < sortedIndexEntries.size()) {
363             String prev = sortedIndexEntries.get(start - 1).normalizedToken();
364             String next = sortedIndexEntries.get(start).normalizedToken();
365             if (findMatchLen(sortCollator, token, prev) >= findMatchLen(sortCollator, token, next))
366                 start--;
367         }
368
369         // If we search for a substring of a string that's in there, return
370         // that.
371         int result = Math.min(start, sortedIndexEntries.size() - 1);
372         result = windBackCase(sortedIndexEntries.get(result).normalizedToken(), result, interrupted);
373         return result;
374     }
375
376     private final int windBackCase(final String token, int result, final AtomicBoolean interrupted) {
377         while (result > 0 && sortedIndexEntries.get(result - 1).normalizedToken().equals(token)) {
378             --result;
379             if (interrupted.get()) {
380                 return result;
381             }
382         }
383         return result;
384     }
385
386     public IndexInfo getIndexInfo() {
387         return new DictionaryInfo.IndexInfo(shortName, sortedIndexEntries.size(), mainTokenCount);
388     }
389
390     private static final int MAX_SEARCH_ROWS = 1000;
391
392     private final Map<String, Integer> prefixToNumRows = new HashMap<>();
393
394     private synchronized final int getUpperBoundOnRowsStartingWith(final String normalizedPrefix,
395             final int maxRows, final AtomicBoolean interrupted) {
396         final Integer numRows = prefixToNumRows.get(normalizedPrefix);
397         if (numRows != null) {
398             return numRows;
399         }
400         final int insertionPointIndex = findInsertionPointIndex(normalizedPrefix, interrupted);
401
402         int rowCount = 0;
403         for (int index = insertionPointIndex; index < sortedIndexEntries.size(); ++index) {
404             if (interrupted.get()) {
405                 return -1;
406             }
407             final IndexEntry indexEntry = sortedIndexEntries.get(index);
408             if (!indexEntry.normalizedToken.startsWith(normalizedPrefix) &&
409                 !NormalizeComparator.withoutDash(indexEntry.normalizedToken).startsWith(normalizedPrefix)) {
410                 break;
411             }
412             rowCount += indexEntry.numRows + indexEntry.htmlEntries.size();
413             if (rowCount > maxRows) {
414                 System.out.println("Giving up, too many words with prefix: " + normalizedPrefix);
415                 break;
416             }
417         }
418         prefixToNumRows.put(normalizedPrefix, rowCount);
419         return rowCount;
420     }
421
422     public final List<RowBase> multiWordSearch(
423         final String searchText, final List<String> searchTokens,
424         final AtomicBoolean interrupted) {
425         final long startMills = System.currentTimeMillis();
426         final List<RowBase> result = new ArrayList<>();
427
428         final Set<String> normalizedNonStoplist = new HashSet<>();
429
430         String bestPrefix = null;
431         int leastRows = Integer.MAX_VALUE;
432         final StringBuilder searchTokensRegex = new StringBuilder();
433         for (int i = 0; i < searchTokens.size(); ++i) {
434             if (interrupted.get()) {
435                 return null;
436             }
437             final String searchToken = searchTokens.get(i);
438             final String normalized = normalizeToken(searchTokens.get(i));
439             // Normalize them all.
440             searchTokens.set(i, normalized);
441
442             if (!stoplist.contains(searchToken)) {
443                 if (normalizedNonStoplist.add(normalized)) {
444                     final int numRows = getUpperBoundOnRowsStartingWith(normalized,
445                                         MAX_SEARCH_ROWS, interrupted);
446                     if (numRows != -1 && numRows < leastRows) {
447                         if (numRows == 0) {
448                             // We really are done here.
449                             return Collections.emptyList();
450                         }
451                         leastRows = numRows;
452                         bestPrefix = normalized;
453                     }
454                 }
455             }
456
457             if (searchTokensRegex.length() > 0) {
458                 searchTokensRegex.append("[\\s]*");
459             }
460             searchTokensRegex.append(Pattern.quote(normalized));
461         }
462         final Pattern pattern = Pattern.compile(searchTokensRegex.toString());
463
464         if (bestPrefix == null) {
465             bestPrefix = searchTokens.get(0);
466             System.out.println("Everything was in the stoplist!");
467         }
468         System.out.println("Searching using prefix: " + bestPrefix + ", leastRows=" + leastRows
469                            + ", searchTokens=" + searchTokens);
470
471         // Place to store the things that match.
472         final Map<RowMatchType, List<RowBase>> matches = new EnumMap<>(
473                 RowMatchType.class);
474         for (final RowMatchType rowMatchType : RowMatchType.values()) {
475             if (rowMatchType != RowMatchType.NO_MATCH) {
476                 matches.put(rowMatchType, new ArrayList<RowBase>());
477             }
478         }
479
480         int matchCount = 0;
481
482         final int exactMatchIndex = findInsertionPointIndex(searchText, interrupted);
483         if (exactMatchIndex != -1) {
484             final IndexEntry exactMatch = sortedIndexEntries.get(exactMatchIndex);
485             if (pattern.matcher(exactMatch.token).find()) {
486                 matches.get(RowMatchType.TITLE_MATCH).add(rows.get(exactMatch.startRow));
487             }
488         }
489
490         final String searchToken = bestPrefix;
491         final int insertionPointIndex = findInsertionPointIndex(searchToken, interrupted);
492         final Set<RowKey> rowsAlreadySeen = new HashSet<>();
493         for (int index = insertionPointIndex; index < sortedIndexEntries.size()
494                 && matchCount < MAX_SEARCH_ROWS; ++index) {
495             if (interrupted.get()) {
496                 return null;
497             }
498             final IndexEntry indexEntry = sortedIndexEntries.get(index);
499             if (!indexEntry.normalizedToken.startsWith(searchToken) &&
500                 !NormalizeComparator.withoutDash(indexEntry.normalizedToken).startsWith(searchToken)) {
501                 break;
502             }
503
504             // System.out.println("Searching indexEntry: " + indexEntry.token);
505
506             // Extra +1 to skip token row.
507             for (int rowIndex = indexEntry.startRow + 1; rowIndex < indexEntry.startRow + 1
508                     + indexEntry.numRows
509                     && rowIndex < rows.size(); ++rowIndex) {
510                 if (interrupted.get()) {
511                     return null;
512                 }
513                 final RowBase row = rows.get(rowIndex);
514                 final RowBase.RowKey rowKey = row.getRowKey();
515                 if (rowsAlreadySeen.contains(rowKey)) {
516                     continue;
517                 }
518                 rowsAlreadySeen.add(rowKey);
519                 final RowMatchType matchType = row.matches(searchTokens, pattern, normalizer(),
520                                                swapPairEntries);
521                 if (matchType != RowMatchType.NO_MATCH) {
522                     matches.get(matchType).add(row);
523                     ++matchCount;
524                 }
525             }
526         }
527         // } // searchTokens
528
529         // Sort them into a reasonable order.
530         final RowBase.LengthComparator lengthComparator = new RowBase.LengthComparator(
531             swapPairEntries);
532         for (final Collection<RowBase> rows : matches.values()) {
533             final List<RowBase> ordered = new ArrayList<>(rows);
534             Collections.sort(ordered, lengthComparator);
535             result.addAll(ordered);
536         }
537
538         System.out.println("searchDuration: " + (System.currentTimeMillis() - startMills));
539         return result;
540     }
541
542     private String normalizeToken(final String searchToken) {
543         if (TransliteratorManager.init(null, null)) {
544             final Transliterator normalizer = normalizer();
545             return normalizer.transliterate(searchToken);
546         } else {
547             // Do our best since the Transliterators aren't up yet.
548             return searchToken.toLowerCase();
549         }
550     }
551
552 }