am aa064139: Merge "Forget user history" into jb-dev

* commit 'aa0641394b7ffc16c462c5d9cc269d1f5114748f':
  Forget user history
This commit is contained in:
satok 2012-05-27 23:01:41 -07:00 committed by Android Git Automerger
commit fff5cd6f76
8 changed files with 333 additions and 100 deletions

View file

@ -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;

View file

@ -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,73 @@ 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();
/** FcValue is a bit set */
public int getFcValue();
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;
@Override
public int getFcValue() {
return mFrequency;
}
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 int getFcValue() {
return mFcp.getFc();
}
@Override
public int notifyTypedAgainAndGetFrequency() {
return mFcp.notifyTypedAgainAndGetFrequency();
}
}
private NodeArray mRoots;
private int[][] mCodes;
@ -183,7 +226,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 +264,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 +308,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 +323,7 @@ public class ExpandableDictionary extends Dictionary {
if (bigramNode == null) {
return false;
}
return bigram.remove(bigramNode);
return bigrams.remove(bigramNode);
}
/**
@ -292,6 +335,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 +505,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 +642,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 +651,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);
}
}
}

View file

@ -496,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);
}
@ -747,7 +747,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) {

View file

@ -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();

View file

@ -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();
}
}

View file

@ -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,6 +27,8 @@ 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;
@ -40,9 +43,6 @@ public class UserHistoryDictionary extends ExpandableDictionary {
/** 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;
@ -81,6 +81,7 @@ public class UserHistoryDictionary extends ExpandableDictionary {
private HashSet<Bigram> mPendingWrites = new HashSet<Bigram>();
private final Object mPendingWritesLock = new Object();
private static volatile boolean sUpdatingDB = false;
private final SharedPreferences mPrefs;
private final static HashMap<String, String> sDictProjectionMap;
@ -101,12 +102,10 @@ public class UserHistoryDictionary extends ExpandableDictionary {
private static class Bigram {
public final String mWord1;
public final String mWord2;
public final int mFrequency;
Bigram(String word1, String word2, int frequency) {
Bigram(String word1, String word2) {
this.mWord1 = word1;
this.mWord2 = word2;
this.mFrequency = frequency;
}
@Override
@ -137,7 +136,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 +146,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.
@ -176,25 +178,20 @@ public class UserHistoryDictionary extends ExpandableDictionary {
* 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);
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());
}
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);
final Bigram bi = new Bigram(word1, word2);
if (!mPendingWrites.contains(bi)) {
mPendingWrites.add(bi);
}
}
@ -203,7 +200,7 @@ public class UserHistoryDictionary extends ExpandableDictionary {
}
public boolean cancelAddingUserHistory(String word1, String word2) {
final Bigram bi = new Bigram(word1, word2, 0);
final Bigram bi = new Bigram(word1, word2);
if (mPendingWrites.contains(bi)) {
mPendingWrites.remove(bi);
return super.removeBigram(word1, word2);
@ -214,12 +211,12 @@ 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;
// Create a background thread to write the pending entries
new UpdateDbTask(sOpenHelper, mPendingWrites, mLocale).execute();
new UpdateDbTask(sOpenHelper, mPendingWrites, mLocale, this).execute();
// Create a new map for writing new entries into while the old one is written to db
mPendingWrites = new HashSet<Bigram>();
}
@ -240,25 +237,30 @@ 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);
// 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));
// TODO: optimize
mPendingWrites.add(new Bigram(word1, word2));
}
cursor.moveToNext();
}
@ -340,12 +342,14 @@ public class UserHistoryDictionary extends ExpandableDictionary {
private final HashSet<Bigram> mMap;
private final DatabaseHelper mDbHelper;
private final String mLocale;
private final UserHistoryDictionary mUserHistoryDictionary;
public UpdateDbTask(DatabaseHelper openHelper, HashSet<Bigram> pendingWrites,
String locale) {
String locale, UserHistoryDictionary dict) {
mMap = pendingWrites;
mLocale = locale;
mDbHelper = openHelper;
mUserHistoryDictionary = dict;
}
/** Prune any old data if the database is getting too big. */
@ -357,7 +361,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;
@ -397,43 +402,70 @@ public class UserHistoryDictionary extends ExpandableDictionary {
}
db.execSQL("PRAGMA foreign_keys = ON;");
// Write all the entries to the db
Iterator<Bigram> iterator = mMap.iterator();
final 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();
final 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);
}
Cursor c = null;
try {
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);
}
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();
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(bi.mWord1, bi.mWord2, mLocale));
pairId = pairIdLong.intValue();
}
// insert new frequency
final int freq;
if (bi.mWord1 == null) {
freq = FREQUENCY_FOR_TYPED;
} else {
final NextWord nw = mUserHistoryDictionary.getBigramWord(
bi.mWord1, bi.mWord2);
if (nw != null) {
final int tempFreq = nw.getFcValue();
// TODO: Check whether the word is valid or not
if (UserHistoryForgettingCurveUtils.needsToSave(
(byte)tempFreq, false)) {
freq = tempFreq;
} else {
freq = -1;
}
} else {
freq = -1;
}
}
if (freq > 0) {
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;

View file

@ -16,14 +16,78 @@
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 void updateLastTouchedTime() {
mLastTouchedTime = System.currentTimeMillis();
}
public ForgettingCurveParams() {
// TODO: Check whether this word is valid or not
this(System.currentTimeMillis());
}
private ForgettingCurveParams(long now) {
this((int)pushCount((byte)0, false), now, now);
}
public ForgettingCurveParams(int fc, long now, long last) {
mFc = (byte)fc;
mLastTouchedTime = last;
updateElapsedTime(now);
}
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 +102,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 +151,26 @@ 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.
public static boolean needsToSave(byte fc, boolean isValid) {
int level = fcToLevel(fc);
if (isValid && level == 0) {
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 {

View file

@ -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];
final long time = Long.valueOf(ss[i + 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();
}
}