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.
19 package com.hughes.android.dictionary.engine;
21 import com.hughes.android.dictionary.DictionaryInfo;
22 import com.hughes.android.dictionary.DictionaryInfo.IndexInfo;
23 import com.hughes.android.dictionary.engine.RowBase.RowKey;
24 import com.hughes.util.CachingList;
25 import com.hughes.util.StringUtil;
26 import com.hughes.util.TransformingList;
27 import com.hughes.util.raf.RAFList;
28 import com.hughes.util.raf.RAFSerializable;
29 import com.hughes.util.raf.RAFSerializer;
30 import com.hughes.util.raf.SerializableSerializer;
31 import com.hughes.util.raf.UniformRAFList;
32 import com.ibm.icu.text.Transliterator;
34 import java.io.DataInput;
35 import java.io.DataOutput;
36 import java.io.IOException;
37 import java.io.PrintStream;
38 import java.io.RandomAccessFile;
39 import java.util.AbstractList;
40 import java.util.ArrayList;
41 import java.util.Collection;
42 import java.util.Collections;
43 import java.util.Comparator;
44 import java.util.EnumMap;
45 import java.util.HashSet;
46 import java.util.HashMap;
47 import java.util.List;
50 import java.util.concurrent.atomic.AtomicBoolean;
51 import java.util.regex.Pattern;
53 public final class Index implements RAFSerializable<Index> {
55 static final int CACHE_SIZE = 5000;
57 public final Dictionary dict;
59 public final String shortName; // Typically the ISO code for the language.
60 public final String longName;
62 // persisted: tells how the entries are sorted.
63 public final Language sortLanguage;
64 final String normalizerRules;
66 // Built from the two above.
67 private Transliterator normalizer;
70 public final List<IndexEntry> sortedIndexEntries;
73 public final Set<String> stoplist;
78 public final List<RowBase> rows;
79 public final boolean swapPairEntries;
82 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<IndexEntry>();
96 this.stoplist = stoplist;
97 rows = new ArrayList<RowBase>();
103 * Deferred initialization because it can be slow.
105 public synchronized Transliterator normalizer() {
106 if (normalizer == null) {
107 normalizer = TransliteratorManager.get(normalizerRules);
113 * Note that using this comparator probably involves doing too many text
116 public NormalizeComparator getSortComparator() {
117 return new NormalizeComparator(normalizer(), sortLanguage.getCollator(), dict.dictFileVersion);
120 public Index(final Dictionary dict, final DataInput inp) throws IOException {
122 RandomAccessFile raf = (RandomAccessFile)inp;
123 shortName = raf.readUTF();
124 longName = raf.readUTF();
125 final String languageCode = raf.readUTF();
126 sortLanguage = Language.lookup(languageCode);
127 normalizerRules = raf.readUTF();
128 swapPairEntries = raf.readBoolean();
129 if (sortLanguage == null) {
130 throw new IOException("Unsupported language: " + languageCode);
132 if (dict.dictFileVersion >= 2) {
133 mainTokenCount = raf.readInt();
135 sortedIndexEntries = CachingList.create(
136 RAFList.create(raf, indexEntrySerializer, raf.getFilePointer(),
137 dict.dictFileVersion, dict.dictInfo + " idx " + languageCode + ": "), CACHE_SIZE);
138 if (dict.dictFileVersion >= 7) {
139 int count = StringUtil.readVarInt(raf);
140 stoplist = new HashSet<String>(count);
141 for (int i = 0; i < count; ++i) {
142 stoplist.add(raf.readUTF());
144 } else if (dict.dictFileVersion >= 4) {
145 stoplist = new SerializableSerializer<Set<String>>().read(raf);
147 stoplist = Collections.emptySet();
149 rows = CachingList.create(
150 UniformRAFList.create(raf, new RowBase.Serializer(this), raf.getFilePointer()),
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 if (dict.dictFileVersion >= 2) {
163 raf.writeInt(mainTokenCount);
165 RAFList.write(raf, sortedIndexEntries, indexEntrySerializer, 32, true);
166 StringUtil.writeVarInt(raf, stoplist.size());
167 for (String i : stoplist) {
170 UniformRAFList.write(raf, rows, new RowBase.Serializer(this), 3 /* bytes per entry */);
173 public void print(final PrintStream out) {
174 for (final RowBase row : rows) {
179 private final RAFSerializer<IndexEntry> indexEntrySerializer = new RAFSerializer<IndexEntry>() {
181 public IndexEntry read(DataInput raf) throws IOException {
182 return new IndexEntry(Index.this, raf);
186 public void write(DataOutput raf, IndexEntry t) throws IOException {
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 public final int numRows; // doesn't count the token row!
196 public List<HtmlEntry> htmlEntries;
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;
203 this.normalizedToken = normalizedToken;
204 this.startRow = startRow;
205 this.numRows = numRows;
206 this.htmlEntries = new ArrayList<HtmlEntry>();
209 public IndexEntry(final Index index, 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);
215 startRow = raf.readInt();
216 numRows = raf.readInt();
218 final boolean hasNormalizedForm = raf.readBoolean();
219 normalizedToken = hasNormalizedForm ? raf.readUTF() : token;
220 if (index.dict.dictFileVersion >= 7) {
221 int size = StringUtil.readVarInt(raf);
223 this.htmlEntries = Collections.emptyList();
225 final int[] htmlEntryIndices = new int[size];
226 for (int i = 0; i < size; ++i) {
227 htmlEntryIndices[i] = StringUtil.readVarInt(raf);
229 this.htmlEntries = new AbstractList<HtmlEntry>() {
231 public HtmlEntry get(int i) {
232 return index.dict.htmlEntries.get(htmlEntryIndices[i]);
236 return htmlEntryIndices.length;
240 } else if (index.dict.dictFileVersion >= 6) {
241 this.htmlEntries = CachingList.create(
242 RAFList.create((RandomAccessFile)raf, index.dict.htmlEntryIndexSerializer,
243 ((RandomAccessFile)raf).getFilePointer(), index.dict.dictFileVersion,
244 index.dict.dictInfo + " htmlEntries: "), 1);
246 this.htmlEntries = Collections.emptyList();
250 public void write(DataOutput raf) throws IOException {
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);
259 StringUtil.writeVarInt(raf, htmlEntries.size());
260 for (HtmlEntry e : htmlEntries)
261 StringUtil.writeVarInt(raf, e.index());
264 public String toString() {
265 return String.format("%s@%d(%d)", token, startRow, numRows);
268 public String normalizedToken() {
269 return normalizedToken;
273 static final TransformingList.Transformer<IndexEntry, String> INDEX_ENTRY_TO_TOKEN = new TransformingList.Transformer<IndexEntry, String>() {
275 public String transform(IndexEntry t1) {
280 public IndexEntry findExact(final String exactToken) {
281 final int result = Collections.binarySearch(
282 TransformingList.create(sortedIndexEntries, INDEX_ENTRY_TO_TOKEN), exactToken,
283 getSortComparator());
285 return sortedIndexEntries.get(result);
290 public IndexEntry findInsertionPoint(String token, final AtomicBoolean interrupted) {
291 final int index = findInsertionPointIndex(token, interrupted);
292 return index != -1 ? sortedIndexEntries.get(index) : null;
295 private int compareIdx(String token, final Comparator sortCollator, int idx) {
296 final IndexEntry entry = sortedIndexEntries.get(idx);
297 return NormalizeComparator.compareWithoutDash(token, entry.normalizedToken(), sortCollator, dict.dictFileVersion);
300 public int findInsertionPointIndex(String token, final AtomicBoolean interrupted) {
301 token = normalizeToken(token);
304 int end = sortedIndexEntries.size();
306 final Comparator sortCollator = sortLanguage.getCollator();
307 while (start < end) {
308 final int mid = (start + end) / 2;
309 if (interrupted.get()) {
312 final IndexEntry midEntry = sortedIndexEntries.get(mid);
314 int comp = NormalizeComparator.compareWithoutDash(token, midEntry.normalizedToken(), sortCollator, dict.dictFileVersion);
316 comp = sortCollator.compare(token, midEntry.normalizedToken());
318 final int result = windBackCase(token, mid, interrupted);
320 } else if (comp < 0) {
321 // System.out.println("Upper bound: " + midEntry + ", norm=" +
322 // midEntry.normalizedToken() + ", mid=" + mid);
324 // Hack for robustness if sort order is broken
326 compareIdx(token, sortCollator, mid + 1) > 0 &&
327 compareIdx(token, sortCollator, mid + 2) > 0) {
333 // System.out.println("Lower bound: " + midEntry + ", norm=" +
334 // midEntry.normalizedToken() + ", mid=" + mid);
336 // Hack for robustness if sort order is broken
337 if (mid - 2 >= start &&
338 compareIdx(token, sortCollator, mid - 1) < 0 &&
339 compareIdx(token, sortCollator, mid - 2) < 0) {
347 // If we search for a substring of a string that's in there, return
349 int result = Math.min(start, sortedIndexEntries.size() - 1);
350 result = windBackCase(sortedIndexEntries.get(result).normalizedToken(), result, interrupted);
354 private final int windBackCase(final String token, int result, final AtomicBoolean interrupted) {
355 while (result > 0 && sortedIndexEntries.get(result - 1).normalizedToken().equals(token)) {
357 if (interrupted.get()) {
364 public IndexInfo getIndexInfo() {
365 return new DictionaryInfo.IndexInfo(shortName, sortedIndexEntries.size(), mainTokenCount);
368 private static final int MAX_SEARCH_ROWS = 1000;
370 private final Map<String, Integer> prefixToNumRows = new HashMap<String, Integer>();
372 private synchronized final int getUpperBoundOnRowsStartingWith(final String normalizedPrefix,
373 final int maxRows, final AtomicBoolean interrupted) {
374 final Integer numRows = prefixToNumRows.get(normalizedPrefix);
375 if (numRows != null) {
378 final int insertionPointIndex = findInsertionPointIndex(normalizedPrefix, interrupted);
381 for (int index = insertionPointIndex; index < sortedIndexEntries.size(); ++index) {
382 if (interrupted.get()) {
385 final IndexEntry indexEntry = sortedIndexEntries.get(index);
386 if (!indexEntry.normalizedToken.startsWith(normalizedPrefix)) {
389 rowCount += indexEntry.numRows + indexEntry.htmlEntries.size();
390 if (rowCount > maxRows) {
391 System.out.println("Giving up, too many words with prefix: " + normalizedPrefix);
395 prefixToNumRows.put(normalizedPrefix, numRows);
399 public final List<RowBase> multiWordSearch(
400 final String searchText, final List<String> searchTokens,
401 final AtomicBoolean interrupted) {
402 final long startMills = System.currentTimeMillis();
403 final List<RowBase> result = new ArrayList<RowBase>();
405 final Set<String> normalizedNonStoplist = new HashSet<String>();
407 String bestPrefix = null;
408 int leastRows = Integer.MAX_VALUE;
409 final StringBuilder searchTokensRegex = new StringBuilder();
410 for (int i = 0; i < searchTokens.size(); ++i) {
411 if (interrupted.get()) {
414 final String searchToken = searchTokens.get(i);
415 final String normalized = normalizeToken(searchTokens.get(i));
416 // Normalize them all.
417 searchTokens.set(i, normalized);
419 if (!stoplist.contains(searchToken)) {
420 if (normalizedNonStoplist.add(normalized)) {
421 final int numRows = getUpperBoundOnRowsStartingWith(normalized,
422 MAX_SEARCH_ROWS, interrupted);
423 if (numRows != -1 && numRows < leastRows) {
425 // We really are done here.
426 return Collections.emptyList();
429 bestPrefix = normalized;
434 if (searchTokensRegex.length() > 0) {
435 searchTokensRegex.append("[\\s]*");
437 searchTokensRegex.append(Pattern.quote(normalized));
439 final Pattern pattern = Pattern.compile(searchTokensRegex.toString());
441 if (bestPrefix == null) {
442 bestPrefix = searchTokens.get(0);
443 System.out.println("Everything was in the stoplist!");
445 System.out.println("Searching using prefix: " + bestPrefix + ", leastRows=" + leastRows
446 + ", searchTokens=" + searchTokens);
448 // Place to store the things that match.
449 final Map<RowMatchType, List<RowBase>> matches = new EnumMap<RowMatchType, List<RowBase>>(
451 for (final RowMatchType rowMatchType : RowMatchType.values()) {
452 if (rowMatchType != RowMatchType.NO_MATCH) {
453 matches.put(rowMatchType, new ArrayList<RowBase>());
459 final int exactMatchIndex = findInsertionPointIndex(searchText, interrupted);
460 if (exactMatchIndex != -1) {
461 final IndexEntry exactMatch = sortedIndexEntries.get(exactMatchIndex);
462 if (pattern.matcher(exactMatch.token).find()) {
463 matches.get(RowMatchType.TITLE_MATCH).add(rows.get(exactMatch.startRow));
467 final String searchToken = bestPrefix;
468 final int insertionPointIndex = findInsertionPointIndex(searchToken, interrupted);
469 final Set<RowKey> rowsAlreadySeen = new HashSet<RowBase.RowKey>();
470 for (int index = insertionPointIndex; index < sortedIndexEntries.size()
471 && matchCount < MAX_SEARCH_ROWS; ++index) {
472 if (interrupted.get()) {
475 final IndexEntry indexEntry = sortedIndexEntries.get(index);
476 if (!indexEntry.normalizedToken.startsWith(searchToken)) {
480 // System.out.println("Searching indexEntry: " + indexEntry.token);
482 // Extra +1 to skip token row.
483 for (int rowIndex = indexEntry.startRow + 1; rowIndex < indexEntry.startRow + 1
485 && rowIndex < rows.size(); ++rowIndex) {
486 if (interrupted.get()) {
489 final RowBase row = rows.get(rowIndex);
490 final RowBase.RowKey rowKey = row.getRowKey();
491 if (rowsAlreadySeen.contains(rowKey)) {
494 rowsAlreadySeen.add(rowKey);
495 final RowMatchType matchType = row.matches(searchTokens, pattern, normalizer(),
497 if (matchType != RowMatchType.NO_MATCH) {
498 matches.get(matchType).add(row);
505 // Sort them into a reasonable order.
506 final RowBase.LengthComparator lengthComparator = new RowBase.LengthComparator(
508 for (final Collection<RowBase> rows : matches.values()) {
509 final List<RowBase> ordered = new ArrayList<RowBase>(rows);
510 Collections.sort(ordered, lengthComparator);
511 result.addAll(ordered);
514 System.out.println("searchDuration: " + (System.currentTimeMillis() - startMills));
518 private String normalizeToken(final String searchToken) {
519 if (TransliteratorManager.init(null, null)) {
520 final Transliterator normalizer = normalizer();
521 return normalizer.transliterate(searchToken);
523 // Do our best since the Transliterators aren't up yet.
524 return searchToken.toLowerCase();