android.view.textservice.SuggestionsInfo Java Examples

The following examples show how to use android.view.textservice.SuggestionsInfo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SpellCheckerSessionBridge.java    From delion with Apache License 2.0 6 votes vote down vote up
/**
 * 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));
}
 
Example #2
Source File: SpellCheckerSessionBridge.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #3
Source File: AndroidWordLevelSpellCheckerSession.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 5 votes vote down vote up
@Override
public SuggestionsInfo onGetSuggestions(final TextInfo textInfo, final int suggestionsLimit) {
    long ident = Binder.clearCallingIdentity();
    try {
        return onGetSuggestionsInternal(textInfo, suggestionsLimit);
    } finally {
        Binder.restoreCallingIdentity(ident);
    }
}
 
Example #4
Source File: SentenceLevelAdapter.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #5
Source File: AndroidSpellCheckerSession.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example #6
Source File: AndroidSpellCheckerSession.java    From Android-Keyboard with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example #7
Source File: AndroidWordLevelSpellCheckerSession.java    From Android-Keyboard with Apache License 2.0 5 votes vote down vote up
@Override
public SuggestionsInfo onGetSuggestions(final TextInfo textInfo, final int suggestionsLimit) {
    long ident = Binder.clearCallingIdentity();
    try {
        return onGetSuggestionsInternal(textInfo, suggestionsLimit);
    } finally {
        Binder.restoreCallingIdentity(ident);
    }
}
 
Example #8
Source File: SentenceLevelAdapter.java    From Android-Keyboard with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #9
Source File: AndroidSpellCheckerSession.java    From openboard with GNU General Public License v3.0 5 votes vote down vote up
@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);
    }
}
 
Example #10
Source File: AndroidWordLevelSpellCheckerSession.java    From openboard with GNU General Public License v3.0 5 votes vote down vote up
@Override
public SuggestionsInfo onGetSuggestions(final TextInfo textInfo, final int suggestionsLimit) {
    long ident = Binder.clearCallingIdentity();
    try {
        return onGetSuggestionsInternal(textInfo, suggestionsLimit);
    } finally {
        Binder.restoreCallingIdentity(ident);
    }
}
 
Example #11
Source File: SentenceLevelAdapter.java    From openboard with GNU General Public License v3.0 5 votes vote down vote up
@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);
}
 
Example #12
Source File: SentenceLevelAdapter.java    From Indic-Keyboard with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #13
Source File: SpellChecker.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public void onGetSuggestions(SuggestionsInfo[] results) {
    final Editable editable = (Editable) mTextView.getText();
    for (int i = 0; i < results.length; ++i) {
        final SpellCheckSpan spellCheckSpan =
                onGetSuggestionsInternal(results[i], USE_SPAN_RANGE, USE_SPAN_RANGE);
        if (spellCheckSpan != null) {
            // onSpellCheckSpanRemoved will recycle this span in the pool
            editable.removeSpan(spellCheckSpan);
        }
    }
    scheduleNewSpellCheck();
}
 
Example #14
Source File: AndroidWordLevelSpellCheckerSession.java    From Indic-Keyboard with Apache License 2.0 5 votes vote down vote up
@Override
public SuggestionsInfo onGetSuggestions(final TextInfo textInfo, final int suggestionsLimit) {
    long ident = Binder.clearCallingIdentity();
    try {
        return onGetSuggestionsInternal(textInfo, suggestionsLimit);
    } finally {
        Binder.restoreCallingIdentity(ident);
    }
}
 
Example #15
Source File: AndroidSpellCheckerSession.java    From Indic-Keyboard with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example #16
Source File: SpellCheckerService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * A batch process of onGetSuggestions.
 * This function will run on the incoming IPC thread.
 * So, this is not called on the main thread,
 * but will be called in series on another thread.
 * @param textInfos an array of the text metadata
 * @param suggestionsLimit the maximum number of suggestions to be returned
 * @param sequentialWords true if textInfos can be treated as sequential words.
 * @return an array of {@link SentenceSuggestionsInfo} returned by
 * {@link SpellCheckerService.Session#onGetSuggestions(TextInfo, int)}
 */
