aboutsummaryrefslogtreecommitdiffstats
path: root/java/src
diff options
context:
space:
mode:
Diffstat (limited to 'java/src')
-rw-r--r--java/src/com/android/inputmethod/keyboard/Key.java64
-rw-r--r--java/src/com/android/inputmethod/keyboard/internal/KeySpecParser.java63
-rw-r--r--java/src/com/android/inputmethod/keyboard/internal/KeyboardCodesSet.java3
-rw-r--r--java/src/com/android/inputmethod/keyboard/internal/KeyboardIconsSet.java12
-rw-r--r--java/src/com/android/inputmethod/keyboard/internal/KeyboardState.java17
-rw-r--r--java/src/com/android/inputmethod/keyboard/internal/KeyboardTextsSet.java14
-rw-r--r--java/src/com/android/inputmethod/latin/AutoCorrection.java20
-rw-r--r--java/src/com/android/inputmethod/latin/BinaryDictionary.java11
-rw-r--r--java/src/com/android/inputmethod/latin/ContactsBinaryDictionary.java4
-rw-r--r--java/src/com/android/inputmethod/latin/ContactsDictionary.java2
-rw-r--r--java/src/com/android/inputmethod/latin/Dictionary.java5
-rw-r--r--java/src/com/android/inputmethod/latin/DictionaryCollection.java12
-rw-r--r--java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java42
-rw-r--r--java/src/com/android/inputmethod/latin/ExpandableDictionary.java137
-rw-r--r--java/src/com/android/inputmethod/latin/LatinIME.java49
-rw-r--r--java/src/com/android/inputmethod/latin/Settings.java3
-rw-r--r--java/src/com/android/inputmethod/latin/SettingsValues.java21
-rw-r--r--java/src/com/android/inputmethod/latin/UserHistoryDictionary.java222
-rw-r--r--java/src/com/android/inputmethod/latin/UserHistoryDictionaryBigramList.java91
-rw-r--r--java/src/com/android/inputmethod/latin/UserHistoryForgettingCurveUtils.java100
-rw-r--r--java/src/com/android/inputmethod/latin/Utils.java38
21 files changed, 673 insertions, 257 deletions
diff --git a/java/src/com/android/inputmethod/keyboard/Key.java b/java/src/com/android/inputmethod/keyboard/Key.java
index 45ed34ed2..ed873a70d 100644
--- a/java/src/com/android/inputmethod/keyboard/Key.java
+++ b/java/src/com/android/inputmethod/keyboard/Key.java
@@ -42,6 +42,7 @@ import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.util.Arrays;
+import java.util.Locale;
/**
* Class for describing the position and characteristics of a single key in the keyboard.
@@ -216,21 +217,22 @@ public class Key {
final int keyYPos = row.getKeyY();
// Horizontal gap is divided equally to both sides of the key.
- mX = (int) (keyXPos + horizontalGap / 2);
+ mX = Math.round(keyXPos + horizontalGap / 2);
mY = keyYPos;
- mWidth = (int) (keyWidth - horizontalGap);
- mHorizontalGap = (int) horizontalGap;
- mHitBox.set((int)keyXPos, keyYPos, (int)(keyXPos + keyWidth) + 1, keyYPos + keyHeight);
+ mWidth = Math.round(keyWidth - horizontalGap);
+ mHorizontalGap = Math.round(horizontalGap);
+ mHitBox.set(Math.round(keyXPos), keyYPos, Math.round(keyXPos + keyWidth) + 1,
+ keyYPos + keyHeight);
// Update row to have current x coordinate.
row.setXPos(keyXPos + keyWidth);
mBackgroundType = style.getInt(keyAttr,
R.styleable.Keyboard_Key_backgroundType, BACKGROUND_TYPE_NORMAL);
- mVisualInsetsLeft = (int) Keyboard.Builder.getDimensionOrFraction(keyAttr,
- R.styleable.Keyboard_Key_visualInsetsLeft, params.mBaseWidth, 0);
- mVisualInsetsRight = (int) Keyboard.Builder.getDimensionOrFraction(keyAttr,
- R.styleable.Keyboard_Key_visualInsetsRight, params.mBaseWidth, 0);
+ mVisualInsetsLeft = Math.round(Keyboard.Builder.getDimensionOrFraction(keyAttr,
+ R.styleable.Keyboard_Key_visualInsetsLeft, params.mBaseWidth, 0));
+ mVisualInsetsRight = Math.round(Keyboard.Builder.getDimensionOrFraction(keyAttr,
+ R.styleable.Keyboard_Key_visualInsetsRight, params.mBaseWidth, 0));
mIconId = KeySpecParser.getIconId(style.getString(keyAttr,
R.styleable.Keyboard_Key_keyIcon));
mDisabledIconId = KeySpecParser.getIconId(style.getString(keyAttr,
@@ -240,7 +242,8 @@ public class Key {
mLabelFlags = style.getFlag(keyAttr, R.styleable.Keyboard_Key_keyLabelFlags)
| row.getDefaultKeyLabelFlags();
- final boolean preserveCase = (mLabelFlags & LABEL_FLAGS_PRESERVE_CASE) != 0;
+ final boolean needsToUpperCase = needsToUpperCase(mLabelFlags, params.mId.mElementId);
+ final Locale locale = params.mId.mLocale;
int actionFlags = style.getFlag(keyAttr, R.styleable.Keyboard_Key_keyActionFlags);
String[] moreKeys = style.getStringArray(keyAttr, R.styleable.Keyboard_Key_moreKeys);
@@ -276,8 +279,8 @@ public class Key {
actionFlags |= ACTION_FLAGS_ENABLE_LONG_PRESS;
mMoreKeys = new MoreKeySpec[moreKeys.length];
for (int i = 0; i < moreKeys.length; i++) {
- mMoreKeys[i] = new MoreKeySpec(adjustCaseOfStringForKeyboardId(
- moreKeys[i], preserveCase, params.mId), params.mCodesSet);
+ mMoreKeys[i] = new MoreKeySpec(
+ moreKeys[i], needsToUpperCase, locale, params.mCodesSet);
}
} else {
mMoreKeys = null;
@@ -287,17 +290,17 @@ public class Key {
if ((mLabelFlags & LABEL_FLAGS_FROM_CUSTOM_ACTION_LABEL) != 0) {
mLabel = params.mId.mCustomActionLabel;
} else {
- mLabel = adjustCaseOfStringForKeyboardId(style.getString(keyAttr,
- R.styleable.Keyboard_Key_keyLabel), preserveCase, params.mId);
+ mLabel = KeySpecParser.toUpperCaseOfStringForLocale(style.getString(keyAttr,
+ R.styleable.Keyboard_Key_keyLabel), needsToUpperCase, locale);
}
if ((mLabelFlags & LABEL_FLAGS_DISABLE_HINT_LABEL) != 0) {
mHintLabel = null;
} else {
- mHintLabel = adjustCaseOfStringForKeyboardId(style.getString(keyAttr,
- R.styleable.Keyboard_Key_keyHintLabel), preserveCase, params.mId);
+ mHintLabel = KeySpecParser.toUpperCaseOfStringForLocale(style.getString(keyAttr,
+ R.styleable.Keyboard_Key_keyHintLabel), needsToUpperCase, locale);
}
- String outputText = adjustCaseOfStringForKeyboardId(style.getString(keyAttr,
- R.styleable.Keyboard_Key_keyOutputText), preserveCase, params.mId);
+ String outputText = KeySpecParser.toUpperCaseOfStringForLocale(style.getString(keyAttr,
+ R.styleable.Keyboard_Key_keyOutputText), needsToUpperCase, locale);
final int code = KeySpecParser.parseCode(style.getString(keyAttr,
R.styleable.Keyboard_Key_code), params.mCodesSet, CODE_UNSPECIFIED);
// Choose the first letter of the label as primary code if not specified.
@@ -326,12 +329,13 @@ public class Key {
mCode = CODE_OUTPUT_TEXT;
}
} else {
- mCode = adjustCaseOfCodeForKeyboardId(code, preserveCase, params.mId);
+ mCode = KeySpecParser.toUpperCaseOfCodeForLocale(code, needsToUpperCase, locale);
}
mOutputText = outputText;
- mAltCode = adjustCaseOfCodeForKeyboardId(KeySpecParser.parseCode(style.getString(keyAttr,
+ mAltCode = KeySpecParser.toUpperCaseOfCodeForLocale(
+ KeySpecParser.parseCode(style.getString(keyAttr,
R.styleable.Keyboard_Key_altCode), params.mCodesSet, CODE_UNSPECIFIED),
- preserveCase, params.mId);
+ needsToUpperCase, locale);
mHashCode = computeHashCode(this);
keyAttr.recycle();
@@ -341,26 +345,16 @@ public class Key {
}
}
- private static int adjustCaseOfCodeForKeyboardId(int code, boolean preserveCase,
- KeyboardId id) {
- if (!Keyboard.isLetterCode(code) || preserveCase) return code;
- final String text = new String(new int[] { code } , 0, 1);
- final String casedText = adjustCaseOfStringForKeyboardId(text, preserveCase, id);
- return StringUtils.codePointCount(casedText) == 1
- ? casedText.codePointAt(0) : CODE_UNSPECIFIED;
- }
-
- private static String adjustCaseOfStringForKeyboardId(String text, boolean preserveCase,
- KeyboardId id) {
- if (text == null || preserveCase) return text;
- switch (id.mElementId) {
+ private static boolean needsToUpperCase(int labelFlags, int keyboardElementId) {
+ if ((labelFlags & LABEL_FLAGS_PRESERVE_CASE) != 0) return false;
+ switch (keyboardElementId) {
case KeyboardId.ELEMENT_ALPHABET_MANUAL_SHIFTED:
case KeyboardId.ELEMENT_ALPHABET_AUTOMATIC_SHIFTED:
case KeyboardId.ELEMENT_ALPHABET_SHIFT_LOCKED:
case KeyboardId.ELEMENT_ALPHABET_SHIFT_LOCK_SHIFTED:
- return text.toUpperCase(id.mLocale);
+ return true;
default:
- return text;
+ return false;
}
}
diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeySpecParser.java b/java/src/com/android/inputmethod/keyboard/internal/KeySpecParser.java
index a44ddf182..c4452a5f5 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/KeySpecParser.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/KeySpecParser.java
@@ -16,6 +16,8 @@
package com.android.inputmethod.keyboard.internal;
+import static com.android.inputmethod.keyboard.Keyboard.CODE_UNSPECIFIED;
+
import android.text.TextUtils;
import com.android.inputmethod.keyboard.Keyboard;
@@ -24,6 +26,7 @@ import com.android.inputmethod.latin.StringUtils;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Locale;
/**
* The string parser of more keys specification.
@@ -63,10 +66,14 @@ public class KeySpecParser {
public final String mOutputText;
public final int mIconId;
- public MoreKeySpec(final String moreKeySpec, final KeyboardCodesSet codesSet) {
- mCode = getCode(moreKeySpec, codesSet);
- mLabel = getLabel(moreKeySpec);
- mOutputText = getOutputText(moreKeySpec);
+ public MoreKeySpec(final String moreKeySpec, boolean needsToUpperCase, Locale locale,
+ final KeyboardCodesSet codesSet) {
+ mCode = toUpperCaseOfCodeForLocale(getCode(moreKeySpec, codesSet),
+ needsToUpperCase, locale);
+ mLabel = toUpperCaseOfStringForLocale(getLabel(moreKeySpec),
+ needsToUpperCase, locale);
+ mOutputText = toUpperCaseOfStringForLocale(getOutputText(moreKeySpec),
+ needsToUpperCase, locale);
mIconId = getIconId(moreKeySpec);
}
}
@@ -76,13 +83,13 @@ public class KeySpecParser {
}
private static boolean hasIcon(String moreKeySpec) {
- return moreKeySpec.regionMatches(true, 0, PREFIX_ICON, 0, PREFIX_ICON.length());
+ return moreKeySpec.startsWith(PREFIX_ICON);
}
private static boolean hasCode(String moreKeySpec) {
final int end = indexOfLabelEnd(moreKeySpec, 0);
- if (end > 0 && end + 1 < moreKeySpec.length() && moreKeySpec.regionMatches(
- true, end + 1, PREFIX_CODE, 0, PREFIX_CODE.length())) {
+ if (end > 0 && end + 1 < moreKeySpec.length() && moreKeySpec.startsWith(
+ PREFIX_CODE, end + 1)) {
return true;
}
return false;
@@ -203,9 +210,9 @@ public class KeySpecParser {
public static int parseCode(String text, KeyboardCodesSet codesSet, int defCode) {
if (text == null) return defCode;
- if (text.regionMatches(true, 0, PREFIX_CODE, 0, PREFIX_CODE.length())) {
+ if (text.startsWith(PREFIX_CODE)) {
return codesSet.getCode(text.substring(PREFIX_CODE.length()));
- } else if (text.regionMatches(true, 0, PREFIX_HEX, 0, PREFIX_HEX.length())) {
+ } else if (text.startsWith(PREFIX_HEX)) {
return Integer.parseInt(text.substring(PREFIX_HEX.length()), 16);
} else {
return Integer.parseInt(text);
@@ -256,9 +263,8 @@ public class KeySpecParser {
}
if (out == null) {
return array;
- } else {
- return out.toArray(new String[out.size()]);
}
+ return out.toArray(new String[out.size()]);
}
public static String[] insertAdditionalMoreKeys(String[] moreKeySpecs,
@@ -353,8 +359,7 @@ public class KeySpecParser {
sb = null;
for (int pos = 0; pos < size; pos++) {
final char c = text.charAt(pos);
- if (text.regionMatches(true, pos, PREFIX_TEXT, 0, prefixLen)
- && textsSet != null) {
+ if (text.startsWith(PREFIX_TEXT, pos) && textsSet != null) {
if (sb == null) {
sb = new StringBuilder(text.substring(0, pos));
}
@@ -386,8 +391,7 @@ public class KeySpecParser {
for (int pos = start; pos < size; pos++) {
final char c = text.charAt(pos);
// Label name should be consisted of [a-zA-Z_0-9].
- if ((c >= 'a' && c <= 'z') || c == '_' || (c >= '0' && c <= '9')
- || (c >= 'A' && c <= 'Z')) {
+ if ((c >= 'a' && c <= 'z') || c == '_' || (c >= '0' && c <= '9')) {
continue;
}
return pos;
@@ -427,12 +431,11 @@ public class KeySpecParser {
final String remain = (size - start > 0) ? text.substring(start) : null;
if (list == null) {
return remain != null ? new String[] { remain } : null;
- } else {
- if (remain != null) {
- list.add(remain);
- }
- return list.toArray(new String[list.size()]);
}
+ if (remain != null) {
+ list.add(remain);
+ }
+ return list.toArray(new String[list.size()]);
}
public static int getIntValue(String[] moreKeys, String key, int defaultValue) {
@@ -444,7 +447,7 @@ public class KeySpecParser {
int value = defaultValue;
for (int i = 0; i < moreKeys.length; i++) {
final String moreKeySpec = moreKeys[i];
- if (moreKeySpec == null || !moreKeySpec.regionMatches(true, 0, key, 0, keyLen)) {
+ if (moreKeySpec == null || !moreKeySpec.startsWith(key)) {
continue;
}
moreKeys[i] = null;
@@ -468,7 +471,7 @@ public class KeySpecParser {
boolean value = false;
for (int i = 0; i < moreKeys.length; i++) {
final String moreKeySpec = moreKeys[i];
- if (moreKeySpec == null || !moreKeySpec.equalsIgnoreCase(key)) {
+ if (moreKeySpec == null || !moreKeySpec.equals(key)) {
continue;
}
moreKeys[i] = null;
@@ -476,4 +479,20 @@ public class KeySpecParser {
}
return value;
}
+
+ public static int toUpperCaseOfCodeForLocale(int code, boolean needsToUpperCase,
+ Locale locale) {
+ if (!Keyboard.isLetterCode(code) || !needsToUpperCase) return code;
+ final String text = new String(new int[] { code } , 0, 1);
+ final String casedText = KeySpecParser.toUpperCaseOfStringForLocale(
+ text, needsToUpperCase, locale);
+ return StringUtils.codePointCount(casedText) == 1
+ ? casedText.codePointAt(0) : CODE_UNSPECIFIED;
+ }
+
+ public static String toUpperCaseOfStringForLocale(String text, boolean needsToUpperCase,
+ Locale locale) {
+ if (text == null || !needsToUpperCase) return text;
+ return text.toUpperCase(locale);
+ }
}
diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyboardCodesSet.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardCodesSet.java
index c10a394c1..67cb74f4d 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/KeyboardCodesSet.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardCodesSet.java
@@ -34,9 +34,6 @@ public class KeyboardCodesSet {
public int getCode(final String name) {
Integer id = sNameToIdMap.get(name);
- if (id == null) {
- id = sNameToIdMap.get(name.toLowerCase());
- }
if (id == null) throw new RuntimeException("Unknown key code: " + name);
return mCodes[id];
}
diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyboardIconsSet.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardIconsSet.java
index a86a9577f..540e63b3f 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/KeyboardIconsSet.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardIconsSet.java
@@ -34,9 +34,8 @@ public class KeyboardIconsSet {
private static final HashMap<Integer, Integer> ATTR_ID_TO_ICON_ID
= new HashMap<Integer, Integer>();
- // Lower case icon name to icon id map.
- private static final HashMap<String, Integer> sLowerCaseNameToIdsMap =
- new HashMap<String, Integer>();
+ // Icon name to icon id map.
+ private static final HashMap<String, Integer> sNameToIdsMap = new HashMap<String, Integer>();
private static final Object[] NAMES_AND_ATTR_IDS = {
"undefined", ATTR_UNDEFINED,
@@ -70,7 +69,7 @@ public class KeyboardIconsSet {
if (attrId != ATTR_UNDEFINED) {
ATTR_ID_TO_ICON_ID.put(attrId, iconId);
}
- sLowerCaseNameToIdsMap.put(name, iconId);
+ sNameToIdsMap.put(name, iconId);
ICON_NAMES[iconId] = name;
iconId++;
}
@@ -100,10 +99,7 @@ public class KeyboardIconsSet {
}
static int getIconId(final String name) {
- Integer iconId = sLowerCaseNameToIdsMap.get(name);
- if (iconId == null) {
- iconId = sLowerCaseNameToIdsMap.get(name.toLowerCase());
- }
+ Integer iconId = sNameToIdsMap.get(name);
if (iconId != null) {
return iconId;
}
diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyboardState.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardState.java
index b63343a50..5aa9a0887 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/KeyboardState.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardState.java
@@ -85,6 +85,9 @@ public class KeyboardState {
private boolean mPrevMainKeyboardWasShiftLocked;
private boolean mPrevSymbolsKeyboardWasShifted;
+ // For handling long press.
+ private boolean mLongPressShiftLockFired;
+
// For handling double tap.
private boolean mIsInAlphabetUnshiftedFromShifted;
private boolean mIsInDoubleTapShiftKey;
@@ -309,6 +312,7 @@ public class KeyboardState {
} else {
mSwitchActions.cancelDoubleTapTimer();
mSwitchActions.cancelLongPressTimer();
+ mLongPressShiftLockFired = false;
mShiftKeyState.onOtherKeyPressed();
mSymbolKeyState.onOtherKeyPressed();
// It is required to reset the auto caps state when all of the following conditions
@@ -366,15 +370,7 @@ public class KeyboardState {
Log.d(TAG, "onLongPressTimeout: code=" + Keyboard.printableCode(code) + " " + this);
}
if (mIsAlphabetMode && code == Keyboard.CODE_SHIFT) {
- if (mAlphabetShiftState.isShiftLocked()) {
- setShiftLocked(false);
- // Shift key is long pressed while shift locked state, we will toggle back to normal
- // state. And mark as if shift key is released.
- mShiftKeyState.onRelease();
- } else {
- // Shift key is long pressed while shift unlocked state.
- setShiftLocked(true);
- }
+ mLongPressShiftLockFired = true;
mSwitchActions.hapticAndAudioFeedback(code);
}
}
@@ -404,6 +400,7 @@ public class KeyboardState {
}
private void onPressShift() {
+ mLongPressShiftLockFired = false;
if (mIsAlphabetMode) {
mIsInDoubleTapShiftKey = mSwitchActions.isInDoubleTapTimeout();
if (!mIsInDoubleTapShiftKey) {
@@ -457,6 +454,8 @@ public class KeyboardState {
// Double tap shift key has been handled in {@link #onPressShift}, so that just
// ignore this release shift key here.
mIsInDoubleTapShiftKey = false;
+ } else if (mLongPressShiftLockFired) {
+ setShiftLocked(!mAlphabetShiftState.isShiftLocked());
} else if (mShiftKeyState.isChording()) {
if (mAlphabetShiftState.isShiftLockShifted()) {
// After chording input while shift locked state.
diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyboardTextsSet.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardTextsSet.java
index 425b5e0bf..f429a3e52 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/KeyboardTextsSet.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardTextsSet.java
@@ -47,7 +47,7 @@ public final class KeyboardTextsSet {
// Language to texts map.
private static final HashMap<String, String[]> sLocaleToTextsMap =
new HashMap<String, String[]>();
- private static final HashMap<String, Integer> sLowerCaseNameToIdsMap =
+ private static final HashMap<String, Integer> sNameToIdsMap =
new HashMap<String, Integer>();
private String[] mTexts;
@@ -77,19 +77,11 @@ public final class KeyboardTextsSet {
}
public String getText(final String name) {
- String lowerCaseName = null;
String text = mResourceNameToTextsMap.get(name);
- if (text == null) {
- lowerCaseName = name.toLowerCase();
- text = mResourceNameToTextsMap.get(lowerCaseName);
- }
if (text != null) {
return text;
}
- Integer id = sLowerCaseNameToIdsMap.get(name);
- if (id == null) {
- id = sLowerCaseNameToIdsMap.get(lowerCaseName); // lowerCaseName != null
- }
+ final Integer id = sNameToIdsMap.get(name);
if (id == null) throw new RuntimeException("Unknown label: " + name);
text = (id < mTexts.length) ? mTexts[id] : null;
return (text == null) ? LANGUAGE_DEFAULT[id] : text;
@@ -2484,7 +2476,7 @@ public final class KeyboardTextsSet {
static {
int id = 0;
for (final String name : NAMES) {
- sLowerCaseNameToIdsMap.put(name, id++);
+ sNameToIdsMap.put(name, id++);
}
for (int i = 0; i < LANGUAGES_AND_TEXTS.length; i += 2) {
diff --git a/java/src/com/android/inputmethod/latin/AutoCorrection.java b/java/src/com/android/inputmethod/latin/AutoCorrection.java
index 32b213e67..e0452483c 100644
--- a/java/src/com/android/inputmethod/latin/AutoCorrection.java
+++ b/java/src/com/android/inputmethod/latin/AutoCorrection.java
@@ -50,7 +50,7 @@ public class AutoCorrection {
}
public static boolean isValidWord(final ConcurrentHashMap<String, Dictionary> dictionaries,
- CharSequence word, boolean ignoreCase) {
+ CharSequence word, boolean ignoreCase) {
if (TextUtils.isEmpty(word)) {
return false;
}
@@ -74,6 +74,24 @@ public class AutoCorrection {
return false;
}
+ public static int getMaxFrequency(final ConcurrentHashMap<String, Dictionary> dictionaries,
+ CharSequence word) {
+ if (TextUtils.isEmpty(word)) {
+ return Dictionary.NOT_A_PROBABILITY;
+ }
+ int maxFreq = -1;
+ for (final String key : dictionaries.keySet()) {
+ if (key.equals(Suggest.DICT_KEY_WHITELIST)) continue;
+ final Dictionary dictionary = dictionaries.get(key);
+ if (null == dictionary) continue;
+ final int tempFreq = dictionary.getFrequency(word);
+ if (tempFreq >= maxFreq) {
+ maxFreq = tempFreq;
+ }
+ }
+ return maxFreq;
+ }
+
public static boolean allowsToBeAutoCorrected(
final ConcurrentHashMap<String, Dictionary> dictionaries,
final CharSequence word, final boolean ignoreCase) {
diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionary.java b/java/src/com/android/inputmethod/latin/BinaryDictionary.java
index e18aee6ff..d0613bd72 100644
--- a/java/src/com/android/inputmethod/latin/BinaryDictionary.java
+++ b/java/src/com/android/inputmethod/latin/BinaryDictionary.java
@@ -84,7 +84,7 @@ public class BinaryDictionary extends Dictionary {
private native long openNative(String sourceDir, long dictOffset, long dictSize,
int typedLetterMultiplier, int fullWordMultiplier, int maxWordLength, int maxWords);
private native void closeNative(long dict);
- private native boolean isValidWordNative(long dict, int[] word, int wordLength);
+ private native int getFrequencyNative(long dict, int[] word, int wordLength);
private native boolean isValidBigramNative(long dict, int[] word1, int[] word2);
private native int getSuggestionsNative(long dict, long proximityInfo, int[] xCoordinates,
int[] yCoordinates, int[] inputCodes, int codesSize, int[] prevWordForBigrams,
@@ -201,9 +201,14 @@ public class BinaryDictionary extends Dictionary {
@Override
public boolean isValidWord(CharSequence word) {
- if (word == null) return false;
+ return getFrequency(word) >= 0;
+ }
+
+ @Override
+ public int getFrequency(CharSequence word) {
+ if (word == null) return -1;
int[] chars = StringUtils.toCodePointArray(word.toString());
- return isValidWordNative(mNativeDict, chars, chars.length);
+ return getFrequencyNative(mNativeDict, chars, chars.length);
}
// TODO: Add a batch process version (isValidBigramMultiple?) to avoid excessive numbers of jni
diff --git a/java/src/com/android/inputmethod/latin/ContactsBinaryDictionary.java b/java/src/com/android/inputmethod/latin/ContactsBinaryDictionary.java
index 0a09c845e..34308dfb3 100644
--- a/java/src/com/android/inputmethod/latin/ContactsBinaryDictionary.java
+++ b/java/src/com/android/inputmethod/latin/ContactsBinaryDictionary.java
@@ -214,6 +214,10 @@ public class ContactsBinaryDictionary extends ExpandableBinaryDictionary {
return false;
}
if (contactCount != sContactCountAtLastRebuild) {
+ if (DEBUG) {
+ Log.d(TAG, "Contact count changed: " + sContactCountAtLastRebuild + " to "
+ + contactCount);
+ }
return true;
}
// Check all contacts since it's not possible to find out which names have changed.
diff --git a/java/src/com/android/inputmethod/latin/ContactsDictionary.java b/java/src/com/android/inputmethod/latin/ContactsDictionary.java
index c9b8d6eb1..cbfbd0ec8 100644
--- a/java/src/com/android/inputmethod/latin/ContactsDictionary.java
+++ b/java/src/com/android/inputmethod/latin/ContactsDictionary.java
@@ -159,7 +159,7 @@ public class ContactsDictionary extends ExpandableDictionary {
super.addWord(word, null /* shortcut */,
FREQUENCY_FOR_CONTACTS);
if (!TextUtils.isEmpty(prevWord)) {
- super.setBigram(prevWord, word,
+ super.setBigramAndGetFrequency(prevWord, word,
FREQUENCY_FOR_CONTACTS_BIGRAM);
}
prevWord = word;
diff --git a/java/src/com/android/inputmethod/latin/Dictionary.java b/java/src/com/android/inputmethod/latin/Dictionary.java
index 231e9ab81..7cd9bc2a8 100644
--- a/java/src/com/android/inputmethod/latin/Dictionary.java
+++ b/java/src/com/android/inputmethod/latin/Dictionary.java
@@ -31,6 +31,7 @@ public abstract class Dictionary {
public static final int UNIGRAM = 0;
public static final int BIGRAM = 1;
+ public static final int NOT_A_PROBABILITY = -1;
/**
* Interface to be implemented by classes requesting words to be fetched from the dictionary.
* @see #getWords(WordComposer, CharSequence, WordCallback, ProximityInfo)
@@ -84,6 +85,10 @@ public abstract class Dictionary {
*/
abstract public boolean isValidWord(CharSequence word);
+ public int getFrequency(CharSequence word) {
+ return NOT_A_PROBABILITY;
+ }
+
/**
* Compares the contents of the character array with the typed word and returns true if they
* are the same.
diff --git a/java/src/com/android/inputmethod/latin/DictionaryCollection.java b/java/src/com/android/inputmethod/latin/DictionaryCollection.java
index f3aa27a22..1a05fcd86 100644
--- a/java/src/com/android/inputmethod/latin/DictionaryCollection.java
+++ b/java/src/com/android/inputmethod/latin/DictionaryCollection.java
@@ -70,6 +70,18 @@ public class DictionaryCollection extends Dictionary {
return false;
}
+ @Override
+ public int getFrequency(CharSequence word) {
+ int maxFreq = -1;
+ for (int i = mDictionaries.size() - 1; i >= 0; --i) {
+ final int tempFreq = mDictionaries.get(i).getFrequency(word);
+ if (tempFreq >= maxFreq) {
+ maxFreq = tempFreq;
+ }
+ }
+ return maxFreq;
+ }
+
public boolean isEmpty() {
return mDictionaries.isEmpty();
}
diff --git a/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java b/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java
index 08f585485..c65404cbc 100644
--- a/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java
+++ b/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java
@@ -294,7 +294,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
*/
protected void loadBinaryDictionary() {
if (DEBUG) {
- Log.d(TAG, "Loading binary dictionary: request="
+ Log.d(TAG, "Loading binary dictionary: " + mFilename + " request="
+ mSharedDictionaryController.mLastUpdateRequestTime + " update="
+ mSharedDictionaryController.mLastUpdateTime);
}
@@ -326,7 +326,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
*/
private void generateBinaryDictionary() {
if (DEBUG) {
- Log.d(TAG, "Generating binary dictionary: request="
+ Log.d(TAG, "Generating binary dictionary: " + mFilename + " request="
+ mSharedDictionaryController.mLastUpdateRequestTime + " update="
+ mSharedDictionaryController.mLastUpdateTime);
}
@@ -371,7 +371,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
mLocalDictionaryController.mLastUpdateRequestTime = time;
mSharedDictionaryController.mLastUpdateRequestTime = time;
if (DEBUG) {
- Log.d(TAG, "Reload request: request=" + time + " update="
+ Log.d(TAG, "Reload request: " + mFilename + ": request=" + time + " update="
+ mSharedDictionaryController.mLastUpdateTime);
}
}
@@ -380,28 +380,46 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
* Reloads the dictionary if required. Reload will occur asynchronously in a separate thread.
*/
void asyncReloadDictionaryIfRequired() {
+ if (!isReloadRequired()) return;
+ if (DEBUG) {
+ Log.d(TAG, "Starting AsyncReloadDictionaryTask: " + mFilename);
+ }
new AsyncReloadDictionaryTask().start();
}
/**
- * Reloads the dictionary if required. Access is controlled on a per dictionary file basis and
- * supports concurrent calls from multiple instances that share the same dictionary file.
+ * Reloads the dictionary if required.
*/
protected final void syncReloadDictionaryIfRequired() {
- if (mBinaryDictionary != null && !mLocalDictionaryController.isOutOfDate()) {
- return;
- }
+ if (!isReloadRequired()) return;
+ syncReloadDictionaryInternal();
+ }
+
+ /**
+ * Returns whether a dictionary reload is required.
+ */
+ private boolean isReloadRequired() {
+ return mBinaryDictionary == null || mLocalDictionaryController.isOutOfDate();
+ }
+ /**
+ * Reloads the dictionary. Access is controlled on a per dictionary file basis and supports
+ * concurrent calls from multiple instances that share the same dictionary file.
+ */
+ private final void syncReloadDictionaryInternal() {
// Ensure that only one thread attempts to read or write to the shared binary dictionary
// file at the same time.
mSharedDictionaryController.lock();
try {
final long time = SystemClock.uptimeMillis();
- if (mSharedDictionaryController.isOutOfDate() || !dictionaryFileExists()) {
+ final boolean dictionaryFileExists = dictionaryFileExists();
+ if (mSharedDictionaryController.isOutOfDate() || !dictionaryFileExists) {
// If the shared dictionary file does not exist or is out of date, the first
// instance that acquires the lock will generate a new one.
- if (hasContentChanged()) {
- // If the source content has changed, rebuild the binary dictionary.
+ if (hasContentChanged() || !dictionaryFileExists) {
+ // If the source content has changed or the dictionary does not exist, rebuild
+ // the binary dictionary. Empty dictionaries are supported (in the case where
+ // loadDictionaryAsync() adds nothing) in order to provide a uniform framework.
mSharedDictionaryController.mLastUpdateTime = time;
generateBinaryDictionary();
loadBinaryDictionary();
@@ -435,7 +453,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
private class AsyncReloadDictionaryTask extends Thread {
@Override
public void run() {
- syncReloadDictionaryIfRequired();
+ syncReloadDictionaryInternal();
}
}
diff --git a/java/src/com/android/inputmethod/latin/ExpandableDictionary.java b/java/src/com/android/inputmethod/latin/ExpandableDictionary.java
index 6c457afd2..34a92fd30 100644
--- a/java/src/com/android/inputmethod/latin/ExpandableDictionary.java
+++ b/java/src/com/android/inputmethod/latin/ExpandableDictionary.java
@@ -21,6 +21,7 @@ import android.content.Context;
import com.android.inputmethod.keyboard.KeyDetector;
import com.android.inputmethod.keyboard.Keyboard;
import com.android.inputmethod.keyboard.ProximityInfo;
+import com.android.inputmethod.latin.UserHistoryForgettingCurveUtils.ForgettingCurveParams;
import java.util.ArrayList;
import java.util.LinkedList;
@@ -80,31 +81,72 @@ public class ExpandableDictionary extends Dictionary {
}
}
- private static class NextWord {
- public final Node mWord;
- private int mFrequency;
+ protected interface NextWord {
+ public Node getWordNode();
+ public int getFrequency();
+ public ForgettingCurveParams getFcParams();
+ public int notifyTypedAgainAndGetFrequency();
+ }
- public NextWord(Node word, int frequency) {
+ private static class NextStaticWord implements NextWord {
+ public final Node mWord;
+ private final int mFrequency;
+ public NextStaticWord(Node word, int frequency) {
mWord = word;
mFrequency = frequency;
}
+ @Override
+ public Node getWordNode() {
+ return mWord;
+ }
+
+ @Override
public int getFrequency() {
return mFrequency;
}
- public int setFrequency(int freq) {
- mFrequency = freq;
- return mFrequency;
+ @Override
+ public ForgettingCurveParams getFcParams() {
+ return null;
}
- public int addFrequency(int add) {
- mFrequency += add;
- if (mFrequency > BIGRAM_MAX_FREQUENCY) mFrequency = BIGRAM_MAX_FREQUENCY;
+ @Override
+ public int notifyTypedAgainAndGetFrequency() {
return mFrequency;
}
}
+ private static class NextHistoryWord implements NextWord {
+ public final Node mWord;
+ public final ForgettingCurveParams mFcp;
+
+ public NextHistoryWord(Node word, ForgettingCurveParams fcp) {
+ mWord = word;
+ mFcp = fcp;
+ }
+
+ @Override
+ public Node getWordNode() {
+ return mWord;
+ }
+
+ @Override
+ public int getFrequency() {
+ return mFcp.getFrequency();
+ }
+
+ @Override
+ public ForgettingCurveParams getFcParams() {
+ return mFcp;
+ }
+
+ @Override
+ public int notifyTypedAgainAndGetFrequency() {
+ return mFcp.notifyTypedAgainAndGetFrequency();
+ }
+ }
+
private NodeArray mRoots;
private int[][] mCodes;
@@ -183,7 +225,7 @@ public class ExpandableDictionary extends Dictionary {
childNode.mShortcutOnly = isShortcutOnly;
children.add(childNode);
}
- if (wordLength == depth + 1) {
+ if (wordLength == depth + 1 && shortcutTarget != null) {
// Terminate this word
childNode.mTerminal = true;
if (isShortcutOnly) {
@@ -221,7 +263,7 @@ public class ExpandableDictionary extends Dictionary {
protected final void getWordsInner(final WordComposer codes,
final CharSequence prevWordForBigrams, final WordCallback callback,
- @SuppressWarnings("unused") final ProximityInfo proximityInfo) {
+ final ProximityInfo proximityInfo) {
mInputLength = codes.size();
if (mCodes.length < mInputLength) mCodes = new int[mInputLength][];
final int[] xCoordinates = codes.getXCoordinates();
@@ -265,13 +307,13 @@ public class ExpandableDictionary extends Dictionary {
// Refer to addOrSetBigram() about word1.toLowerCase()
final Node firstWord = searchWord(mRoots, word1.toLowerCase(), 0, null);
final Node secondWord = searchWord(mRoots, word2, 0, null);
- LinkedList<NextWord> bigram = firstWord.mNGrams;
+ LinkedList<NextWord> bigrams = firstWord.mNGrams;
NextWord bigramNode = null;
- if (bigram == null || bigram.size() == 0) {
+ if (bigrams == null || bigrams.size() == 0) {
return false;
} else {
- for (NextWord nw : bigram) {
- if (nw.mWord == secondWord) {
+ for (NextWord nw : bigrams) {
+ if (nw.getWordNode() == secondWord) {
bigramNode = nw;
break;
}
@@ -280,7 +322,7 @@ public class ExpandableDictionary extends Dictionary {
if (bigramNode == null) {
return false;
}
- return bigram.remove(bigramNode);
+ return bigrams.remove(bigramNode);
}
/**
@@ -292,6 +334,23 @@ public class ExpandableDictionary extends Dictionary {
return (node == null) ? -1 : node.mFrequency;
}
+ protected NextWord getBigramWord(String word1, String word2) {
+ // Refer to addOrSetBigram() about word1.toLowerCase()
+ final Node firstWord = searchWord(mRoots, word1.toLowerCase(), 0, null);
+ final Node secondWord = searchWord(mRoots, word2, 0, null);
+ LinkedList<NextWord> bigrams = firstWord.mNGrams;
+ if (bigrams == null || bigrams.size() == 0) {
+ return null;
+ } else {
+ for (NextWord nw : bigrams) {
+ if (nw.getWordNode() == secondWord) {
+ return nw;
+ }
+ }
+ }
+ return null;
+ }
+
private static int computeSkippedWordFinalFreq(int freq, int snr, int inputLength) {
// The computation itself makes sense for >= 2, but the == 2 case returns 0
// anyway so we may as well test against 3 instead and return the constant
@@ -445,43 +504,45 @@ public class ExpandableDictionary extends Dictionary {
}
}
- protected int setBigram(String word1, String word2, int frequency) {
- return addOrSetBigram(word1, word2, frequency, false);
+ public int setBigramAndGetFrequency(String word1, String word2, int frequency) {
+ return setBigramAndGetFrequency(word1, word2, frequency, null /* unused */);
}
- protected int addBigram(String word1, String word2, int frequency) {
- return addOrSetBigram(word1, word2, frequency, true);
+ public int setBigramAndGetFrequency(String word1, String word2, ForgettingCurveParams fcp) {
+ return setBigramAndGetFrequency(word1, word2, 0 /* unused */, fcp);
}
/**
* Adds bigrams to the in-memory trie structure that is being used to retrieve any word
* @param frequency frequency for this bigram
* @param addFrequency if true, it adds to current frequency, else it overwrites the old value
- * @return returns the final frequency
+ * @return returns the final bigram frequency
*/
- private int addOrSetBigram(String word1, String word2, int frequency, boolean addFrequency) {
+ private int setBigramAndGetFrequency(
+ String word1, String word2, int frequency, ForgettingCurveParams fcp) {
// We don't want results to be different according to case of the looked up left hand side
// word. We do want however to return the correct case for the right hand side.
// So we want to squash the case of the left hand side, and preserve that of the right
// hand side word.
Node firstWord = searchWord(mRoots, word1.toLowerCase(), 0, null);
Node secondWord = searchWord(mRoots, word2, 0, null);
- LinkedList<NextWord> bigram = firstWord.mNGrams;
- if (bigram == null || bigram.size() == 0) {
+ LinkedList<NextWord> bigrams = firstWord.mNGrams;
+ if (bigrams == null || bigrams.size() == 0) {
firstWord.mNGrams = new LinkedList<NextWord>();
- bigram = firstWord.mNGrams;
+ bigrams = firstWord.mNGrams;
} else {
- for (NextWord nw : bigram) {
- if (nw.mWord == secondWord) {
- if (addFrequency) {
- return nw.addFrequency(frequency);
- } else {
- return nw.setFrequency(frequency);
- }
+ for (NextWord nw : bigrams) {
+ if (nw.getWordNode() == secondWord) {
+ return nw.notifyTypedAgainAndGetFrequency();
}
}
}
- firstWord.mNGrams.add(new NextWord(secondWord, frequency));
+ if (fcp != null) {
+ // history
+ firstWord.mNGrams.add(new NextHistoryWord(secondWord, fcp));
+ } else {
+ firstWord.mNGrams.add(new NextStaticWord(secondWord, frequency));
+ }
return frequency;
}
@@ -580,7 +641,7 @@ public class ExpandableDictionary extends Dictionary {
Node node;
int freq;
for (NextWord nextWord : terminalNodes) {
- node = nextWord.mWord;
+ node = nextWord.getWordNode();
freq = nextWord.getFrequency();
int index = BinaryDictionary.MAX_WORD_LENGTH;
do {
@@ -589,8 +650,10 @@ public class ExpandableDictionary extends Dictionary {
node = node.mParent;
} while (node != null);
- callback.addWord(mLookedUpString, index, BinaryDictionary.MAX_WORD_LENGTH - index,
- freq, mDicTypeId, Dictionary.BIGRAM);
+ if (freq >= 0) {
+ callback.addWord(mLookedUpString, index, BinaryDictionary.MAX_WORD_LENGTH - index,
+ freq, mDicTypeId, Dictionary.BIGRAM);
+ }
}
}
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index 298ea2e55..bd9a0c1d4 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -45,6 +45,8 @@ import android.text.TextUtils;
import android.util.Log;
import android.util.PrintWriterPrinter;
import android.util.Printer;
+import android.view.KeyCharacterMap;
+import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
@@ -494,7 +496,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
resetContactsDictionary(oldContactsDictionary);
mUserHistoryDictionary = new UserHistoryDictionary(
- this, localeStr, Suggest.DIC_USER_HISTORY);
+ this, localeStr, Suggest.DIC_USER_HISTORY, mPrefs);
mSuggest.setUserHistoryDictionary(mUserHistoryDictionary);
}
@@ -753,7 +755,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
KeyboardView inputView = mKeyboardSwitcher.getKeyboardView();
if (inputView != null) inputView.closing();
- if (mUserHistoryDictionary != null) mUserHistoryDictionary.flushPendingWrites();
}
private void onFinishInputViewInternal(boolean finishingInput) {
@@ -1237,6 +1238,16 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
}
}
+ static private void sendUpDownEnterOrBackspace(final int code, final InputConnection ic) {
+ final long eventTime = SystemClock.uptimeMillis();
+ ic.sendKeyEvent(new KeyEvent(eventTime, eventTime,
+ KeyEvent.ACTION_DOWN, code, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0,
+ KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE));
+ ic.sendKeyEvent(new KeyEvent(SystemClock.uptimeMillis(), eventTime,
+ KeyEvent.ACTION_UP, code, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0,
+ KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE));
+ }
+
private void sendKeyCodePoint(int code) {
// TODO: Remove this special handling of digit letters.
// For backward compatibility. See {@link InputMethodService#sendKeyChar(char)}.
@@ -1247,8 +1258,19 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
final InputConnection ic = getCurrentInputConnection();
if (ic != null) {
- final String text = new String(new int[] { code }, 0, 1);
- ic.commitText(text, text.length());
+ // 16 is android.os.Build.VERSION_CODES.JELLY_BEAN but we can't write it because
+ // we want to be able to compile against the Ice Cream Sandwich SDK.
+ if (Keyboard.CODE_ENTER == code && mTargetApplicationInfo != null
+ && mTargetApplicationInfo.targetSdkVersion < 16) {
+ // Backward compatibility mode. Before Jelly bean, the keyboard would simulate
+ // a hardware keyboard event on pressing enter or delete. This is bad for many
+ // reasons (there are race conditions with commits) but some applications are
+ // relying on this behavior so we continue to support it for older apps.
+ sendUpDownEnterOrBackspace(KeyEvent.KEYCODE_ENTER, ic);
+ } else {
+ final String text = new String(new int[] { code }, 0, 1);
+ ic.commitText(text, text.length());
+ }
if (ProductionFlag.IS_EXPERIMENTAL) {
ResearchLogger.latinIME_sendKeyCodePoint(code);
}
@@ -1475,7 +1497,18 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
// This should never happen.
Log.e(TAG, "Backspace when we don't know the selection position");
}
- ic.deleteSurroundingText(1, 0);
+ // 16 is android.os.Build.VERSION_CODES.JELLY_BEAN but we can't write it because
+ // we want to be able to compile against the Ice Cream Sandwich SDK.
+ if (mTargetApplicationInfo != null
+ && mTargetApplicationInfo.targetSdkVersion < 16) {
+ // Backward compatibility mode. Before Jelly bean, the keyboard would simulate
+ // a hardware keyboard event on pressing enter or delete. This is bad for many
+ // reasons (there are race conditions with commits) but some applications are
+ // relying on this behavior so we continue to support it for older apps.
+ sendUpDownEnterOrBackspace(KeyEvent.KEYCODE_DEL, ic);
+ } else {
+ ic.deleteSurroundingText(1, 0);
+ }
if (ProductionFlag.IS_EXPERIMENTAL) {
ResearchLogger.latinIME_deleteSurroundingText(1);
}
@@ -2071,8 +2104,12 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
} else {
secondWord = suggestion.toString();
}
+ // We demote unrecognized word and words with 0-frequency (assuming they would be
+ // profanity etc.) by specifying them as "invalid".
+ final int maxFreq = AutoCorrection.getMaxFrequency(
+ mSuggest.getUnigramDictionaries(), suggestion);
mUserHistoryDictionary.addToUserHistory(null == prevWord ? null : prevWord.toString(),
- secondWord);
+ secondWord, maxFreq > 0);
return prevWord;
}
return null;
diff --git a/java/src/com/android/inputmethod/latin/Settings.java b/java/src/com/android/inputmethod/latin/Settings.java
index 74c4aea0c..08f3e8456 100644
--- a/java/src/com/android/inputmethod/latin/Settings.java
+++ b/java/src/com/android/inputmethod/latin/Settings.java
@@ -58,6 +58,8 @@ public class Settings extends InputMethodSettingsFragment
public static final String PREF_SHOW_SUGGESTIONS_SETTING = "show_suggestions_setting";
public static final String PREF_MISC_SETTINGS = "misc_settings";
public static final String PREF_USABILITY_STUDY_MODE = "usability_study_mode";
+ public static final String PREF_LAST_USER_DICTIONARY_WRITE_TIME =
+ "last_user_dictionary_write_time";
public static final String PREF_ADVANCED_SETTINGS = "pref_advanced_settings";
public static final String PREF_SUPPRESS_LANGUAGE_SWITCH_KEY =
"pref_suppress_language_switch_key";
@@ -244,7 +246,6 @@ public class Settings extends InputMethodSettingsFragment
refreshEnablingsOfKeypressSoundAndVibrationSettings(prefs, res);
}
- @SuppressWarnings("unused")
@Override
public void onResume() {
super.onResume();
diff --git a/java/src/com/android/inputmethod/latin/SettingsValues.java b/java/src/com/android/inputmethod/latin/SettingsValues.java
index 932920a60..4aae6a85e 100644
--- a/java/src/com/android/inputmethod/latin/SettingsValues.java
+++ b/java/src/com/android/inputmethod/latin/SettingsValues.java
@@ -28,6 +28,8 @@ import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
/**
* When you call the constructor of this class, you may want to change the current system locale by
@@ -351,4 +353,23 @@ public class SettingsValues {
// TODO: use mUsabilityStudyMode instead of reading it again here
return prefs.getBoolean(Settings.PREF_USABILITY_STUDY_MODE, true);
}
+
+ public static long getLastUserHistoryWriteTime(
+ final SharedPreferences prefs, final String locale) {
+ final String str = prefs.getString(Settings.PREF_LAST_USER_DICTIONARY_WRITE_TIME, "");
+ final HashMap<String, Long> map = Utils.localeAndTimeStrToHashMap(str);
+ if (map.containsKey(locale)) {
+ return map.get(locale);
+ }
+ return 0;
+ }
+
+ public static void setLastUserHistoryWriteTime(
+ final SharedPreferences prefs, final String locale) {
+ final String oldStr = prefs.getString(Settings.PREF_LAST_USER_DICTIONARY_WRITE_TIME, "");
+ final HashMap<String, Long> map = Utils.localeAndTimeStrToHashMap(oldStr);
+ map.put(locale, System.currentTimeMillis());
+ final String newStr = Utils.localeAndTimeHashMapToStr(map);
+ prefs.edit().putString(Settings.PREF_LAST_USER_DICTIONARY_WRITE_TIME, newStr).apply();
+ }
}
diff --git a/java/src/com/android/inputmethod/latin/UserHistoryDictionary.java b/java/src/com/android/inputmethod/latin/UserHistoryDictionary.java
index efafacc8a..c8ad40b12 100644
--- a/java/src/com/android/inputmethod/latin/UserHistoryDictionary.java
+++ b/java/src/com/android/inputmethod/latin/UserHistoryDictionary.java
@@ -18,6 +18,7 @@ package com.android.inputmethod.latin;
import android.content.ContentValues;
import android.content.Context;
+import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
@@ -26,9 +27,9 @@ import android.os.AsyncTask;
import android.provider.BaseColumns;
import android.util.Log;
+import com.android.inputmethod.latin.UserHistoryForgettingCurveUtils.ForgettingCurveParams;
+
import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
/**
* Locally gathers stats about the words user types and various other signals like auto-correction
@@ -36,13 +37,11 @@ import java.util.Iterator;
*/
public class UserHistoryDictionary extends ExpandableDictionary {
private static final String TAG = "UserHistoryDictionary";
+ public static final boolean DBG_SAVE_RESTORE = false;
/** Any pair being typed or picked */
private static final int FREQUENCY_FOR_TYPED = 2;
- /** Maximum frequency for all pairs */
- private static final int FREQUENCY_MAX = 127;
-
/** Maximum number of pairs. Pruning will start when databases goes above this number. */
private static int sMaxHistoryBigrams = 10000;
@@ -78,9 +77,11 @@ public class UserHistoryDictionary extends ExpandableDictionary {
/** Locale for which this auto dictionary is storing words */
private String mLocale;
- private HashSet<Bigram> mPendingWrites = new HashSet<Bigram>();
+ private UserHistoryDictionaryBigramList mBigramList =
+ new UserHistoryDictionaryBigramList();
private final Object mPendingWritesLock = new Object();
private static volatile boolean sUpdatingDB = false;
+ private final SharedPreferences mPrefs;
private final static HashMap<String, String> sDictProjectionMap;
@@ -98,37 +99,6 @@ public class UserHistoryDictionary extends ExpandableDictionary {
private static DatabaseHelper sOpenHelper = null;
- private static class Bigram {
- public final String mWord1;
- public final String mWord2;
- public final int mFrequency;
-
- Bigram(String word1, String word2, int frequency) {
- this.mWord1 = word1;
- this.mWord2 = word2;
- this.mFrequency = frequency;
- }
-
- @Override
- public boolean equals(Object bigram) {
- if (!(bigram instanceof Bigram)) {
- return false;
- }
- final Bigram bigram2 = (Bigram) bigram;
- final boolean eq1 =
- mWord1 == null ? bigram2.mWord1 == null : mWord1.equals(bigram2.mWord1);
- if (!eq1) {
- return false;
- }
- return mWord2 == null ? bigram2.mWord2 == null : mWord2.equals(bigram2.mWord2);
- }
-
- @Override
- public int hashCode() {
- return (mWord1 + " " + mWord2).hashCode();
- }
- }
-
public void setDatabaseMax(int maxHistoryBigram) {
sMaxHistoryBigrams = maxHistoryBigram;
}
@@ -137,7 +107,8 @@ public class UserHistoryDictionary extends ExpandableDictionary {
sDeleteHistoryBigrams = deleteHistoryBigram;
}
- public UserHistoryDictionary(final Context context, final String locale, final int dicTypeId) {
+ public UserHistoryDictionary(final Context context, final String locale, final int dicTypeId,
+ SharedPreferences sp) {
super(context, dicTypeId);
mLocale = locale;
if (sOpenHelper == null) {
@@ -146,11 +117,13 @@ public class UserHistoryDictionary extends ExpandableDictionary {
if (mLocale != null && mLocale.length() > 1) {
loadDictionary();
}
+ mPrefs = sp;
}
@Override
public void close() {
flushPendingWrites();
+ SettingsValues.setLastUserHistoryWriteTime(mPrefs, mLocale);
// Don't close the database as locale changes will require it to be reopened anyway
// Also, the database is written to somewhat frequently, so it needs to be kept alive
// throughout the life of the process.
@@ -175,38 +148,30 @@ public class UserHistoryDictionary extends ExpandableDictionary {
* context, as in beginning of a sentence for example.
* The second word may not be null (a NullPointerException would be thrown).
*/
- public int addToUserHistory(final String word1, String word2) {
- super.addWord(word2, null /* shortcut */, FREQUENCY_FOR_TYPED);
+ public int addToUserHistory(final String word1, String word2, boolean isValid) {
+ super.addWord(word2, null /* the "shortcut" parameter is null */, FREQUENCY_FOR_TYPED);
// Do not insert a word as a bigram of itself
if (word2.equals(word1)) {
return 0;
}
-
- int freq;
+ final int freq;
if (null == word1) {
freq = FREQUENCY_FOR_TYPED;
} else {
- freq = super.addBigram(word1, word2, FREQUENCY_FOR_TYPED);
+ freq = super.setBigramAndGetFrequency(word1, word2, new ForgettingCurveParams(isValid));
}
- if (freq > FREQUENCY_MAX) freq = FREQUENCY_MAX;
synchronized (mPendingWritesLock) {
- if (freq == FREQUENCY_FOR_TYPED || mPendingWrites.isEmpty()) {
- mPendingWrites.add(new Bigram(word1, word2, freq));
- } else {
- Bigram bi = new Bigram(word1, word2, freq);
- mPendingWrites.remove(bi);
- mPendingWrites.add(bi);
- }
+ mBigramList.addBigram(word1, word2);
}
return freq;
}
public boolean cancelAddingUserHistory(String word1, String word2) {
- final Bigram bi = new Bigram(word1, word2, 0);
- if (mPendingWrites.contains(bi)) {
- mPendingWrites.remove(bi);
- return super.removeBigram(word1, word2);
+ synchronized (mPendingWritesLock) {
+ if (mBigramList.removeBigram(word1, word2)) {
+ return super.removeBigram(word1, word2);
+ }
}
return false;
}
@@ -214,14 +179,14 @@ public class UserHistoryDictionary extends ExpandableDictionary {
/**
* Schedules a background thread to write any pending words to the database.
*/
- public void flushPendingWrites() {
+ private void flushPendingWrites() {
synchronized (mPendingWritesLock) {
// Nothing pending? Return
- if (mPendingWrites.isEmpty()) return;
+ if (mBigramList.isEmpty()) return;
// Create a background thread to write the pending entries
- new UpdateDbTask(sOpenHelper, mPendingWrites, mLocale).execute();
+ new UpdateDbTask(sOpenHelper, mBigramList, mLocale, this).execute();
// Create a new map for writing new entries into while the old one is written to db
- mPendingWrites = new HashSet<Bigram>();
+ mBigramList = new UserHistoryDictionaryBigramList();
}
}
@@ -240,25 +205,34 @@ public class UserHistoryDictionary extends ExpandableDictionary {
@Override
public void loadDictionaryAsync() {
+ final long last = SettingsValues.getLastUserHistoryWriteTime(mPrefs, mLocale);
+ final long now = System.currentTimeMillis();
// Load the words that correspond to the current input locale
final Cursor cursor = query(MAIN_COLUMN_LOCALE + "=?", new String[] { mLocale });
if (null == cursor) return;
try {
if (cursor.moveToFirst()) {
- int word1Index = cursor.getColumnIndex(MAIN_COLUMN_WORD1);
- int word2Index = cursor.getColumnIndex(MAIN_COLUMN_WORD2);
- int frequencyIndex = cursor.getColumnIndex(FREQ_COLUMN_FREQUENCY);
+ final int word1Index = cursor.getColumnIndex(MAIN_COLUMN_WORD1);
+ final int word2Index = cursor.getColumnIndex(MAIN_COLUMN_WORD2);
+ final int frequencyIndex = cursor.getColumnIndex(FREQ_COLUMN_FREQUENCY);
while (!cursor.isAfterLast()) {
- String word1 = cursor.getString(word1Index);
- String word2 = cursor.getString(word2Index);
- int frequency = cursor.getInt(frequencyIndex);
+ final String word1 = cursor.getString(word1Index);
+ final String word2 = cursor.getString(word2Index);
+ final int frequency = cursor.getInt(frequencyIndex);
+ if (DBG_SAVE_RESTORE) {
+ Log.d(TAG, "--- Load user history: " + word1 + ", " + word2);
+ }
// Safeguard against adding really long words. Stack may overflow due
// to recursive lookup
if (null == word1) {
super.addWord(word2, null /* shortcut */, frequency);
} else if (word1.length() < BinaryDictionary.MAX_WORD_LENGTH
&& word2.length() < BinaryDictionary.MAX_WORD_LENGTH) {
- super.setBigram(word1, word2, frequency);
+ super.setBigramAndGetFrequency(
+ word1, word2, new ForgettingCurveParams(frequency, now, last));
+ }
+ synchronized(mPendingWritesLock) {
+ mBigramList.addBigram(word1, word2);
}
cursor.moveToNext();
}
@@ -337,15 +311,18 @@ public class UserHistoryDictionary extends ExpandableDictionary {
* the in-memory trie.
*/
private static class UpdateDbTask extends AsyncTask<Void, Void, Void> {
- private final HashSet<Bigram> mMap;
+ private final UserHistoryDictionaryBigramList mBigramList;
private final DatabaseHelper mDbHelper;
private final String mLocale;
+ private final UserHistoryDictionary mUserHistoryDictionary;
- public UpdateDbTask(DatabaseHelper openHelper, HashSet<Bigram> pendingWrites,
- String locale) {
- mMap = pendingWrites;
+ public UpdateDbTask(
+ DatabaseHelper openHelper, UserHistoryDictionaryBigramList pendingWrites,
+ String locale, UserHistoryDictionary dict) {
+ mBigramList = pendingWrites;
mLocale = locale;
mDbHelper = openHelper;
+ mUserHistoryDictionary = dict;
}
/** Prune any old data if the database is getting too big. */
@@ -357,7 +334,8 @@ public class UserHistoryDictionary extends ExpandableDictionary {
int totalRowCount = c.getCount();
// prune out old data if we have too much data
if (totalRowCount > sMaxHistoryBigrams) {
- int numDeleteRows = (totalRowCount - sMaxHistoryBigrams) + sDeleteHistoryBigrams;
+ int numDeleteRows = (totalRowCount - sMaxHistoryBigrams)
+ + sDeleteHistoryBigrams;
int pairIdColumnId = c.getColumnIndex(FREQ_COLUMN_PAIR_ID);
c.moveToFirst();
int count = 0;
@@ -396,44 +374,76 @@ public class UserHistoryDictionary extends ExpandableDictionary {
return null;
}
db.execSQL("PRAGMA foreign_keys = ON;");
- // Write all the entries to the db
- Iterator<Bigram> iterator = mMap.iterator();
- while (iterator.hasNext()) {
- // TODO: this process of making a text search for each pair each time
- // is terribly inefficient. Optimize this.
- Bigram bi = iterator.next();
-
- // find pair id
- final Cursor c;
- if (null != bi.mWord1) {
- c = db.query(MAIN_TABLE_NAME, new String[] { MAIN_COLUMN_ID },
- MAIN_COLUMN_WORD1 + "=? AND " + MAIN_COLUMN_WORD2 + "=? AND "
- + MAIN_COLUMN_LOCALE + "=?",
- new String[] { bi.mWord1, bi.mWord2, mLocale }, null, null, null);
- } else {
- c = db.query(MAIN_TABLE_NAME, new String[] { MAIN_COLUMN_ID },
- MAIN_COLUMN_WORD1 + " IS NULL AND " + MAIN_COLUMN_WORD2 + "=? AND "
- + MAIN_COLUMN_LOCALE + "=?",
- new String[] { bi.mWord2, mLocale }, null, null, null);
- }
+ final boolean addLevel0Bigram = mBigramList.size() <= sMaxHistoryBigrams;
- int pairId;
- if (c.moveToFirst()) {
- // existing pair
- pairId = c.getInt(c.getColumnIndex(MAIN_COLUMN_ID));
- db.delete(FREQ_TABLE_NAME, FREQ_COLUMN_PAIR_ID + "=?",
- new String[] { Integer.toString(pairId) });
- } else {
- // new pair
- Long pairIdLong = db.insert(MAIN_TABLE_NAME, null,
- getContentValues(bi.mWord1, bi.mWord2, mLocale));
- pairId = pairIdLong.intValue();
+ // Write all the entries to the db
+ for (String word1 : mBigramList.keySet()) {
+ for (String word2 : mBigramList.getBigrams(word1)) {
+ // TODO: this process of making a text search for each pair each time
+ // is terribly inefficient. Optimize this.
+ // find pair id
+ Cursor c = null;
+ try {
+ if (null != word1) {
+ c = db.query(MAIN_TABLE_NAME, new String[] { MAIN_COLUMN_ID },
+ MAIN_COLUMN_WORD1 + "=? AND " + MAIN_COLUMN_WORD2 + "=? AND "
+ + MAIN_COLUMN_LOCALE + "=?",
+ new String[] { word1, word2, mLocale }, null, null,
+ null);
+ } else {
+ c = db.query(MAIN_TABLE_NAME, new String[] { MAIN_COLUMN_ID },
+ MAIN_COLUMN_WORD1 + " IS NULL AND " + MAIN_COLUMN_WORD2
+ + "=? AND " + MAIN_COLUMN_LOCALE + "=?",
+ new String[] { word2, mLocale }, null, null, null);
+ }
+
+ final int pairId;
+ if (c.moveToFirst()) {
+ // existing pair
+ pairId = c.getInt(c.getColumnIndex(MAIN_COLUMN_ID));
+ db.delete(FREQ_TABLE_NAME, FREQ_COLUMN_PAIR_ID + "=?",
+ new String[] { Integer.toString(pairId) });
+ } else {
+ // new pair
+ Long pairIdLong = db.insert(MAIN_TABLE_NAME, null,
+ getContentValues(word1, word2, mLocale));
+ pairId = pairIdLong.intValue();
+ }
+ // insert new frequency
+ final int freq;
+ if (word1 == null) {
+ freq = FREQUENCY_FOR_TYPED;
+ } else {
+ final NextWord nw = mUserHistoryDictionary.getBigramWord(word1, word2);
+ if (nw != null) {
+ final ForgettingCurveParams fcp = nw.getFcParams();
+ final int tempFreq = fcp.getFc();
+ final boolean isValid = fcp.isValid();
+ if (UserHistoryForgettingCurveUtils.needsToSave(
+ (byte)tempFreq, isValid, addLevel0Bigram)) {
+ freq = tempFreq;
+ } else {
+ freq = -1;
+ }
+ } else {
+ freq = -1;
+ }
+ }
+ if (freq > 0) {
+ if (DBG_SAVE_RESTORE) {
+ Log.d(TAG, "--- Save user history: " + word1 + ", " + word2);
+ }
+ db.insert(FREQ_TABLE_NAME, null,
+ getFrequencyContentValues(pairId, freq));
+ }
+ } finally {
+ if (c != null) {
+ c.close();
+ }
+ }
}
- c.close();
-
- // insert new frequency
- db.insert(FREQ_TABLE_NAME, null, getFrequencyContentValues(pairId, bi.mFrequency));
}
+
checkPruneData(db);
sUpdatingDB = false;
diff --git a/java/src/com/android/inputmethod/latin/UserHistoryDictionaryBigramList.java b/java/src/com/android/inputmethod/latin/UserHistoryDictionaryBigramList.java
new file mode 100644
index 000000000..409f921ff
--- /dev/null
+++ b/java/src/com/android/inputmethod/latin/UserHistoryDictionaryBigramList.java
@@ -0,0 +1,91 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.inputmethod.latin;
+
+import android.util.Log;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * A store of bigrams which will be updated when the user history dictionary is closed
+ * All bigrams including stale ones in SQL DB should be stored in this class to avoid adding stale
+ * bigrams when we write to the SQL DB.
+ */
+public class UserHistoryDictionaryBigramList {
+ private static final String TAG = UserHistoryDictionaryBigramList.class.getSimpleName();
+ private static final HashSet<String> EMPTY_STRING_SET = new HashSet<String>();
+ private final HashMap<String, HashSet<String>> mBigramMap =
+ new HashMap<String, HashSet<String>>();
+ private int mSize = 0;
+
+ public void evictAll() {
+ mSize = 0;
+ mBigramMap.clear();
+ }
+
+ public void addBigram(String word1, String word2) {
+ if (UserHistoryDictionary.DBG_SAVE_RESTORE) {
+ Log.d(TAG, "--- add bigram: " + word1 + ", " + word2);
+ }
+ final HashSet<String> set;
+ if (mBigramMap.containsKey(word1)) {
+ set = mBigramMap.get(word1);
+ } else {
+ set = new HashSet<String>();
+ mBigramMap.put(word1, set);
+ }
+ if (!set.contains(word2)) {
+ ++mSize;
+ set.add(word2);
+ }
+ }
+
+ public int size() {
+ return mSize;
+ }
+
+ public boolean isEmpty() {
+ return mBigramMap.isEmpty();
+ }
+
+ public Set<String> keySet() {
+ return mBigramMap.keySet();
+ }
+
+ public HashSet<String> getBigrams(String word1) {
+ if (!mBigramMap.containsKey(word1)) {
+ return EMPTY_STRING_SET;
+ } else {
+ return mBigramMap.get(word1);
+ }
+ }
+
+ public boolean removeBigram(String word1, String word2) {
+ final HashSet<String> set = getBigrams(word1);
+ if (set.isEmpty()) {
+ return false;
+ }
+ if (set.contains(word2)) {
+ set.remove(word2);
+ --mSize;
+ return true;
+ }
+ return false;
+ }
+}
diff --git a/java/src/com/android/inputmethod/latin/UserHistoryForgettingCurveUtils.java b/java/src/com/android/inputmethod/latin/UserHistoryForgettingCurveUtils.java
index eb3881726..9cd8c6778 100644
--- a/java/src/com/android/inputmethod/latin/UserHistoryForgettingCurveUtils.java
+++ b/java/src/com/android/inputmethod/latin/UserHistoryForgettingCurveUtils.java
@@ -16,14 +16,91 @@
package com.android.inputmethod.latin;
+import android.text.format.DateUtils;
+import android.util.Log;
+
public class UserHistoryForgettingCurveUtils {
+ private static final String TAG = UserHistoryForgettingCurveUtils.class.getSimpleName();
+ private static final boolean DEBUG = false;
private static final int FC_FREQ_MAX = 127;
/* package */ static final int COUNT_MAX = 3;
private static final int FC_LEVEL_MAX = 3;
/* package */ static final int ELAPSED_TIME_MAX = 15;
private static final int ELAPSED_TIME_INTERVAL_HOURS = 6;
+ private static final long ELAPSED_TIME_INTERVAL_MILLIS = ELAPSED_TIME_INTERVAL_HOURS
+ * DateUtils.HOUR_IN_MILLIS;
private static final int HALF_LIFE_HOURS = 48;
+ private UserHistoryForgettingCurveUtils() {
+ // This utility class is not publicly instantiable.
+ }
+
+ public static class ForgettingCurveParams {
+ private byte mFc;
+ long mLastTouchedTime = 0;
+ private final boolean mIsValid;
+
+ private void updateLastTouchedTime() {
+ mLastTouchedTime = System.currentTimeMillis();
+ }
+
+ public ForgettingCurveParams(boolean isValid) {
+ this(System.currentTimeMillis(), isValid);
+ }
+
+ private ForgettingCurveParams(long now, boolean isValid) {
+ this((int)pushCount((byte)0, isValid), now, now, isValid);
+ }
+
+ /** This constructor is called when the user history bigram dictionary is being restored. */
+ public ForgettingCurveParams(int fc, long now, long last) {
+ // All words with level >= 1 had been saved.
+ // Invalid words with level == 0 had been saved.
+ // Valid words words with level == 0 had *not* been saved.
+ this(fc, now, last, fcToLevel((byte)fc) > 0);
+ }
+
+ private ForgettingCurveParams(int fc, long now, long last, boolean isValid) {
+ mIsValid = isValid;
+ mFc = (byte)fc;
+ mLastTouchedTime = last;
+ updateElapsedTime(now);
+ }
+
+ public boolean isValid() {
+ return mIsValid;
+ }
+
+ public byte getFc() {
+ updateElapsedTime(System.currentTimeMillis());
+ return mFc;
+ }
+
+ public int getFrequency() {
+ updateElapsedTime(System.currentTimeMillis());
+ return UserHistoryForgettingCurveUtils.fcToFreq(mFc);
+ }
+
+ public int notifyTypedAgainAndGetFrequency() {
+ updateLastTouchedTime();
+ // TODO: Check whether this word is valid or not
+ mFc = pushCount(mFc, false);
+ return UserHistoryForgettingCurveUtils.fcToFreq(mFc);
+ }
+
+ private void updateElapsedTime(long now) {
+ final int elapsedTimeCount =
+ (int)((now - mLastTouchedTime) / ELAPSED_TIME_INTERVAL_MILLIS);
+ if (elapsedTimeCount <= 0) {
+ return;
+ }
+ for (int i = 0; i < elapsedTimeCount; ++i) {
+ mLastTouchedTime += ELAPSED_TIME_INTERVAL_MILLIS;
+ mFc = pushElapsedTime(mFc);
+ }
+ }
+ }
+
/* package */ static int fcToElapsedTime(byte fc) {
return fc & 0x0F;
}
@@ -38,8 +115,8 @@ public class UserHistoryForgettingCurveUtils {
private static int calcFreq(int elapsedTime, int count, int level) {
if (level <= 0) {
- // Reserved words, just return 0
- return 0;
+ // Reserved words, just return -1
+ return -1;
}
if (count == COUNT_MAX) {
// Temporary promote because it's frequently typed recently
@@ -87,12 +164,31 @@ public class UserHistoryForgettingCurveUtils {
// Upgrade level
++level;
count = 0;
+ if (DEBUG) {
+ Log.d(TAG, "Upgrade level.");
+ }
} else {
++count;
}
return calcFc(0, count, level);
}
+ // TODO: isValid should be false for a word whose frequency is 0,
+ // or that is not in the dictionary.
+ /**
+ * Check wheather we should save the bigram to the SQL DB or not
+ */
+ public static boolean needsToSave(byte fc, boolean isValid, boolean addLevel0Bigram) {
+ int level = fcToLevel(fc);
+ if (level == 0) {
+ if (isValid || !addLevel0Bigram) {
+ return false;
+ }
+ }
+ final int elapsedTime = fcToElapsedTime(fc);
+ return (elapsedTime < ELAPSED_TIME_MAX - 1 || level > 0);
+ }
+
private static class MathUtils {
public static final int[][] SCORE_TABLE = new int[FC_LEVEL_MAX][ELAPSED_TIME_MAX + 1];
static {
diff --git a/java/src/com/android/inputmethod/latin/Utils.java b/java/src/com/android/inputmethod/latin/Utils.java
index 036ff74b8..b3e46baf5 100644
--- a/java/src/com/android/inputmethod/latin/Utils.java
+++ b/java/src/com/android/inputmethod/latin/Utils.java
@@ -44,8 +44,10 @@ import java.io.IOException;
import java.io.PrintWriter;
import java.nio.channels.FileChannel;
import java.text.SimpleDateFormat;
+import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
+import java.util.Map;
public class Utils {
private Utils() {
@@ -484,4 +486,40 @@ public class Utils {
}
return sDeviceOverrideValueMap.get(key);
}
+
+ private static final HashMap<String, Long> EMPTY_LT_HASH_MAP = new HashMap<String, Long>();
+ private static final String LOCALE_AND_TIME_STR_SEPARATER = ",";
+ public static HashMap<String, Long> localeAndTimeStrToHashMap(String str) {
+ if (TextUtils.isEmpty(str)) {
+ return EMPTY_LT_HASH_MAP;
+ }
+ final String[] ss = str.split(LOCALE_AND_TIME_STR_SEPARATER);
+ final int N = ss.length;
+ if (N < 2 || N % 2 != 0) {
+ return EMPTY_LT_HASH_MAP;
+ }
+ final HashMap<String, Long> retval = new HashMap<String, Long>();
+ for (int i = 0; i < N / 2; ++i) {
+ final String localeStr = ss[i * 2];
+ final long time = Long.valueOf(ss[i * 2 + 1]);
+ retval.put(localeStr, time);
+ }
+ return retval;
+ }
+
+ public static String localeAndTimeHashMapToStr(HashMap<String, Long> map) {
+ if (map == null || map.isEmpty()) {
+ return "";
+ }
+ final StringBuilder builder = new StringBuilder();
+ for (String localeStr : map.keySet()) {
+ if (builder.length() > 0) {
+ builder.append(LOCALE_AND_TIME_STR_SEPARATER);
+ }
+ final Long time = map.get(localeStr);
+ builder.append(localeStr).append(LOCALE_AND_TIME_STR_SEPARATER);
+ builder.append(String.valueOf(time));
+ }
+ return builder.toString();
+ }
}