Implement the common Dictionary interface (A89)

This will de-duplicate a lot of existing code.

Change-Id: Idaffb2fde23b9741f057bcb2ecb3dde9d12ea5c5
This commit is contained in:
Jean Chalard 2012-07-09 18:25:27 +09:00
parent a69f12a246
commit b30d2185f2
5 changed files with 78 additions and 8 deletions

View file

@ -105,6 +105,17 @@ public class BinaryDictionary extends Dictionary {
TYPED_LETTER_MULTIPLIER, FULL_WORD_SCORE_MULTIPLIER, MAX_WORD_LENGTH, MAX_WORDS); TYPED_LETTER_MULTIPLIER, FULL_WORD_SCORE_MULTIPLIER, MAX_WORD_LENGTH, MAX_WORDS);
} }
@Override
public ArrayList<SuggestedWordInfo> 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 @Override
protected ArrayList<SuggestedWordInfo> getBigrams(final WordComposer codes, protected ArrayList<SuggestedWordInfo> getBigrams(final WordComposer codes,
final CharSequence previousWord) { final CharSequence previousWord) {
@ -143,6 +154,7 @@ public class BinaryDictionary extends Dictionary {
return suggestions; return suggestions;
} }
// TODO: rename this to getWordsInternal, then move to native code
// proximityInfo and/or prevWordForBigrams may not be null. // proximityInfo and/or prevWordForBigrams may not be null.
@Override @Override
protected ArrayList<SuggestedWordInfo> getWords(final WordComposer codes, protected ArrayList<SuggestedWordInfo> getWords(final WordComposer codes,

View file

@ -61,14 +61,8 @@ public abstract class Dictionary {
*/ */
// TODO: pass more context than just the previous word, to enable better suggestions (n-gram // TODO: pass more context than just the previous word, to enable better suggestions (n-gram
// and more) // and more)
public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer, abstract public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
final CharSequence prevWord, final ProximityInfo proximityInfo) { final CharSequence prevWord, final ProximityInfo proximityInfo);
if (composer.size() <= 1) {
return TextUtils.isEmpty(prevWord) ? null : getBigrams(composer, prevWord);
} else {
return getWords(composer, prevWord, proximityInfo);
}
}
/** /**
* Searches for words in the dictionary that match the characters in the composer. Matched * 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. * @param proximityInfo the object for key proximity. May be ignored by some implementations.
* @return the list of suggestions * @return the list of suggestions
*/ */
// TODO: remove this
abstract protected ArrayList<SuggestedWordInfo> getWords(final WordComposer composer, abstract protected ArrayList<SuggestedWordInfo> getWords(final WordComposer composer,
final CharSequence prevWordForBigrams, final ProximityInfo proximityInfo); final CharSequence prevWordForBigrams, final ProximityInfo proximityInfo);
@ -87,6 +82,7 @@ public abstract class Dictionary {
* @param previousWord the word before * @param previousWord the word before
* @return the list of suggestions * @return the list of suggestions
*/ */
// TODO: remove this
abstract protected ArrayList<SuggestedWordInfo> getBigrams(final WordComposer composer, abstract protected ArrayList<SuggestedWordInfo> getBigrams(final WordComposer composer,
final CharSequence previousWord); final CharSequence previousWord);

View file

@ -54,6 +54,26 @@ public class DictionaryCollection extends Dictionary {
mDictionaries.removeAll(Collections.singleton(null)); mDictionaries.removeAll(Collections.singleton(null));
} }
@Override
public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
final CharSequence prevWord, final ProximityInfo proximityInfo) {
final CopyOnWriteArrayList<Dictionary> 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<SuggestedWordInfo> suggestions = dictionaries.get(0).getSuggestions(composer,
prevWord, proximityInfo);
if (null == suggestions) suggestions = new ArrayList<SuggestedWordInfo>();
final int length = dictionaries.size();
for (int i = 0; i < length; ++ i) {
final ArrayList<SuggestedWordInfo> sugg = dictionaries.get(i).getSuggestions(composer,
prevWord, proximityInfo);
if (null != sugg) suggestions.addAll(sugg);
}
return suggestions;
}
// TODO: remove this
@Override @Override
protected ArrayList<SuggestedWordInfo> getWords(final WordComposer composer, protected ArrayList<SuggestedWordInfo> getWords(final WordComposer composer,
final CharSequence prevWordForBigrams, final ProximityInfo proximityInfo) { final CharSequence prevWordForBigrams, final ProximityInfo proximityInfo) {
@ -73,6 +93,7 @@ public class DictionaryCollection extends Dictionary {
return suggestions; return suggestions;
} }
// TODO: remove this
@Override @Override
protected ArrayList<SuggestedWordInfo> getBigrams(final WordComposer composer, protected ArrayList<SuggestedWordInfo> getBigrams(final WordComposer composer,
final CharSequence previousWord) { final CharSequence previousWord) {

View file

@ -16,6 +16,7 @@ package com.android.inputmethod.latin;
import android.content.Context; import android.content.Context;
import android.os.SystemClock; import android.os.SystemClock;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.android.inputmethod.keyboard.ProximityInfo; import com.android.inputmethod.keyboard.ProximityInfo;
@ -191,6 +192,23 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
mFusionDictionary.setBigram(prevWord, word, frequency); mFusionDictionary.setBigram(prevWord, word, frequency);
} }
@Override
public ArrayList<SuggestedWordInfo> 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 @Override
protected ArrayList<SuggestedWordInfo> getWords(final WordComposer codes, protected ArrayList<SuggestedWordInfo> getWords(final WordComposer codes,
final CharSequence prevWordForBigrams, final ProximityInfo proximityInfo) { final CharSequence prevWordForBigrams, final ProximityInfo proximityInfo) {
@ -214,6 +232,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
return null; return null;
} }
// TODO: remove this
@Override @Override
protected ArrayList<SuggestedWordInfo> getBigrams(final WordComposer codes, protected ArrayList<SuggestedWordInfo> getBigrams(final WordComposer codes,
final CharSequence previousWord) { final CharSequence previousWord) {

View file

@ -17,6 +17,7 @@
package com.android.inputmethod.latin; package com.android.inputmethod.latin;
import android.content.Context; import android.content.Context;
import android.text.TextUtils;
import com.android.inputmethod.keyboard.KeyDetector; import com.android.inputmethod.keyboard.KeyDetector;
import com.android.inputmethod.keyboard.Keyboard; import com.android.inputmethod.keyboard.Keyboard;
@ -246,6 +247,26 @@ public class ExpandableDictionary extends Dictionary {
addWordRec(childNode.mChildren, word, depth + 1, shortcutTarget, frequency, childNode); addWordRec(childNode.mChildren, word, depth + 1, shortcutTarget, frequency, childNode);
} }
@Override
public ArrayList<SuggestedWordInfo> 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<SuggestedWordInfo> suggestions =
getWordsInner(composer, prevWord, proximityInfo);
return suggestions;
} else {
if (TextUtils.isEmpty(prevWord)) return null;
final ArrayList<SuggestedWordInfo> suggestions = new ArrayList<SuggestedWordInfo>();
runBigramReverseLookUp(prevWord, suggestions);
return suggestions;
}
}
// TODO: remove this
@Override @Override
protected ArrayList<SuggestedWordInfo> getWords(final WordComposer codes, protected ArrayList<SuggestedWordInfo> getWords(final WordComposer codes,
final CharSequence prevWordForBigrams, final ProximityInfo proximityInfo) { final CharSequence prevWordForBigrams, final ProximityInfo proximityInfo) {
@ -269,6 +290,7 @@ public class ExpandableDictionary extends Dictionary {
} }
} }
// TODO: remove this
@Override @Override
protected ArrayList<SuggestedWordInfo> getBigrams(final WordComposer codes, protected ArrayList<SuggestedWordInfo> getBigrams(final WordComposer codes,
final CharSequence previousWord) { final CharSequence previousWord) {