public SuggestionsInfo[] onGetSuggestionsMultiple(TextInfo[] textInfos,
        int suggestionsLimit, boolean sequentialWords) {
    final int length = textInfos.length;
    final SuggestionsInfo[] retval = new SuggestionsInfo[length];
    for (int i = 0; i < length; ++i) {
        retval[i] = onGetSuggestions(textInfos[i], suggestionsLimit);
        retval[i].setCookieAndSequence(
                textInfos[i].getCookie(), textInfos[i].getSequence());
    }
    return retval;
}
 
Example #17
Source File: AndroidSpellCheckerService.java    From Indic-Keyboard with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
}
 
Example #18
Source File: SpellCheckerSessionBridge.java    From delion with Apache License 2.0 4 votes vote down vote up
@Override
public void onGetSuggestions(SuggestionsInfo[] results) {}
 
Example #19
Source File: SpellCheckerSessionBridge.java    From 365browser with Apache License 2.0 4 votes vote down vote up
@Override
public void onGetSuggestions(SuggestionsInfo[] results) {}
 
Example #20
Source File: AndroidSpellCheckerService.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
}
 
Example #21
Source File: AndroidSpellCheckerService.java    From Android-Keyboard with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
}
 
Example #22
Source File: AndroidSpellCheckerService.java    From openboard with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 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);
}
 
