Rename the user history dictionary.

UserBigramDictionary -> UserHistoryDictionary.

Also update all methods, strings and comments, except those needed
for backward compatibility (which only include the name of the
database file).

Change-Id: I0bccea29880dc566b90100575b83baaa947b03ae
This commit is contained in:
Jean Chalard 2012-03-16 18:01:27 +09:00
parent 660776e09b
commit 9ffb94fa13
3 changed files with 40 additions and 59 deletions

View file

@ -202,7 +202,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
private boolean mShouldSwitchToLastSubtype = true; private boolean mShouldSwitchToLastSubtype = true;
private UserDictionary mUserDictionary; private UserDictionary mUserDictionary;
private UserBigramDictionary mUserBigramDictionary; private UserHistoryDictionary mUserHistoryDictionary;
private boolean mIsUserDictionaryAvailable; private boolean mIsUserDictionaryAvailable;
private LastComposedWord mLastComposedWord = LastComposedWord.NOT_A_COMPOSED_WORD; private LastComposedWord mLastComposedWord = LastComposedWord.NOT_A_COMPOSED_WORD;
@ -526,11 +526,9 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
resetContactsDictionary(oldContactsDictionary); resetContactsDictionary(oldContactsDictionary);
// TODO: rename UserBigramDictionary into UserHistoryDictionary mUserHistoryDictionary
mUserBigramDictionary = new UserHistoryDictionary(this, this, localeStr, Suggest.DIC_USER_HISTORY);
= new UserBigramDictionary(this, this, localeStr, Suggest.DIC_USER_BIGRAM); mSuggest.setUserHistoryDictionary(mUserHistoryDictionary);
mSuggest.setUserUnigramDictionary(mUserBigramDictionary);
mSuggest.setUserBigramDictionary(mUserBigramDictionary);
LocaleUtils.setSystemLocale(res, savedLocale); LocaleUtils.setSystemLocale(res, savedLocale);
} }
@ -772,7 +770,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
KeyboardView inputView = mKeyboardSwitcher.getKeyboardView(); KeyboardView inputView = mKeyboardSwitcher.getKeyboardView();
if (inputView != null) inputView.closing(); if (inputView != null) inputView.closing();
if (mUserBigramDictionary != null) mUserBigramDictionary.flushPendingWrites(); if (mUserHistoryDictionary != null) mUserHistoryDictionary.flushPendingWrites();
} }
private void onFinishInputViewInternal(boolean finishingInput) { private void onFinishInputViewInternal(boolean finishingInput) {
@ -1990,9 +1988,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
setSuggestionStripShown(isSuggestionsStripVisible()); setSuggestionStripShown(isSuggestionsStripVisible());
} }
/**
* Adds to the UserBigramDictionary and/or UserUnigramDictionary
*/
private void addToUserHistoryDictionary(final CharSequence suggestion) { private void addToUserHistoryDictionary(final CharSequence suggestion) {
if (suggestion == null || suggestion.length() < 1) return; if (suggestion == null || suggestion.length() < 1) return;
@ -2004,16 +1999,16 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
return; return;
} }
if (mUserBigramDictionary != null) { if (mUserHistoryDictionary != null) {
mUserBigramDictionary.addUnigram(suggestion.toString());
final InputConnection ic = getCurrentInputConnection(); final InputConnection ic = getCurrentInputConnection();
final CharSequence prevWord;
if (null != ic) { if (null != ic) {
final CharSequence prevWord = prevWord = EditingUtils.getPreviousWord(ic, mSettingsValues.mWordSeparators);
EditingUtils.getPreviousWord(ic, mSettingsValues.mWordSeparators); } else {
if (null != prevWord) { prevWord = null;
mUserBigramDictionary.addBigramPair(prevWord.toString(), suggestion.toString());
}
} }
mUserHistoryDictionary.addToUserHistory(null == prevWord ? null : prevWord.toString(),
suggestion.toString());
} }
} }

View file

