Skip to content

Commit

Permalink
Try to find an exact, non-normalized match.
Browse files Browse the repository at this point in the history
If the search string had to be normalized the match
we found might not be the best one.

Fixes GitHub issue #131.
  • Loading branch information
rdoeffinger committed Dec 4, 2020
1 parent ebaed38 commit ff4d0ec
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/com/hughes/android/dictionary/engine/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ private int findMatchLen(final Comparator<Object> sortCollator, String a, String
}

private int findInsertionPointIndex(String token, final AtomicBoolean interrupted) {
String orig_token = token;
token = normalizeToken(token);

int start = 0;
Expand All @@ -329,7 +330,8 @@ private int findInsertionPointIndex(String token, final AtomicBoolean interrupte
if (comp == 0)
comp = sortCollator.compare(token, midEntry.normalizedToken());
if (comp == 0) {
return windBackCase(token, mid, interrupted);
start = end = mid;
break;
} else if (comp < 0) {
// System.out.println("Upper bound: " + midEntry + ", norm=" +
// midEntry.normalizedToken() + ", mid=" + mid);
Expand Down Expand Up @@ -366,6 +368,23 @@ private int findInsertionPointIndex(String token, final AtomicBoolean interrupte
start--;
}

// If the search term was normalized, try to find an exact match first
if (!orig_token.equalsIgnoreCase(token)) {
int matchLen = findMatchLen(sortCollator, token, sortedIndexEntries.get(start).normalizedToken());
int scan = start;
while (scan >= 0 && scan < sortedIndexEntries.size()) {
IndexEntry e = sortedIndexEntries.get(scan);
if (e.token.equalsIgnoreCase(orig_token))
{
return scan;
}
if (matchLen > findMatchLen(sortCollator, token, e.normalizedToken()))
break;
if (interrupted.get()) return start;
scan++;
}
}

// If we search for a substring of a string that's in there, return
// that.
int result = Math.min(start, sortedIndexEntries.size() - 1);
Expand Down

0 comments on commit ff4d0ec

Please sign in to comment.