Example #23
Source File: SpellChecker.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private void createMisspelledSuggestionSpan(Editable editable, SuggestionsInfo suggestionsInfo,
        SpellCheckSpan spellCheckSpan, int offset, int length) {
    final int spellCheckSpanStart = editable.getSpanStart(spellCheckSpan);
    final int spellCheckSpanEnd = editable.getSpanEnd(spellCheckSpan);
    if (spellCheckSpanStart < 0 || spellCheckSpanEnd <= spellCheckSpanStart)
        return; // span was removed in the meantime

    final int start;
    final int end;
    if (offset != USE_SPAN_RANGE && length != USE_SPAN_RANGE) {
        start = spellCheckSpanStart + offset;
        end = start + length;
    } else {
        start = spellCheckSpanStart;
        end = spellCheckSpanEnd;
    }

    final int suggestionsCount = suggestionsInfo.getSuggestionsCount();
    String[] suggestions;
    if (suggestionsCount > 0) {
        suggestions = new String[suggestionsCount];
        for (int i = 0; i < suggestionsCount; i++) {
            suggestions[i] = suggestionsInfo.getSuggestionAt(i);
        }
    } else {
        suggestions = ArrayUtils.emptyArray(String.class);
    }

    SuggestionSpan suggestionSpan = new SuggestionSpan(mTextView.getContext(), suggestions,
            SuggestionSpan.FLAG_EASY_CORRECT | SuggestionSpan.FLAG_MISSPELLED);
    // TODO: Remove mIsSentenceSpellCheckSupported by extracting an interface
    // to share the logic of word level spell checker and sentence level spell checker
    if (mIsSentenceSpellCheckSupported) {
        final Long key = Long.valueOf(TextUtils.packRangeInLong(start, end));
        final SuggestionSpan tempSuggestionSpan = mSuggestionSpanCache.get(key);
        if (tempSuggestionSpan != null) {
            if (DBG) {
                Log.i(TAG, "Cached span on the same position is cleard. "
                        + editable.subSequence(start, end));
            }
            editable.removeSpan(tempSuggestionSpan);
        }
        mSuggestionSpanCache.put(key, suggestionSpan);
    }
    editable.setSpan(suggestionSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    mTextView.invalidateRegion(start, end, false /* No cursor involved */);
}
 
Example #24
Source File: SpellChecker.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private SpellCheckSpan onGetSuggestionsInternal(
        SuggestionsInfo suggestionsInfo, int offset, int length) {
    if (suggestionsInfo == null || suggestionsInfo.getCookie() != mCookie) {
        return null;
    }
    final Editable editable = (Editable) mTextView.getText();
    final int sequenceNumber = suggestionsInfo.getSequence();
    for (int k = 0; k < mLength; ++k) {
        if (sequenceNumber == mIds[k]) {
            final int attributes = suggestionsInfo.getSuggestionsAttributes();
            final boolean isInDictionary =
                    ((attributes & SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY) > 0);
            final boolean looksLikeTypo =
                    ((attributes & SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO) > 0);

            final SpellCheckSpan spellCheckSpan = mSpellCheckSpans[k];
            //TODO: we need to change that rule for results from a sentence-level spell
            // checker that will probably be in dictionary.
            if (!isInDictionary && looksLikeTypo) {
                createMisspelledSuggestionSpan(
                        editable, suggestionsInfo, spellCheckSpan, offset, length);
            } else {
                // Valid word -- isInDictionary || !looksLikeTypo
                if (mIsSentenceSpellCheckSupported) {
                    // Allow the spell checker to remove existing misspelled span by
                    // overwriting the span over the same place
                    final int spellCheckSpanStart = editable.getSpanStart(spellCheckSpan);
                    final int spellCheckSpanEnd = editable.getSpanEnd(spellCheckSpan);
                    final int start;
                    final int end;
                    if (offset != USE_SPAN_RANGE && length != USE_SPAN_RANGE) {
                        start = spellCheckSpanStart + offset;
                        end = start + length;
                    } else {
                        start = spellCheckSpanStart;
                        end = spellCheckSpanEnd;
                    }
                    if (spellCheckSpanStart >= 0 && spellCheckSpanEnd > spellCheckSpanStart
                            && end > start) {
                        final Long key = Long.valueOf(TextUtils.packRangeInLong(start, end));
                        final SuggestionSpan tempSuggestionSpan = mSuggestionSpanCache.get(key);
                        if (tempSuggestionSpan != null) {
                            if (DBG) {
                                Log.i(TAG, "Remove existing misspelled span. "
                                        + editable.subSequence(start, end));
                            }
                            editable.removeSpan(tempSuggestionSpan);
                            mSuggestionSpanCache.remove(key);
                        }
                    }
                }
            }
            return spellCheckSpan;
        }
    }
    return null;
}
 
Example #25
Source File: SpellCheckerService.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
public static SentenceSuggestionsInfo reconstructSuggestions(
        SentenceTextInfoParams originalTextInfoParams, SuggestionsInfo[] results) {
    if (results == null || results.length == 0) {
        return null;
    }
    if (DBG) {
        Log.w(TAG, "Adapter: onGetSuggestions: got " + results.length);
    }
    if (originalTextInfoParams == null) {
        if (DBG) {
            Log.w(TAG, "Adapter: originalTextInfoParams is 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;
        if (DBG) {
            final int size = reconstructedSuggestions[i].getSuggestionsCount();
            Log.w(TAG, "reconstructedSuggestions(" + i + ")" + size + ", first = "
                    + (size > 0 ? reconstructedSuggestions[i].getSuggestionAt(0)
                            : "<none>") + ", offset = " + offsets[i] + ", length = "
                    + lengths[i]);
        }
    }
    return new SentenceSuggestionsInfo(reconstructedSuggestions, offsets, lengths);
}
 
Example #26
Source File: AndroidWordLevelSpellCheckerSession.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 2 votes vote down vote up
/**
 * 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);
}
 
Example #27
Source File: AndroidSpellCheckerService.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 2 votes vote down vote up
/**
 * 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);
}
 
Example #28
Source File: AndroidSpellCheckerService.java    From Android-Keyboard with Apache License 2.0 2 votes vote down vote up
/**
 * 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);
}
 
Example #29
Source File: AndroidWordLevelSpellCheckerSession.java    From Android-Keyboard with Apache License 2.0 2 votes vote down vote up
/**
 * 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);
}
 
Example #30
Source File: AndroidSpellCheckerService.java    From openboard with GNU General Public License v3.0 2 votes vote down vote up
/**
 * 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);
}