Search in sources :

Example 11 with SuggestedWordInfo

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

the class SuggestedWordsTests method transformWordInfo.

// Helper for testGetTransformedWordInfo
private static SuggestedWordInfo transformWordInfo(final String info, final int trailingSingleQuotesCount) {
    final SuggestedWordInfo suggestedWordInfo = createTypedWordInfo(info);
    final SuggestedWordInfo returnedWordInfo = Suggest.getTransformedSuggestedWordInfo(suggestedWordInfo, Locale.ENGLISH, false, /* isAllUpperCase */
    false, /* isFirstCharCapitalized */
    trailingSingleQuotesCount);
    assertEquals(suggestedWordInfo.mAutoCommitFirstWordConfidence, returnedWordInfo.mAutoCommitFirstWordConfidence);
    return returnedWordInfo;
}
Also used : SuggestedWordInfo(com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo)

Example 12 with SuggestedWordInfo

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

the class SuggestedWordsTests method testGetTypedWordInfoOrNull.

public void testGetTypedWordInfoOrNull() {
    final String TYPED_WORD = "typed";
    final SuggestedWordInfo TYPED_WORD_INFO = createTypedWordInfo(TYPED_WORD);
    final int NUMBER_OF_ADDED_SUGGESTIONS = 5;
    final ArrayList<SuggestedWordInfo> list = new ArrayList<>();
    list.add(TYPED_WORD_INFO);
    for (int i = 0; i < NUMBER_OF_ADDED_SUGGESTIONS; ++i) {
        list.add(createCorrectionWordInfo(Integer.toString(i)));
    }
    // Make sure getTypedWordInfoOrNull() returns non-null object.
    final SuggestedWords wordsWithTypedWord = new SuggestedWords(list, null, /* rawSuggestions */
    TYPED_WORD_INFO, false, /* typedWordValid */
    false, /* willAutoCorrect */
    false, /* isObsoleteSuggestions */
    SuggestedWords.INPUT_STYLE_NONE, SuggestedWords.NOT_A_SEQUENCE_NUMBER);
    final SuggestedWordInfo typedWord = wordsWithTypedWord.getTypedWordInfoOrNull();
    assertNotNull(typedWord);
    assertEquals(TYPED_WORD, typedWord.mWord);
    // Make sure getTypedWordInfoOrNull() returns null when no typed word.
    list.remove(0);
    final SuggestedWords wordsWithoutTypedWord = new SuggestedWords(list, null, /* rawSuggestions */
    null, /* typedWord */
    false, /* typedWordValid */
    false, /* willAutoCorrect */
    false, /* isObsoleteSuggestions */
    SuggestedWords.INPUT_STYLE_NONE, SuggestedWords.NOT_A_SEQUENCE_NUMBER);
    assertNull(wordsWithoutTypedWord.getTypedWordInfoOrNull());
    // Make sure getTypedWordInfoOrNull() returns null.
    assertNull(SuggestedWords.getEmptyInstance().getTypedWordInfoOrNull());
    final SuggestedWords emptySuggestedWords = new SuggestedWords(new ArrayList<SuggestedWordInfo>(), null, /* rawSuggestions */
    null, /* typedWord */
    false, /* typedWordValid */
    false, /* willAutoCorrect */
    false, /* isObsoleteSuggestions */
    SuggestedWords.INPUT_STYLE_NONE, SuggestedWords.NOT_A_SEQUENCE_NUMBER);
    assertNull(emptySuggestedWords.getTypedWordInfoOrNull());
    assertNull(SuggestedWords.getEmptyInstance().getTypedWordInfoOrNull());
}
Also used : SuggestedWordInfo(com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo) ArrayList(java.util.ArrayList)

Example 13 with SuggestedWordInfo

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

the class SuggestedWordsTests method testGetTransformedSuggestedWordInfo.

