Пример #1
0
  /**
   * Returns a list of suggested next words for the given typed word
   *
   * @return list of suggestions.
   */
  public List<CharSequence> getNextSuggestions(WordComposer wordComposerOfCompletedWord) {
    if (mUserDictionary == null
        || wordComposerOfCompletedWord.length() < mMinimumWordSizeToStartCorrecting)
      return Collections.emptyList();
    mNextSuggestions.clear();
    mIsAllUpperCase = wordComposerOfCompletedWord.isAllUpperCase();

    // only adding VALID words
    final CharSequence preferredWord = wordComposerOfCompletedWord.getPreferredWord();
    if (isValidWord(preferredWord)) {
      mUserDictionary.getNextWords(
          preferredWord.toString().toLowerCase(mLocale),
          mPrefMaxSuggestions,
          mNextSuggestions,
          mLocaleSpecificPunctuations);
      if (mIsAllUpperCase) {
        for (int suggestionIndex = 0;
            suggestionIndex < mNextSuggestions.size();
            suggestionIndex++) {
          mNextSuggestions.set(
              suggestionIndex,
              mNextSuggestions.get(suggestionIndex).toString().toUpperCase(mLocale));
        }
      }
    }
    return mNextSuggestions;
  }
Пример #2
0
  /**
   * Returns a list of words that match the list of character codes passed in. This list will be
   * overwritten the next time this function is called.
   *
   * @return list of suggestions.
   */
  public List<CharSequence> getSuggestions(
      WordComposer wordComposer, boolean includeTypedWordIfValid) {
    mExplodedAbbreviations.clear();
    mHaveCorrection = false;
    mIsFirstCharCapitalized = wordComposer.isFirstCharCapitalized();
    mIsAllUpperCase = wordComposer.isAllUpperCase();
    collectGarbage();
    Arrays.fill(mPriorities, 0);

    // Save a lowercase version of the original word
    mOriginalWord = wordComposer.getTypedWord();
    if (mOriginalWord.length() > 0) {
      mOriginalWord = mOriginalWord.toString();
      mLowerOriginalWord = mOriginalWord.toString().toLowerCase(mLocale);
    } else {
      mLowerOriginalWord = "";
    }

    // Search the dictionary only if there are at least mMinimumWordSizeToStartCorrecting
    // (configurable)
    // characters
    if (wordComposer.length() >= mMinimumWordSizeToStartCorrecting) {
      if (mContactsDictionary != null) {
        mContactsDictionary.getWords(wordComposer, this);
      }

      if (mUserDictionary != null) {
        mUserDictionary.getWords(wordComposer, this);
      }

      if (mSuggestions.size() > 0 && isValidWord(mOriginalWord)) {
        mHaveCorrection = true;
      }

      if (mMainDict != null) {
        mMainDict.getWords(wordComposer, this);
      }

      if (mAutoTextEnabled && mAbbreviationDictionary != null) {
        mAbbreviationDictionary.getWords(wordComposer, this);
      }

      if (
      /*mMainDictionaryEnabled &&*/ mSuggestions.size() > 0) {
        mHaveCorrection = true;
      }
    }

    // now, we'll look at the next-words-suggestions list, and add all the ones that begins
    // with the typed word. These suggestions are top priority, so they will be added
    // at the top of the list
    final int typedWordLength = mLowerOriginalWord.length();
    // since the next-word-suggestions are order by usage, we'd like to add them at the
    // same order
    int nextWordInsertionIndex = 0;
    for (CharSequence nextWordSuggestion : mNextSuggestions) {
      if (nextWordSuggestion.length() >= typedWordLength
          && nextWordSuggestion.subSequence(0, typedWordLength).equals(mOriginalWord)) {
        mSuggestions.add(nextWordInsertionIndex, nextWordSuggestion);
        nextWordInsertionIndex++; // next next-word will have lower usage, so it should be added
                                  // after this one.
      }
    }

    // adding the typed word at the head of the suggestions list
    if (!TextUtils.isEmpty(mOriginalWord)) {
      mSuggestions.add(0, mOriginalWord.toString());

      if (mExplodedAbbreviations.size() > 0) {
        // typed at zero, exploded at 1 index. These are super high priority
        int explodedWordInsertionIndex = 1;
        for (String explodedWord : mExplodedAbbreviations) {
          mSuggestions.add(explodedWordInsertionIndex, explodedWord);
          explodedWordInsertionIndex++;
        }

        mHaveCorrection = true; // so the exploded text will be auto-committed.
      }
    }

    if (mLowerOriginalWord.length() > 0) {
      CharSequence autoText =
          mAutoTextEnabled && mAutoText != null
              ? mAutoText.lookup(mLowerOriginalWord, 0, mLowerOriginalWord.length())
              : null;
      // Is there an AutoText correction?
      // Is that correction already the current prediction (or original
      // word)?
      boolean canAdd =
          (!TextUtils.isEmpty(autoText)) && (!TextUtils.equals(autoText, mOriginalWord));
      if (canAdd) {
        mHaveCorrection = true;
        if (mSuggestions.size() == 0) {
          mSuggestions.add(mOriginalWord);
        }
        mSuggestions.add(1, autoText);
      }
    }

    // removing possible duplicates to typed.
    int maxSearchIndex = Math.min(5, mSuggestions.size());
    for (int suggestionIndex = 1; suggestionIndex < maxSearchIndex; suggestionIndex++) {
      if (TextUtils.equals(mOriginalWord, mSuggestions.get(suggestionIndex))) {
        mSuggestions.remove(suggestionIndex);
        maxSearchIndex--;
      }
    }

    // Check if the first suggestion has a minimum number of characters in common
    if (mHaveCorrection
        && mMainDictionaryEnabled
        && mSuggestions.size() > 1
        && mExplodedAbbreviations.size() == 0) {
      if (!haveSufficientCommonality(mLowerOriginalWord, mSuggestions.get(1))) {
        mHaveCorrection = false;
      }
    }
    return mSuggestions;
  }