/** * http://www.tutorialspoint.com/android/android_spelling_checker.htm * Sort of copy-paste, huh. * * I need to find time to refine this code * * * @param results results */ @Override public void onGetSuggestions(SuggestionsInfo[] results) { final StringBuilder sb = new StringBuilder(); for (int i = 0; i < results.length; ++i) { // Returned suggestions are contained in SuggestionsInfo final int len = results[i].getSuggestionsCount(); sb.append('\n'); for (int j = 0; j < len; ++j) { sb.append("," + results[i].getSuggestionAt(j)); } sb.append(" (" + len + ")"); } }
private void dumpSuggestionsInfoInternal( final StringBuilder sb, final SuggestionsInfo si, final int length, final int offset) { // Returned suggestions are contained in SuggestionsInfo final int len = si.getSuggestionsCount(); sb.append('\n'); for (int j = 0; j < len; ++j) { if (j != 0) { sb.append(", "); } sb.append(si.getSuggestionAt(j)); } sb.append(" (" + len + ")"); if (length != NOT_A_LENGTH) { sb.append(" length = " + length + ", offset = " + offset); } }
/** * Checks for typos and sends results back to native through a JNI call. * @param results Results returned by the Android spellchecker. */ @Override public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) { ArrayList<Integer> offsets = new ArrayList<Integer>(); ArrayList<Integer> lengths = new ArrayList<Integer>(); for (SentenceSuggestionsInfo result : results) { for (int i = 0; i < result.getSuggestionsCount(); i++) { // If a word looks like a typo, record its offset and length. if ((result.getSuggestionsInfoAt(i).getSuggestionsAttributes() & SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO) == SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO) { offsets.add(result.getOffsetAt(i)); lengths.add(result.getLengthAt(i)); } } } nativeProcessSpellCheckResults(mNativeSpellCheckerSessionBridge, convertListToArray(offsets), convertListToArray(lengths)); }
@Override public void onGetSuggestions(SuggestionsInfo[] results) { StringBuilder sb = new StringBuilder(); for (int i=0; i<results.length ;++i) { int len = results[i].getSuggestionsCount(); sb.append('\n'); for (int j=0; j<len; j++) { if(value.contentEquals(results[i].getSuggestionAt(j))==true) { rightbox.setText("Correct"); } else { sb.append(results[i].getSuggestionAt(j)+"\n"); } } } suggestbox.setText(sb.toString()); }
@Override public SuggestionsInfo[] onGetSuggestionsMultiple(TextInfo[] textInfos, int suggestionsLimit, boolean sequentialWords) { long ident = Binder.clearCallingIdentity(); try { final int length = textInfos.length; final SuggestionsInfo[] retval = new SuggestionsInfo[length]; for (int i = 0; i < length; ++i) { final String prevWord; if (sequentialWords && i > 0) { final String prevWordCandidate = textInfos[i - 1].getText(); // Note that an empty string would be used to indicate the initial word // in the future. prevWord = TextUtils.isEmpty(prevWordCandidate) ? null : prevWordCandidate; } else { prevWord = null; } retval[i] = onGetSuggestionsInternal(textInfos[i], prevWord, suggestionsLimit); retval[i].setCookieAndSequence(textInfos[i].getCookie(), textInfos[i].getSequence()); } return retval; } finally { Binder.restoreCallingIdentity(ident); } }
private void dumpSuggestionsInfoInternal( final List<String> sb, final SuggestionsInfo si, final int length, final int offset) { // Returned suggestions are contained in SuggestionsInfo final int len = si.getSuggestionsCount(); for (int j = 0; j < len; ++j) { sb.add(si.getSuggestionAt(j)); } }
/** * Callback for {@link SpellCheckerSession#getSuggestions(TextInfo, int)} * and {@link SpellCheckerSession#getSuggestions(TextInfo[], int, boolean)} * @param results an array of {@link SuggestionsInfo}s. * These results are suggestions for {@link TextInfo}s queried by * {@link SpellCheckerSession#getSuggestions(TextInfo, int)} or * {@link SpellCheckerSession#getSuggestions(TextInfo[], int, boolean)} */ @Override public void onGetSuggestions(final SuggestionsInfo[] arg0) { Log.d(TAG, "onGetSuggestions"); final StringBuilder sb = new StringBuilder(); for (int i = 0; i < arg0.length; ++i) { dumpSuggestionsInfoInternal(sb, arg0[i], 0, NOT_A_LENGTH); } runOnUiThread(new Runnable() { @Override public void run() { mMainView.append(sb.toString()); } }); }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) public static SentenceSuggestionsInfo reconstructSuggestions( SentenceTextInfoParams originalTextInfoParams, SuggestionsInfo[] results) { if (results == null || results.length == 0) { return null; } if (originalTextInfoParams == null) { return null; } final int originalCookie = originalTextInfoParams.mOriginalTextInfo.getCookie(); final int originalSequence = originalTextInfoParams.mOriginalTextInfo.getSequence(); final int querySize = originalTextInfoParams.mSize; final int[] offsets = new int[querySize]; final int[] lengths = new int[querySize]; final SuggestionsInfo[] reconstructedSuggestions = new SuggestionsInfo[querySize]; for (int i = 0; i < querySize; ++i) { final SentenceWordItem item = originalTextInfoParams.mItems.get(i); SuggestionsInfo result = null; for (int j = 0; j < results.length; ++j) { final SuggestionsInfo cur = results[j]; if (cur != null && cur.getSequence() == item.mTextInfo.getSequence()) { result = cur; result.setCookieAndSequence(originalCookie, originalSequence); break; } } offsets[i] = item.mStart; lengths[i] = item.mLength; reconstructedSuggestions[i] = result != null ? result : EMPTY_SUGGESTIONS_INFO; } return new SentenceSuggestionsInfo(reconstructedSuggestions, offsets, lengths); }
@Override public SuggestionsInfo onGetSuggestions(final TextInfo textInfo, final int suggestionsLimit) { long ident = Binder.clearCallingIdentity(); try { return onGetSuggestionsInternal(textInfo, suggestionsLimit); } finally { Binder.restoreCallingIdentity(ident); } }
@Override public SuggestionsInfo[] onGetSuggestionsMultiple(TextInfo[] textInfos, int suggestionsLimit, boolean sequentialWords) { long ident = Binder.clearCallingIdentity(); try { final int length = textInfos.length; final SuggestionsInfo[] retval = new SuggestionsInfo[length]; for (int i = 0; i < length; ++i) { final CharSequence prevWord; if (sequentialWords && i > 0) { final TextInfo prevTextInfo = textInfos[i - 1]; final CharSequence prevWordCandidate = TextInfoCompatUtils.getCharSequenceOrString(prevTextInfo); // Note that an empty string would be used to indicate the initial word // in the future. prevWord = TextUtils.isEmpty(prevWordCandidate) ? null : prevWordCandidate; } else { prevWord = null; } final NgramContext ngramContext = new NgramContext(new NgramContext.WordInfo(prevWord)); final TextInfo textInfo = textInfos[i]; retval[i] = onGetSuggestionsInternal(textInfo, ngramContext, suggestionsLimit); retval[i].setCookieAndSequence(textInfo.getCookie(), textInfo.getSequence()); } return retval; } finally { Binder.restoreCallingIdentity(ident); } }
/** * Checks for typos and sends results back to native through a JNI call. * @param results Results returned by the Android spellchecker. */ @Override public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) { mStopMs = SystemClock.elapsedRealtime(); if (mNativeSpellCheckerSessionBridge == 0) { return; } ArrayList<Integer> offsets = new ArrayList<Integer>(); ArrayList<Integer> lengths = new ArrayList<Integer>(); for (SentenceSuggestionsInfo result : results) { if (result == null) { // In some cases null can be returned by the selected spellchecking service, // see crbug.com/651458. In this case skip to next result to avoid a // NullPointerException later on. continue; } for (int i = 0; i < result.getSuggestionsCount(); i++) { // If a word looks like a typo, record its offset and length. if ((result.getSuggestionsInfoAt(i).getSuggestionsAttributes() & SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO) == SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO) { offsets.add(result.getOffsetAt(i)); lengths.add(result.getLengthAt(i)); } } } nativeProcessSpellCheckResults(mNativeSpellCheckerSessionBridge, convertListToArray(offsets), convertListToArray(lengths)); RecordHistogram.recordTimesHistogram("SpellCheck.Android.Latency", mStopMs - mStartMs, TimeUnit.MILLISECONDS); }
/** * Returns an empty suggestionInfo with flags signaling the word is in the dictionary. * @return the empty SuggestionsInfo with the appropriate flags set. */ public static SuggestionsInfo getInDictEmptySuggestions() { return new SuggestionsInfo(SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY, EMPTY_STRING_ARRAY); }
@Override public void onGetSuggestions(SuggestionsInfo[] results) {}
public static SuggestionsInfo getNotInDictEmptySuggestions() { return new SuggestionsInfo(0, EMPTY_STRING_ARRAY); }
public static SuggestionsInfo getInDictEmptySuggestions() { return new SuggestionsInfo(SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY, EMPTY_STRING_ARRAY); }
/** * Gets a list of suggestions for a specific string. This returns a list of possible * corrections for the text passed as an argument. It may split or group words, and * even perform grammatical analysis. */ private SuggestionsInfo onGetSuggestionsInternal(final TextInfo textInfo, final int suggestionsLimit) { return onGetSuggestionsInternal(textInfo, null, suggestionsLimit); }
/** * Returns an empty SuggestionsInfo with flags signaling the word is not in the dictionary. * @param reportAsTypo whether this should include the flag LOOKS_LIKE_TYPO, for red underline. * @return the empty SuggestionsInfo with the appropriate flags set. */ public static SuggestionsInfo getNotInDictEmptySuggestions(final boolean reportAsTypo) { return new SuggestionsInfo(reportAsTypo ? SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO : 0, EMPTY_STRING_ARRAY); }