Refactor on UserHistoryDictionary

Bug: 9429906
Change-Id: I576a91643bdaf5017cc826ac2e07a74a9a275d60
This commit is contained in:
Satoshi Kataoka 2013-07-26 12:35:11 +09:00
parent adc91027d2
commit ffcbbaf127
17 changed files with 40 additions and 37 deletions

View file

@ -38,7 +38,7 @@ public final class BinaryDictionary extends Dictionary {
private static final String TAG = BinaryDictionary.class.getSimpleName(); private static final String TAG = BinaryDictionary.class.getSimpleName();
// Must be equal to MAX_WORD_LENGTH in native/jni/src/defines.h // Must be equal to MAX_WORD_LENGTH in native/jni/src/defines.h
private static final int MAX_WORD_LENGTH = Constants.Dictionary.MAX_WORD_LENGTH; private static final int MAX_WORD_LENGTH = Constants.DICTIONARY_MAX_WORD_LENGTH;
// Must be equal to MAX_RESULTS in native/jni/src/defines.h // Must be equal to MAX_RESULTS in native/jni/src/defines.h
private static final int MAX_RESULTS = 18; private static final int MAX_RESULTS = 18;

View file

@ -126,15 +126,6 @@ public final class Constants {
} }
} }
public static final class Dictionary {
// Must be equal to MAX_WORD_LENGTH in native/jni/src/defines.h
public static final int MAX_WORD_LENGTH = 48;
private Dictionary() {
// This utility class is no publicly instantiable.
}
}
public static final int NOT_A_CODE = -1; public static final int NOT_A_CODE = -1;
public static final int NOT_A_COORDINATE = -1; public static final int NOT_A_COORDINATE = -1;
@ -142,6 +133,10 @@ public final class Constants {
public static final int SPELL_CHECKER_COORDINATE = -3; public static final int SPELL_CHECKER_COORDINATE = -3;
public static final int EXTERNAL_KEYBOARD_COORDINATE = -4; public static final int EXTERNAL_KEYBOARD_COORDINATE = -4;
// Must be equal to MAX_WORD_LENGTH in native/jni/src/defines.h
public static final int DICTIONARY_MAX_WORD_LENGTH = 48;
public static boolean isValidCoordinate(final int coordinate) { public static boolean isValidCoordinate(final int coordinate) {
// Detect {@link NOT_A_COORDINATE}, {@link SUGGESTION_STRIP_COORDINATE}, // Detect {@link NOT_A_COORDINATE}, {@link SUGGESTION_STRIP_COORDINATE},
// and {@link SPELL_CHECKER_COORDINATE}. // and {@link SPELL_CHECKER_COORDINATE}.

View file

@ -48,7 +48,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
/** /**
* The maximum length of a word in this dictionary. * The maximum length of a word in this dictionary.
*/ */
protected static final int MAX_WORD_LENGTH = Constants.Dictionary.MAX_WORD_LENGTH; protected static final int MAX_WORD_LENGTH = Constants.DICTIONARY_MAX_WORD_LENGTH;
/** /**
* A static map of locks, each of which controls access to a single binary dictionary file. They * A static map of locks, each of which controls access to a single binary dictionary file. They

View file

@ -43,7 +43,7 @@ public class ExpandableDictionary extends Dictionary {
protected static final int BIGRAM_MAX_FREQUENCY = 255; protected static final int BIGRAM_MAX_FREQUENCY = 255;
private Context mContext; private Context mContext;
private char[] mWordBuilder = new char[Constants.Dictionary.MAX_WORD_LENGTH]; private char[] mWordBuilder = new char[Constants.DICTIONARY_MAX_WORD_LENGTH];
private int mMaxDepth; private int mMaxDepth;
private int mInputLength; private int mInputLength;
@ -87,7 +87,7 @@ public class ExpandableDictionary extends Dictionary {
} }
} }
protected interface NextWord { public interface NextWord {
public Node getWordNode(); public Node getWordNode();
public int getFrequency(); public int getFrequency();
public ForgettingCurveParams getFcParams(); public ForgettingCurveParams getFcParams();
@ -161,7 +161,7 @@ public class ExpandableDictionary extends Dictionary {
super(dictType); super(dictType);
mContext = context; mContext = context;
clearDictionary(); clearDictionary();
mCodes = new int[Constants.Dictionary.MAX_WORD_LENGTH][]; mCodes = new int[Constants.DICTIONARY_MAX_WORD_LENGTH][];
} }
public void loadDictionary() { public void loadDictionary() {
@ -198,11 +198,11 @@ public class ExpandableDictionary extends Dictionary {
} }
public int getMaxWordLength() { public int getMaxWordLength() {
return Constants.Dictionary.MAX_WORD_LENGTH; return Constants.DICTIONARY_MAX_WORD_LENGTH;
} }
public void addWord(final String word, final String shortcutTarget, final int frequency) { public void addWord(final String word, final String shortcutTarget, final int frequency) {
if (word.length() >= Constants.Dictionary.MAX_WORD_LENGTH) { if (word.length() >= Constants.DICTIONARY_MAX_WORD_LENGTH) {
return; return;
} }
addWordRec(mRoots, word, 0, shortcutTarget, frequency, null); addWordRec(mRoots, word, 0, shortcutTarget, frequency, null);
@ -258,7 +258,7 @@ public class ExpandableDictionary extends Dictionary {
final boolean blockOffensiveWords) { final boolean blockOffensiveWords) {
if (reloadDictionaryIfRequired()) return null; if (reloadDictionaryIfRequired()) return null;
if (composer.size() > 1) { if (composer.size() > 1) {
if (composer.size() >= Constants.Dictionary.MAX_WORD_LENGTH) { if (composer.size() >= Constants.DICTIONARY_MAX_WORD_LENGTH) {
return null; return null;
} }
final ArrayList<SuggestedWordInfo> suggestions = final ArrayList<SuggestedWordInfo> suggestions =
@ -629,7 +629,7 @@ public class ExpandableDictionary extends Dictionary {
} }
// Local to reverseLookUp, but do not allocate each time. // Local to reverseLookUp, but do not allocate each time.
private final char[] mLookedUpString = new char[Constants.Dictionary.MAX_WORD_LENGTH]; private final char[] mLookedUpString = new char[Constants.DICTIONARY_MAX_WORD_LENGTH];
/** /**
* reverseLookUp retrieves the full word given a list of terminal nodes and adds those words * reverseLookUp retrieves the full word given a list of terminal nodes and adds those words
@ -644,7 +644,7 @@ public class ExpandableDictionary extends Dictionary {
for (NextWord nextWord : terminalNodes) { for (NextWord nextWord : terminalNodes) {
node = nextWord.getWordNode(); node = nextWord.getWordNode();
freq = nextWord.getFrequency(); freq = nextWord.getFrequency();
int index = Constants.Dictionary.MAX_WORD_LENGTH; int index = Constants.DICTIONARY_MAX_WORD_LENGTH;
do { do {
--index; --index;
mLookedUpString[index] = node.mCode; mLookedUpString[index] = node.mCode;
@ -656,7 +656,7 @@ public class ExpandableDictionary extends Dictionary {
// to ignore the word in this case. // to ignore the word in this case.
if (freq >= 0 && node == null) { if (freq >= 0 && node == null) {
suggestions.add(new SuggestedWordInfo(new String(mLookedUpString, index, suggestions.add(new SuggestedWordInfo(new String(mLookedUpString, index,
Constants.Dictionary.MAX_WORD_LENGTH - index), Constants.DICTIONARY_MAX_WORD_LENGTH - index),
freq, SuggestedWordInfo.KIND_CORRECTION, mDictType)); freq, SuggestedWordInfo.KIND_CORRECTION, mDictType));
} }
} }

View file

@ -49,7 +49,7 @@ public final class LastComposedWord {
public final String mPrevWord; public final String mPrevWord;
public final int mCapitalizedMode; public final int mCapitalizedMode;
public final InputPointers mInputPointers = public final InputPointers mInputPointers =
new InputPointers(Constants.Dictionary.MAX_WORD_LENGTH); new InputPointers(Constants.DICTIONARY_MAX_WORD_LENGTH);
private boolean mActive; private boolean mActive;

View file

@ -75,6 +75,7 @@ import com.android.inputmethod.keyboard.KeyboardSwitcher;
import com.android.inputmethod.keyboard.MainKeyboardView; import com.android.inputmethod.keyboard.MainKeyboardView;
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
import com.android.inputmethod.latin.define.ProductionFlag; import com.android.inputmethod.latin.define.ProductionFlag;
import com.android.inputmethod.latin.personalization.UserHistoryDictionary;
import com.android.inputmethod.latin.settings.Settings; import com.android.inputmethod.latin.settings.Settings;
import com.android.inputmethod.latin.settings.SettingsActivity; import com.android.inputmethod.latin.settings.SettingsActivity;
import com.android.inputmethod.latin.settings.SettingsValues; import com.android.inputmethod.latin.settings.SettingsValues;

View file

@ -51,7 +51,7 @@ public final class RichInputConnection {
private static final boolean DEBUG_PREVIOUS_TEXT = false; private static final boolean DEBUG_PREVIOUS_TEXT = false;
private static final boolean DEBUG_BATCH_NESTING = false; private static final boolean DEBUG_BATCH_NESTING = false;
// Provision for a long word pair and a separator // Provision for a long word pair and a separator
private static final int LOOKBACK_CHARACTER_NUM = Constants.Dictionary.MAX_WORD_LENGTH * 2 + 1; private static final int LOOKBACK_CHARACTER_NUM = Constants.DICTIONARY_MAX_WORD_LENGTH * 2 + 1;
private static final Pattern spaceRegex = Pattern.compile("\\s+"); private static final Pattern spaceRegex = Pattern.compile("\\s+");
private static final int INVALID_CURSOR_POSITION = -1; private static final int INVALID_CURSOR_POSITION = -1;

View file

@ -22,6 +22,7 @@ import android.text.TextUtils;
import com.android.inputmethod.annotations.UsedForTesting; import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.keyboard.ProximityInfo; import com.android.inputmethod.keyboard.ProximityInfo;
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
import com.android.inputmethod.latin.personalization.UserHistoryDictionary;
import com.android.inputmethod.latin.utils.AutoCorrectionUtils; import com.android.inputmethod.latin.utils.AutoCorrectionUtils;
import com.android.inputmethod.latin.utils.BoundedTreeSet; import com.android.inputmethod.latin.utils.BoundedTreeSet;
import com.android.inputmethod.latin.utils.CollectionUtils; import com.android.inputmethod.latin.utils.CollectionUtils;

View file

@ -26,7 +26,7 @@ import java.util.Arrays;
* A place to store the currently composing word with information such as adjacent key codes as well * A place to store the currently composing word with information such as adjacent key codes as well
*/ */
public final class WordComposer { public final class WordComposer {
private static final int MAX_WORD_LENGTH = Constants.Dictionary.MAX_WORD_LENGTH; private static final int MAX_WORD_LENGTH = Constants.DICTIONARY_MAX_WORD_LENGTH;
private static final boolean DBG = LatinImeLogger.sDBG; private static final boolean DBG = LatinImeLogger.sDBG;
public static final int CAPS_MODE_OFF = 0; public static final int CAPS_MODE_OFF = 0;

View file

@ -181,7 +181,7 @@ public final class BinaryDictIOUtils {
final FileHeader header = BinaryDictInputOutput.readHeader(buffer); final FileHeader header = BinaryDictInputOutput.readHeader(buffer);
int wordPos = 0; int wordPos = 0;
final int wordLen = word.codePointCount(0, word.length()); final int wordLen = word.codePointCount(0, word.length());
for (int depth = 0; depth < Constants.Dictionary.MAX_WORD_LENGTH; ++depth) { for (int depth = 0; depth < Constants.DICTIONARY_MAX_WORD_LENGTH; ++depth) {
if (wordPos >= wordLen) return FormatSpec.NOT_VALID_WORD; if (wordPos >= wordLen) return FormatSpec.NOT_VALID_WORD;
do { do {
@ -746,7 +746,7 @@ public final class BinaryDictIOUtils {
final int[] codePoints = FusionDictionary.getCodePoints(word); final int[] codePoints = FusionDictionary.getCodePoints(word);
final int wordLen = codePoints.length; final int wordLen = codePoints.length;
for (int depth = 0; depth < Constants.Dictionary.MAX_WORD_LENGTH; ++depth) { for (int depth = 0; depth < Constants.DICTIONARY_MAX_WORD_LENGTH; ++depth) {
if (wordPos >= wordLen) break; if (wordPos >= wordLen) break;
nodeOriginAddress = buffer.position(); nodeOriginAddress = buffer.position();
int nodeParentAddress = -1; int nodeParentAddress = -1;

View file

@ -167,7 +167,7 @@ public final class FormatSpec {
// TODO: Make this value adaptative to content data, store it in the header, and // TODO: Make this value adaptative to content data, store it in the header, and
// use it in the reading code. // use it in the reading code.
static final int MAX_WORD_LENGTH = Constants.Dictionary.MAX_WORD_LENGTH; static final int MAX_WORD_LENGTH = Constants.DICTIONARY_MAX_WORD_LENGTH;
static final int PARENT_ADDRESS_SIZE = 3; static final int PARENT_ADDRESS_SIZE = 3;
static final int FORWARD_LINK_ADDRESS_SIZE = 3; static final int FORWARD_LINK_ADDRESS_SIZE = 3;

View file

@ -462,7 +462,7 @@ public final class FusionDictionary implements Iterable<Word> {
final ArrayList<WeightedString> shortcutTargets, final ArrayList<WeightedString> shortcutTargets,
final boolean isNotAWord, final boolean isBlacklistEntry) { final boolean isNotAWord, final boolean isBlacklistEntry) {
assert(frequency >= 0 && frequency <= 255); assert(frequency >= 0 && frequency <= 255);
if (word.length >= Constants.Dictionary.MAX_WORD_LENGTH) { if (word.length >= Constants.DICTIONARY_MAX_WORD_LENGTH) {
MakedictLog.w("Ignoring a word that is too long: word.length = " + word.length); MakedictLog.w("Ignoring a word that is too long: word.length = " + word.length);
return; return;
} }

View file

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package com.android.inputmethod.latin; package com.android.inputmethod.latin.personalization;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
@ -23,6 +23,11 @@ import android.util.Log;
import com.android.inputmethod.annotations.UsedForTesting; import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.keyboard.ProximityInfo; import com.android.inputmethod.keyboard.ProximityInfo;
import com.android.inputmethod.latin.Constants;
import com.android.inputmethod.latin.Dictionary;
import com.android.inputmethod.latin.ExpandableDictionary;
import com.android.inputmethod.latin.LatinImeLogger;
import com.android.inputmethod.latin.WordComposer;
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
import com.android.inputmethod.latin.makedict.FormatSpec.FormatOptions; import com.android.inputmethod.latin.makedict.FormatSpec.FormatOptions;
import com.android.inputmethod.latin.settings.Settings; import com.android.inputmethod.latin.settings.Settings;
@ -152,8 +157,8 @@ public final class UserHistoryDictionary extends ExpandableDictionary {
* The second word may not be null (a NullPointerException would be thrown). * The second word may not be null (a NullPointerException would be thrown).
*/ */
public int addToUserHistory(final String word1, final String word2, final boolean isValid) { public int addToUserHistory(final String word1, final String word2, final boolean isValid) {
if (word2.length() >= Constants.Dictionary.MAX_WORD_LENGTH || if (word2.length() >= Constants.DICTIONARY_MAX_WORD_LENGTH ||
(word1 != null && word1.length() >= Constants.Dictionary.MAX_WORD_LENGTH)) { (word1 != null && word1.length() >= Constants.DICTIONARY_MAX_WORD_LENGTH)) {
return -1; return -1;
} }
if (mBigramListLock.tryLock()) { if (mBigramListLock.tryLock()) {
@ -244,8 +249,8 @@ public final class UserHistoryDictionary extends ExpandableDictionary {
@Override @Override
public void setBigram(final String word1, final String word2, final int frequency) { public void setBigram(final String word1, final String word2, final int frequency) {
if (word1.length() < Constants.Dictionary.MAX_WORD_LENGTH if (word1.length() < Constants.DICTIONARY_MAX_WORD_LENGTH
&& word2.length() < Constants.Dictionary.MAX_WORD_LENGTH) { && word2.length() < Constants.DICTIONARY_MAX_WORD_LENGTH) {
profTotal++; profTotal++;
if (DBG_SAVE_RESTORE) { if (DBG_SAVE_RESTORE) {
Log.d(TAG, "load bigram: " + word1 + "," + word2 + "," + frequency); Log.d(TAG, "load bigram: " + word1 + "," + word2 + "," + frequency);
@ -404,7 +409,8 @@ public final class UserHistoryDictionary extends ExpandableDictionary {
} }
@UsedForTesting @UsedForTesting
void forceAddWordForTest(final String word1, final String word2, final boolean isValid) { /* package for test */ void forceAddWordForTest(
final String word1, final String word2, final boolean isValid) {
mBigramListLock.lock(); mBigramListLock.lock();
try { try {
addToUserHistory(word1, word2, isValid); addToUserHistory(word1, word2, isValid);

View file

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package com.android.inputmethod.latin; package com.android.inputmethod.latin.personalization;
import android.util.Log; import android.util.Log;

View file

@ -19,7 +19,6 @@ package com.android.inputmethod.latin.utils;
import android.util.Log; import android.util.Log;
import com.android.inputmethod.annotations.UsedForTesting; import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.latin.UserHistoryDictionaryBigramList;
import com.android.inputmethod.latin.makedict.BinaryDictIOUtils; import com.android.inputmethod.latin.makedict.BinaryDictIOUtils;
import com.android.inputmethod.latin.makedict.BinaryDictInputOutput; import com.android.inputmethod.latin.makedict.BinaryDictInputOutput;
import com.android.inputmethod.latin.makedict.BinaryDictInputOutput.FusionDictionaryBufferInterface; import com.android.inputmethod.latin.makedict.BinaryDictInputOutput.FusionDictionaryBufferInterface;
@ -28,6 +27,7 @@ import com.android.inputmethod.latin.makedict.FusionDictionary;
import com.android.inputmethod.latin.makedict.FusionDictionary.Node; import com.android.inputmethod.latin.makedict.FusionDictionary.Node;
import com.android.inputmethod.latin.makedict.PendingAttribute; import com.android.inputmethod.latin.makedict.PendingAttribute;
import com.android.inputmethod.latin.makedict.UnsupportedFormatException; import com.android.inputmethod.latin.makedict.UnsupportedFormatException;
import com.android.inputmethod.latin.personalization.UserHistoryDictionaryBigramList;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;

View file

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package com.android.inputmethod.latin; package com.android.inputmethod.latin.personalization;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;

View file

@ -21,10 +21,10 @@ import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.LargeTest; import android.test.suitebuilder.annotation.LargeTest;
import android.util.Log; import android.util.Log;
import com.android.inputmethod.latin.UserHistoryDictionaryBigramList;
import com.android.inputmethod.latin.makedict.FormatSpec; import com.android.inputmethod.latin.makedict.FormatSpec;
import com.android.inputmethod.latin.makedict.FusionDictionary; import com.android.inputmethod.latin.makedict.FusionDictionary;
import com.android.inputmethod.latin.makedict.FusionDictionary.CharGroup; import com.android.inputmethod.latin.makedict.FusionDictionary.CharGroup;
import com.android.inputmethod.latin.personalization.UserHistoryDictionaryBigramList;
import com.android.inputmethod.latin.utils.ByteArrayWrapper; import com.android.inputmethod.latin.utils.ByteArrayWrapper;
import com.android.inputmethod.latin.utils.UserHistoryDictIOUtils.BigramDictionaryInterface; import com.android.inputmethod.latin.utils.UserHistoryDictIOUtils.BigramDictionaryInterface;
import com.android.inputmethod.latin.utils.UserHistoryDictIOUtils.OnAddWordListener; import com.android.inputmethod.latin.utils.UserHistoryDictIOUtils.OnAddWordListener;