@ -62,9 +62,8 @@ public class Suggest implements Dictionary.WordCallback {
public static final int DIC_USER_TYPED = 0; public static final int DIC_USER_TYPED = 0;
public static final int DIC_MAIN = 1; public static final int DIC_MAIN = 1;
public static final int DIC_USER = 2; public static final int DIC_USER = 2;
public static final int DIC_USER_UNIGRAM = 3; public static final int DIC_USER_HISTORY = 3;
public static final int DIC_CONTACTS = 4; public static final int DIC_CONTACTS = 4;
public static final int DIC_USER_BIGRAM = 5;
public static final int DIC_WHITELIST = 6; public static final int DIC_WHITELIST = 6;
// If you add a type of dictionary, increment DIC_TYPE_LAST_ID // If you add a type of dictionary, increment DIC_TYPE_LAST_ID
// TODO: this value seems unused. Remove it? // TODO: this value seems unused. Remove it?
@ -73,10 +72,10 @@ public class Suggest implements Dictionary.WordCallback {
public static final String DICT_KEY_CONTACTS = "contacts"; public static final String DICT_KEY_CONTACTS = "contacts";
// User dictionary, the system-managed one. // User dictionary, the system-managed one.
public static final String DICT_KEY_USER = "user"; public static final String DICT_KEY_USER = "user";
// User unigram dictionary, internal to LatinIME // User history dictionary for the unigram map, internal to LatinIME
public static final String DICT_KEY_USER_UNIGRAM = "user_unigram"; public static final String DICT_KEY_USER_HISTORY_UNIGRAM = "history_unigram";
// User bigram dictionary, internal to LatinIME // User history dictionary for the bigram map, internal to LatinIME
public static final String DICT_KEY_USER_BIGRAM = "user_bigram"; public static final String DICT_KEY_USER_HISTORY_BIGRAM = "history_bigram";
public static final String DICT_KEY_WHITELIST ="whitelist"; public static final String DICT_KEY_WHITELIST ="whitelist";
private static final boolean DBG = LatinImeLogger.sDBG; private static final boolean DBG = LatinImeLogger.sDBG;
@ -203,12 +202,11 @@ public class Suggest implements Dictionary.WordCallback {
addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_CONTACTS, contactsDictionary); addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_CONTACTS, contactsDictionary);
} }
public void setUserUnigramDictionary(Dictionary userUnigramDictionary) { public void setUserHistoryDictionary(Dictionary userHistoryDictionary) {
addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_USER_UNIGRAM, userUnigramDictionary); addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_USER_HISTORY_UNIGRAM,
} userHistoryDictionary);
addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_USER_HISTORY_BIGRAM,
public void setUserBigramDictionary(Dictionary userBigramDictionary) { userHistoryDictionary);
addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_USER_BIGRAM, userBigramDictionary);
} }
public void setAutoCorrectionThreshold(double threshold) { public void setAutoCorrectionThreshold(double threshold) {
@ -347,7 +345,7 @@ public class Suggest implements Dictionary.WordCallback {
// At second character typed, search the unigrams (scores being affected by bigrams) // At second character typed, search the unigrams (scores being affected by bigrams)
for (final String key : mUnigramDictionaries.keySet()) { for (final String key : mUnigramDictionaries.keySet()) {
// Skip UserUnigramDictionary and WhitelistDictionary to lookup // Skip UserUnigramDictionary and WhitelistDictionary to lookup
if (key.equals(DICT_KEY_USER_UNIGRAM) || key.equals(DICT_KEY_WHITELIST)) if (key.equals(DICT_KEY_USER_HISTORY_UNIGRAM) || key.equals(DICT_KEY_WHITELIST))
continue; continue;
final Dictionary dictionary = mUnigramDictionaries.get(key); final Dictionary dictionary = mUnigramDictionaries.get(key);
dictionary.getWords(wordComposerForLookup, this, proximityInfo); dictionary.getWords(wordComposerForLookup, this, proximityInfo);

View file

@ -24,7 +24,6 @@ import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder; import android.database.sqlite.SQLiteQueryBuilder;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.provider.BaseColumns; import android.provider.BaseColumns;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import java.util.HashMap; import java.util.HashMap;
@ -32,12 +31,11 @@ import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
/** /**
* Stores all the pairs user types in databases. Prune the database if the size * Locally gathers stats about the words user types and various other signals like auto-correction
* gets too big. Unlike AutoDictionary, it even stores the pairs that are already * cancellation or manual picks. This allows the keyboard to adapt to the typist over time.
* in the dictionary.
*/ */
public class UserBigramDictionary extends ExpandableDictionary { public class UserHistoryDictionary extends ExpandableDictionary {
private static final String TAG = "UserBigramDictionary"; private static final String TAG = "UserHistoryDictionary";
/** 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;
@ -46,14 +44,14 @@ public class UserBigramDictionary extends ExpandableDictionary {
private static final int FREQUENCY_MAX = 127; 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 sMaxUserBigrams = 10000; private static int sMaxHistoryBigrams = 10000;
/** /**
* When it hits maximum bigram pair, it will delete until you are left with * When it hits maximum bigram pair, it will delete until you are left with
* only (sMaxUserBigrams - sDeleteUserBigrams) pairs. * only (sMaxHistoryBigrams - sDeleteHistoryBigrams) pairs.
* Do not keep this number small to avoid deleting too often. * Do not keep this number small to avoid deleting too often.
*/ */
private static int sDeleteUserBigrams = 1000; private static int sDeleteHistoryBigrams = 1000;
/** /**
* Database version should increase if the database structure changes * Database version should increase if the database structure changes
@ -65,7 +63,7 @@ public class UserBigramDictionary extends ExpandableDictionary {
/** Name of the words table in the database */ /** Name of the words table in the database */
private static final String MAIN_TABLE_NAME = "main"; private static final String MAIN_TABLE_NAME = "main";
// TODO: Consume less space by using a unique id for locale instead of the whole // TODO: Consume less space by using a unique id for locale instead of the whole
// 2-5 character string. (Same TODO from AutoDictionary) // 2-5 character string.
private static final String MAIN_COLUMN_ID = BaseColumns._ID; private static final String MAIN_COLUMN_ID = BaseColumns._ID;
private static final String MAIN_COLUMN_WORD1 = "word1"; private static final String MAIN_COLUMN_WORD1 = "word1";
private static final String MAIN_COLUMN_WORD2 = "word2"; private static final String MAIN_COLUMN_WORD2 = "word2";
@ -133,15 +131,15 @@ public class UserBigramDictionary extends ExpandableDictionary {
} }
} }
public void setDatabaseMax(int maxUserBigram) { public void setDatabaseMax(int maxHistoryBigram) {
sMaxUserBigrams = maxUserBigram; sMaxHistoryBigrams = maxHistoryBigram;
} }
public void setDatabaseDelete(int deleteUserBigram) { public void setDatabaseDelete(int deleteHistoryBigram) {
sDeleteUserBigrams = deleteUserBigram; sDeleteHistoryBigrams = deleteHistoryBigram;
} }
public UserBigramDictionary(Context context, LatinIME ime, String locale, int dicTypeId) { public UserHistoryDictionary(Context context, LatinIME ime, String locale, int dicTypeId) {
super(context, dicTypeId); super(context, dicTypeId);
mIme = ime; mIme = ime;
mLocale = locale; mLocale = locale;
@ -172,16 +170,6 @@ public class UserBigramDictionary extends ExpandableDictionary {
return false; return false;
} }
/**
* Add a single word without context.
*
* This is a temporary method to match the interface to UserUnigramDictionary. In the end
* this should be merged with addBigramPair.
*/
public void addUnigram(final String newWord) {
addBigramPair(null, newWord);
}
/** /**
* Pair will be added to the user history dictionary. * Pair will be added to the user history dictionary.
* *
@ -190,11 +178,12 @@ public class UserBigramDictionary extends ExpandableDictionary {
* context, as in beginning of a sentence for example. * context, as in beginning of a sentence for example.
* 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 addBigramPair(final String word1, String word2) { public int addToUserHistory(final String word1, String word2) {
// remove caps if second word is autocapitalized // remove caps if second word is autocapitalized
if (mIme != null && mIme.isAutoCapitalized()) { if (mIme != null && mIme.isAutoCapitalized()) {
word2 = Character.toLowerCase(word2.charAt(0)) + word2.substring(1); word2 = Character.toLowerCase(word2.charAt(0)) + word2.substring(1);
} }
super.addWord(word2, 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;
@ -203,7 +192,6 @@ public class UserBigramDictionary extends ExpandableDictionary {
int freq; int freq;
if (null == word1) { if (null == word1) {
freq = FREQUENCY_FOR_TYPED; freq = FREQUENCY_FOR_TYPED;
super.addWord(word2, FREQUENCY_FOR_TYPED);
} else { } else {
freq = super.addBigram(word1, word2, FREQUENCY_FOR_TYPED); freq = super.addBigram(word1, word2, FREQUENCY_FOR_TYPED);
} }
@ -366,8 +354,8 @@ public class UserBigramDictionary extends ExpandableDictionary {
try { try {
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 > sMaxUserBigrams) { if (totalRowCount > sMaxHistoryBigrams) {
int numDeleteRows = (totalRowCount - sMaxUserBigrams) + sDeleteUserBigrams; 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;