Forget user history

Bug: 4192129

Change-Id: Ic98398e5383093a1c24373849eadc48fc4d3626f
This commit is contained in:
satok 2012-05-18 21:28:09 +09:00 committed by Satoshi Kataoka
parent 739ff3cfc4
commit fd53b8cc2b
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 */, super.addWord(word, null /* shortcut */,
FREQUENCY_FOR_CONTACTS); FREQUENCY_FOR_CONTACTS);
if (!TextUtils.isEmpty(prevWord)) { if (!TextUtils.isEmpty(prevWord)) {
super.setBigram(prevWord, word, super.setBigramAndGetFrequency(prevWord, word,
FREQUENCY_FOR_CONTACTS_BIGRAM); FREQUENCY_FOR_CONTACTS_BIGRAM);
} }
prevWord = word; prevWord = word;

View file

@ -21,6 +21,7 @@ import android.content.Context;
import com.android.inputmethod.keyboard.KeyDetector; import com.android.inputmethod.keyboard.KeyDetector;
import com.android.inputmethod.keyboard.Keyboard; import com.android.inputmethod.keyboard.Keyboard;
import com.android.inputmethod.keyboard.ProximityInfo; import com.android.inputmethod.keyboard.ProximityInfo;
import com.android.inputmethod.latin.UserHistoryForgettingCurveUtils.ForgettingCurveParams;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
@ -80,31 +81,73 @@ public class ExpandableDictionary extends Dictionary {
} }
} }
private static class NextWord { protected interface NextWord {
public final Node mWord; public Node getWordNode();
private int mFrequency; 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; mWord = word;
mFrequency = frequency; mFrequency = frequency;
} }
@Override
public Node getWordNode() {
return mWord;
}
@Override
public int getFrequency() { public int getFrequency() {
return mFrequency; return mFrequency;
} }
public int setFrequency(int freq) { @Override
mFrequency = freq; public int getFcValue() {
return mFrequency; return mFrequency;
} }
public int addFrequency(int add) { @Override
mFrequency += add; public int notifyTypedAgainAndGetFrequency() {
if (mFrequency > BIGRAM_MAX_FREQUENCY) mFrequency = BIGRAM_MAX_FREQUENCY;
return mFrequency; 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 NodeArray mRoots;
private int[][] mCodes; private int[][] mCodes;
@ -183,7 +226,7 @@ public class ExpandableDictionary extends Dictionary {
childNode.mShortcutOnly = isShortcutOnly; childNode.mShortcutOnly = isShortcutOnly;
children.add(childNode); children.add(childNode);
} }
if (wordLength == depth + 1) { if (wordLength == depth + 1 && shortcutTarget != null) {
// Terminate this word // Terminate this word
childNode.mTerminal = true; childNode.mTerminal = true;
if (isShortcutOnly) { if (isShortcutOnly) {
@ -221,7 +264,7 @@ public class ExpandableDictionary extends Dictionary {
protected final void getWordsInner(final WordComposer codes, protected final void getWordsInner(final WordComposer codes,
final CharSequence prevWordForBigrams, final WordCallback callback, final CharSequence prevWordForBigrams, final WordCallback callback,
@SuppressWarnings("unused") final ProximityInfo proximityInfo) { final ProximityInfo proximityInfo) {
mInputLength = codes.size(); mInputLength = codes.size();
if (mCodes.length < mInputLength) mCodes = new int[mInputLength][]; if (mCodes.length < mInputLength) mCodes = new int[mInputLength][];
final int[] xCoordinates = codes.getXCoordinates(); final int[] xCoordinates = codes.getXCoordinates();
@ -265,13 +308,13 @@ public class ExpandableDictionary extends Dictionary {
// Refer to addOrSetBigram() about word1.toLowerCase() // Refer to addOrSetBigram() about word1.toLowerCase()
final Node firstWord = searchWord(mRoots, word1.toLowerCase(), 0, null); final Node firstWord = searchWord(mRoots, word1.toLowerCase(), 0, null);
final Node secondWord = searchWord(mRoots, word2, 0, null); final Node secondWord = searchWord(mRoots, word2, 0, null);
LinkedList<NextWord> bigram = firstWord.mNGrams; LinkedList<NextWord> bigrams = firstWord.mNGrams;
NextWord bigramNode = null; NextWord bigramNode = null;
if (bigram == null || bigram.size() == 0) { if (bigrams == null || bigrams.size() == 0) {
return false; return false;
} else { } else {
for (NextWord nw : bigram) { for (NextWord nw : bigrams) {
if (nw.mWord == secondWord) { if (nw.getWordNode() == secondWord) {
bigramNode = nw; bigramNode = nw;
break; break;
} }
@ -280,7 +323,7 @@ public class ExpandableDictionary extends Dictionary {
if (bigramNode == null) { if (bigramNode == null) {
return false; 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; 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) { private static int computeSkippedWordFinalFreq(int freq, int snr, int inputLength) {
// The computation itself makes sense for >= 2, but the == 2 case returns 0 // 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 // 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) { public int setBigramAndGetFrequency(String word1, String word2, int frequency) {
return addOrSetBigram(word1, word2, frequency, false); return setBigramAndGetFrequency(word1, word2, frequency, null /* unused */);
} }
protected int addBigram(String word1, String word2, int frequency) { public int setBigramAndGetFrequency(String word1, String word2, ForgettingCurveParams fcp) {
return addOrSetBigram(word1, word2, frequency, true); return setBigramAndGetFrequency(word1, word2, 0 /* unused */, fcp);
} }
/** /**
* Adds bigrams to the in-memory trie structure that is being used to retrieve any word * Adds bigrams to the in-memory trie structure that is being used to retrieve any word
* @param frequency frequency for this bigram * @param frequency frequency for this bigram
* @param addFrequency if true, it adds to current frequency, else it overwrites the old value * @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 // 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. // 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 // So we want to squash the case of the left hand side, and preserve that of the right
// hand side word. // hand side word.
Node firstWord = searchWord(mRoots, word1.toLowerCase(), 0, null); Node firstWord = searchWord(mRoots, word1.toLowerCase(), 0, null);
Node secondWord = searchWord(mRoots, word2, 0, null); Node secondWord = searchWord(mRoots, word2, 0, null);
LinkedList<NextWord> bigram = firstWord.mNGrams; LinkedList<NextWord> bigrams = firstWord.mNGrams;
if (bigram == null || bigram.size() == 0) { if (bigrams == null || bigrams.size() == 0) {
firstWord.mNGrams = new LinkedList<NextWord>(); firstWord.mNGrams = new LinkedList<NextWord>();
bigram = firstWord.mNGrams; bigrams = firstWord.mNGrams;
} else { } else {
for (NextWord nw : bigram) { for (NextWord nw : bigrams) {
if (nw.mWord == secondWord) { if (nw.getWordNode() == secondWord) {
if (addFrequency) { return nw.notifyTypedAgainAndGetFrequency();
return nw.addFrequency(frequency);
} else {
return nw.setFrequency(frequency);
}
} }
} }
} }
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; return frequency;
} }
@ -580,7 +642,7 @@ public class ExpandableDictionary extends Dictionary {
Node node; Node node;
int freq; int freq;
for (NextWord nextWord : terminalNodes) { for (NextWord nextWord : terminalNodes) {
node = nextWord.mWord; node = nextWord.getWordNode();
freq = nextWord.getFrequency(); freq = nextWord.getFrequency();
int index = BinaryDictionary.MAX_WORD_LENGTH; int index = BinaryDictionary.MAX_WORD_LENGTH;
do { do {
@ -589,8 +651,10 @@ public class ExpandableDictionary extends Dictionary {
node = node.mParent; node = node.mParent;
} while (node != null); } while (node != null);
callback.addWord(mLookedUpString, index, BinaryDictionary.MAX_WORD_LENGTH - index, if (freq >= 0) {
freq, mDicTypeId, Dictionary.BIGRAM); callback.addWord(mLookedUpString, index, BinaryDictionary.MAX_WORD_LENGTH - index,
freq, mDicTypeId, Dictionary.BIGRAM);
}
} }
} }

View file

@ -494,7 +494,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
resetContactsDictionary(oldContactsDictionary); resetContactsDictionary(oldContactsDictionary);
mUserHistoryDictionary = new UserHistoryDictionary( mUserHistoryDictionary = new UserHistoryDictionary(
this, localeStr, Suggest.DIC_USER_HISTORY); this, localeStr, Suggest.DIC_USER_HISTORY, mPrefs);
mSuggest.setUserHistoryDictionary(mUserHistoryDictionary); mSuggest.setUserHistoryDictionary(mUserHistoryDictionary);
} }
@ -745,7 +745,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
KeyboardView inputView = mKeyboardSwitcher.getKeyboardView(); KeyboardView inputView = mKeyboardSwitcher.getKeyboardView();
if (inputView != null) inputView.closing(); if (inputView != null) inputView.closing();
if (mUserHistoryDictionary != null) mUserHistoryDictionary.flushPendingWrites();
} }
private void onFinishInputViewInternal(boolean finishingInput) { 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_SHOW_SUGGESTIONS_SETTING = "show_suggestions_setting";
public static final String PREF_MISC_SETTINGS = "misc_settings"; 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_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_ADVANCED_SETTINGS = "pref_advanced_settings";
public static final String PREF_SUPPRESS_LANGUAGE_SWITCH_KEY = public static final String PREF_SUPPRESS_LANGUAGE_SWITCH_KEY =
"pref_suppress_language_switch_key"; "pref_suppress_language_switch_key";
@ -244,7 +246,6 @@ public class Settings extends InputMethodSettingsFragment
refreshEnablingsOfKeypressSoundAndVibrationSettings(prefs, res); refreshEnablingsOfKeypressSoundAndVibrationSettings(prefs, res);
} }
@SuppressWarnings("unused")
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();

View file

@ -28,6 +28,8 @@ import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; 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 * 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 // TODO: use mUsabilityStudyMode instead of reading it again here
return prefs.getBoolean(Settings.PREF_USABILITY_STUDY_MODE, true); 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.ContentValues;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor; import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteOpenHelper;
@ -26,6 +27,8 @@ import android.os.AsyncTask;
import android.provider.BaseColumns; import android.provider.BaseColumns;
import android.util.Log; import android.util.Log;
import com.android.inputmethod.latin.UserHistoryForgettingCurveUtils.ForgettingCurveParams;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
@ -40,9 +43,6 @@ public class UserHistoryDictionary extends ExpandableDictionary {
/** Any pair being typed or picked */ /** Any pair being typed or picked */
private static final int FREQUENCY_FOR_TYPED = 2; 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. */ /** Maximum number of pairs. Pruning will start when databases goes above this number. */
private static int sMaxHistoryBigrams = 10000; private static int sMaxHistoryBigrams = 10000;
@ -81,6 +81,7 @@ public class UserHistoryDictionary extends ExpandableDictionary {
private HashSet<Bigram> mPendingWrites = new HashSet<Bigram>(); private HashSet<Bigram> mPendingWrites = new HashSet<Bigram>();
private final Object mPendingWritesLock = new Object(); private final Object mPendingWritesLock = new Object();
private static volatile boolean sUpdatingDB = false; private static volatile boolean sUpdatingDB = false;
private final SharedPreferences mPrefs;
private final static HashMap<String, String> sDictProjectionMap; private final static HashMap<String, String> sDictProjectionMap;
@ -101,12 +102,10 @@ public class UserHistoryDictionary extends ExpandableDictionary {
private static class Bigram { private static class Bigram {
public final String mWord1; public final String mWord1;
public final String mWord2; public final String mWord2;
public final int mFrequency;
Bigram(String word1, String word2, int frequency) { Bigram(String word1, String word2) {
this.mWord1 = word1; this.mWord1 = word1;
this.mWord2 = word2; this.mWord2 = word2;
this.mFrequency = frequency;
} }
@Override @Override
@ -137,7 +136,8 @@ public class UserHistoryDictionary extends ExpandableDictionary {
sDeleteHistoryBigrams = deleteHistoryBigram; 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); super(context, dicTypeId);
mLocale = locale; mLocale = locale;
if (sOpenHelper == null) { if (sOpenHelper == null) {
@ -146,11 +146,13 @@ public class UserHistoryDictionary extends ExpandableDictionary {
if (mLocale != null && mLocale.length() > 1) { if (mLocale != null && mLocale.length() > 1) {
loadDictionary(); loadDictionary();
} }
mPrefs = sp;
} }
@Override @Override
public void close() { public void close() {
flushPendingWrites(); flushPendingWrites();
SettingsValues.setLastUserHistoryWriteTime(mPrefs, mLocale);
// Don't close the database as locale changes will require it to be reopened anyway // 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 // Also, the database is written to somewhat frequently, so it needs to be kept alive
// throughout the life of the process. // 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). * The second word may not be null (a NullPointerException would be thrown).
*/ */
public int addToUserHistory(final String word1, String word2) { 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 // Do not insert a word as a bigram of itself
if (word2.equals(word1)) { if (word2.equals(word1)) {
return 0; return 0;
} }
final int freq;
int freq;
if (null == word1) { if (null == word1) {
freq = FREQUENCY_FOR_TYPED; freq = FREQUENCY_FOR_TYPED;
} else { } 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) { synchronized (mPendingWritesLock) {
if (freq == FREQUENCY_FOR_TYPED || mPendingWrites.isEmpty()) { final Bigram bi = new Bigram(word1, word2);
mPendingWrites.add(new Bigram(word1, word2, freq)); if (!mPendingWrites.contains(bi)) {
} else {
Bigram bi = new Bigram(word1, word2, freq);
mPendingWrites.remove(bi);
mPendingWrites.add(bi); mPendingWrites.add(bi);
} }
} }
@ -203,7 +200,7 @@ public class UserHistoryDictionary extends ExpandableDictionary {
} }
public boolean cancelAddingUserHistory(String word1, String word2) { 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)) { if (mPendingWrites.contains(bi)) {
mPendingWrites.remove(bi); mPendingWrites.remove(bi);
return super.removeBigram(word1, word2); 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. * Schedules a background thread to write any pending words to the database.
*/ */
public void flushPendingWrites() { private void flushPendingWrites() {
synchronized (mPendingWritesLock) { synchronized (mPendingWritesLock) {
// Nothing pending? Return // Nothing pending? Return
if (mPendingWrites.isEmpty()) return; if (mPendingWrites.isEmpty()) return;
// Create a background thread to write the pending entries // 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 // Create a new map for writing new entries into while the old one is written to db
mPendingWrites = new HashSet<Bigram>(); mPendingWrites = new HashSet<Bigram>();
} }
@ -240,25 +237,30 @@ public class UserHistoryDictionary extends ExpandableDictionary {
@Override @Override
public void loadDictionaryAsync() { 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 // Load the words that correspond to the current input locale
final Cursor cursor = query(MAIN_COLUMN_LOCALE + "=?", new String[] { mLocale }); final Cursor cursor = query(MAIN_COLUMN_LOCALE + "=?", new String[] { mLocale });
if (null == cursor) return; if (null == cursor) return;
try { try {
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
int word1Index = cursor.getColumnIndex(MAIN_COLUMN_WORD1); final int word1Index = cursor.getColumnIndex(MAIN_COLUMN_WORD1);
int word2Index = cursor.getColumnIndex(MAIN_COLUMN_WORD2); final int word2Index = cursor.getColumnIndex(MAIN_COLUMN_WORD2);
int frequencyIndex = cursor.getColumnIndex(FREQ_COLUMN_FREQUENCY); final int frequencyIndex = cursor.getColumnIndex(FREQ_COLUMN_FREQUENCY);
while (!cursor.isAfterLast()) { while (!cursor.isAfterLast()) {
String word1 = cursor.getString(word1Index); final String word1 = cursor.getString(word1Index);
String word2 = cursor.getString(word2Index); final String word2 = cursor.getString(word2Index);
int frequency = cursor.getInt(frequencyIndex); final int frequency = cursor.getInt(frequencyIndex);
// Safeguard against adding really long words. Stack may overflow due // Safeguard against adding really long words. Stack may overflow due
// to recursive lookup // to recursive lookup
if (null == word1) { if (null == word1) {
super.addWord(word2, null /* shortcut */, frequency); super.addWord(word2, null /* shortcut */, frequency);
} else if (word1.length() < BinaryDictionary.MAX_WORD_LENGTH } else if (word1.length() < BinaryDictionary.MAX_WORD_LENGTH
&& word2.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(); cursor.moveToNext();
} }
@ -340,12 +342,14 @@ public class UserHistoryDictionary extends ExpandableDictionary {
private final HashSet<Bigram> mMap; private final HashSet<Bigram> mMap;
private final DatabaseHelper mDbHelper; private final DatabaseHelper mDbHelper;
private final String mLocale; private final String mLocale;
private final UserHistoryDictionary mUserHistoryDictionary;
public UpdateDbTask(DatabaseHelper openHelper, HashSet<Bigram> pendingWrites, public UpdateDbTask(DatabaseHelper openHelper, HashSet<Bigram> pendingWrites,
String locale) { String locale, UserHistoryDictionary dict) {
mMap = pendingWrites; mMap = pendingWrites;
mLocale = locale; mLocale = locale;
mDbHelper = openHelper; mDbHelper = openHelper;
mUserHistoryDictionary = dict;
} }
/** Prune any old data if the database is getting too big. */ /** Prune any old data if the database is getting too big. */
@ -357,7 +361,8 @@ public class UserHistoryDictionary extends ExpandableDictionary {
int totalRowCount = c.getCount(); int totalRowCount = c.getCount();
// prune out old data if we have too much data // prune out old data if we have too much data
if (totalRowCount > sMaxHistoryBigrams) { if (totalRowCount > sMaxHistoryBigrams) {
int numDeleteRows = (totalRowCount - sMaxHistoryBigrams) + sDeleteHistoryBigrams; int numDeleteRows = (totalRowCount - sMaxHistoryBigrams)
+ sDeleteHistoryBigrams;
int pairIdColumnId = c.getColumnIndex(FREQ_COLUMN_PAIR_ID); int pairIdColumnId = c.getColumnIndex(FREQ_COLUMN_PAIR_ID);
c.moveToFirst(); c.moveToFirst();
int count = 0; int count = 0;
@ -397,43 +402,70 @@ public class UserHistoryDictionary extends ExpandableDictionary {
} }
db.execSQL("PRAGMA foreign_keys = ON;"); db.execSQL("PRAGMA foreign_keys = ON;");
// Write all the entries to the db // Write all the entries to the db
Iterator<Bigram> iterator = mMap.iterator(); final Iterator<Bigram> iterator = mMap.iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
// TODO: this process of making a text search for each pair each time // TODO: this process of making a text search for each pair each time
// is terribly inefficient. Optimize this. // is terribly inefficient. Optimize this.
Bigram bi = iterator.next(); final Bigram bi = iterator.next();
// find pair id // find pair id
final Cursor c; Cursor c = null;
if (null != bi.mWord1) { try {
c = db.query(MAIN_TABLE_NAME, new String[] { MAIN_COLUMN_ID }, if (null != bi.mWord1) {
MAIN_COLUMN_WORD1 + "=? AND " + MAIN_COLUMN_WORD2 + "=? AND " c = db.query(MAIN_TABLE_NAME, new String[] { MAIN_COLUMN_ID },
+ MAIN_COLUMN_LOCALE + "=?", MAIN_COLUMN_WORD1 + "=? AND " + MAIN_COLUMN_WORD2 + "=? AND "
new String[] { bi.mWord1, bi.mWord2, mLocale }, null, null, null); + MAIN_COLUMN_LOCALE + "=?",
} else { new String[] { bi.mWord1, bi.mWord2, mLocale }, null, null,
c = db.query(MAIN_TABLE_NAME, new String[] { MAIN_COLUMN_ID }, null);
MAIN_COLUMN_WORD1 + " IS NULL AND " + MAIN_COLUMN_WORD2 + "=? AND " } else {
+ MAIN_COLUMN_LOCALE + "=?", c = db.query(MAIN_TABLE_NAME, new String[] { MAIN_COLUMN_ID },
new String[] { bi.mWord2, mLocale }, null, null, null); MAIN_COLUMN_WORD1 + " IS NULL AND " + MAIN_COLUMN_WORD2 + "=? AND "
} + MAIN_COLUMN_LOCALE + "=?",
new String[] { bi.mWord2, mLocale }, null, null, null);
}
int pairId; final int pairId;
if (c.moveToFirst()) { if (c.moveToFirst()) {
// existing pair // existing pair
pairId = c.getInt(c.getColumnIndex(MAIN_COLUMN_ID)); pairId = c.getInt(c.getColumnIndex(MAIN_COLUMN_ID));
db.delete(FREQ_TABLE_NAME, FREQ_COLUMN_PAIR_ID + "=?", db.delete(FREQ_TABLE_NAME, FREQ_COLUMN_PAIR_ID + "=?",
new String[] { Integer.toString(pairId) }); new String[] { Integer.toString(pairId) });
} else { } else {
// new pair // new pair
Long pairIdLong = db.insert(MAIN_TABLE_NAME, null, Long pairIdLong = db.insert(MAIN_TABLE_NAME, null,
getContentValues(bi.mWord1, bi.mWord2, mLocale)); getContentValues(bi.mWord1, bi.mWord2, mLocale));
pairId = pairIdLong.intValue(); 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); checkPruneData(db);
sUpdatingDB = false; sUpdatingDB = false;

View file

@ -16,14 +16,78 @@
package com.android.inputmethod.latin; package com.android.inputmethod.latin;
import android.text.format.DateUtils;
import android.util.Log;
public class UserHistoryForgettingCurveUtils { 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; private static final int FC_FREQ_MAX = 127;
/* package */ static final int COUNT_MAX = 3; /* package */ static final int COUNT_MAX = 3;
private static final int FC_LEVEL_MAX = 3; private static final int FC_LEVEL_MAX = 3;
/* package */ static final int ELAPSED_TIME_MAX = 15; /* package */ static final int ELAPSED_TIME_MAX = 15;
private static final int ELAPSED_TIME_INTERVAL_HOURS = 6; 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 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) { /* package */ static int fcToElapsedTime(byte fc) {
return fc & 0x0F; return fc & 0x0F;
} }
@ -38,8 +102,8 @@ public class UserHistoryForgettingCurveUtils {
private static int calcFreq(int elapsedTime, int count, int level) { private static int calcFreq(int elapsedTime, int count, int level) {
if (level <= 0) { if (level <= 0) {
// Reserved words, just return 0 // Reserved words, just return -1
return 0; return -1;
} }
if (count == COUNT_MAX) { if (count == COUNT_MAX) {
// Temporary promote because it's frequently typed recently // Temporary promote because it's frequently typed recently
@ -87,12 +151,26 @@ public class UserHistoryForgettingCurveUtils {
// Upgrade level // Upgrade level
++level; ++level;
count = 0; count = 0;
if (DEBUG) {
Log.d(TAG, "Upgrade level.");
}
} else { } else {
++count; ++count;
} }
return calcFc(0, count, level); 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 { private static class MathUtils {
public static final int[][] SCORE_TABLE = new int[FC_LEVEL_MAX][ELAPSED_TIME_MAX + 1]; public static final int[][] SCORE_TABLE = new int[FC_LEVEL_MAX][ELAPSED_TIME_MAX + 1];
static { static {

View file

@ -44,8 +44,10 @@ import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map;
public class Utils { public class Utils {
private Utils() { private Utils() {
@ -484,4 +486,40 @@ public class Utils {
} }
return sDeviceOverrideValueMap.get(key); 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();
}
} }