From b30d2185f24e3d531f5d46249e7c97391705e469 Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Mon, 9 Jul 2012 18:25:27 +0900 Subject: [PATCH] Implement the common Dictionary interface (A89) This will de-duplicate a lot of existing code. Change-Id: Idaffb2fde23b9741f057bcb2ecb3dde9d12ea5c5 --- .../inputmethod/latin/BinaryDictionary.java | 12 ++++++++++ .../android/inputmethod/latin/Dictionary.java | 12 ++++------ .../latin/DictionaryCollection.java | 21 ++++++++++++++++++ .../latin/ExpandableBinaryDictionary.java | 19 ++++++++++++++++ .../latin/ExpandableDictionary.java | 22 +++++++++++++++++++ 5 files changed, 78 insertions(+), 8 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionary.java b/java/src/com/android/inputmethod/latin/BinaryDictionary.java index 15646b871..03e286240 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionary.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionary.java @@ -105,6 +105,17 @@ public class BinaryDictionary extends Dictionary { TYPED_LETTER_MULTIPLIER, FULL_WORD_SCORE_MULTIPLIER, MAX_WORD_LENGTH, MAX_WORDS); } + @Override + public ArrayList getSuggestions(final WordComposer composer, + final CharSequence prevWord, final ProximityInfo proximityInfo) { + if (composer.size() <= 1) { + return TextUtils.isEmpty(prevWord) ? null : getBigrams(composer, prevWord); + } else { + return getWords(composer, prevWord, proximityInfo); + } + } + + // TODO: rename this to getBigramsInternal, then move to native code @Override protected ArrayList getBigrams(final WordComposer codes, final CharSequence previousWord) { @@ -143,6 +154,7 @@ public class BinaryDictionary extends Dictionary { return suggestions; } + // TODO: rename this to getWordsInternal, then move to native code // proximityInfo and/or prevWordForBigrams may not be null. @Override protected ArrayList getWords(final WordComposer codes, diff --git a/java/src/com/android/inputmethod/latin/Dictionary.java b/java/src/com/android/inputmethod/latin/Dictionary.java index 4b02e11bf..38ef00da7 100644 --- a/java/src/com/android/inputmethod/latin/Dictionary.java +++ b/java/src/com/android/inputmethod/latin/Dictionary.java @@ -61,14 +61,8 @@ public abstract class Dictionary { */ // TODO: pass more context than just the previous word, to enable better suggestions (n-gram // and more) - public ArrayList getSuggestions(final WordComposer composer, - final CharSequence prevWord, final ProximityInfo proximityInfo) { - if (composer.size() <= 1) { - return TextUtils.isEmpty(prevWord) ? null : getBigrams(composer, prevWord); - } else { - return getWords(composer, prevWord, proximityInfo); - } - } + abstract public ArrayList getSuggestions(final WordComposer composer, + final CharSequence prevWord, final ProximityInfo proximityInfo); /** * Searches for words in the dictionary that match the characters in the composer. Matched @@ -78,6 +72,7 @@ public abstract class Dictionary { * @param proximityInfo the object for key proximity. May be ignored by some implementations. * @return the list of suggestions */ + // TODO: remove this abstract protected ArrayList getWords(final WordComposer composer, final CharSequence prevWordForBigrams, final ProximityInfo proximityInfo); @@ -87,6 +82,7 @@ public abstract class Dictionary { * @param previousWord the word before * @return the list of suggestions */ + // TODO: remove this abstract protected ArrayList getBigrams(final WordComposer composer, final CharSequence previousWord); diff --git a/java/src/com/android/inputmethod/latin/DictionaryCollection.java b/java/src/com/android/inputmethod/latin/DictionaryCollection.java index d2ddebf3e..7c589ef17 100644 --- a/java/src/com/android/inputmethod/latin/DictionaryCollection.java +++ b/java/src/com/android/inputmethod/latin/DictionaryCollection.java @@ -54,6 +54,26 @@ public class DictionaryCollection extends Dictionary { mDictionaries.removeAll(Collections.singleton(null)); } + @Override + public ArrayList getSuggestions(final WordComposer composer, + final CharSequence prevWord, final ProximityInfo proximityInfo) { + final CopyOnWriteArrayList dictionaries = mDictionaries; + if (dictionaries.isEmpty()) return null; + // To avoid creating unnecessary objects, we get the list out of the first + // dictionary and add the rest to it if not null, hence the get(0) + ArrayList suggestions = dictionaries.get(0).getSuggestions(composer, + prevWord, proximityInfo); + if (null == suggestions) suggestions = new ArrayList(); + final int length = dictionaries.size(); + for (int i = 0; i < length; ++ i) { + final ArrayList sugg = dictionaries.get(i).getSuggestions(composer, + prevWord, proximityInfo); + if (null != sugg) suggestions.addAll(sugg); + } + return suggestions; + } + + // TODO: remove this @Override protected ArrayList getWords(final WordComposer composer, final CharSequence prevWordForBigrams, final ProximityInfo proximityInfo) { @@ -73,6 +93,7 @@ public class DictionaryCollection extends Dictionary { return suggestions; } + // TODO: remove this @Override protected ArrayList getBigrams(final WordComposer composer, final CharSequence previousWord) { diff --git a/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java b/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java index 40d46a873..dd949f121 100644 --- a/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java +++ b/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java @@ -16,6 +16,7 @@ package com.android.inputmethod.latin; import android.content.Context; import android.os.SystemClock; +import android.text.TextUtils; import android.util.Log; import com.android.inputmethod.keyboard.ProximityInfo; @@ -191,6 +192,23 @@ abstract public class ExpandableBinaryDictionary extends Dictionary { mFusionDictionary.setBigram(prevWord, word, frequency); } + @Override + public ArrayList getSuggestions(final WordComposer composer, + final CharSequence prevWord, final ProximityInfo proximityInfo) { + asyncReloadDictionaryIfRequired(); + if (mLocalDictionaryController.tryLock()) { + try { + if (mBinaryDictionary != null) { + return mBinaryDictionary.getSuggestions(composer, prevWord, proximityInfo); + } + } finally { + mLocalDictionaryController.unlock(); + } + } + return null; + } + + // TODO: remove this @Override protected ArrayList getWords(final WordComposer codes, final CharSequence prevWordForBigrams, final ProximityInfo proximityInfo) { @@ -214,6 +232,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary { return null; } + // TODO: remove this @Override protected ArrayList getBigrams(final WordComposer codes, final CharSequence previousWord) { diff --git a/java/src/com/android/inputmethod/latin/ExpandableDictionary.java b/java/src/com/android/inputmethod/latin/ExpandableDictionary.java index a3c183c27..6cd4f65cb 100644 --- a/java/src/com/android/inputmethod/latin/ExpandableDictionary.java +++ b/java/src/com/android/inputmethod/latin/ExpandableDictionary.java @@ -17,6 +17,7 @@ package com.android.inputmethod.latin; import android.content.Context; +import android.text.TextUtils; import com.android.inputmethod.keyboard.KeyDetector; import com.android.inputmethod.keyboard.Keyboard; @@ -246,6 +247,26 @@ public class ExpandableDictionary extends Dictionary { addWordRec(childNode.mChildren, word, depth + 1, shortcutTarget, frequency, childNode); } + @Override + public ArrayList getSuggestions(final WordComposer composer, + final CharSequence prevWord, final ProximityInfo proximityInfo) { + if (reloadDictionaryIfRequired()) return null; + if (composer.size() <= 1) { + if (composer.size() >= BinaryDictionary.MAX_WORD_LENGTH) { + return null; + } + final ArrayList suggestions = + getWordsInner(composer, prevWord, proximityInfo); + return suggestions; + } else { + if (TextUtils.isEmpty(prevWord)) return null; + final ArrayList suggestions = new ArrayList(); + runBigramReverseLookUp(prevWord, suggestions); + return suggestions; + } + } + + // TODO: remove this @Override protected ArrayList getWords(final WordComposer codes, final CharSequence prevWordForBigrams, final ProximityInfo proximityInfo) { @@ -269,6 +290,7 @@ public class ExpandableDictionary extends Dictionary { } } + // TODO: remove this @Override protected ArrayList getBigrams(final WordComposer codes, final CharSequence previousWord) {