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.BufferedOutputStream;
18 import java.io.DataInput;
19 import java.io.DataOutput;
20 import java.io.DataOutputStream;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.PrintStream;
24 import java.io.RandomAccessFile;
25 import java.util.AbstractList;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.Comparator;
30 import java.util.EnumMap;
31 import java.util.HashMap;
32 import java.util.HashSet;
33 import java.util.List;
34 import java.util.Locale;
37 import java.util.concurrent.atomic.AtomicBoolean;
38 import java.util.regex.Pattern;
40 import com.hughes.android.dictionary.DictionaryInfo;
41 import com.hughes.android.dictionary.DictionaryInfo.IndexInfo;
42 import com.hughes.android.dictionary.engine.RowBase.RowKey;
43 import com.hughes.util.CachingList;
44 import com.hughes.util.DataInputBuffer;
45 import com.hughes.util.StringUtil;
46 import com.hughes.util.TransformingList;
47 import com.hughes.util.raf.RAFList;
48 import com.hughes.util.raf.RAFSerializer;
49 import com.hughes.util.raf.UniformRAFList;
50 import com.ibm.icu.text.Transliterator;
52 public final class Index {
54 private static final int CACHE_SIZE = 5000;
56 public final Dictionary dict;
58 public final String shortName; // Typically the ISO code for the language.
59 public final String longName;
61 // persisted: tells how the entries are sorted.
62 public final Language sortLanguage;
63 public final String normalizerRules;
65 // Built from the two above.
66 private Transliterator normalizer;
69 public final List<IndexEntry> sortedIndexEntries;
72 public final Set<String> stoplist;
77 public final List<RowBase> rows;
78 public final boolean swapPairEntries;
81 @SuppressWarnings("WeakerAccess")
82 public int mainTokenCount = -1;
84 // --------------------------------------------------------------------------
86 public Index(final Dictionary dict, final String shortName, final String longName,
87 final Language sortLanguage, final String normalizerRules,
88 final boolean swapPairEntries, final Set<String> stoplist) {
90 this.shortName = shortName;
91 this.longName = longName;
92 this.sortLanguage = sortLanguage;
93 this.normalizerRules = normalizerRules;
94 this.swapPairEntries = swapPairEntries;
95 sortedIndexEntries = new ArrayList<>();
96 this.stoplist = stoplist;
97 rows = new ArrayList<>();
103 * Deferred initialization because it can be slow.
105 @SuppressWarnings("WeakerAccess")
106 public synchronized Transliterator normalizer() {
107 if (normalizer == null) {
108 normalizer = TransliteratorManager.get(normalizerRules);
114 * Note that using this comparator probably involves doing too many text
117 @SuppressWarnings("WeakerAccess")
118 public NormalizeComparator getSortComparator() {
119 return new NormalizeComparator(normalizer(), sortLanguage.getCollator(), dict.dictFileVersion);
122 public Index(final Dictionary dict, final DataInputBuffer raf) throws IOException {
124 shortName = raf.readUTF();
125 longName = raf.readUTF();
126 final String languageCode = raf.readUTF();
127 sortLanguage = Language.lookup(languageCode);
128 normalizerRules = raf.readUTF();
129 swapPairEntries = raf.readBoolean();
130 if (dict.dictFileVersion >= 2) {
131 mainTokenCount = raf.readInt();
133 sortedIndexEntries = CachingList.create(
134 RAFList.create(raf, new IndexEntrySerializer(),
135 dict.dictFileVersion, dict.dictInfo + " idx " + languageCode + ": "), CACHE_SIZE, true);
136 if (dict.dictFileVersion >= 7) {
137 int count = StringUtil.readVarInt(raf);
138 stoplist = new HashSet<>(count);
139 for (int i = 0; i < count; ++i) {
140 stoplist.add(raf.readUTF());
142 } else if (dict.dictFileVersion >= 4) {
143 stoplist = new HashSet<>();
144 raf.readInt(); // length
146 byte b = raf.readByte();
147 raf.skipBytes(b == 'L' ? 71 : 33);
148 while ((b = raf.readByte()) == 0x74) {
149 stoplist.add(raf.readUTF());
151 if (b != 0x78) throw new IOException("Invalid data in dictionary stoplist!");
153 stoplist = Collections.emptySet();
155 rows = CachingList.create(
156 UniformRAFList.create(raf, new RowBase.Serializer(this)),
160 public void write(final DataOutput out) throws IOException {
161 RandomAccessFile raf = (RandomAccessFile)out;
162 raf.writeUTF(shortName);
163 raf.writeUTF(longName);
164 raf.writeUTF(sortLanguage.getIsoCode());
165 raf.writeUTF(normalizerRules);
166 raf.writeBoolean(swapPairEntries);
167 raf.writeInt(mainTokenCount);
168 RAFList.write(raf, sortedIndexEntries, new IndexEntrySerializer(), 32, true);
169 StringUtil.writeVarInt(raf, stoplist.size());
170 for (String i : stoplist) {
173 DataOutputStream outb = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(raf.getFD())));
174 UniformRAFList.write(outb, rows, new RowBase.Serializer(this), 3 /* bytes per entry */);
178 public void print(final PrintStream out) {
179 for (final RowBase row : rows) {
184 private final class IndexEntrySerializer implements RAFSerializer<IndexEntry> {
186 public IndexEntry read(DataInput raf) throws IOException {
187 return new IndexEntry(Index.this, raf);
191 public void write(DataOutput raf, IndexEntry t) throws IOException {
196 public static final class IndexEntry {
197 public final String token;
198 private final String normalizedToken;
199 public final int startRow;
200 final int numRows; // doesn't count the token row!
201 public List<HtmlEntry> htmlEntries;
203 public IndexEntry(final Index index, final String token, final String normalizedToken,
204 final int startRow, final int numRows, final List<HtmlEntry> htmlEntries) {
205 assert token.equals(token.trim());
206 assert token.length() > 0;
208 this.normalizedToken = normalizedToken;
209 this.startRow = startRow;
210 this.numRows = numRows;
211 this.htmlEntries = htmlEntries;
214 IndexEntry(final Index index, final DataInput raf) throws IOException {
215 token = raf.readUTF();
216 if (index.dict.dictFileVersion >= 7) {
217 startRow = StringUtil.readVarInt(raf);
218 numRows = StringUtil.readVarInt(raf);
220 startRow = raf.readInt();
221 numRows = raf.readInt();
223 final boolean hasNormalizedForm = raf.readBoolean();
224 normalizedToken = hasNormalizedForm ? raf.readUTF() : token;
225 if (index.dict.dictFileVersion >= 7) {
226 int size = StringUtil.readVarInt(raf);
228 this.htmlEntries = Collections.emptyList();
230 final int[] htmlEntryIndices = new int[size];
231 for (int i = 0; i < size; ++i) {
232 htmlEntryIndices[i] = StringUtil.readVarInt(raf);
234 this.htmlEntries = new AbstractList<HtmlEntry>() {
236 public HtmlEntry get(int i) {
237 return index.dict.htmlEntries.get(htmlEntryIndices[i]);
242 return htmlEntryIndices.length;
246 } else if (index.dict.dictFileVersion >= 6) {
247 this.htmlEntries = CachingList.create(
248 RAFList.create((DataInputBuffer)raf, index.dict.htmlEntryIndexSerializer,
249 index.dict.dictFileVersion,
250 index.dict.dictInfo + " htmlEntries: "), 1, false);
252 this.htmlEntries = Collections.emptyList();
256 public void write(DataOutput raf) throws IOException {
258 StringUtil.writeVarInt(raf, startRow);
259 StringUtil.writeVarInt(raf, numRows);
260 final boolean hasNormalizedForm = !token.equals(normalizedToken);
261 raf.writeBoolean(hasNormalizedForm);
262 if (hasNormalizedForm) {
263 raf.writeUTF(normalizedToken);
265 StringUtil.writeVarInt(raf, htmlEntries.size());
266 for (HtmlEntry e : htmlEntries)
267 StringUtil.writeVarInt(raf, e.index());
270 public String toString() {
271 return String.format("%s@%d(%d)", token, startRow, numRows);
274 String normalizedToken() {
275 return normalizedToken;
279 private static final TransformingList.Transformer<IndexEntry, String> INDEX_ENTRY_TO_TOKEN = new TransformingList.Transformer<IndexEntry, String>() {
281 public String transform(IndexEntry t1) {
286 public IndexEntry findExact(final String exactToken) {
287 final int result = Collections.binarySearch(
288 TransformingList.create(sortedIndexEntries, INDEX_ENTRY_TO_TOKEN), exactToken,
289 getSortComparator());
291 return sortedIndexEntries.get(result);
296 public IndexEntry findInsertionPoint(String token, final AtomicBoolean interrupted) {
297 final int index = findInsertionPointIndex(token, interrupted);
298 return index != -1 ? sortedIndexEntries.get(index) : null;
301 private int compareIdx(String token, final Comparator<Object> sortCollator, int idx) {
302 final IndexEntry entry = sortedIndexEntries.get(idx);
303 return NormalizeComparator.compareWithoutDash(token, entry.normalizedToken(), sortCollator, dict.dictFileVersion);
306 private int findMatchLen(final Comparator<Object> sortCollator, String a, String b) {
308 int end = Math.min(a.length(), b.length());
311 int mid = (start + end + 1) / 2;
312 if (sortCollator.compare(a.substring(0, mid), b.substring(0, mid)) == 0)
320 private int findInsertionPointIndex(String token, final AtomicBoolean interrupted) {
321 String orig_token = token;
322 token = normalizeToken(token);
325 int end = sortedIndexEntries.size();
327 final Comparator<Object> sortCollator = sortLanguage.getCollator();
328 while (start < end) {
329 final int mid = (start + end) / 2;
330 if (interrupted.get()) {
333 final IndexEntry midEntry = sortedIndexEntries.get(mid);
335 int comp = NormalizeComparator.compareWithoutDash(token, midEntry.normalizedToken(), sortCollator, dict.dictFileVersion);
337 comp = sortCollator.compare(token, midEntry.normalizedToken());
341 } else if (comp < 0) {
342 // System.out.println("Upper bound: " + midEntry + ", norm=" +
343 // midEntry.normalizedToken() + ", mid=" + mid);
345 // Hack for robustness if sort order is broken
347 compareIdx(token, sortCollator, mid + 1) > 0 &&
348 compareIdx(token, sortCollator, mid + 2) > 0) {
354 // System.out.println("Lower bound: " + midEntry + ", norm=" +
355 // midEntry.normalizedToken() + ", mid=" + mid);
357 // Hack for robustness if sort order is broken
358 if (mid - 2 >= start &&
359 compareIdx(token, sortCollator, mid - 1) < 0 &&
360 compareIdx(token, sortCollator, mid - 2) < 0) {
368 // if the word before is the better match, move
370 if (start > 0 && start < sortedIndexEntries.size()) {
371 String prev = sortedIndexEntries.get(start - 1).normalizedToken();
372 String next = sortedIndexEntries.get(start).normalizedToken();
373 if (findMatchLen(sortCollator, token, prev) >= findMatchLen(sortCollator, token, next))
377 // If the search term was normalized, try to find an exact match first
378 if (!orig_token.equalsIgnoreCase(token)) {
379 int matchLen = findMatchLen(sortCollator, token, sortedIndexEntries.get(start).normalizedToken());
381 while (scan >= 0 && scan < sortedIndexEntries.size()) {
382 IndexEntry e = sortedIndexEntries.get(scan);
383 if (e.token.equalsIgnoreCase(orig_token))
387 if (matchLen > findMatchLen(sortCollator, token, e.normalizedToken()))
389 if (interrupted.get()) return start;
394 // If we search for a substring of a string that's in there, return
396 int result = Math.min(start, sortedIndexEntries.size() - 1);
397 result = windBackCase(sortedIndexEntries.get(result).normalizedToken(), result, interrupted);
401 private int windBackCase(final String token, int result, final AtomicBoolean interrupted) {
402 while (result > 0 && sortedIndexEntries.get(result - 1).normalizedToken().equals(token)) {
404 if (interrupted.get()) {
411 public IndexInfo getIndexInfo() {
412 return new DictionaryInfo.IndexInfo(shortName, sortedIndexEntries.size(), mainTokenCount);
415 private static final int MAX_SEARCH_ROWS = 1000;
417 private final Map<String, Integer> prefixToNumRows = new HashMap<>();
419 private synchronized int getUpperBoundOnRowsStartingWith(final String normalizedPrefix,
420 final int maxRows, final AtomicBoolean interrupted) {
421 final Integer numRows = prefixToNumRows.get(normalizedPrefix);
422 if (numRows != null) {
425 final int insertionPointIndex = findInsertionPointIndex(normalizedPrefix, interrupted);
428 for (int index = insertionPointIndex; index < sortedIndexEntries.size(); ++index) {
429 if (interrupted.get()) {
432 final IndexEntry indexEntry = sortedIndexEntries.get(index);
433 if (!indexEntry.normalizedToken.startsWith(normalizedPrefix) &&
434 !NormalizeComparator.withoutDash(indexEntry.normalizedToken).startsWith(normalizedPrefix)) {
437 rowCount += indexEntry.numRows + indexEntry.htmlEntries.size();
438 if (rowCount > maxRows) {
439 System.out.println("Giving up, too many words with prefix: " + normalizedPrefix);
443 prefixToNumRows.put(normalizedPrefix, rowCount);
447 public final List<RowBase> multiWordSearch(
448 final String searchText, final List<String> searchTokens,
449 final AtomicBoolean interrupted) {
450 final long startMills = System.currentTimeMillis();
451 final List<RowBase> result = new ArrayList<>();
453 final Set<String> normalizedNonStoplist = new HashSet<>();
455 String bestPrefix = null;
456 int leastRows = Integer.MAX_VALUE;
457 final StringBuilder searchTokensRegex = new StringBuilder();
458 for (int i = 0; i < searchTokens.size(); ++i) {
459 if (interrupted.get()) {
462 final String searchToken = searchTokens.get(i);
463 final String normalized = normalizeToken(searchTokens.get(i));
464 // Normalize them all.
465 searchTokens.set(i, normalized);
467 if (!stoplist.contains(searchToken)) {
468 if (normalizedNonStoplist.add(normalized)) {
469 final int numRows = getUpperBoundOnRowsStartingWith(normalized,
470 MAX_SEARCH_ROWS, interrupted);
471 if (numRows != -1 && numRows < leastRows) {
473 // We really are done here.
474 return Collections.emptyList();
477 bestPrefix = normalized;
482 if (searchTokensRegex.length() > 0) {
483 searchTokensRegex.append("[\\s]*");
485 searchTokensRegex.append(Pattern.quote(normalized));
487 final Pattern pattern = Pattern.compile(searchTokensRegex.toString());
489 if (bestPrefix == null) {
490 bestPrefix = searchTokens.get(0);
491 System.out.println("Everything was in the stoplist!");
493 System.out.println("Searching using prefix: " + bestPrefix + ", leastRows=" + leastRows
494 + ", searchTokens=" + searchTokens);
496 // Place to store the things that match.
497 final Map<RowMatchType, List<RowBase>> matches = new EnumMap<>(
499 for (final RowMatchType rowMatchType : RowMatchType.values()) {
500 if (rowMatchType != RowMatchType.NO_MATCH) {
501 matches.put(rowMatchType, new ArrayList<RowBase>());
507 final int exactMatchIndex = findInsertionPointIndex(searchText, interrupted);
508 if (exactMatchIndex != -1) {
509 final IndexEntry exactMatch = sortedIndexEntries.get(exactMatchIndex);
510 if (pattern.matcher(exactMatch.token).find()) {
511 matches.get(RowMatchType.TITLE_MATCH).add(rows.get(exactMatch.startRow));
515 final String searchToken = bestPrefix;
516 final int insertionPointIndex = findInsertionPointIndex(searchToken, interrupted);
517 final Set<RowKey> rowsAlreadySeen = new HashSet<>();
518 for (int index = insertionPointIndex; index < sortedIndexEntries.size()
519 && matchCount < MAX_SEARCH_ROWS; ++index) {
520 if (interrupted.get()) {
523 final IndexEntry indexEntry = sortedIndexEntries.get(index);
524 if (!indexEntry.normalizedToken.startsWith(searchToken) &&
525 !NormalizeComparator.withoutDash(indexEntry.normalizedToken).startsWith(searchToken)) {
529 // System.out.println("Searching indexEntry: " + indexEntry.token);
531 // Extra +1 to skip token row.
532 for (int rowIndex = indexEntry.startRow + 1; rowIndex < indexEntry.startRow + 1
534 && rowIndex < rows.size(); ++rowIndex) {
535 if (interrupted.get()) {
538 final RowBase row = rows.get(rowIndex);
539 final RowBase.RowKey rowKey = row.getRowKey();
540 if (rowsAlreadySeen.contains(rowKey)) {
543 rowsAlreadySeen.add(rowKey);
544 final RowMatchType matchType = row.matches(searchTokens, pattern, normalizer(),
546 if (matchType != RowMatchType.NO_MATCH) {
547 matches.get(matchType).add(row);
554 // Sort them into a reasonable order.
555 final RowBase.LengthComparator lengthComparator = new RowBase.LengthComparator(
557 for (final Collection<RowBase> rows : matches.values()) {
558 final List<RowBase> ordered = new ArrayList<>(rows);
559 Collections.sort(ordered, lengthComparator);
560 result.addAll(ordered);
563 System.out.println("searchDuration: " + (System.currentTimeMillis() - startMills));
567 private String normalizeToken(final String searchToken) {
568 if (TransliteratorManager.init(null, null)) {
569 final Transliterator normalizer = normalizer();
570 return normalizer.transliterate(searchToken);
572 // Do our best since the Transliterators aren't up yet.
573 return searchToken.toLowerCase(Locale.US);