private void loadDictionariesForLocale(final Locale newlocale) throws InterruptedException { mDictionaryFacilitator.resetDictionaries( mContext, newlocale, false /* useContactsDict */, false /* usePersonalizedDicts */, false /* forceReloadMainDictionary */, null /* listener */); mDictionaryFacilitator.waitForLoadingMainDictionary( TIMEOUT_TO_WAIT_LOADING_DICTIONARIES_IN_SECONDS, TimeUnit.SECONDS); }
private boolean checkDistracterUsingMaxFreqencyOfExactMatches(final String testedWord) { // The tested word is a distracter when there is a word that is exact matched to the tested // word and its probability is higher than the tested word's probability. final int perfectMatchFreq = mDictionaryFacilitator.getFrequency(testedWord); final int exactMatchFreq = mDictionaryFacilitator.getMaxFrequencyOfExactMatches(testedWord); final boolean isDistracter = perfectMatchFreq < exactMatchFreq; if (DEBUG) { Log.d(TAG, "perfectMatchFreq: " + perfectMatchFreq); Log.d(TAG, "exactMatchFreq: " + exactMatchFreq); Log.d(TAG, "isDistracter: " + isDistracter); } return isDistracter; }
private boolean checkDistracterUsingGetSuggestions(final String testedWord) { if (mKeyboard == null) { return false; } final SettingsValuesForSuggestion settingsValuesForSuggestion = new SettingsValuesForSuggestion( false /* blockPotentiallyOffensive */, false /* spaceAwareGestureEnabled */, null /* additionalFeaturesSettingValues */); final int trailingSingleQuotesCount = StringUtils.getTrailingSingleQuotesCount(testedWord); final String consideredWord = trailingSingleQuotesCount > 0 ? testedWord.substring(0, testedWord.length() - trailingSingleQuotesCount) : testedWord; final WordComposer composer = new WordComposer(); final int[] codePoints = StringUtils.toCodePointArray(testedWord); synchronized (mLock) { final int[] coordinates = mKeyboard.getCoordinates(codePoints); composer.setComposingWord(codePoints, coordinates); final SuggestionResults suggestionResults = mDictionaryFacilitator.getSuggestionResults( composer, PrevWordsInfo.EMPTY_PREV_WORDS_INFO, mKeyboard.getProximityInfo(), settingsValuesForSuggestion, 0 /* sessionId */); if (suggestionResults.isEmpty()) { return false; } final SuggestedWordInfo firstSuggestion = suggestionResults.first(); final boolean isDistractor = suggestionExceedsDistracterThreshold( firstSuggestion, consideredWord, DISTRACTER_WORD_SCORE_THRESHOLD); if (DEBUG) { Log.d(TAG, "isDistracter: " + isDistractor); } return isDistractor; } }
@Override public void close() { mDictionaryFacilitator.closeDictionaries(); }
/** * Determine whether a word is a distracter to words in dictionaries. * * @param prevWordsInfo the information of previous words. Not used for now. * @param testedWord the word that will be tested to see whether it is a distracter to words in * dictionaries. * @param locale the locale of word. * @return true if testedWord is a distracter, otherwise false. */ @Override public boolean isDistracterToWordsInDictionaries( final PrevWordsInfo prevWordsInfo, final String testedWord, final Locale locale) { if (locale == null) { return false; } if (!locale.equals(mDictionaryFacilitator.getLocale())) { synchronized (mLock) { if (!mLocaleToSubtypeMap.containsKey(locale)) { Log.e(TAG, "Locale " + locale + " is not enabled."); // TODO: Investigate what we should do for disabled locales. return false; } loadKeyboardForLocale(locale); // Reset dictionaries for the locale. try { mDistractersCache.evictAll(); loadDictionariesForLocale(locale); } catch (final InterruptedException e) { Log.e(TAG, "Interrupted while waiting for loading dicts in DistracterFilter", e); return false; } } } if (DEBUG) { Log.d(TAG, "testedWord: " + testedWord); } final Boolean isCachedDistracter = mDistractersCache.get(testedWord); if (isCachedDistracter != null && isCachedDistracter) { if (DEBUG) { Log.d(TAG, "isDistracter: true (cache hit)"); } return true; } final boolean isDistracterCheckedByGetMaxFreqencyOfExactMatches = checkDistracterUsingMaxFreqencyOfExactMatches(testedWord); if (isDistracterCheckedByGetMaxFreqencyOfExactMatches) { // Add the word to the cache. mDistractersCache.put(testedWord, Boolean.TRUE); return true; } final boolean isValidWord = mDictionaryFacilitator.isValidWord(testedWord, false /* ignoreCase */); if (isValidWord) { // Valid word is not a distractor. if (DEBUG) { Log.d(TAG, "isDistracter: false (valid word)"); } return false; } final boolean isDistracterCheckedByGetSuggestion = checkDistracterUsingGetSuggestions(testedWord); if (isDistracterCheckedByGetSuggestion) { // Add the word to the cache. mDistractersCache.put(testedWord, Boolean.TRUE); return true; } return false; }