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.DataInput;
18 import java.io.DataOutput;
19 import java.io.IOException;
20 import java.io.PrintStream;
21 import java.io.RandomAccessFile;
22 import java.util.AbstractList;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.Comparator;
27 import java.util.EnumMap;
28 import java.util.HashMap;
29 import java.util.HashSet;
30 import java.util.List;
33 import java.util.concurrent.atomic.AtomicBoolean;
34 import java.util.regex.Pattern;
36 import com.hughes.android.dictionary.DictionaryInfo;
37 import com.hughes.android.dictionary.DictionaryInfo.IndexInfo;
38 import com.hughes.android.dictionary.engine.RowBase.RowKey;
39 import com.hughes.util.CachingList;
40 import com.hughes.util.DataInputBuffer;
41 import com.hughes.util.StringUtil;
42 import com.hughes.util.TransformingList;
43 import com.hughes.util.raf.RAFList;
44 import com.hughes.util.raf.RAFSerializer;
45 import com.hughes.util.raf.UniformRAFList;
46 import com.ibm.icu.text.Transliterator;
48 public final class Index {
50 private static final int CACHE_SIZE = 5000;
52 public final Dictionary dict;
54 public final String shortName; // Typically the ISO code for the language.
55 public final String longName;
57 // persisted: tells how the entries are sorted.
58 public final Language sortLanguage;
59 public final String normalizerRules;
61 // Built from the two above.
62 private Transliterator normalizer;
65 public final List<IndexEntry> sortedIndexEntries;
68 public final Set<String> stoplist;
73 public final List<RowBase> rows;
74 public final boolean swapPairEntries;
77 @SuppressWarnings("WeakerAccess")
78 public int mainTokenCount = -1;
80 // --------------------------------------------------------------------------
82 public Index(final Dictionary dict, final String shortName, final String longName,
83 final Language sortLanguage, final String normalizerRules,
84 final boolean swapPairEntries, final Set<String> stoplist) {
86 this.shortName = shortName;
87 this.longName = longName;
88 this.sortLanguage = sortLanguage;
89 this.normalizerRules = normalizerRules;
90 this.swapPairEntries = swapPairEntries;
91 sortedIndexEntries = new ArrayList<>();
92 this.stoplist = stoplist;
93 rows = new ArrayList<>();
99 * Deferred initialization because it can be slow.
101 @SuppressWarnings("WeakerAccess")
102 public synchronized Transliterator normalizer() {
103 if (normalizer == null) {
104 normalizer = TransliteratorManager.get(normalizerRules);
110 * Note that using this comparator probably involves doing too many text
113 @SuppressWarnings("WeakerAccess")
114 public NormalizeComparator getSortComparator() {
115 return new NormalizeComparator(normalizer(), sortLanguage.getCollator(), dict.dictFileVersion);
118 public Index(final Dictionary dict, final DataInputBuffer raf) throws IOException {
120 shortName = raf.readUTF();
121 longName = raf.readUTF();
122 final String languageCode = raf.readUTF();
123 sortLanguage = Language.lookup(languageCode);
124 normalizerRules = raf.readUTF();
125 swapPairEntries = raf.readBoolean();
126 if (dict.dictFileVersion >= 2) {
127 mainTokenCount = raf.readInt();
129 sortedIndexEntries = CachingList.create(
130 RAFList.create(raf, new IndexEntrySerializer(),
131 dict.dictFileVersion, dict.dictInfo + " idx " + languageCode + ": "), CACHE_SIZE, true);
132 if (dict.dictFileVersion >= 7) {
133 int count = StringUtil.readVarInt(raf);
134 stoplist = new HashSet<>(count);
135 for (int i = 0; i < count; ++i) {
136 stoplist.add(raf.readUTF());
138 } else if (dict.dictFileVersion >= 4) {
139 raf.readInt(); // length
141 stoplist = new HashSet<>();
143 while ((b = raf.readByte()) == 0x74) {
144 stoplist.add(raf.readUTF());
146 if (b != 0x78) throw new IOException("Invalid data in dictionary stoplist!");
148 stoplist = Collections.emptySet();
150 rows = CachingList.create(
151 UniformRAFList.create(raf, new RowBase.Serializer(this)),
155 public void write(final DataOutput out) throws IOException {
156 RandomAccessFile raf = (RandomAccessFile)out;
157 raf.writeUTF(shortName);
158 raf.writeUTF(longName);
159 raf.writeUTF(sortLanguage.getIsoCode());
160 raf.writeUTF(normalizerRules);
161 raf.writeBoolean(swapPairEntries);
162 raf.writeInt(mainTokenCount);
163 RAFList.write(raf, sortedIndexEntries, new IndexEntrySerializer(), 32, true);
164 StringUtil.writeVarInt(raf, stoplist.size());
165 for (String i : stoplist) {
168 UniformRAFList.write(raf, rows, new RowBase.Serializer(this), 3 /* bytes per entry */);
171 public void print(final PrintStream out) {
172 for (final RowBase row : rows) {
177 private final class IndexEntrySerializer implements RAFSerializer<IndexEntry> {
179 public IndexEntry read(DataInput raf) throws IOException {
180 return new IndexEntry(Index.this, raf);
184 public void write(DataOutput raf, IndexEntry t) throws IOException {
189 public static final class IndexEntry {
190 public final String token;
191 private final String normalizedToken;
192 public final int startRow;
193 final int numRows; // doesn't count the token row!
194 public List<HtmlEntry> htmlEntries;
196 public IndexEntry(final Index index, final String token, final String normalizedToken,
197 final int startRow, final int numRows) {
198 assert token.equals(token.trim());
199 assert token.length() > 0;
201 this.normalizedToken = normalizedToken;
202 this.startRow = startRow;
203 this.numRows = numRows;
204 this.htmlEntries = new ArrayList<>();
207 IndexEntry(final Index index, final DataInput raf) throws IOException {
208 token = raf.readUTF();
209 if (index.dict.dictFileVersion >= 7) {
210 startRow = StringUtil.readVarInt(raf);
211 numRows = StringUtil.readVarInt(raf);
213 startRow = raf.readInt();
214 numRows = raf.readInt();
216 final boolean hasNormalizedForm = raf.readBoolean();
217 normalizedToken = hasNormalizedForm ? raf.readUTF() : token;
218 if (index.dict.dictFileVersion >= 7) {
219 int size = StringUtil.readVarInt(raf);
221 this.htmlEntries = Collections.emptyList();
223 final int[] htmlEntryIndices = new int[size];
224 for (int i = 0; i < size; ++i) {
225 htmlEntryIndices[i] = StringUtil.readVarInt(raf);
227 this.htmlEntries = new AbstractList<HtmlEntry>() {
229 public HtmlEntry get(int i) {
230 return index.dict.htmlEntries.get(htmlEntryIndices[i]);
235 return htmlEntryIndices.length;
239 } else if (index.dict.dictFileVersion >= 6) {
240 this.htmlEntries = CachingList.create(
241 RAFList.create((DataInputBuffer)raf, index.dict.htmlEntryIndexSerializer,
242 index.dict.dictFileVersion,
243 index.dict.dictInfo + " htmlEntries: "), 1, false);
245 this.htmlEntries = Collections.emptyList();
249 public void write(DataOutput raf) throws IOException {
251 StringUtil.writeVarInt(raf, startRow);
252 StringUtil.writeVarInt(raf, numRows);
253 final boolean hasNormalizedForm = !token.equals(normalizedToken);
254 raf.writeBoolean(hasNormalizedForm);
255 if (hasNormalizedForm) {
256 raf.writeUTF(normalizedToken);
258 StringUtil.writeVarInt(raf, htmlEntries.size());
259 for (HtmlEntry e : htmlEntries)
260 StringUtil.writeVarInt(raf, e.index());
263 public String toString() {
264 return String.format("%s@%d(%d)", token, startRow, numRows);
267 String normalizedToken() {
268 return normalizedToken;
272 private static final TransformingList.Transformer<IndexEntry, String> INDEX_ENTRY_TO_TOKEN = new TransformingList.Transformer<IndexEntry, String>() {
274 public String transform(IndexEntry t1) {
279 public IndexEntry findExact(final String exactToken) {
280 final int result = Collections.binarySearch(
281 TransformingList.create(sortedIndexEntries, INDEX_ENTRY_TO_TOKEN), exactToken,
282 getSortComparator());
284 return sortedIndexEntries.get(result);
289 public IndexEntry findInsertionPoint(String token, final AtomicBoolean interrupted) {
290 final int index = findInsertionPointIndex(token, interrupted);
291 return index != -1 ? sortedIndexEntries.get(index) : null;
294 private int compareIdx(String token, final Comparator<Object> sortCollator, int idx) {
295 final IndexEntry entry = sortedIndexEntries.get(idx);
296 return NormalizeComparator.compareWithoutDash(token, entry.normalizedToken(), sortCollator, dict.dictFileVersion);
299 private int findMatchLen(final Comparator<Object> sortCollator, String a, String b) {
301 int end = Math.min(a.length(), b.length());
304 int mid = (start + end + 1) / 2;
305 if (sortCollator.compare(a.substring(0, mid), b.substring(0, mid)) == 0)
313 private int findInsertionPointIndex(String token, final AtomicBoolean interrupted) {
314 token = normalizeToken(token);
317 int end = sortedIndexEntries.size();
319 final Comparator<Object> sortCollator = sortLanguage.getCollator();
320 while (start < end) {
321 final int mid = (start + end) / 2;
322 if (interrupted.get()) {
325 final IndexEntry midEntry = sortedIndexEntries.get(mid);
327 int comp = NormalizeComparator.compareWithoutDash(token, midEntry.normalizedToken(), sortCollator, dict.dictFileVersion);
329 comp = sortCollator.compare(token, midEntry.normalizedToken());
331 return windBackCase(token, mid, interrupted);
332 } else if (comp < 0) {
333 // System.out.println("Upper bound: " + midEntry + ", norm=" +
334 // midEntry.normalizedToken() + ", mid=" + mid);
336 // Hack for robustness if sort order is broken
338 compareIdx(token, sortCollator, mid + 1) > 0 &&
339 compareIdx(token, sortCollator, mid + 2) > 0) {
345 // System.out.println("Lower bound: " + midEntry + ", norm=" +
346 // midEntry.normalizedToken() + ", mid=" + mid);
348 // Hack for robustness if sort order is broken
349 if (mid - 2 >= start &&
350 compareIdx(token, sortCollator, mid - 1) < 0 &&
351 compareIdx(token, sortCollator, mid - 2) < 0) {
359 // if the word before is the better match, move
361 if (start > 0 && start < sortedIndexEntries.size()) {
362 String prev = sortedIndexEntries.get(start - 1).normalizedToken();
363 String next = sortedIndexEntries.get(start).normalizedToken();
364 if (findMatchLen(sortCollator, token, prev) >= findMatchLen(sortCollator, token, next))
368 // If we search for a substring of a string that's in there, return
370 int result = Math.min(start, sortedIndexEntries.size() - 1);
371 result = windBackCase(sortedIndexEntries.get(result).normalizedToken(), result, interrupted);
375 private int windBackCase(final String token, int result, final AtomicBoolean interrupted) {
376 while (result > 0 && sortedIndexEntries.get(result - 1).normalizedToken().equals(token)) {
378 if (interrupted.get()) {
385 public IndexInfo getIndexInfo() {
386 return new DictionaryInfo.IndexInfo(shortName, sortedIndexEntries.size(), mainTokenCount);
389 private static final int MAX_SEARCH_ROWS = 1000;
391 private final Map<String, Integer> prefixToNumRows = new HashMap<>();
393 private synchronized int getUpperBoundOnRowsStartingWith(final String normalizedPrefix,
394 final int maxRows, final AtomicBoolean interrupted) {
395 final Integer numRows = prefixToNumRows.get(normalizedPrefix);
396 if (numRows != null) {
399 final int insertionPointIndex = findInsertionPointIndex(normalizedPrefix, interrupted);
402 for (int index = insertionPointIndex; index < sortedIndexEntries.size(); ++index) {
403 if (interrupted.get()) {
406 final IndexEntry indexEntry = sortedIndexEntries.get(index);
407 if (!indexEntry.normalizedToken.startsWith(normalizedPrefix) &&
408 !NormalizeComparator.withoutDash(indexEntry.normalizedToken).startsWith(normalizedPrefix)) {
411 rowCount += indexEntry.numRows + indexEntry.htmlEntries.size();
412 if (rowCount > maxRows) {
413 System.out.println("Giving up, too many words with prefix: " + normalizedPrefix);
417 prefixToNumRows.put(normalizedPrefix, rowCount);
421 public final List<RowBase> multiWordSearch(
422 final String searchText, final List<String> searchTokens,
423 final AtomicBoolean interrupted) {
424 final long startMills = System.currentTimeMillis();
425 final List<RowBase> result = new ArrayList<>();
427 final Set<String> normalizedNonStoplist = new HashSet<>();
429 String bestPrefix = null;
430 int leastRows = Integer.MAX_VALUE;
431 final StringBuilder searchTokensRegex = new StringBuilder();
432 for (int i = 0; i < searchTokens.size(); ++i) {
433 if (interrupted.get()) {
436 final String searchToken = searchTokens.get(i);
437 final String normalized = normalizeToken(searchTokens.get(i));
438 // Normalize them all.
439 searchTokens.set(i, normalized);
441 if (!stoplist.contains(searchToken)) {
442 if (normalizedNonStoplist.add(normalized)) {
443 final int numRows = getUpperBoundOnRowsStartingWith(normalized,
444 MAX_SEARCH_ROWS, interrupted);
445 if (numRows != -1 && numRows < leastRows) {
447 // We really are done here.
448 return Collections.emptyList();
451 bestPrefix = normalized;
456 if (searchTokensRegex.length() > 0) {
457 searchTokensRegex.append("[\\s]*");
459 searchTokensRegex.append(Pattern.quote(normalized));
461 final Pattern pattern = Pattern.compile(searchTokensRegex.toString());
463 if (bestPrefix == null) {
464 bestPrefix = searchTokens.get(0);
465 System.out.println("Everything was in the stoplist!");
467 System.out.println("Searching using prefix: " + bestPrefix + ", leastRows=" + leastRows
468 + ", searchTokens=" + searchTokens);
470 // Place to store the things that match.
471 final Map<RowMatchType, List<RowBase>> matches = new EnumMap<>(
473 for (final RowMatchType rowMatchType : RowMatchType.values()) {
474 if (rowMatchType != RowMatchType.NO_MATCH) {
475 matches.put(rowMatchType, new ArrayList<RowBase>());
481 final int exactMatchIndex = findInsertionPointIndex(searchText, interrupted);
482 if (exactMatchIndex != -1) {
483 final IndexEntry exactMatch = sortedIndexEntries.get(exactMatchIndex);
484 if (pattern.matcher(exactMatch.token).find()) {
485 matches.get(RowMatchType.TITLE_MATCH).add(rows.get(exactMatch.startRow));
489 final String searchToken = bestPrefix;
490 final int insertionPointIndex = findInsertionPointIndex(searchToken, interrupted);
491 final Set<RowKey> rowsAlreadySeen = new HashSet<>();
492 for (int index = insertionPointIndex; index < sortedIndexEntries.size()
493 && matchCount < MAX_SEARCH_ROWS; ++index) {
494 if (interrupted.get()) {
497 final IndexEntry indexEntry = sortedIndexEntries.get(index);
498 if (!indexEntry.normalizedToken.startsWith(searchToken) &&
499 !NormalizeComparator.withoutDash(indexEntry.normalizedToken).startsWith(searchToken)) {
503 // System.out.println("Searching indexEntry: " + indexEntry.token);
505 // Extra +1 to skip token row.
506 for (int rowIndex = indexEntry.startRow + 1; rowIndex < indexEntry.startRow + 1
508 && rowIndex < rows.size(); ++rowIndex) {
509 if (interrupted.get()) {
512 final RowBase row = rows.get(rowIndex);
513 final RowBase.RowKey rowKey = row.getRowKey();
514 if (rowsAlreadySeen.contains(rowKey)) {
517 rowsAlreadySeen.add(rowKey);
518 final RowMatchType matchType = row.matches(searchTokens, pattern, normalizer(),
520 if (matchType != RowMatchType.NO_MATCH) {
521 matches.get(matchType).add(row);
528 // Sort them into a reasonable order.
529 final RowBase.LengthComparator lengthComparator = new RowBase.LengthComparator(
531 for (final Collection<RowBase> rows : matches.values()) {
532 final List<RowBase> ordered = new ArrayList<>(rows);
533 ordered.sort(lengthComparator);
534 result.addAll(ordered);
537 System.out.println("searchDuration: " + (System.currentTimeMillis() - startMills));
541 private String normalizeToken(final String searchToken) {
542 if (TransliteratorManager.init(null, null)) {
543 final Transliterator normalizer = normalizer();
544 return normalizer.transliterate(searchToken);
546 // Do our best since the Transliterators aren't up yet.
547 return searchToken.toLowerCase();