Cleanup the dictionary type.

Stop storing an int in each of the different class types, and
just store a string in the top class.

Change-Id: I2af1832743e6fe78e5c1364f6d9cc21252bf5831
main
Jean Chalard 2012-06-27 17:31:09 +09:00
parent c356df8e08
commit 05efe576f9
16 changed files with 47 additions and 44 deletions

View File

@ -49,7 +49,6 @@ public class BinaryDictionary extends Dictionary {
private static final int TYPED_LETTER_MULTIPLIER = 2; private static final int TYPED_LETTER_MULTIPLIER = 2;
private int mDicTypeId;
private long mNativeDict; private long mNativeDict;
private final int[] mInputCodes = new int[MAX_WORD_LENGTH]; private final int[] mInputCodes = new int[MAX_WORD_LENGTH];
private final char[] mOutputChars = new char[MAX_WORD_LENGTH * MAX_WORDS]; private final char[] mOutputChars = new char[MAX_WORD_LENGTH * MAX_WORDS];
@ -69,12 +68,12 @@ public class BinaryDictionary extends Dictionary {
* @param offset the offset of the dictionary data within the file. * @param offset the offset of the dictionary data within the file.
* @param length the length of the binary data. * @param length the length of the binary data.
* @param useFullEditDistance whether to use the full edit distance in suggestions * @param useFullEditDistance whether to use the full edit distance in suggestions
* @param dicTypeId the dictionary type id of the dictionary * @param dictType the dictionary type, as a human-readable string
*/ */
public BinaryDictionary(final Context context, public BinaryDictionary(final Context context,
final String filename, final long offset, final long length, final String filename, final long offset, final long length,
final boolean useFullEditDistance, final Locale locale, final int dicTypeId) { final boolean useFullEditDistance, final Locale locale, final String dictType) {
mDicTypeId = dicTypeId; super(dictType);
mUseFullEditDistance = useFullEditDistance; mUseFullEditDistance = useFullEditDistance;
loadDictionary(filename, offset, length); loadDictionary(filename, offset, length);
} }
@ -90,7 +89,7 @@ public class BinaryDictionary extends Dictionary {
private native boolean isValidBigramNative(long dict, int[] word1, int[] word2); private native boolean isValidBigramNative(long dict, int[] word1, int[] word2);
private native int getSuggestionsNative(long dict, long proximityInfo, int[] xCoordinates, private native int getSuggestionsNative(long dict, long proximityInfo, int[] xCoordinates,
int[] yCoordinates, int[] times, int[] pointerIds, int[] inputCodes, int codesSize, int[] yCoordinates, int[] times, int[] pointerIds, int[] inputCodes, int codesSize,
int commitPoint, boolean isGesture, int dicTypeId, int commitPoint, boolean isGesture,
int[] prevWordCodePointArray, boolean useFullEditDistance, char[] outputChars, int[] prevWordCodePointArray, boolean useFullEditDistance, char[] outputChars,
int[] scores, int[] outputIndices); int[] scores, int[] outputIndices);
private native int getBigramsNative(long dict, int[] prevWord, int prevWordLength, private native int getBigramsNative(long dict, int[] prevWord, int prevWordLength,
@ -202,8 +201,7 @@ public class BinaryDictionary extends Dictionary {
return getSuggestionsNative(mNativeDict, proximityInfo.getNativeProximityInfo(), return getSuggestionsNative(mNativeDict, proximityInfo.getNativeProximityInfo(),
codes.getXCoordinates(), codes.getYCoordinates(), emptyArray, emptyArray, mInputCodes, codes.getXCoordinates(), codes.getYCoordinates(), emptyArray, emptyArray, mInputCodes,
codesSize, 0 /* unused */, false, mDicTypeId, codesSize, 0 /* unused */, false, prevWordCodePointArray, mUseFullEditDistance,
prevWordCodePointArray, mUseFullEditDistance,
outputChars, scores, spaceIndices); outputChars, scores, spaceIndices);
} }

View File

@ -62,8 +62,8 @@ public class ContactsBinaryDictionary extends ExpandableBinaryDictionary {
*/ */
private final boolean mUseFirstLastBigrams; private final boolean mUseFirstLastBigrams;
public ContactsBinaryDictionary(final Context context, final int dicTypeId, Locale locale) { public ContactsBinaryDictionary(final Context context, Locale locale) {
super(context, getFilenameWithLocale(NAME, locale.toString()), dicTypeId); super(context, getFilenameWithLocale(NAME, locale.toString()), Suggest.DICT_KEY_CONTACTS);
mLocale = locale; mLocale = locale;
mUseFirstLastBigrams = useFirstLastBigramsForLocale(locale); mUseFirstLastBigrams = useFirstLastBigramsForLocale(locale);
registerObserver(context); registerObserver(context);

View File

@ -33,6 +33,12 @@ public abstract class Dictionary {
public static final int NOT_A_PROBABILITY = -1; public static final int NOT_A_PROBABILITY = -1;
protected final String mDictType;
public Dictionary(final String dictType) {
mDictType = dictType;
}
/** /**
* Searches for words in the dictionary that match the characters in the composer. Matched * Searches for words in the dictionary that match the characters in the composer. Matched
* words are returned as an ArrayList. * words are returned as an ArrayList.

View File

@ -33,11 +33,13 @@ public class DictionaryCollection extends Dictionary {
private final String TAG = DictionaryCollection.class.getSimpleName(); private final String TAG = DictionaryCollection.class.getSimpleName();
protected final CopyOnWriteArrayList<Dictionary> mDictionaries; protected final CopyOnWriteArrayList<Dictionary> mDictionaries;
public DictionaryCollection() { public DictionaryCollection(final String dictType) {
super(dictType);
mDictionaries = new CopyOnWriteArrayList<Dictionary>(); mDictionaries = new CopyOnWriteArrayList<Dictionary>();
} }
public DictionaryCollection(Dictionary... dictionaries) { public DictionaryCollection(final String dictType, Dictionary... dictionaries) {
super(dictType);
if (null == dictionaries) { if (null == dictionaries) {
mDictionaries = new CopyOnWriteArrayList<Dictionary>(); mDictionaries = new CopyOnWriteArrayList<Dictionary>();
} else { } else {
@ -46,7 +48,8 @@ public class DictionaryCollection extends Dictionary {
} }
} }
public DictionaryCollection(Collection<Dictionary> dictionaries) { public DictionaryCollection(final String dictType, Collection<Dictionary> dictionaries) {
super(dictType);
mDictionaries = new CopyOnWriteArrayList<Dictionary>(dictionaries); mDictionaries = new CopyOnWriteArrayList<Dictionary>(dictionaries);
mDictionaries.removeAll(Collections.singleton(null)); mDictionaries.removeAll(Collections.singleton(null));
} }

View File

@ -49,7 +49,8 @@ public class DictionaryFactory {
final Locale locale, final boolean useFullEditDistance) { final Locale locale, final boolean useFullEditDistance) {
if (null == locale) { if (null == locale) {
Log.e(TAG, "No locale defined for dictionary"); Log.e(TAG, "No locale defined for dictionary");
return new DictionaryCollection(createBinaryDictionary(context, locale)); return new DictionaryCollection(Suggest.DICT_KEY_MAIN,
createBinaryDictionary(context, locale));
} }
final LinkedList<Dictionary> dictList = new LinkedList<Dictionary>(); final LinkedList<Dictionary> dictList = new LinkedList<Dictionary>();
@ -59,7 +60,7 @@ public class DictionaryFactory {
for (final AssetFileAddress f : assetFileList) { for (final AssetFileAddress f : assetFileList) {
final BinaryDictionary binaryDictionary = final BinaryDictionary binaryDictionary =
new BinaryDictionary(context, f.mFilename, f.mOffset, f.mLength, new BinaryDictionary(context, f.mFilename, f.mOffset, f.mLength,
useFullEditDistance, locale, Suggest.DIC_MAIN); useFullEditDistance, locale, Suggest.DICT_KEY_MAIN);
if (binaryDictionary.isValidDictionary()) { if (binaryDictionary.isValidDictionary()) {
dictList.add(binaryDictionary); dictList.add(binaryDictionary);
} }
@ -69,7 +70,7 @@ public class DictionaryFactory {
// If the list is empty, that means we should not use any dictionary (for example, the user // If the list is empty, that means we should not use any dictionary (for example, the user
// explicitly disabled the main dictionary), so the following is okay. dictList is never // explicitly disabled the main dictionary), so the following is okay. dictList is never
// null, but if for some reason it is, DictionaryCollection handles it gracefully. // null, but if for some reason it is, DictionaryCollection handles it gracefully.
return new DictionaryCollection(dictList); return new DictionaryCollection(Suggest.DICT_KEY_MAIN, dictList);
} }
/** /**
@ -112,7 +113,7 @@ public class DictionaryFactory {
return null; return null;
} }
return new BinaryDictionary(context, sourceDir, afd.getStartOffset(), afd.getLength(), return new BinaryDictionary(context, sourceDir, afd.getStartOffset(), afd.getLength(),
false /* useFullEditDistance */, locale, Suggest.DIC_MAIN); false /* useFullEditDistance */, locale, Suggest.DICT_KEY_MAIN);
} catch (android.content.res.Resources.NotFoundException e) { } catch (android.content.res.Resources.NotFoundException e) {
Log.e(TAG, "Could not find the resource"); Log.e(TAG, "Could not find the resource");
return null; return null;
@ -140,7 +141,7 @@ public class DictionaryFactory {
long startOffset, long length, final boolean useFullEditDistance, Locale locale) { long startOffset, long length, final boolean useFullEditDistance, Locale locale) {
if (dictionary.isFile()) { if (dictionary.isFile()) {
return new BinaryDictionary(context, dictionary.getAbsolutePath(), startOffset, length, return new BinaryDictionary(context, dictionary.getAbsolutePath(), startOffset, length,
useFullEditDistance, locale, Suggest.DIC_MAIN); useFullEditDistance, locale, Suggest.DICT_KEY_MAIN);
} else { } else {
Log.e(TAG, "Could not find the file. path=" + dictionary.getAbsolutePath()); Log.e(TAG, "Could not find the file. path=" + dictionary.getAbsolutePath());
return null; return null;

View File

@ -76,9 +76,6 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
/** The expandable fusion dictionary used to generate the binary dictionary. */ /** The expandable fusion dictionary used to generate the binary dictionary. */
private FusionDictionary mFusionDictionary; private FusionDictionary mFusionDictionary;
/** The dictionary type id. */
public final int mDicTypeId;
/** /**
* The name of this dictionary, used as the filename for storing the binary dictionary. Multiple * The name of this dictionary, used as the filename for storing the binary dictionary. Multiple
* dictionary instances with the same filename is supported, with access controlled by * dictionary instances with the same filename is supported, with access controlled by
@ -124,11 +121,11 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
* @param context The application context of the parent. * @param context The application context of the parent.
* @param filename The filename for this binary dictionary. Multiple dictionaries with the same * @param filename The filename for this binary dictionary. Multiple dictionaries with the same
* filename is supported. * filename is supported.
* @param dictType The type of this dictionary. * @param dictType the dictionary type, as a human-readable string
*/ */
public ExpandableBinaryDictionary( public ExpandableBinaryDictionary(
final Context context, final String filename, final int dictType) { final Context context, final String filename, final String dictType) {
mDicTypeId = dictType; super(dictType);
mFilename = filename; mFilename = filename;
mContext = context; mContext = context;
mBinaryDictionary = null; mBinaryDictionary = null;
@ -308,7 +305,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
// Build the new binary dictionary // Build the new binary dictionary
final BinaryDictionary newBinaryDictionary = final BinaryDictionary newBinaryDictionary =
new BinaryDictionary(mContext, filename, 0, length, true /* useFullEditDistance */, new BinaryDictionary(mContext, filename, 0, length, true /* useFullEditDistance */,
null, mDicTypeId); null, mDictType);
if (mBinaryDictionary != null) { if (mBinaryDictionary != null) {
// Ensure all threads accessing the current dictionary have finished before swapping in // Ensure all threads accessing the current dictionary have finished before swapping in

View File

@ -38,7 +38,6 @@ public class ExpandableDictionary extends Dictionary {
private Context mContext; private Context mContext;
private char[] mWordBuilder = new char[BinaryDictionary.MAX_WORD_LENGTH]; private char[] mWordBuilder = new char[BinaryDictionary.MAX_WORD_LENGTH];
private int mDicTypeId;
private int mMaxDepth; private int mMaxDepth;
private int mInputLength; private int mInputLength;
@ -152,11 +151,11 @@ public class ExpandableDictionary extends Dictionary {
private int[][] mCodes; private int[][] mCodes;
public ExpandableDictionary(Context context, int dicTypeId) { public ExpandableDictionary(final Context context, final String dictType) {
super(dictType);
mContext = context; mContext = context;
clearDictionary(); clearDictionary();
mCodes = new int[BinaryDictionary.MAX_WORD_LENGTH][]; mCodes = new int[BinaryDictionary.MAX_WORD_LENGTH][];
mDicTypeId = dicTypeId;
} }
public void loadDictionary() { public void loadDictionary() {

View File

@ -499,8 +499,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
// If the locale has changed then recreate the contacts dictionary. This // If the locale has changed then recreate the contacts dictionary. This
// allows locale dependent rules for handling bigram name predictions. // allows locale dependent rules for handling bigram name predictions.
oldContactsDictionary.close(); oldContactsDictionary.close();
dictionaryToUse = new ContactsBinaryDictionary( dictionaryToUse = new ContactsBinaryDictionary(this, locale);
this, Suggest.DIC_CONTACTS, locale);
} else { } else {
// Make sure the old contacts dictionary is opened. If it is already open, // Make sure the old contacts dictionary is opened. If it is already open,
// this is a no-op, so it's safe to call it anyways. // this is a no-op, so it's safe to call it anyways.
@ -508,7 +507,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
dictionaryToUse = oldContactsDictionary; dictionaryToUse = oldContactsDictionary;
} }
} else { } else {
dictionaryToUse = new ContactsBinaryDictionary(this, Suggest.DIC_CONTACTS, locale); dictionaryToUse = new ContactsBinaryDictionary(this, locale);
} }
} }

View File

@ -28,7 +28,7 @@ public class SynchronouslyLoadedContactsBinaryDictionary extends ContactsBinaryD
private boolean mClosed; private boolean mClosed;
public SynchronouslyLoadedContactsBinaryDictionary(final Context context, final Locale locale) { public SynchronouslyLoadedContactsBinaryDictionary(final Context context, final Locale locale) {
super(context, Suggest.DIC_CONTACTS, locale); super(context, locale);
} }
@Override @Override

View File

@ -69,7 +69,7 @@ public class UserBinaryDictionary extends ExpandableBinaryDictionary {
public UserBinaryDictionary(final Context context, final String locale, public UserBinaryDictionary(final Context context, final String locale,
final boolean alsoUseMoreRestrictiveLocales) { final boolean alsoUseMoreRestrictiveLocales) {
super(context, getFilenameWithLocale(NAME, locale), Suggest.DIC_USER); super(context, getFilenameWithLocale(NAME, locale), Suggest.DICT_KEY_USER);
if (null == locale) throw new NullPointerException(); // Catch the error earlier if (null == locale) throw new NullPointerException(); // Catch the error earlier
mLocale = locale; mLocale = locale;
mAlsoUseMoreRestrictiveLocales = alsoUseMoreRestrictiveLocales; mAlsoUseMoreRestrictiveLocales = alsoUseMoreRestrictiveLocales;

View File

@ -128,14 +128,14 @@ public class UserHistoryDictionary extends ExpandableDictionary {
} }
} }
final UserHistoryDictionary dict = final UserHistoryDictionary dict =
new UserHistoryDictionary(context, locale, dictTypeId, sp); new UserHistoryDictionary(context, locale, sp);
sLangDictCache.put(locale, new SoftReference<UserHistoryDictionary>(dict)); sLangDictCache.put(locale, new SoftReference<UserHistoryDictionary>(dict));
return dict; return dict;
} }
private UserHistoryDictionary(final Context context, final String locale, final int dicTypeId, private UserHistoryDictionary(final Context context, final String locale,
SharedPreferences sp) { final SharedPreferences sp) {
super(context, dicTypeId); super(context, Suggest.DICT_KEY_USER_HISTORY);
mLocale = locale; mLocale = locale;
mPrefs = sp; mPrefs = sp;
if (sOpenHelper == null) { if (sOpenHelper == null) {

View File

@ -37,7 +37,7 @@ public class WhitelistDictionary extends ExpandableDictionary {
// TODO: Conform to the async load contact of ExpandableDictionary // TODO: Conform to the async load contact of ExpandableDictionary
public WhitelistDictionary(final Context context, final Locale locale) { public WhitelistDictionary(final Context context, final Locale locale) {
super(context, Suggest.DIC_WHITELIST); super(context, Suggest.DICT_KEY_WHITELIST);
// TODO: Move whitelist dictionary into main dictionary. // TODO: Move whitelist dictionary into main dictionary.
final RunInLocale<Void> job = new RunInLocale<Void>() { final RunInLocale<Void> job = new RunInLocale<Void>() {
@Override @Override

View File

@ -129,7 +129,7 @@ static jlong latinime_BinaryDictionary_open(JNIEnv *env, jobject object,
static int latinime_BinaryDictionary_getSuggestions(JNIEnv *env, jobject object, jlong dict, static int latinime_BinaryDictionary_getSuggestions(JNIEnv *env, jobject object, jlong dict,
jlong proximityInfo, jintArray xCoordinatesArray, jintArray yCoordinatesArray, jlong proximityInfo, jintArray xCoordinatesArray, jintArray yCoordinatesArray,
jintArray timesArray, jintArray pointerIdArray, jintArray inputArray, jint arraySize, jintArray timesArray, jintArray pointerIdArray, jintArray inputArray, jint arraySize,
jint commitPoint, jboolean isGesture, jint dicTypeId, jint commitPoint, jboolean isGesture,
jintArray prevWordForBigrams, jboolean useFullEditDistance, jcharArray outputArray, jintArray prevWordForBigrams, jboolean useFullEditDistance, jcharArray outputArray,
jintArray frequencyArray, jintArray spaceIndexArray) { jintArray frequencyArray, jintArray spaceIndexArray) {
Dictionary *dictionary = (Dictionary*) dict; Dictionary *dictionary = (Dictionary*) dict;
@ -148,7 +148,7 @@ static int latinime_BinaryDictionary_getSuggestions(JNIEnv *env, jobject object,
jsize prevWordLength = prevWordChars ? env->GetArrayLength(prevWordForBigrams) : 0; jsize prevWordLength = prevWordChars ? env->GetArrayLength(prevWordForBigrams) : 0;
int count = dictionary->getSuggestions(pInfo, xCoordinates, yCoordinates, times, pointerIds, int count = dictionary->getSuggestions(pInfo, xCoordinates, yCoordinates, times, pointerIds,
inputCodes, arraySize, prevWordChars, prevWordLength, commitPoint, isGesture, inputCodes, arraySize, prevWordChars, prevWordLength, commitPoint, isGesture,
dicTypeId, useFullEditDistance, (unsigned short*) outputChars, useFullEditDistance, (unsigned short*) outputChars,
frequencies, spaceIndices); frequencies, spaceIndices);
if (prevWordChars) { if (prevWordChars) {
env->ReleaseIntArrayElements(prevWordForBigrams, prevWordChars, JNI_ABORT); env->ReleaseIntArrayElements(prevWordForBigrams, prevWordChars, JNI_ABORT);
@ -260,7 +260,7 @@ void releaseDictBuf(void* dictBuf, const size_t length, int fd) {
static JNINativeMethod sMethods[] = { static JNINativeMethod sMethods[] = {
{"openNative", "(Ljava/lang/String;JJIIII)J", (void*)latinime_BinaryDictionary_open}, {"openNative", "(Ljava/lang/String;JJIIII)J", (void*)latinime_BinaryDictionary_open},
{"closeNative", "(J)V", (void*)latinime_BinaryDictionary_close}, {"closeNative", "(J)V", (void*)latinime_BinaryDictionary_close},
{"getSuggestionsNative", "(JJ[I[I[I[I[IIIZI[IZ[C[I[I)I", {"getSuggestionsNative", "(JJ[I[I[I[I[IIIZ[IZ[C[I[I)I",
(void*) latinime_BinaryDictionary_getSuggestions}, (void*) latinime_BinaryDictionary_getSuggestions},
{"getFrequencyNative", "(J[II)I", (void*)latinime_BinaryDictionary_getFrequency}, {"getFrequencyNative", "(J[II)I", (void*)latinime_BinaryDictionary_getFrequency},
{"isValidBigramNative", "(J[I[I)Z", (void*)latinime_BinaryDictionary_isValidBigram}, {"isValidBigramNative", "(J[I[I)Z", (void*)latinime_BinaryDictionary_isValidBigram},

View File

@ -36,14 +36,14 @@ class Dictionary {
int getSuggestions(ProximityInfo *proximityInfo, int *xcoordinates, int *ycoordinates, int getSuggestions(ProximityInfo *proximityInfo, int *xcoordinates, int *ycoordinates,
int *times, int *pointerIds, int *codes, int codesSize, int *prevWordChars, int *times, int *pointerIds, int *codes, int codesSize, int *prevWordChars,
int prevWordLength, int commitPoint, bool isGesture, int dicTypeId, int prevWordLength, int commitPoint, bool isGesture,
bool useFullEditDistance, unsigned short *outWords, bool useFullEditDistance, unsigned short *outWords,
int *frequencies, int *spaceIndices) { int *frequencies, int *spaceIndices) {
int result = 0; int result = 0;
if (isGesture) { if (isGesture) {
mGestureDecoder->setPrevWord(prevWordChars, prevWordLength); mGestureDecoder->setPrevWord(prevWordChars, prevWordLength);
result = mGestureDecoder->getSuggestions(proximityInfo, xcoordinates, ycoordinates, result = mGestureDecoder->getSuggestions(proximityInfo, xcoordinates, ycoordinates,
times, pointerIds, codes, codesSize, commitPoint, dicTypeId == 1 /* main */, times, pointerIds, codes, codesSize, commitPoint,
outWords, frequencies, spaceIndices); outWords, frequencies, spaceIndices);
} else { } else {
std::map<int, int> bigramMap; std::map<int, int> bigramMap;

View File

@ -30,7 +30,7 @@ class GestureDecoderImpl : public IncrementalDecoder {
} }
int getSuggestions(ProximityInfo *pInfo, int *inputXs, int *inputYs, int *times, int getSuggestions(ProximityInfo *pInfo, int *inputXs, int *inputYs, int *times,
int *pointerIds, int *codes, int inputSize, int commitPoint, bool isMainDict, int *pointerIds, int *codes, int inputSize, int commitPoint,
unsigned short *outWords, int *frequencies, int *outputIndices) { unsigned short *outWords, int *frequencies, int *outputIndices) {
return 0; return 0;
} }

View File

@ -28,7 +28,7 @@ class IncrementalDecoderInterface {
public: public:
virtual int getSuggestions(ProximityInfo *pInfo, int *inputXs, int *inputYs, int *times, virtual int getSuggestions(ProximityInfo *pInfo, int *inputXs, int *inputYs, int *times,
int *pointerIds, int *codes, int inputSize, int commitPoint, bool isMainDict, int *pointerIds, int *codes, int inputSize, int commitPoint,
unsigned short *outWords, int *frequencies, int *outputIndices) = 0; unsigned short *outWords, int *frequencies, int *outputIndices) = 0;
virtual void reset() = 0; virtual void reset() = 0;
virtual void setDict(const UnigramDictionary *dict, const BigramDictionary *bigram, virtual void setDict(const UnigramDictionary *dict, const BigramDictionary *bigram,