Beispiel #1
0
  public boolean isValidWord(final CharSequence word) {
    if (word == null || word.length() == 0) {
      return false;
    }

    Log.v(
        TAG,
        "Suggest::isValidWord(%s) mMainDictionaryEnabled:%s mAutoTextEnabled: %s user-dictionary-enabled: %s contacts-dictionary-enabled: %s",
        word,
        mMainDictionaryEnabled,
        mAutoTextEnabled,
        mUserDictionary != null,
        mContactsDictionary != null);

    if (mMainDictionaryEnabled || mAutoTextEnabled) {
      final boolean validFromMain =
          (mMainDictionaryEnabled && mMainDict != null && mMainDict.isValidWord(word));
      final boolean validFromUser = (mUserDictionary != null && mUserDictionary.isValidWord(word));
      final boolean validFromContacts =
          (mContactsDictionary != null && mContactsDictionary.isValidWord(word));

      Log.v(
          TAG,
          "Suggest::isValidWord(%s)validFromMain: %s validFromUser: %s validFromContacts: %s",
          word,
          validFromMain,
          validFromUser,
          validFromContacts);
      return validFromMain || validFromUser || /* validFromAuto || */ validFromContacts;
    } else {
      return false;
    }
  }
Beispiel #2
0
  public void setMainDictionary(
      Context askContext, @Nullable DictionaryAddOnAndBuilder dictionaryBuilder) {
    Log.d(
        TAG,
        "Suggest: Got main dictionary! Type: "
            + ((dictionaryBuilder == null) ? "NULL" : dictionaryBuilder.getName()));
    if (mMainDict != null) {
      mMainDict.close();
      mMainDict = null;
    }
    mLocale =
        CompatUtils.getLocaleForLanguageTag(
            dictionaryBuilder == null ? null : dictionaryBuilder.getLanguage());

    if (mAbbreviationDictionary != null) {
      mAbbreviationDictionary.close();
      mAbbreviationDictionary = null;
    }

    if (dictionaryBuilder == null) {
      mMainDict = null;
      mAutoText = null;
      mAbbreviationDictionary = null;
      mLocaleSpecificPunctuations = null;
    } else {
      try {
        System.gc();

        mMainDict = dictionaryBuilder.createDictionary();
        DictionaryASyncLoader loader = new DictionaryASyncLoader(null);
        loader.execute(mMainDict);
      } catch (Exception e) {
        e.printStackTrace();
      }
      mAutoText = dictionaryBuilder.createAutoText();
      mLocaleSpecificPunctuations = dictionaryBuilder.createInitialSuggestions();

      mAbbreviationDictionary =
          new AbbreviationsDictionary(askContext, dictionaryBuilder.getLanguage());
      DictionaryASyncLoader loader = new DictionaryASyncLoader(null);
      loader.execute(mAbbreviationDictionary);
    }
  }
Beispiel #3
0
 /** Sets an optional contacts dictionary resource to be loaded. */
 public void setContactsDictionary(Context context, boolean enabled) {
   if (!enabled && mContactsDictionary != null) {
     // had one, but now config says it should be off
     Log.i(TAG, "Contacts dictionary has been disabled! Closing resources.");
     mContactsDictionary.close();
     mContactsDictionary = null;
   } else if (enabled && mContactsDictionary == null) {
     // config says it should be on, but I have none.
     mContactsDictionary = mDictionaryFactory.createContactsDictionary(context);
     if (mContactsDictionary != null) { // not all devices has contacts-dictionary
       DictionaryASyncLoader loader = new DictionaryASyncLoader(null);
       loader.execute(mContactsDictionary);
     }
   }
 }
Beispiel #4
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;
  }
Beispiel #5
0
 public void setAutoDictionary(Dictionary autoDictionary) {
   if (mAutoDictionary != autoDictionary && mAutoDictionary != null) mAutoDictionary.close();
   mAutoDictionary = autoDictionary;
 }