Merge "Rename AutoDictionary to UserUnigramDictionary."

This commit is contained in:
Jean Chalard 2011-07-14 20:45:48 -07:00 committed by Android (Google) Code Review
commit faf913c1b2
3 changed files with 52 additions and 41 deletions

View file

@ -155,7 +155,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
private UserDictionary mUserDictionary; private UserDictionary mUserDictionary;
private UserBigramDictionary mUserBigramDictionary; private UserBigramDictionary mUserBigramDictionary;
private AutoDictionary mAutoDictionary; private UserUnigramDictionary mUserUnigramDictionary;
// TODO: Create an inner class to group options and pseudo-options to improve readability. // TODO: Create an inner class to group options and pseudo-options to improve readability.
// These variables are initialized according to the {@link EditorInfo#inputType}. // These variables are initialized according to the {@link EditorInfo#inputType}.
@ -443,10 +443,12 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
resetContactsDictionary(); resetContactsDictionary();
mAutoDictionary = new AutoDictionary(this, this, localeStr, Suggest.DIC_AUTO); mUserUnigramDictionary
mSuggest.setAutoDictionary(mAutoDictionary); = new UserUnigramDictionary(this, this, localeStr, Suggest.DIC_USER_UNIGRAM);
mSuggest.setUserUnigramDictionary(mUserUnigramDictionary);
mUserBigramDictionary = new UserBigramDictionary(this, this, localeStr, Suggest.DIC_USER); mUserBigramDictionary
= new UserBigramDictionary(this, this, localeStr, Suggest.DIC_USER_BIGRAM);
mSuggest.setUserBigramDictionary(mUserBigramDictionary); mSuggest.setUserBigramDictionary(mUserBigramDictionary);
updateCorrectionMode(); updateCorrectionMode();
@ -672,7 +674,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 (mAutoDictionary != null) mAutoDictionary.flushPendingWrites(); if (mUserUnigramDictionary != null) mUserUnigramDictionary.flushPendingWrites();
if (mUserBigramDictionary != null) mUserBigramDictionary.flushPendingWrites(); if (mUserBigramDictionary != null) mUserBigramDictionary.flushPendingWrites();
} }
@ -951,8 +953,8 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
} }
mCommittedLength = mComposingStringBuilder.length(); mCommittedLength = mComposingStringBuilder.length();
TextEntryState.acceptedTyped(mComposingStringBuilder); TextEntryState.acceptedTyped(mComposingStringBuilder);
addToAutoAndUserBigramDictionaries(mComposingStringBuilder, addToUserUnigramAndBigramDictionaries(mComposingStringBuilder,
AutoDictionary.FREQUENCY_FOR_TYPED); UserUnigramDictionary.FREQUENCY_FOR_TYPED);
} }
updateSuggestions(); updateSuggestions();
} }
@ -1557,8 +1559,9 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
TextEntryState.acceptedDefault(mWordComposer.getTypedWord(), mBestWord, separatorCode); TextEntryState.acceptedDefault(mWordComposer.getTypedWord(), mBestWord, separatorCode);
mExpectingUpdateSelection = true; mExpectingUpdateSelection = true;
commitBestWord(mBestWord); commitBestWord(mBestWord);
// Add the word to the auto dictionary if it's not a known word // Add the word to the user unigram dictionary if it's not a known word
addToAutoAndUserBigramDictionaries(mBestWord, AutoDictionary.FREQUENCY_FOR_TYPED); addToUserUnigramAndBigramDictionaries(mBestWord,
UserUnigramDictionary.FREQUENCY_FOR_TYPED);
return true; return true;
} }
return false; return false;
@ -1627,7 +1630,8 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
commitBestWord(suggestion); commitBestWord(suggestion);
// Add the word to the auto dictionary if it's not a known word // Add the word to the auto dictionary if it's not a known word
if (index == 0) { if (index == 0) {
addToAutoAndUserBigramDictionaries(suggestion, AutoDictionary.FREQUENCY_FOR_PICKED); addToUserUnigramAndBigramDictionaries(suggestion,
UserUnigramDictionary.FREQUENCY_FOR_PICKED);
} else { } else {
addToOnlyBigramDictionary(suggestion, 1); addToOnlyBigramDictionary(suggestion, 1);
} }
@ -1725,7 +1729,8 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
setSuggestionStripShown(isCandidateStripVisible()); setSuggestionStripShown(isCandidateStripVisible());
} }
private void addToAutoAndUserBigramDictionaries(CharSequence suggestion, int frequencyDelta) { private void addToUserUnigramAndBigramDictionaries(CharSequence suggestion,
int frequencyDelta) {
checkAddToDictionary(suggestion, frequencyDelta, false); checkAddToDictionary(suggestion, frequencyDelta, false);
} }
@ -1734,7 +1739,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
} }
/** /**
* Adds to the UserBigramDictionary and/or AutoDictionary * Adds to the UserBigramDictionary and/or UserUnigramDictionary
* @param selectedANotTypedWord true if it should be added to bigram dictionary if possible * @param selectedANotTypedWord true if it should be added to bigram dictionary if possible
*/ */
private void checkAddToDictionary(CharSequence suggestion, int frequencyDelta, private void checkAddToDictionary(CharSequence suggestion, int frequencyDelta,
@ -1749,14 +1754,14 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
return; return;
} }
final boolean selectedATypedWordAndItsInAutoDic = final boolean selectedATypedWordAndItsInUserUnigramDic =
!selectedANotTypedWord && mAutoDictionary.isValidWord(suggestion); !selectedANotTypedWord && mUserUnigramDictionary.isValidWord(suggestion);
final boolean isValidWord = AutoCorrection.isValidWord( final boolean isValidWord = AutoCorrection.isValidWord(
mSuggest.getUnigramDictionaries(), suggestion, true); mSuggest.getUnigramDictionaries(), suggestion, true);
final boolean needsToAddToAutoDictionary = selectedATypedWordAndItsInAutoDic final boolean needsToAddToUserUnigramDictionary = selectedATypedWordAndItsInUserUnigramDic
|| !isValidWord; || !isValidWord;
if (needsToAddToAutoDictionary) { if (needsToAddToUserUnigramDictionary) {
mAutoDictionary.addWord(suggestion.toString(), frequencyDelta); mUserUnigramDictionary.addWord(suggestion.toString(), frequencyDelta);
} }
if (mUserBigramDictionary != null) { if (mUserBigramDictionary != null) {

View file

@ -60,18 +60,24 @@ public class Suggest implements Dictionary.WordCallback {
*/ */
public static final int MAXIMUM_BIGRAM_FREQUENCY = 127; public static final int MAXIMUM_BIGRAM_FREQUENCY = 127;
// It seems the following values are only used for logging.
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_AUTO = 3; public static final int DIC_USER_UNIGRAM = 3;
public static final int DIC_CONTACTS = 4; public static final int DIC_CONTACTS = 4;
public static final int DIC_USER_BIGRAM = 5;
// If you add a type of dictionary, increment DIC_TYPE_LAST_ID // If you add a type of dictionary, increment DIC_TYPE_LAST_ID
public static final int DIC_TYPE_LAST_ID = 4; // TODO: this value seems unused. Remove it?
public static final int DIC_TYPE_LAST_ID = 5;
public static final String DICT_KEY_MAIN = "main"; public static final String DICT_KEY_MAIN = "main";
public static final String DICT_KEY_CONTACTS = "contacts"; public static final String DICT_KEY_CONTACTS = "contacts";
public static final String DICT_KEY_AUTO = "auto"; // 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
public static final String DICT_KEY_USER_UNIGRAM = "user_unigram";
// User bigram dictionary, internal to LatinIME
public static final String DICT_KEY_USER_BIGRAM = "user_bigram"; public static final String DICT_KEY_USER_BIGRAM = "user_bigram";
public static final String DICT_KEY_WHITELIST ="whitelist"; public static final String DICT_KEY_WHITELIST ="whitelist";
@ -177,7 +183,7 @@ public class Suggest implements Dictionary.WordCallback {
/** /**
* Sets an optional user dictionary resource to be loaded. The user dictionary is consulted * Sets an optional user dictionary resource to be loaded. The user dictionary is consulted
* before the main dictionary, if set. * before the main dictionary, if set. This refers to the system-managed user dictionary.
*/ */
public void setUserDictionary(Dictionary userDictionary) { public void setUserDictionary(Dictionary userDictionary) {
addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_USER, userDictionary); addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_USER, userDictionary);
@ -193,8 +199,8 @@ public class Suggest implements Dictionary.WordCallback {
addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_CONTACTS, contactsDictionary); addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_CONTACTS, contactsDictionary);
} }
public void setAutoDictionary(Dictionary autoDictionary) { public void setUserUnigramDictionary(Dictionary userUnigramDictionary) {
addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_AUTO, autoDictionary); addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_USER_UNIGRAM, userUnigramDictionary);
} }
public void setUserBigramDictionary(Dictionary userBigramDictionary) { public void setUserBigramDictionary(Dictionary userBigramDictionary) {
@ -335,8 +341,8 @@ public class Suggest implements Dictionary.WordCallback {
} else if (wordComposer.size() > 1) { } else if (wordComposer.size() > 1) {
// 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 AutoDictionary and WhitelistDictionary to lookup // Skip UserUnigramDictionary and WhitelistDictionary to lookup
if (key.equals(DICT_KEY_AUTO) || key.equals(DICT_KEY_WHITELIST)) if (key.equals(DICT_KEY_USER_UNIGRAM) || key.equals(DICT_KEY_WHITELIST))
continue; continue;
final Dictionary dictionary = mUnigramDictionaries.get(key); final Dictionary dictionary = mUnigramDictionaries.get(key);
dictionary.getWords(wordComposer, this); dictionary.getWords(wordComposer, this);

View file

@ -31,12 +31,11 @@ import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
/** /**
* Stores new words temporarily until they are promoted to the user dictionary * This class (inherited from the old AutoDictionary) is used for user history
* for longevity. Words in the auto dictionary are used to determine if it's ok * based dictionary. It stores words that the user typed to supply a provision
* to accept a word that's not in the main or user dictionary. Using a new word * for suggesting and re-ordering of candidates.
* repeatedly will promote it to the user dictionary.
*/ */
public class AutoDictionary extends ExpandableDictionary { public class UserUnigramDictionary extends ExpandableDictionary {
// Weight added to a user picking a new word from the suggestion strip // Weight added to a user picking a new word from the suggestion strip
static final int FREQUENCY_FOR_PICKED = 3; static final int FREQUENCY_FOR_PICKED = 3;
// Weight added to a user typing a new word that doesn't get corrected (or is reverted) // Weight added to a user typing a new word that doesn't get corrected (or is reverted)
@ -45,12 +44,13 @@ public class AutoDictionary extends ExpandableDictionary {
private static final int VALIDITY_THRESHOLD = 2 * FREQUENCY_FOR_PICKED; private static final int VALIDITY_THRESHOLD = 2 * FREQUENCY_FOR_PICKED;
private LatinIME mIme; private LatinIME mIme;
// Locale for which this auto dictionary is storing words // Locale for which this user unigram dictionary is storing words
private String mLocale; private String mLocale;
private HashMap<String,Integer> mPendingWrites = new HashMap<String,Integer>(); private HashMap<String,Integer> mPendingWrites = new HashMap<String,Integer>();
private final Object mPendingWritesLock = new Object(); private final Object mPendingWritesLock = new Object();
// TODO: we should probably change the database name
private static final String DATABASE_NAME = "auto_dict.db"; private static final String DATABASE_NAME = "auto_dict.db";
private static final int DATABASE_VERSION = 1; private static final int DATABASE_VERSION = 1;
@ -65,8 +65,8 @@ public class AutoDictionary extends ExpandableDictionary {
/** Sort by descending order of frequency. */ /** Sort by descending order of frequency. */
public static final String DEFAULT_SORT_ORDER = COLUMN_FREQUENCY + " DESC"; public static final String DEFAULT_SORT_ORDER = COLUMN_FREQUENCY + " DESC";
/** Name of the words table in the auto_dict.db */ /** Name of the words table in the database */
private static final String AUTODICT_TABLE_NAME = "words"; private static final String USER_UNIGRAM_DICT_TABLE_NAME = "words";
private static HashMap<String, String> sDictProjectionMap; private static HashMap<String, String> sDictProjectionMap;
@ -80,7 +80,7 @@ public class AutoDictionary extends ExpandableDictionary {
private static DatabaseHelper sOpenHelper = null; private static DatabaseHelper sOpenHelper = null;
public AutoDictionary(Context context, LatinIME ime, String locale, int dicTypeId) { public UserUnigramDictionary(Context context, LatinIME ime, String locale, int dicTypeId) {
super(context, dicTypeId); super(context, dicTypeId);
mIme = ime; mIme = ime;
mLocale = locale; mLocale = locale;
@ -177,7 +177,7 @@ public class AutoDictionary extends ExpandableDictionary {
@Override @Override
public void onCreate(SQLiteDatabase db) { public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + AUTODICT_TABLE_NAME + " (" db.execSQL("CREATE TABLE " + USER_UNIGRAM_DICT_TABLE_NAME + " ("
+ COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_ID + " INTEGER PRIMARY KEY,"
+ COLUMN_WORD + " TEXT," + COLUMN_WORD + " TEXT,"
+ COLUMN_FREQUENCY + " INTEGER," + COLUMN_FREQUENCY + " INTEGER,"
@ -187,16 +187,16 @@ public class AutoDictionary extends ExpandableDictionary {
@Override @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w("AutoDictionary", "Upgrading database from version " + oldVersion + " to " Log.w("UserUnigramDictionary", "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data"); + newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + AUTODICT_TABLE_NAME); db.execSQL("DROP TABLE IF EXISTS " + USER_UNIGRAM_DICT_TABLE_NAME);
onCreate(db); onCreate(db);
} }
} }
private Cursor query(String selection, String[] selectionArgs) { private Cursor query(String selection, String[] selectionArgs) {
SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(AUTODICT_TABLE_NAME); qb.setTables(USER_UNIGRAM_DICT_TABLE_NAME);
qb.setProjectionMap(sDictProjectionMap); qb.setProjectionMap(sDictProjectionMap);
// Get the database and run the query // Get the database and run the query
@ -229,10 +229,10 @@ public class AutoDictionary extends ExpandableDictionary {
Set<Entry<String,Integer>> mEntries = mMap.entrySet(); Set<Entry<String,Integer>> mEntries = mMap.entrySet();
for (Entry<String,Integer> entry : mEntries) { for (Entry<String,Integer> entry : mEntries) {
Integer freq = entry.getValue(); Integer freq = entry.getValue();
db.delete(AUTODICT_TABLE_NAME, COLUMN_WORD + "=? AND " + COLUMN_LOCALE + "=?", db.delete(USER_UNIGRAM_DICT_TABLE_NAME, COLUMN_WORD + "=? AND " + COLUMN_LOCALE
new String[] { entry.getKey(), mLocale }); + "=?", new String[] { entry.getKey(), mLocale });
if (freq != null) { if (freq != null) {
db.insert(AUTODICT_TABLE_NAME, null, db.insert(USER_UNIGRAM_DICT_TABLE_NAME, null,
getContentValues(entry.getKey(), freq, mLocale)); getContentValues(entry.getKey(), freq, mLocale));
} }
} }