public void testGetTransformedSuggestedWordInfo() {
    SuggestedWordInfo result = transformWordInfo("word", 0);
    assertEquals(result.mWord, "word");
    result = transformWordInfo("word", 1);
    assertEquals(result.mWord, "word'");
    result = transformWordInfo("word", 3);
    assertEquals(result.mWord, "word'''");
    result = transformWordInfo("didn't", 0);
    assertEquals(result.mWord, "didn't");
    result = transformWordInfo("didn't", 1);
    assertEquals(result.mWord, "didn't");
    result = transformWordInfo("didn't", 3);
    assertEquals(result.mWord, "didn't''");
}
Also used : SuggestedWordInfo(com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo)

Example 14 with SuggestedWordInfo

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

the class Suggest method getSuggestedWordsForNonBatchInput.

// Retrieves suggestions for non-batch input (typing, recorrection, predictions...)
// and calls the callback function with the suggestions.
private void getSuggestedWordsForNonBatchInput(final WordComposer wordComposer, final NgramContext ngramContext, final Keyboard keyboard, final SettingsValuesForSuggestion settingsValuesForSuggestion, final int inputStyleIfNotPrediction, final boolean isCorrectionEnabled, final int sequenceNumber, final OnGetSuggestedWordsCallback callback) {
    final String typedWordString = wordComposer.getTypedWord();
    final int trailingSingleQuotesCount = StringUtils.getTrailingSingleQuotesCount(typedWordString);
    final String consideredWord = trailingSingleQuotesCount > 0 ? typedWordString.substring(0, typedWordString.length() - trailingSingleQuotesCount) : typedWordString;
    final SuggestionResults suggestionResults = mDictionaryFacilitator.getSuggestionResults(wordComposer.getComposedDataSnapshot(), ngramContext, keyboard, settingsValuesForSuggestion, SESSION_ID_TYPING, inputStyleIfNotPrediction);
    final Locale locale = mDictionaryFacilitator.getLocale();
    final ArrayList<SuggestedWordInfo> suggestionsContainer = getTransformedSuggestedWordInfoList(wordComposer, suggestionResults, trailingSingleQuotesCount, locale);
    boolean foundInDictionary = false;
    Dictionary sourceDictionaryOfRemovedWord = null;
    for (final SuggestedWordInfo info : suggestionsContainer) {
        // quality we can find.
        if (!foundInDictionary && typedWordString.equals(info.mWord)) {
            // Use this source if the old match had lower quality than this match
            sourceDictionaryOfRemovedWord = info.mSourceDict;
            foundInDictionary = true;
            break;
        }
    }
    final int firstOcurrenceOfTypedWordInSuggestions = SuggestedWordInfo.removeDups(typedWordString, suggestionsContainer);
    final SuggestedWordInfo whitelistedWordInfo = getWhitelistedWordInfoOrNull(suggestionsContainer);
    final String whitelistedWord = whitelistedWordInfo == null ? null : whitelistedWordInfo.mWord;
    final boolean resultsArePredictions = !wordComposer.isComposingWord();
    // We allow auto-correction if whitelisting is not required or the word is whitelisted,
    // or if the word had more than one char and was not suggested.
    final boolean allowsToBeAutoCorrected = (SHOULD_AUTO_CORRECT_USING_NON_WHITE_LISTED_SUGGESTION || whitelistedWord != null) || (consideredWord.length() > 1 && (sourceDictionaryOfRemovedWord == null));
    final boolean hasAutoCorrection;
    // the setting "Auto-correction" is "off": we still suggest, but we don't auto-correct.
    if (!isCorrectionEnabled || // If the word does not allow to be auto-corrected, then we don't auto-correct.
    !allowsToBeAutoCorrected || // If we are doing prediction, then we never auto-correct of course
    resultsArePredictions || // for auto-correction
    suggestionResults.isEmpty() || // was type with a lot of care
    wordComposer.hasDigits() || // certainly intentional (and careful input)
    wordComposer.isMostlyCaps() || // We never auto-correct when suggestions are resumed because it would be unexpected
    wordComposer.isResumed() || // TODO: now that we have personalization, we may want to re-evaluate this decision
    !mDictionaryFacilitator.hasAtLeastOneInitializedMainDictionary() || // TODO: we may want to have shortcut-only entries auto-correct in the future.
    suggestionResults.first().isKindOf(SuggestedWordInfo.KIND_SHORTCUT)) {
        hasAutoCorrection = false;
    } else {
        final SuggestedWordInfo firstSuggestion = suggestionResults.first();
        if (suggestionResults.mFirstSuggestionExceedsConfidenceThreshold && firstOcurrenceOfTypedWordInSuggestions != 0) {
            hasAutoCorrection = true;
        } else if (!AutoCorrectionUtils.suggestionExceedsThreshold(firstSuggestion, consideredWord, mAutoCorrectionThreshold)) {
            // Score is too low for autocorrect
            hasAutoCorrection = false;
        } else {
            // We have a high score, so we need to check if this suggestion is in the correct
            // form to allow auto-correcting to it in this language. For details of how this
            // is determined, see #isAllowedByAutoCorrectionWithSpaceFilter.
            // TODO: this should not have its own logic here but be handled by the dictionary.
            hasAutoCorrection = isAllowedByAutoCorrectionWithSpaceFilter(firstSuggestion);
        }
    }
    final SuggestedWordInfo typedWordInfo = new SuggestedWordInfo(typedWordString, "", /* prevWordsContext */
    SuggestedWordInfo.MAX_SCORE, SuggestedWordInfo.KIND_TYPED, null == sourceDictionaryOfRemovedWord ? Dictionary.DICTIONARY_USER_TYPED : sourceDictionaryOfRemovedWord, SuggestedWordInfo.NOT_AN_INDEX, /* indexOfTouchPointOfSecondWord */
    SuggestedWordInfo.NOT_A_CONFIDENCE);
    if (!TextUtils.isEmpty(typedWordString)) {
        suggestionsContainer.add(0, typedWordInfo);
    }
    final ArrayList<SuggestedWordInfo> suggestionsList;
    if (DBG && !suggestionsContainer.isEmpty()) {
        suggestionsList = getSuggestionsInfoListWithDebugInfo(typedWordString, suggestionsContainer);
    } else {
        suggestionsList = suggestionsContainer;
    }
    final int inputStyle;
    if (resultsArePredictions) {
        inputStyle = suggestionResults.mIsBeginningOfSentence ? SuggestedWords.INPUT_STYLE_BEGINNING_OF_SENTENCE_PREDICTION : SuggestedWords.INPUT_STYLE_PREDICTION;
    } else {
        inputStyle = inputStyleIfNotPrediction;
    }
    final boolean isTypedWordValid = firstOcurrenceOfTypedWordInSuggestions > -1 || (!resultsArePredictions && !allowsToBeAutoCorrected);
    callback.onGetSuggestedWords(new SuggestedWords(suggestionsList, suggestionResults.mRawSuggestions, typedWordInfo, isTypedWordValid, hasAutoCorrection, /* willAutoCorrect */
    false, /* isObsoleteSuggestions */
    inputStyle, sequenceNumber));
}
Also used : Locale(java.util.Locale) SuggestionResults(com.android.inputmethod.latin.utils.SuggestionResults) SuggestedWordInfo(com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo)

