Refactor BinaryDictionary a bit

This changes:
* Flag initialization code in BinaryDictionary to be more unit test friendly.
* Removing unnecessary class hierarchy of LatinIME inner class WordAlternatives.
* Formatting normalized score neatly.

Change-Id: I9f10c093d4f36258fe883cf3fb10cbbda2770497
main
Tadashi G. Takaoka 2011-03-14 17:23:26 -07:00
parent 89ff5adc80
commit 071f47140c
4 changed files with 53 additions and 65 deletions

View File

@ -58,24 +58,27 @@ public class BinaryDictionary extends Dictionary {
private final int[] mBigramScores = new int[MAX_BIGRAMS]; private final int[] mBigramScores = new int[MAX_BIGRAMS];
private final KeyboardSwitcher mKeyboardSwitcher = KeyboardSwitcher.getInstance(); private final KeyboardSwitcher mKeyboardSwitcher = KeyboardSwitcher.getInstance();
private final SubtypeSwitcher mSubtypeSwitcher = SubtypeSwitcher.getInstance();
private static class Flags { public static class Flag {
private static class FlagEntry { public final String mName;
public final String mName; public final int mValue;
public final int mValue;
public FlagEntry(String name, int value) { public Flag(String name, int value) {
mName = name; mName = name;
mValue = value; mValue = value;
}
} }
public static final FlagEntry[] ALL_FLAGS = {
// Here should reside all flags that trigger some special processing
// These *must* match the definition in UnigramDictionary enum in
// unigram_dictionary.h so please update both at the same time.
new FlagEntry("requiresGermanUmlautProcessing", 0x1)
};
} }
public static final Flag FLAG_REQUIRES_GERMAN_UMLAUT_PROCESSING =
new Flag("requiresGermanUmlautProcessing", 0x1);
private static final Flag[] ALL_FLAGS = {
// Here should reside all flags that trigger some special processing
// These *must* match the definition in UnigramDictionary enum in
// unigram_dictionary.h so please update both at the same time.
FLAG_REQUIRES_GERMAN_UMLAUT_PROCESSING,
};
private int mFlags = 0; private int mFlags = 0;
private BinaryDictionary() { private BinaryDictionary() {
@ -110,12 +113,12 @@ public class BinaryDictionary extends Dictionary {
return null; return null;
} }
} }
sInstance.initFlags(); sInstance.mFlags = initFlags(ALL_FLAGS, SubtypeSwitcher.getInstance());
return sInstance; return sInstance;
} }
/* package for test */ static BinaryDictionary initDictionary(File dictionary, long startOffset, /* package for test */ static BinaryDictionary initDictionary(File dictionary, long startOffset,
long length, int dicTypeId) { long length, int dicTypeId, Flag[] flagArray) {
synchronized (sInstance) { synchronized (sInstance) {
sInstance.closeInternal(); sInstance.closeInternal();
if (dictionary.isFile()) { if (dictionary.isFile()) {
@ -126,16 +129,17 @@ public class BinaryDictionary extends Dictionary {
return null; return null;
} }
} }
sInstance.mFlags = initFlags(flagArray, null);
return sInstance; return sInstance;
} }
private void initFlags() { private static int initFlags(Flag[] flagArray, SubtypeSwitcher switcher) {
int flags = 0; int flags = 0;
for (Flags.FlagEntry entry : Flags.ALL_FLAGS) { for (Flag entry : flagArray) {
if (mSubtypeSwitcher.currentSubtypeContainsExtraValueKey(entry.mName)) if (switcher == null || switcher.currentSubtypeContainsExtraValueKey(entry.mName))
flags |= entry.mValue; flags |= entry.mValue;
} }
mFlags = flags; return flags;
} }
static { static {

View File

@ -212,15 +212,25 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
private final ArrayList<WordAlternatives> mWordHistory = new ArrayList<WordAlternatives>(); private final ArrayList<WordAlternatives> mWordHistory = new ArrayList<WordAlternatives>();
public abstract static class WordAlternatives { public class WordAlternatives {
protected CharSequence mChosenWord; private final CharSequence mChosenWord;
private final WordComposer mWordComposer;
public WordAlternatives() { public WordAlternatives(CharSequence chosenWord, WordComposer wordComposer) {
// Nothing mChosenWord = chosenWord;
mWordComposer = wordComposer;
} }
public WordAlternatives(CharSequence chosenWord) { public CharSequence getChosenWord() {
mChosenWord = chosenWord; return mChosenWord;
}
public CharSequence getOriginalWord() {
return mWordComposer.getTypedWord();
}
public SuggestedWords.Builder getAlternatives() {
return getTypedSuggestions(mWordComposer);
} }
@Override @Override
@ -228,35 +238,9 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
return mChosenWord.hashCode(); return mChosenWord.hashCode();
} }
public abstract CharSequence getOriginalWord();
public CharSequence getChosenWord() {
return mChosenWord;
}
public abstract SuggestedWords.Builder getAlternatives();
}
public class TypedWordAlternatives extends WordAlternatives {
private WordComposer word;
public TypedWordAlternatives() {
// Nothing
}
public TypedWordAlternatives(CharSequence chosenWord, WordComposer wordComposer) {
super(chosenWord);
word = wordComposer;
}
@Override @Override
public CharSequence getOriginalWord() { public boolean equals(Object o) {
return word.getTypedWord(); return o instanceof CharSequence && TextUtils.equals(mChosenWord, (CharSequence)o);
}
@Override
public SuggestedWords.Builder getAlternatives() {
return getTypedSuggestions(word);
} }
} }
@ -1436,7 +1420,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
// Make a copy of the CharSequence, since it is/could be a mutable CharSequence // Make a copy of the CharSequence, since it is/could be a mutable CharSequence
final String resultCopy = result.toString(); final String resultCopy = result.toString();
TypedWordAlternatives entry = new TypedWordAlternatives(resultCopy, WordAlternatives entry = new WordAlternatives(resultCopy,
new WordComposer(mWord)); new WordComposer(mWord));
mWordHistory.add(entry); mWordHistory.add(entry);
} }
@ -1727,9 +1711,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
// Search old suggestions to suggest re-corrected suggestions. // Search old suggestions to suggest re-corrected suggestions.
for (WordAlternatives entry : mWordHistory) { for (WordAlternatives entry : mWordHistory) {
if (TextUtils.equals(entry.getChosenWord(), touching.mWord)) { if (TextUtils.equals(entry.getChosenWord(), touching.mWord)) {
if (entry instanceof TypedWordAlternatives) { foundWord = entry.mWordComposer;
foundWord = ((TypedWordAlternatives) entry).word;
}
alternatives = entry; alternatives = entry;
break; break;
} }
@ -1749,7 +1731,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
// Found a match, show suggestions // Found a match, show suggestions
if (foundWord != null || alternatives != null) { if (foundWord != null || alternatives != null) {
if (alternatives == null) { if (alternatives == null) {
alternatives = new TypedWordAlternatives(touching.mWord, foundWord); alternatives = new WordAlternatives(touching.mWord, foundWord);
} }
showCorrections(alternatives); showCorrections(alternatives);
if (foundWord != null) { if (foundWord != null) {

View File

@ -110,8 +110,10 @@ public class Suggest implements Dictionary.WordCallback {
init(context, BinaryDictionary.initDictionary(context, dictionaryResId, DIC_MAIN)); init(context, BinaryDictionary.initDictionary(context, dictionaryResId, DIC_MAIN));
} }
/* package for test */ Suggest(File dictionary, long startOffset, long length) { /* package for test */ Suggest(File dictionary, long startOffset, long length,
init(null, BinaryDictionary.initDictionary(dictionary, startOffset, length, DIC_MAIN)); BinaryDictionary.Flag[] flagArray) {
init(null, BinaryDictionary.initDictionary(dictionary, startOffset, length, DIC_MAIN,
flagArray));
} }
private void init(Context context, BinaryDictionary mainDict) { private void init(Context context, BinaryDictionary mainDict) {
@ -367,8 +369,8 @@ public class Suggest implements Dictionary.WordCallback {
scoreInfoList.add(new SuggestedWords.SuggestedWordInfo("+", false)); scoreInfoList.add(new SuggestedWords.SuggestedWordInfo("+", false));
for (int i = 0; i < mScores.length; ++i) { for (int i = 0; i < mScores.length; ++i) {
if (normalizedScore > 0) { if (normalizedScore > 0) {
final String scoreThreshold = Integer.toString(mScores[i]) + " (" + final String scoreThreshold = String.format("%d (%4.2f)", mScores[i],
normalizedScore + ")"; normalizedScore);
scoreInfoList.add( scoreInfoList.add(
new SuggestedWords.SuggestedWordInfo(scoreThreshold, false)); new SuggestedWords.SuggestedWordInfo(scoreThreshold, false));
normalizedScore = 0.0; normalizedScore = 0.0;

View File

@ -42,7 +42,7 @@ public class SuggestHelper {
protected SuggestHelper(Context context, File dictionaryPath, long startOffset, long length, protected SuggestHelper(Context context, File dictionaryPath, long startOffset, long length,
KeyboardId keyboardId) { KeyboardId keyboardId) {
mSuggest = new Suggest(dictionaryPath, startOffset, length); mSuggest = new Suggest(dictionaryPath, startOffset, length, null);
mKeyboard = new LatinKeyboard(context, keyboardId); mKeyboard = new LatinKeyboard(context, keyboardId);
mKeyDetector = new ProximityKeyDetector(); mKeyDetector = new ProximityKeyDetector();
init(); init();