am 12e63799: Merge "Quit sorting suggestions in AndroidSpellCheckerService."

* commit '12e63799d37c21362b146df0d45449ae17d8b85f':
  Quit sorting suggestions in AndroidSpellCheckerService.
main
Keisuke Kuroyanagi 2014-07-10 07:26:57 +00:00 committed by Android Git Automerger
commit f2067d6987
2 changed files with 19 additions and 55 deletions

View File

@ -231,66 +231,43 @@ public final class AndroidSpellCheckerService extends SpellCheckerService
} }
private final ArrayList<String> mSuggestions; private final ArrayList<String> mSuggestions;
private final int[] mScores; private final ArrayList<Integer> mScores;
private final String mOriginalText; private final String mOriginalText;
private final float mRecommendedThreshold; private final float mRecommendedThreshold;
private final int mMaxLength; private final int mMaxLength;
private int mLength = 0;
SuggestionsGatherer(final String originalText, final float recommendedThreshold, SuggestionsGatherer(final String originalText, final float recommendedThreshold,
final int maxLength) { final int maxLength) {
mOriginalText = originalText; mOriginalText = originalText;
mRecommendedThreshold = recommendedThreshold; mRecommendedThreshold = recommendedThreshold;
mMaxLength = maxLength; mMaxLength = maxLength;
mSuggestions = new ArrayList<>(maxLength + 1); mSuggestions = new ArrayList<>();
mScores = new int[mMaxLength]; mScores = new ArrayList<>();
} }
synchronized public boolean addWord(char[] word, int[] spaceIndices, int wordOffset, public void addResults(final SuggestionResults suggestionResults) {
int wordLength, int score) { if (suggestionResults == null) {
final int positionIndex = Arrays.binarySearch(mScores, 0, mLength, score); return;
// binarySearch returns the index if the element exists, and -<insertion index> - 1
// if it doesn't. See documentation for binarySearch.
final int insertIndex = positionIndex >= 0 ? positionIndex : -positionIndex - 1;
// Weak <- insertIndex == 0, ..., insertIndex == mLength -> Strong
if (insertIndex == 0 && mLength >= mMaxLength) {
return true;
} }
// suggestionResults is sorted.
final String wordString = new String(word, wordOffset, wordLength); for (final SuggestedWordInfo suggestedWordInfo : suggestionResults) {
if (mLength < mMaxLength) { mSuggestions.add(suggestedWordInfo.mWord);
final int copyLen = mLength - insertIndex; mScores.add(suggestedWordInfo.mScore);
++mLength;
System.arraycopy(mScores, insertIndex, mScores, insertIndex + 1, copyLen);
mSuggestions.add(insertIndex, wordString);
mScores[insertIndex] = score;
} else {
System.arraycopy(mScores, 1, mScores, 0, insertIndex - 1);
mSuggestions.add(insertIndex, wordString);
mSuggestions.remove(0);
mScores[insertIndex - 1] = score;
} }
return true;
} }
public Result getResults(final int capitalizeType, final Locale locale) { public Result getResults(final int capitalizeType, final Locale locale) {
final String[] gatheredSuggestions; final String[] gatheredSuggestions;
final boolean hasRecommendedSuggestions; final boolean hasRecommendedSuggestions;
if (0 == mLength) { if (mSuggestions.isEmpty()) {
gatheredSuggestions = null; gatheredSuggestions = null;
hasRecommendedSuggestions = false; hasRecommendedSuggestions = false;
} else { } else {
if (DBG) { if (DBG) {
if (mLength != mSuggestions.size()) { for (int i = 0; i < mSuggestions.size(); i++) {
Log.e(TAG, "Suggestion size is not the same as stored mLength"); Log.i(TAG, "" + mScores.get(i) + " " + mSuggestions.get(i));
}
for (int i = mLength - 1; i >= 0; --i) {
Log.i(TAG, "" + mScores[i] + " " + mSuggestions.get(i));
} }
} }
Collections.reverse(mSuggestions);
StringUtils.removeDupes(mSuggestions); StringUtils.removeDupes(mSuggestions);
if (StringUtils.CAPITALIZE_ALL == capitalizeType) { if (StringUtils.CAPITALIZE_ALL == capitalizeType) {
for (int i = 0; i < mSuggestions.size(); ++i) { for (int i = 0; i < mSuggestions.size(); ++i) {
@ -309,7 +286,7 @@ public final class AndroidSpellCheckerService extends SpellCheckerService
// into a String[]. // into a String[].
gatheredSuggestions = mSuggestions.toArray(EMPTY_STRING_ARRAY); gatheredSuggestions = mSuggestions.toArray(EMPTY_STRING_ARRAY);
final int bestScore = mScores[mLength - 1]; final int bestScore = mScores.get(0);
final String bestSuggestion = mSuggestions.get(0); final String bestSuggestion = mSuggestions.get(0);
final float normalizedScore = final float normalizedScore =
BinaryDictionaryUtils.calcNormalizedScore( BinaryDictionaryUtils.calcNormalizedScore(

View File

@ -259,14 +259,6 @@ public abstract class AndroidWordLevelSpellCheckerSession extends Session {
} }
final String text = inText.replaceAll( final String text = inText.replaceAll(
AndroidSpellCheckerService.APOSTROPHE, AndroidSpellCheckerService.SINGLE_QUOTE); AndroidSpellCheckerService.APOSTROPHE, AndroidSpellCheckerService.SINGLE_QUOTE);
// TODO: Don't gather suggestions if the limit is <= 0 unless necessary
//final SuggestionsGatherer suggestionsGatherer = new SuggestionsGatherer(text,
//mService.mSuggestionThreshold, mService.mRecommendedThreshold,
//suggestionsLimit);
final SuggestionsGatherer suggestionsGatherer = mService.newSuggestionsGatherer(
text, suggestionsLimit);
final int capitalizeType = StringUtils.getCapitalizationType(text); final int capitalizeType = StringUtils.getCapitalizationType(text);
boolean isInDict = true; boolean isInDict = true;
if (!mService.hasMainDictionaryForLocale(mLocale)) { if (!mService.hasMainDictionaryForLocale(mLocale)) {
@ -289,18 +281,13 @@ public abstract class AndroidWordLevelSpellCheckerSession extends Session {
composer.setComposingWord(codePoints, coordinates); composer.setComposingWord(codePoints, coordinates);
final SuggestionResults suggestionResults = mService.getSuggestionResults( final SuggestionResults suggestionResults = mService.getSuggestionResults(
mLocale, composer, prevWordsInfo, proximityInfo); mLocale, composer, prevWordsInfo, proximityInfo);
if (suggestionResults != null) { // TODO: Don't gather suggestions if the limit is <= 0 unless necessary
for (final SuggestedWordInfo suggestion : suggestionResults) { final SuggestionsGatherer suggestionsGatherer = mService.newSuggestionsGatherer(
final String suggestionStr = suggestion.mWord; text, suggestionsLimit);
suggestionsGatherer.addWord(suggestionStr.toCharArray(), null, 0, suggestionsGatherer.addResults(suggestionResults);
suggestionStr.length(), suggestion.mScore);
}
}
isInDict = isInDictForAnyCapitalization(text, capitalizeType);
final SuggestionsGatherer.Result result = suggestionsGatherer.getResults( final SuggestionsGatherer.Result result = suggestionsGatherer.getResults(
capitalizeType, mLocale); capitalizeType, mLocale);
isInDict = isInDictForAnyCapitalization(text, capitalizeType);
if (DBG) { if (DBG) {
Log.i(TAG, "Spell checking results for " + text + " with suggestion limit " Log.i(TAG, "Spell checking results for " + text + " with suggestion limit "
+ suggestionsLimit); + suggestionsLimit);