diff options
Diffstat (limited to 'java/src/com/android/inputmethod/latin/Suggest.java')
-rw-r--r-- | java/src/com/android/inputmethod/latin/Suggest.java | 80 |
1 files changed, 24 insertions, 56 deletions
diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java index 845df81f6..9e478fab4 100644 --- a/java/src/com/android/inputmethod/latin/Suggest.java +++ b/java/src/com/android/inputmethod/latin/Suggest.java @@ -43,20 +43,6 @@ public class Suggest implements Dictionary.WordCallback { public static final int CORRECTION_FULL = 1; public static final int CORRECTION_FULL_BIGRAM = 2; - /** - * Words that appear in both bigram and unigram data gets multiplier ranging from - * BIGRAM_MULTIPLIER_MIN to BIGRAM_MULTIPLIER_MAX depending on the score from - * bigram data. - */ - public static final double BIGRAM_MULTIPLIER_MIN = 1.2; - public static final double BIGRAM_MULTIPLIER_MAX = 1.5; - - /** - * Maximum possible bigram frequency. Will depend on how many bits are being used in data - * structure. Maximum bigram frequency will get the BIGRAM_MULTIPLIER_MAX as the multiplier. - */ - public static final int MAXIMUM_BIGRAM_FREQUENCY = 127; - // It seems the following values are only used for logging. public static final int DIC_USER_TYPED = 0; public static final int DIC_MAIN = 1; @@ -79,7 +65,7 @@ public class Suggest implements Dictionary.WordCallback { private static final boolean DBG = LatinImeLogger.sDBG; - private Dictionary mMainDict; + private boolean mHasMainDictionary; private Dictionary mContactsDict; private WhitelistDictionary mWhiteListDictionary; private final HashMap<String, Dictionary> mUnigramDictionaries = @@ -91,7 +77,7 @@ public class Suggest implements Dictionary.WordCallback { private static final int PREF_MAX_BIGRAMS = 60; - private double mAutoCorrectionThreshold; + private float mAutoCorrectionThreshold; private ArrayList<SuggestedWordInfo> mSuggestions = new ArrayList<SuggestedWordInfo>(); private ArrayList<SuggestedWordInfo> mBigramSuggestions = new ArrayList<SuggestedWordInfo>(); @@ -110,8 +96,12 @@ public class Suggest implements Dictionary.WordCallback { /* package for test */ Suggest(final Context context, final File dictionary, final long startOffset, final long length, final Locale locale) { - initSynchronously(context, DictionaryFactory.createDictionaryForTest(context, dictionary, - startOffset, length /* useFullEditDistance */, false, locale), locale); + final Dictionary mainDict = DictionaryFactory.createDictionaryForTest(context, dictionary, + startOffset, length /* useFullEditDistance */, false, locale); + mHasMainDictionary = null != mainDict; + addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_MAIN, mainDict); + addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_MAIN, mainDict); + initWhitelistAndAutocorrectAndPool(context, locale); } private void initWhitelistAndAutocorrectAndPool(final Context context, final Locale locale) { @@ -127,14 +117,6 @@ public class Suggest implements Dictionary.WordCallback { initWhitelistAndAutocorrectAndPool(context, locale); } - private void initSynchronously(final Context context, final Dictionary mainDict, - final Locale locale) { - mMainDict = mainDict; - addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_MAIN, mainDict); - addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_MAIN, mainDict); - initWhitelistAndAutocorrectAndPool(context, locale); - } - private static void addOrReplaceDictionary(HashMap<String, Dictionary> dictionaries, String key, Dictionary dict) { final Dictionary oldDict = (dict == null) @@ -146,13 +128,13 @@ public class Suggest implements Dictionary.WordCallback { } public void resetMainDict(final Context context, final Locale locale) { - mMainDict = null; + mHasMainDictionary = false; new Thread("InitializeBinaryDictionary") { @Override public void run() { - final Dictionary newMainDict = DictionaryFactory.createDictionaryFromManager( - context, locale); - mMainDict = newMainDict; + final DictionaryCollection newMainDict = + DictionaryFactory.createMainDictionaryFromManager(context, locale); + mHasMainDictionary = null != newMainDict && !newMainDict.isEmpty(); addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_MAIN, newMainDict); addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_MAIN, newMainDict); } @@ -162,7 +144,7 @@ public class Suggest implements Dictionary.WordCallback { // The main dictionary could have been loaded asynchronously. Don't cache the return value // of this method. public boolean hasMainDictionary() { - return mMainDict != null; + return mHasMainDictionary; } public Dictionary getContactsDictionary() { @@ -203,7 +185,7 @@ public class Suggest implements Dictionary.WordCallback { userHistoryDictionary); } - public void setAutoCorrectionThreshold(double threshold) { + public void setAutoCorrectionThreshold(float threshold) { mAutoCorrectionThreshold = threshold; } @@ -376,7 +358,13 @@ public class Suggest implements Dictionary.WordCallback { // a boolean flag. Right now this is handled with a slight hack in // WhitelistDictionary#shouldForciblyAutoCorrectFrom. final boolean allowsToBeAutoCorrected = AutoCorrection.allowsToBeAutoCorrected( - getUnigramDictionaries(), consideredWord, wordComposer.isFirstCharCapitalized()); + getUnigramDictionaries(), consideredWord, wordComposer.isFirstCharCapitalized()) + // If we don't have a main dictionary, we never want to auto-correct. The reason for this + // is, the user may have a contact whose name happens to match a valid word in their + // language, and it will unexpectedly auto-correct. For example, if the user types in + // English with no dictionary and has a "Will" in their contact list, "will" would + // always auto-correct to "Will" which is unwanted. Hence, no main dict => no auto-correct. + && mHasMainDictionary; boolean autoCorrectionAvailable = hasAutoCorrection; if (correctionMode == CORRECTION_FULL || correctionMode == CORRECTION_FULL_BIGRAM) { @@ -420,8 +408,6 @@ public class Suggest implements Dictionary.WordCallback { final String typedWord, final ArrayList<SuggestedWordInfo> suggestions) { final SuggestedWordInfo typedWordInfo = suggestions.get(0); typedWordInfo.setDebugString("+"); - double normalizedScore = BinaryDictionary.calcNormalizedScore( - typedWord, typedWordInfo.toString(), typedWordInfo.mScore); final int suggestionsSize = suggestions.size(); final ArrayList<SuggestedWordInfo> suggestionsList = new ArrayList<SuggestedWordInfo>(suggestionsSize); @@ -430,10 +416,11 @@ public class Suggest implements Dictionary.WordCallback { // than i because we added the typed word to mSuggestions without touching mScores. for (int i = 0; i < suggestionsSize - 1; ++i) { final SuggestedWordInfo cur = suggestions.get(i + 1); + final float normalizedScore = BinaryDictionary.calcNormalizedScore( + typedWord, cur.toString(), cur.mScore); final String scoreInfoString; if (normalizedScore > 0) { scoreInfoString = String.format("%d (%4.2f)", cur.mScore, normalizedScore); - normalizedScore = 0.0; } else { scoreInfoString = Integer.toString(cur.mScore); } @@ -476,25 +463,6 @@ public class Suggest implements Dictionary.WordCallback { } } } else { - if (dataType == Dictionary.UNIGRAM) { - // Check if the word was already added before (by bigram data) - int bigramSuggestion = searchBigramSuggestion(word,offset,length); - if(bigramSuggestion >= 0) { - dataTypeForLog = Dictionary.BIGRAM; - // turn freq from bigram into multiplier specified above - double multiplier = (((double) mBigramSuggestions.get(bigramSuggestion).mScore) - / MAXIMUM_BIGRAM_FREQUENCY) - * (BIGRAM_MULTIPLIER_MAX - BIGRAM_MULTIPLIER_MIN) - + BIGRAM_MULTIPLIER_MIN; - /* Log.d(TAG,"bigram num: " + bigramSuggestion - + " wordB: " + mBigramSuggestions.get(bigramSuggestion).toString() - + " currentScore: " + score + " bigramScore: " - + mBigramScores[bigramSuggestion] - + " multiplier: " + multiplier); */ - score = (int)Math.round((score * multiplier)); - } - } - // Check the last one's score and bail if (suggestions.size() >= prefMaxSuggestions && suggestions.get(prefMaxSuggestions - 1).mScore >= score) return true; @@ -563,7 +531,7 @@ public class Suggest implements Dictionary.WordCallback { for (final Dictionary dictionary : dictionaries) { dictionary.close(); } - mMainDict = null; + mHasMainDictionary = false; } // TODO: Resolve the inconsistencies between the native auto correction algorithms and |