am 5434f464: Remove unexpectedly split words with single quotes

* commit '5434f46481c6331c3f107e6940cb49ba9dd5ea4d':
  Remove unexpectedly split words with single quotes
This commit is contained in:
satok 2012-05-11 03:33:41 -07:00 committed by Android Git Automerger
commit 59f091875c

View file

@ -23,6 +23,7 @@ import android.service.textservice.SpellCheckerService;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.util.LruCache; import android.util.LruCache;
import android.view.textservice.SentenceSuggestionsInfo;
import android.view.textservice.SuggestionsInfo; import android.view.textservice.SuggestionsInfo;
import android.view.textservice.TextInfo; import android.view.textservice.TextInfo;
@ -487,6 +488,10 @@ public class AndroidSpellCheckerService extends SpellCheckerService
} }
mUnigramSuggestionsInfoCache.put(query, new SuggestionsParams(suggestions, flags)); mUnigramSuggestionsInfoCache.put(query, new SuggestionsParams(suggestions, flags));
} }
public void remove(String key) {
mUnigramSuggestionsInfoCache.remove(key);
}
} }
AndroidSpellCheckerSession(final AndroidSpellCheckerService service) { AndroidSpellCheckerSession(final AndroidSpellCheckerService service) {
@ -569,6 +574,96 @@ public class AndroidSpellCheckerService extends SpellCheckerService
return (letterCount * 4 < length * 3); return (letterCount * 4 < length * 3);
} }
private SentenceSuggestionsInfo fixWronglyInvalidatedWordWithSingleQuote(
TextInfo ti, SentenceSuggestionsInfo ssi) {
final String typedText = ti.getText();
if (!typedText.contains(SINGLE_QUOTE)) {
return null;
}
final int N = ssi.getSuggestionsCount();
final ArrayList<Integer> additionalOffsets = new ArrayList<Integer>();
final ArrayList<Integer> additionalLengths = new ArrayList<Integer>();
final ArrayList<SuggestionsInfo> additionalSuggestionsInfos =
new ArrayList<SuggestionsInfo>();
for (int i = 0; i < N; ++i) {
final SuggestionsInfo si = ssi.getSuggestionsInfoAt(i);
final int flags = si.getSuggestionsAttributes();
if ((flags & SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY) == 0) {
continue;
}
final int offset = ssi.getOffsetAt(i);
final int length = ssi.getLengthAt(i);
final String subText = typedText.substring(offset, offset + length);
if (!subText.contains(SINGLE_QUOTE)) {
continue;
}
final String[] splitTexts = subText.split(SINGLE_QUOTE, -1);
if (splitTexts == null || splitTexts.length <= 1) {
continue;
}
final int splitNum = splitTexts.length;
for (int j = 0; j < splitNum; ++j) {
final String splitText = splitTexts[j];
if (TextUtils.isEmpty(splitText)) {
continue;
}
if (mSuggestionsCache.getSuggestionsFromCache(splitText) == null) {
continue;
}
final int newLength = splitText.length();
// Neither RESULT_ATTR_IN_THE_DICTIONARY nor RESULT_ATTR_LOOKS_LIKE_TYPO
final int newFlags = 0;
final SuggestionsInfo newSi = new SuggestionsInfo(newFlags, EMPTY_STRING_ARRAY);
newSi.setCookieAndSequence(si.getCookie(), si.getSequence());
if (DBG) {
Log.d(TAG, "Override and remove old span over: "
+ splitText + ", " + offset + "," + newLength);
}
additionalOffsets.add(offset);
additionalLengths.add(newLength);
additionalSuggestionsInfos.add(newSi);
}
}
final int additionalSize = additionalOffsets.size();
if (additionalSize <= 0) {
return null;
}
final int suggestionsSize = N + additionalSize;
final int[] newOffsets = new int[suggestionsSize];
final int[] newLengths = new int[suggestionsSize];
final SuggestionsInfo[] newSuggestionsInfos = new SuggestionsInfo[suggestionsSize];
int i;
for (i = 0; i < N; ++i) {
newOffsets[i] = ssi.getOffsetAt(i);
newLengths[i] = ssi.getLengthAt(i);
newSuggestionsInfos[i] = ssi.getSuggestionsInfoAt(i);
}
for (; i < suggestionsSize; ++i) {
newOffsets[i] = additionalOffsets.get(i - N);
newLengths[i] = additionalLengths.get(i - N);
newSuggestionsInfos[i] = additionalSuggestionsInfos.get(i - N);
}
return new SentenceSuggestionsInfo(newSuggestionsInfos, newOffsets, newLengths);
}
@Override
public SentenceSuggestionsInfo[] onGetSentenceSuggestionsMultiple(
TextInfo[] textInfos, int suggestionsLimit) {
final SentenceSuggestionsInfo[] retval = super.onGetSentenceSuggestionsMultiple(
textInfos, suggestionsLimit);
if (retval == null || retval.length != textInfos.length) {
return retval;
}
for (int i = 0; i < retval.length; ++i) {
final SentenceSuggestionsInfo tempSsi =
fixWronglyInvalidatedWordWithSingleQuote(textInfos[i], retval[i]);
if (tempSsi != null) {
retval[i] = tempSsi;
}
}
return retval;
}
// Note : this must be reentrant // Note : this must be reentrant
/** /**
* Gets a list of suggestions for a specific string. This returns a list of possible * Gets a list of suggestions for a specific string. This returns a list of possible