Example 15 with SuggestedWordInfo

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

the class InputLogic method commitCurrentAutoCorrection.

/**
     * Commit the current auto-correction.
     *
     * This will commit the best guess of the keyboard regarding what the user meant by typing
     * the currently composing word. The IME computes suggestions and assigns a confidence score
     * to each of them; when it's confident enough in one suggestion, it replaces the typed string
     * by this suggestion at commit time. When it's not confident enough, or when it has no
     * suggestions, or when the settings or environment does not allow for auto-correction, then
     * this method just commits the typed string.
     * Note that if suggestions are currently being computed in the background, this method will
     * block until the computation returns. This is necessary for consistency (it would be very
     * strange if pressing space would commit a different word depending on how fast you press).
     *
     * @param settingsValues the current value of the settings.
     * @param separator the separator that's causing the commit to happen.
     */
private void commitCurrentAutoCorrection(final SettingsValues settingsValues, final String separator, final LatinIME.UIHandler handler) {
    // Complete any pending suggestions query first
    if (handler.hasPendingUpdateSuggestions()) {
        handler.cancelUpdateSuggestionStrip();
        // To know the input style here, we should retrieve the in-flight "update suggestions"
        // message and read its arg1 member here. However, the Handler class does not let
        // us retrieve this message, so we can't do that. But in fact, we notice that
        // we only ever come here when the input style was typing. In the case of batch
        // input, we update the suggestions synchronously when the tail batch comes. Likewise
        // for application-specified completions. As for recorrections, we never auto-correct,
        // so we don't come here either. Hence, the input style is necessarily
        // INPUT_STYLE_TYPING.
        performUpdateSuggestionStripSync(settingsValues, SuggestedWords.INPUT_STYLE_TYPING);
    }
    final SuggestedWordInfo autoCorrectionOrNull = mWordComposer.getAutoCorrectionOrNull();
    final String typedWord = mWordComposer.getTypedWord();
    final String stringToCommit = (autoCorrectionOrNull != null) ? autoCorrectionOrNull.mWord : typedWord;
    if (stringToCommit != null) {
        if (TextUtils.isEmpty(typedWord)) {
            throw new RuntimeException("We have an auto-correction but the typed word " + "is empty? Impossible! I must commit suicide.");
        }
        final boolean isBatchMode = mWordComposer.isBatchMode();
        commitChosenWord(settingsValues, stringToCommit, LastComposedWord.COMMIT_TYPE_DECIDED_WORD, separator);
        if (!typedWord.equals(stringToCommit)) {
            // This will make the correction flash for a short while as a visual clue
            // to the user that auto-correction happened. It has no other effect; in particular
            // note that this won't affect the text inside the text field AT ALL: it only makes
            // the segment of text starting at the supplied index and running for the length
            // of the auto-correction flash. At this moment, the "typedWord" argument is
            // ignored by TextView.
            mConnection.commitCorrection(new CorrectionInfo(mConnection.getExpectedSelectionEnd() - stringToCommit.length(), typedWord, stringToCommit));
            String prevWordsContext = (autoCorrectionOrNull != null) ? autoCorrectionOrNull.mPrevWordsContext : "";
            StatsUtils.onAutoCorrection(typedWord, stringToCommit, isBatchMode, mDictionaryFacilitator, prevWordsContext);
            StatsUtils.onWordCommitAutoCorrect(stringToCommit, isBatchMode);
        } else {
            StatsUtils.onWordCommitUserTyped(stringToCommit, isBatchMode);
        }
    }
}
Also used : SuggestedWordInfo(com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo) SpannableString(android.text.SpannableString) CorrectionInfo(android.view.inputmethod.CorrectionInfo)

Aggregations

SuggestedWordInfo (com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo)18 ArrayList (java.util.ArrayList)9 SpannableString (android.text.SpannableString)4 SuggestedWords (com.android.inputmethod.latin.SuggestedWords)4 Locale (java.util.Locale)4 SuggestionResults (com.android.inputmethod.latin.utils.SuggestionResults)3 SuggestionSpan (android.text.style.SuggestionSpan)2 OnGetSuggestedWordsCallback (com.android.inputmethod.latin.Suggest.OnGetSuggestedWordsCallback)2 Spannable (android.text.Spannable)1 CorrectionInfo (android.view.inputmethod.CorrectionInfo)1 UsedForTesting (com.android.inputmethod.annotations.UsedForTesting)1 InputPointers (com.android.inputmethod.latin.common.InputPointers)1 UserHistoryDictionary (com.android.inputmethod.latin.personalization.UserHistoryDictionary)1 AsyncResultHolder (com.android.inputmethod.latin.utils.AsyncResultHolder)1 TextRange (com.android.inputmethod.latin.utils.TextRange)1 Nonnull (javax.annotation.Nonnull)1