am ffcbbaf1: Refactor on UserHistoryDictionary

* commit 'ffcbbaf12788a9fc9398607a548e552d7d2bf05e':
  Refactor on UserHistoryDictionary
main
Satoshi Kataoka 2013-07-25 21:05:21 -07:00 committed by Android Git Automerger
commit d6ee1ded52
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();
// 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
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_COORDINATE = -1;
@ -142,6 +133,10 @@ public final class Constants {
public static final int SPELL_CHECKER_COORDINATE = -3;
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) {
// Detect {@link NOT_A_COORDINATE}, {@link SUGGESTION_STRIP_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.
*/
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

View File

@ -43,7 +43,7 @@ public class ExpandableDictionary extends Dictionary {
protected static final int BIGRAM_MAX_FREQUENCY = 255;
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 mInputLength;
@ -87,7 +87,7 @@ public class ExpandableDictionary extends Dictionary {
}
}
protected interface NextWord {
public interface NextWord {
public Node getWordNode();
public int getFrequency();
public ForgettingCurveParams getFcParams();
@ -161,7 +161,7 @@ public class ExpandableDictionary extends Dictionary {
super(dictType);
mContext = context;
clearDictionary();
mCodes = new int[Constants.Dictionary.MAX_WORD_LENGTH][];
mCodes = new int[Constants.DICTIONARY_MAX_WORD_LENGTH][];
}
public void loadDictionary() {
@ -198,11 +198,11 @@ public class ExpandableDictionary extends Dictionary {
}
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) {
if (word.length() >= Constants.Dictionary.MAX_WORD_LENGTH) {
if (word.length() >= Constants.DICTIONARY_MAX_WORD_LENGTH) {
return;
}
addWordRec(mRoots, word, 0, shortcutTarget, frequency, null);
@ -258,7 +258,7 @@ public class ExpandableDictionary extends Dictionary {
final boolean blockOffensiveWords) {
if (reloadDictionaryIfRequired()) return null;
if (composer.size() > 1) {
if (composer.size() >= Constants.Dictionary.MAX_WORD_LENGTH) {
if (composer.size() >= Constants.DICTIONARY_MAX_WORD_LENGTH) {
return null;
}
final ArrayList<SuggestedWordInfo> suggestions =
@ -629,7 +629,7 @@ public class ExpandableDictionary extends Dictionary {
}
// 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
@ -644,7 +644,7 @@ public class ExpandableDictionary extends Dictionary {
for (NextWord nextWord : terminalNodes) {
node = nextWord.getWordNode();
freq = nextWord.getFrequency();
int index = Constants.Dictionary.MAX_WORD_LENGTH;
int index = Constants.DICTIONARY_MAX_WORD_LENGTH;
do {
--index;
mLookedUpString[index] = node.mCode;
@ -656,7 +656,7 @@ public class ExpandableDictionary extends Dictionary {
// to ignore the word in this case.
if (freq >= 0 && node == null) {
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));
}
}

View File

@ -49,7 +49,7 @@ public final class LastComposedWord {
public final String mPrevWord;
public final int mCapitalizedMode;
public final InputPointers mInputPointers =
new InputPointers(Constants.Dictionary.MAX_WORD_LENGTH);
new InputPointers(Constants.DICTIONARY_MAX_WORD_LENGTH);
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.latin.SuggestedWords.SuggestedWordInfo;
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.SettingsActivity;
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_BATCH_NESTING = false;
// 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 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.keyboard.ProximityInfo;
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.BoundedTreeSet;
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
*/
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;
public static final int CAPS_MODE_OFF = 0;

View File

@ -181,7 +181,7 @@ public final class BinaryDictIOUtils {
final FileHeader header = BinaryDictInputOutput.readHeader(buffer);
int wordPos = 0;
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;
do {
@ -746,7 +746,7 @@ public final class BinaryDictIOUtils {
final int[] codePoints = FusionDictionary.getCodePoints(word);
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;
nodeOriginAddress = buffer.position();
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
// 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 FORWARD_LINK_ADDRESS_SIZE = 3;

View File

@ -462,7 +462,7 @@ public final class FusionDictionary implements Iterable<Word> {
final ArrayList<WeightedString> shortcutTargets,
final boolean isNotAWord, final boolean isBlacklistEntry) {
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);
return;
}

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.android.inputmethod.latin;
package com.android.inputmethod.latin.personalization;
import android.content.Context;
import android.content.SharedPreferences;
@ -23,6 +23,11 @@ import android.util.Log;
import com.android.inputmethod.annotations.UsedForTesting;
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.makedict.FormatSpec.FormatOptions;
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).
*/
public int addToUserHistory(final String word1, final String word2, final boolean isValid) {
if (word2.length() >= Constants.Dictionary.MAX_WORD_LENGTH ||
(word1 != null && word1.length() >= Constants.Dictionary.MAX_WORD_LENGTH)) {
if (word2.length() >= Constants.DICTIONARY_MAX_WORD_LENGTH ||
(word1 != null && word1.length() >= Constants.DICTIONARY_MAX_WORD_LENGTH)) {
return -1;
}
if (mBigramListLock.tryLock()) {
@ -244,8 +249,8 @@ public final class UserHistoryDictionary extends ExpandableDictionary {
@Override
public void setBigram(final String word1, final String word2, final int frequency) {
if (word1.length() < Constants.Dictionary.MAX_WORD_LENGTH
&& word2.length() < Constants.Dictionary.MAX_WORD_LENGTH) {
if (word1.length() < Constants.DICTIONARY_MAX_WORD_LENGTH
&& word2.length() < Constants.DICTIONARY_MAX_WORD_LENGTH) {
profTotal++;
if (DBG_SAVE_RESTORE) {
Log.d(TAG, "load bigram: " + word1 + "," + word2 + "," + frequency);
@ -404,7 +409,8 @@ public final class UserHistoryDictionary extends ExpandableDictionary {
}
@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();
try {
addToUserHistory(word1, word2, isValid);

View File

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

View File

@ -19,7 +19,6 @@ package com.android.inputmethod.latin.utils;
import android.util.Log;
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.BinaryDictInputOutput;
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.PendingAttribute;
import com.android.inputmethod.latin.makedict.UnsupportedFormatException;
import com.android.inputmethod.latin.personalization.UserHistoryDictionaryBigramList;
import java.io.IOException;
import java.io.OutputStream;

View File

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

View File

@ -21,10 +21,10 @@ import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.LargeTest;
import android.util.Log;
import com.android.inputmethod.latin.UserHistoryDictionaryBigramList;
import com.android.inputmethod.latin.makedict.FormatSpec;
import com.android.inputmethod.latin.makedict.FusionDictionary;
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.UserHistoryDictIOUtils.BigramDictionaryInterface;
import com.android.inputmethod.latin.utils.UserHistoryDictIOUtils.OnAddWordListener;