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

View file

@ -60,18 +60,24 @@ public class Suggest implements Dictionary.WordCallback {
*/
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_MAIN = 1;
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_USER_BIGRAM = 5;
// 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_CONTACTS = "contacts";
public static final String DICT_KEY_AUTO = "auto";
// User dictionary, the system-managed one.
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_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
* 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) {
addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_USER, userDictionary);
@ -193,8 +199,8 @@ public class Suggest implements Dictionary.WordCallback {
addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_CONTACTS, contactsDictionary);
}
public void setAutoDictionary(Dictionary autoDictionary) {
addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_AUTO, autoDictionary);
public void setUserUnigramDictionary(Dictionary userUnigramDictionary) {
addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_USER_UNIGRAM, userUnigramDictionary);
}
public void setUserBigramDictionary(Dictionary userBigramDictionary) {
@ -335,8 +341,8 @@ public class Suggest implements Dictionary.WordCallback {
} else if (wordComposer.size() > 1) {
// At second character typed, search the unigrams (scores being affected by bigrams)
for (final String key : mUnigramDictionaries.keySet()) {
// Skip AutoDictionary and WhitelistDictionary to lookup
if (key.equals(DICT_KEY_AUTO) || key.equals(DICT_KEY_WHITELIST))
// Skip UserUnigramDictionary and WhitelistDictionary to lookup
if (key.equals(DICT_KEY_USER_UNIGRAM) || key.equals(DICT_KEY_WHITELIST))
continue;
final Dictionary dictionary = mUnigramDictionaries.get(key);
dictionary.getWords(wordComposer, this);

View file

@ -31,12 +31,11 @@ import java.util.Map.Entry;
import java.util.Set;
/**
* Stores new words temporarily until they are promoted to the user dictionary
* for longevity. Words in the auto dictionary are used to determine if it's ok
* to accept a word that's not in the main or user dictionary. Using a new word
* repeatedly will promote it to the user dictionary.
* This class (inherited from the old AutoDictionary) is used for user history
* based dictionary. It stores words that the user typed to supply a provision
* for suggesting and re-ordering of candidates.
*/
public class AutoDictionary extends ExpandableDictionary {
public class UserUnigramDictionary extends ExpandableDictionary {
// Weight added to a user picking a new word from the suggestion strip
static final int FREQUENCY_FOR_PICKED = 3;
// 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 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 HashMap<String,Integer> mPendingWrites = new HashMap<String,Integer>();
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 int DATABASE_VERSION = 1;
@ -65,8 +65,8 @@ public class AutoDictionary extends ExpandableDictionary {
/** Sort by descending order of frequency. */
public static final String DEFAULT_SORT_ORDER = COLUMN_FREQUENCY + " DESC";
/** Name of the words table in the auto_dict.db */
private static final String AUTODICT_TABLE_NAME = "words";
/** Name of the words table in the database */
private static final String USER_UNIGRAM_DICT_TABLE_NAME = "words";
private static HashMap<String, String> sDictProjectionMap;
@ -80,7 +80,7 @@ public class AutoDictionary extends ExpandableDictionary {
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);
mIme = ime;
mLocale = locale;
@ -177,7 +177,7 @@ public class AutoDictionary extends ExpandableDictionary {
@Override
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_WORD + " TEXT,"
+ COLUMN_FREQUENCY + " INTEGER,"
@ -187,16 +187,16 @@ public class AutoDictionary extends ExpandableDictionary {
@Override
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");
db.execSQL("DROP TABLE IF EXISTS " + AUTODICT_TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + USER_UNIGRAM_DICT_TABLE_NAME);
onCreate(db);
}
}
private Cursor query(String selection, String[] selectionArgs) {
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(AUTODICT_TABLE_NAME);
qb.setTables(USER_UNIGRAM_DICT_TABLE_NAME);
qb.setProjectionMap(sDictProjectionMap);
// Get the database and run the query
@ -229,10 +229,10 @@ public class AutoDictionary extends ExpandableDictionary {
Set<Entry<String,Integer>> mEntries = mMap.entrySet();
for (Entry<String,Integer> entry : mEntries) {
Integer freq = entry.getValue();
db.delete(AUTODICT_TABLE_NAME, COLUMN_WORD + "=? AND " + COLUMN_LOCALE + "=?",
new String[] { entry.getKey(), mLocale });
db.delete(USER_UNIGRAM_DICT_TABLE_NAME, COLUMN_WORD + "=? AND " + COLUMN_LOCALE
+ "=?", new String[] { entry.getKey(), mLocale });
if (freq != null) {
db.insert(AUTODICT_TABLE_NAME, null,
db.insert(USER_UNIGRAM_DICT_TABLE_NAME, null,
getContentValues(entry.getKey(), freq, mLocale));
}
}