Search in sources :

Example 1 with WordComposer

use of com.android.inputmethod.latin.WordComposer in project android_packages_inputmethods_LatinIME by CyanogenMod.

the class AndroidWordLevelSpellCheckerSession method onGetSuggestionsInternal.

protected SuggestionsInfo onGetSuggestionsInternal(final TextInfo textInfo, final NgramContext ngramContext, final int suggestionsLimit) {
    try {
        final String text = textInfo.getText().replaceAll(AndroidSpellCheckerService.APOSTROPHE, AndroidSpellCheckerService.SINGLE_QUOTE).replaceAll("^" + quotesRegexp, "").replaceAll(quotesRegexp + "$", "");
        if (!mService.hasMainDictionaryForLocale(mLocale)) {
            return AndroidSpellCheckerService.getNotInDictEmptySuggestions(false);
        }
        // Handle special patterns like email, URI, telephone number.
        final int checkability = getCheckabilityInScript(text, mScript);
        if (CHECKABILITY_CHECKABLE != checkability) {
            if (CHECKABILITY_CONTAINS_PERIOD == checkability) {
                final String[] splitText = text.split(Constants.REGEXP_PERIOD);
                boolean allWordsAreValid = true;
                for (final String word : splitText) {
                    if (!mService.isValidWord(mLocale, word)) {
                        allWordsAreValid = false;
                        break;
                    }
                }
                if (allWordsAreValid) {
                    return new SuggestionsInfo(SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO | SuggestionsInfo.RESULT_ATTR_HAS_RECOMMENDED_SUGGESTIONS, new String[] { TextUtils.join(Constants.STRING_SPACE, splitText) });
                }
            }
            return mService.isValidWord(mLocale, text) ? AndroidSpellCheckerService.getInDictEmptySuggestions() : AndroidSpellCheckerService.getNotInDictEmptySuggestions(CHECKABILITY_CONTAINS_PERIOD == checkability);
        }
        // Handle normal words.
        final int capitalizeType = StringUtils.getCapitalizationType(text);
        if (isInDictForAnyCapitalization(text, capitalizeType)) {
            if (DebugFlags.DEBUG_ENABLED) {
                Log.i(TAG, "onGetSuggestionsInternal() : [" + text + "] is a valid word");
            }
            return AndroidSpellCheckerService.getInDictEmptySuggestions();
        }
        if (DebugFlags.DEBUG_ENABLED) {
            Log.i(TAG, "onGetSuggestionsInternal() : [" + text + "] is NOT a valid word");
        }
        final Keyboard keyboard = mService.getKeyboardForLocale(mLocale);
        if (null == keyboard) {
            Log.w(TAG, "onGetSuggestionsInternal() : No keyboard for locale: " + mLocale);
            // If there is no keyboard for this locale, don't do any spell-checking.
            return AndroidSpellCheckerService.getNotInDictEmptySuggestions(false);
        }
        final WordComposer composer = new WordComposer();
        final int[] codePoints = StringUtils.toCodePointArray(text);
        final int[] coordinates;
        coordinates = keyboard.getCoordinates(codePoints);
        composer.setComposingWord(codePoints, coordinates);
        // TODO: Don't gather suggestions if the limit is <= 0 unless necessary
        final SuggestionResults suggestionResults = mService.getSuggestionResults(mLocale, composer.getComposedDataSnapshot(), ngramContext, keyboard);
        final Result result = getResult(capitalizeType, mLocale, suggestionsLimit, mService.getRecommendedThreshold(), text, suggestionResults);
        if (DebugFlags.DEBUG_ENABLED) {
            if (result.mSuggestions != null && result.mSuggestions.length > 0) {
                final StringBuilder builder = new StringBuilder();
                for (String suggestion : result.mSuggestions) {
                    builder.append(" [");
                    builder.append(suggestion);
                    builder.append("]");
                }
                Log.i(TAG, "onGetSuggestionsInternal() : Suggestions =" + builder);
            }
        }
        // Handle word not in dictionary.
        // This is called only once per unique word, so entering multiple
        // instances of the same word does not result in more than one call
        // to this method.
        // Also, upon changing the orientation of the device, this is called
        // again for every unique invalid word in the text box.
        StatsUtils.onInvalidWordIdentification(text);
        final int flags = SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO | (result.mHasRecommendedSuggestions ? SuggestionsInfoCompatUtils.getValueOf_RESULT_ATTR_HAS_RECOMMENDED_SUGGESTIONS() : 0);
        final SuggestionsInfo retval = new SuggestionsInfo(flags, result.mSuggestions);
        mSuggestionsCache.putSuggestionsToCache(text, result.mSuggestions, flags);
        return retval;
    } catch (RuntimeException e) {
        // Don't kill the keyboard if there is a bug in the spell checker
        Log.e(TAG, "Exception while spellchecking", e);
        return AndroidSpellCheckerService.getNotInDictEmptySuggestions(false);
    }
}
Also used : WordComposer(com.android.inputmethod.latin.WordComposer) SuggestionResults(com.android.inputmethod.latin.utils.SuggestionResults) Keyboard(com.android.inputmethod.keyboard.Keyboard) SuggestionsInfo(android.view.textservice.SuggestionsInfo)

Aggregations

SuggestionsInfo (android.view.textservice.SuggestionsInfo)1 Keyboard (com.android.inputmethod.keyboard.Keyboard)1 WordComposer (com.android.inputmethod.latin.WordComposer)1 SuggestionResults (com.android.inputmethod.latin.utils.SuggestionResults)1