Merge "Change the return type of getWords and getBigrams (A8)"

main
Jean Chalard 2012-06-26 01:33:04 -07:00 committed by Android (Google) Code Review
commit 90981e423a
8 changed files with 86 additions and 45 deletions

View File

@ -107,9 +107,9 @@ public class BinaryDictionary extends Dictionary {
} }
@Override @Override
public void getBigrams(final WordComposer codes, final CharSequence previousWord, public ArrayList<SuggestedWordInfo> getBigrams(final WordComposer codes,
final WordCallback callback) { final CharSequence previousWord, final WordCallback callback) {
if (mNativeDict == 0) return; if (mNativeDict == 0) return null;
int[] codePoints = StringUtils.toCodePointArray(previousWord.toString()); int[] codePoints = StringUtils.toCodePointArray(previousWord.toString());
Arrays.fill(mOutputChars_bigrams, (char) 0); Arrays.fill(mOutputChars_bigrams, (char) 0);
@ -142,12 +142,14 @@ public class BinaryDictionary extends Dictionary {
} }
} }
Utils.addAllSuggestions(mDicTypeId, Dictionary.BIGRAM, suggestions, callback); Utils.addAllSuggestions(mDicTypeId, Dictionary.BIGRAM, suggestions, callback);
return suggestions;
} }
// proximityInfo and/or prevWordForBigrams may not be null. // proximityInfo and/or prevWordForBigrams may not be null.
@Override @Override
public void getWords(final WordComposer codes, final CharSequence prevWordForBigrams, public ArrayList<SuggestedWordInfo> getWords(final WordComposer codes,
final WordCallback callback, final ProximityInfo proximityInfo) { final CharSequence prevWordForBigrams, final WordCallback callback,
final ProximityInfo proximityInfo) {
final int count = getSuggestions(codes, prevWordForBigrams, proximityInfo, mOutputChars, final int count = getSuggestions(codes, prevWordForBigrams, proximityInfo, mOutputChars,
mScores, mSpaceIndices); mScores, mSpaceIndices);
@ -167,6 +169,7 @@ public class BinaryDictionary extends Dictionary {
} }
} }
Utils.addAllSuggestions(mDicTypeId, Dictionary.UNIGRAM, suggestions, callback); Utils.addAllSuggestions(mDicTypeId, Dictionary.UNIGRAM, suggestions, callback);
return suggestions;
} }
/* package for test */ boolean isValidDictionary() { /* package for test */ boolean isValidDictionary() {

View File

@ -120,12 +120,6 @@ public class ContactsBinaryDictionary extends ExpandableBinaryDictionary {
} }
} }
@Override
public void getBigrams(final WordComposer codes, final CharSequence previousWord,
final WordCallback callback) {
super.getBigrams(codes, previousWord, callback);
}
private boolean useFirstLastBigramsForLocale(Locale locale) { private boolean useFirstLastBigramsForLocale(Locale locale) {
// TODO: Add firstname/lastname bigram rules for other languages. // TODO: Add firstname/lastname bigram rules for other languages.
if (locale != null && locale.getLanguage().equals(Locale.ENGLISH.getLanguage())) { if (locale != null && locale.getLanguage().equals(Locale.ENGLISH.getLanguage())) {

View File

@ -17,6 +17,9 @@
package com.android.inputmethod.latin; package com.android.inputmethod.latin;
import com.android.inputmethod.keyboard.ProximityInfo; import com.android.inputmethod.keyboard.ProximityInfo;
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
import java.util.ArrayList;
/** /**
* Abstract base class for a dictionary that can do a fuzzy search for words based on a set of key * Abstract base class for a dictionary that can do a fuzzy search for words based on a set of key
@ -61,9 +64,10 @@ public abstract class Dictionary {
* @param prevWordForBigrams the previous word, or null if none * @param prevWordForBigrams the previous word, or null if none
* @param callback the callback object to send matched words to as possible candidates * @param callback the callback object to send matched words to as possible candidates
* @param proximityInfo the object for key proximity. May be ignored by some implementations. * @param proximityInfo the object for key proximity. May be ignored by some implementations.
* @return the list of suggestions
* @see WordCallback#addWord(char[], int, int, int, int, int) * @see WordCallback#addWord(char[], int, int, int, int, int)
*/ */
abstract public void getWords(final WordComposer composer, abstract public ArrayList<SuggestedWordInfo> getWords(final WordComposer composer,
final CharSequence prevWordForBigrams, final WordCallback callback, final CharSequence prevWordForBigrams, final WordCallback callback,
final ProximityInfo proximityInfo); final ProximityInfo proximityInfo);
@ -73,11 +77,10 @@ public abstract class Dictionary {
* @param composer the key sequence to match * @param composer the key sequence to match
* @param previousWord the word before * @param previousWord the word before
* @param callback the callback object to send possible word following previous word * @param callback the callback object to send possible word following previous word
* @return the list of suggestions
*/ */
public void getBigrams(final WordComposer composer, final CharSequence previousWord, public abstract ArrayList<SuggestedWordInfo> getBigrams(final WordComposer composer,
final WordCallback callback) { final CharSequence previousWord, final WordCallback callback);
// empty base implementation
}
/** /**
* Checks if the given word occurs in the dictionary * Checks if the given word occurs in the dictionary

View File

@ -17,9 +17,11 @@
package com.android.inputmethod.latin; package com.android.inputmethod.latin;
import com.android.inputmethod.keyboard.ProximityInfo; import com.android.inputmethod.keyboard.ProximityInfo;
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
import android.util.Log; import android.util.Log;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
@ -50,17 +52,42 @@ public class DictionaryCollection extends Dictionary {
} }
@Override @Override
public void getWords(final WordComposer composer, final CharSequence prevWordForBigrams, public ArrayList<SuggestedWordInfo> getWords(final WordComposer composer,
final WordCallback callback, final ProximityInfo proximityInfo) { final CharSequence prevWordForBigrams, final WordCallback callback,
for (final Dictionary dict : mDictionaries) final ProximityInfo proximityInfo) {
dict.getWords(composer, prevWordForBigrams, callback, proximityInfo); final CopyOnWriteArrayList<Dictionary> dictionaries = mDictionaries;
if (dictionaries.isEmpty()) return null;
// To avoid creating unnecessary objects, we get the list out of the first
// dictionary and add the rest to it if not null, hence the get(0)
ArrayList<SuggestedWordInfo> suggestions = dictionaries.get(0).getWords(composer,
prevWordForBigrams, callback, proximityInfo);
if (null == suggestions) suggestions = new ArrayList<SuggestedWordInfo>();
final int length = dictionaries.size();
for (int i = 0; i < length; ++ i) {
final ArrayList<SuggestedWordInfo> sugg = dictionaries.get(i).getWords(composer,
prevWordForBigrams, callback, proximityInfo);
if (null != sugg) suggestions.addAll(sugg);
}
return suggestions;
} }
@Override @Override
public void getBigrams(final WordComposer composer, final CharSequence previousWord, public ArrayList<SuggestedWordInfo> getBigrams(final WordComposer composer,
final WordCallback callback) { final CharSequence previousWord, final WordCallback callback) {
for (final Dictionary dict : mDictionaries) final CopyOnWriteArrayList<Dictionary> dictionaries = mDictionaries;
dict.getBigrams(composer, previousWord, callback); if (dictionaries.isEmpty()) return null;
// To avoid creating unnecessary objects, we get the list out of the first
// dictionary and add the rest to it if not null, hence the get(0)
ArrayList<SuggestedWordInfo> suggestions = dictionaries.get(0).getBigrams(composer,
previousWord, callback);
if (null == suggestions) suggestions = new ArrayList<SuggestedWordInfo>();
final int length = dictionaries.size();
for (int i = 0; i < length; ++ i) {
final ArrayList<SuggestedWordInfo> sugg =
dictionaries.get(i).getBigrams(composer, previousWord, callback);
if (null != sugg) suggestions.addAll(sugg);
}
return suggestions;
} }
@Override @Override

View File

@ -19,6 +19,7 @@ import android.os.SystemClock;
import android.util.Log; import android.util.Log;
import com.android.inputmethod.keyboard.ProximityInfo; import com.android.inputmethod.keyboard.ProximityInfo;
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
import com.android.inputmethod.latin.makedict.BinaryDictInputOutput; import com.android.inputmethod.latin.makedict.BinaryDictInputOutput;
import com.android.inputmethod.latin.makedict.FusionDictionary; import com.android.inputmethod.latin.makedict.FusionDictionary;
import com.android.inputmethod.latin.makedict.FusionDictionary.Node; import com.android.inputmethod.latin.makedict.FusionDictionary.Node;
@ -194,13 +195,14 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
} }
@Override @Override
public void getWords(final WordComposer codes, final CharSequence prevWordForBigrams, public ArrayList<SuggestedWordInfo> getWords(final WordComposer codes,
final WordCallback callback, final ProximityInfo proximityInfo) { final CharSequence prevWordForBigrams, final WordCallback callback,
final ProximityInfo proximityInfo) {
asyncReloadDictionaryIfRequired(); asyncReloadDictionaryIfRequired();
getWordsInner(codes, prevWordForBigrams, callback, proximityInfo); return getWordsInner(codes, prevWordForBigrams, callback, proximityInfo);
} }
protected final void getWordsInner(final WordComposer codes, protected final ArrayList<SuggestedWordInfo> getWordsInner(final WordComposer codes,
final CharSequence prevWordForBigrams, final WordCallback callback, final CharSequence prevWordForBigrams, final WordCallback callback,
final ProximityInfo proximityInfo) { final ProximityInfo proximityInfo) {
// Ensure that there are no concurrent calls to getWords. If there are, do nothing and // Ensure that there are no concurrent calls to getWords. If there are, do nothing and
@ -208,32 +210,35 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
if (mLocalDictionaryController.tryLock()) { if (mLocalDictionaryController.tryLock()) {
try { try {
if (mBinaryDictionary != null) { if (mBinaryDictionary != null) {
mBinaryDictionary.getWords(codes, prevWordForBigrams, callback, proximityInfo); return mBinaryDictionary.getWords(codes, prevWordForBigrams, callback,
proximityInfo);
} }
} finally { } finally {
mLocalDictionaryController.unlock(); mLocalDictionaryController.unlock();
} }
} }
return null;
} }
@Override @Override
public void getBigrams(final WordComposer codes, final CharSequence previousWord, public ArrayList<SuggestedWordInfo> getBigrams(final WordComposer codes,
final WordCallback callback) { final CharSequence previousWord, final WordCallback callback) {
asyncReloadDictionaryIfRequired(); asyncReloadDictionaryIfRequired();
getBigramsInner(codes, previousWord, callback); return getBigramsInner(codes, previousWord, callback);
} }
protected void getBigramsInner(final WordComposer codes, final CharSequence previousWord, protected ArrayList<SuggestedWordInfo> getBigramsInner(final WordComposer codes,
final WordCallback callback) { final CharSequence previousWord, final WordCallback callback) {
if (mLocalDictionaryController.tryLock()) { if (mLocalDictionaryController.tryLock()) {
try { try {
if (mBinaryDictionary != null) { if (mBinaryDictionary != null) {
mBinaryDictionary.getBigrams(codes, previousWord, callback); return mBinaryDictionary.getBigrams(codes, previousWord, callback);
} }
} finally { } finally {
mLocalDictionaryController.unlock(); mLocalDictionaryController.unlock();
} }
} }
return null;
} }
@Override @Override

View File

@ -248,20 +248,22 @@ public class ExpandableDictionary extends Dictionary {
} }
@Override @Override
public void getWords(final WordComposer codes, final CharSequence prevWordForBigrams, public ArrayList<SuggestedWordInfo> getWords(final WordComposer codes,
final WordCallback callback, final ProximityInfo proximityInfo) { final CharSequence prevWordForBigrams, final WordCallback callback,
final ProximityInfo proximityInfo) {
synchronized (mUpdatingLock) { synchronized (mUpdatingLock) {
// If we need to update, start off a background task // If we need to update, start off a background task
if (mRequiresReload) startDictionaryLoadingTaskLocked(); if (mRequiresReload) startDictionaryLoadingTaskLocked();
// Currently updating contacts, don't return any results. // Currently updating contacts, don't return any results.
if (mUpdatingDictionary) return; if (mUpdatingDictionary) return null;
} }
if (codes.size() >= BinaryDictionary.MAX_WORD_LENGTH) { if (codes.size() >= BinaryDictionary.MAX_WORD_LENGTH) {
return; return null;
} }
final ArrayList<SuggestedWordInfo> suggestions = final ArrayList<SuggestedWordInfo> suggestions =
getWordsInner(codes, prevWordForBigrams, proximityInfo); getWordsInner(codes, prevWordForBigrams, proximityInfo);
Utils.addAllSuggestions(mDicTypeId, Dictionary.UNIGRAM, suggestions, callback); Utils.addAllSuggestions(mDicTypeId, Dictionary.UNIGRAM, suggestions, callback);
return suggestions;
} }
protected final ArrayList<SuggestedWordInfo> getWordsInner(final WordComposer codes, protected final ArrayList<SuggestedWordInfo> getWordsInner(final WordComposer codes,
@ -611,13 +613,15 @@ public class ExpandableDictionary extends Dictionary {
} }
@Override @Override
public void getBigrams(final WordComposer codes, final CharSequence previousWord, public ArrayList<SuggestedWordInfo> getBigrams(final WordComposer codes,
final WordCallback callback) { final CharSequence previousWord, final WordCallback callback) {
if (!reloadDictionaryIfRequired()) { if (!reloadDictionaryIfRequired()) {
final ArrayList<SuggestedWordInfo> suggestions = new ArrayList<SuggestedWordInfo>(); final ArrayList<SuggestedWordInfo> suggestions = new ArrayList<SuggestedWordInfo>();
runBigramReverseLookUp(previousWord, suggestions); runBigramReverseLookUp(previousWord, suggestions);
Utils.addAllSuggestions(mDicTypeId, Dictionary.BIGRAM, suggestions, callback); Utils.addAllSuggestions(mDicTypeId, Dictionary.BIGRAM, suggestions, callback);
return suggestions;
} }
return null;
} }
/** /**

View File

@ -19,7 +19,9 @@ package com.android.inputmethod.latin;
import android.content.Context; import android.content.Context;
import com.android.inputmethod.keyboard.ProximityInfo; import com.android.inputmethod.keyboard.ProximityInfo;
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
import java.util.ArrayList;
import java.util.Locale; import java.util.Locale;
public class SynchronouslyLoadedContactsBinaryDictionary extends ContactsBinaryDictionary { public class SynchronouslyLoadedContactsBinaryDictionary extends ContactsBinaryDictionary {
@ -30,11 +32,11 @@ public class SynchronouslyLoadedContactsBinaryDictionary extends ContactsBinaryD
} }
@Override @Override
public synchronized void getWords(final WordComposer codes, public synchronized ArrayList<SuggestedWordInfo> getWords(final WordComposer codes,
final CharSequence prevWordForBigrams, final WordCallback callback, final CharSequence prevWordForBigrams, final WordCallback callback,
final ProximityInfo proximityInfo) { final ProximityInfo proximityInfo) {
syncReloadDictionaryIfRequired(); syncReloadDictionaryIfRequired();
getWordsInner(codes, prevWordForBigrams, callback, proximityInfo); return getWordsInner(codes, prevWordForBigrams, callback, proximityInfo);
} }
@Override @Override

View File

@ -19,6 +19,9 @@ package com.android.inputmethod.latin;
import android.content.Context; import android.content.Context;
import com.android.inputmethod.keyboard.ProximityInfo; import com.android.inputmethod.keyboard.ProximityInfo;
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
import java.util.ArrayList;
public class SynchronouslyLoadedUserBinaryDictionary extends UserBinaryDictionary { public class SynchronouslyLoadedUserBinaryDictionary extends UserBinaryDictionary {
@ -32,11 +35,11 @@ public class SynchronouslyLoadedUserBinaryDictionary extends UserBinaryDictionar
} }
@Override @Override
public synchronized void getWords(final WordComposer codes, public synchronized ArrayList<SuggestedWordInfo> getWords(final WordComposer codes,
final CharSequence prevWordForBigrams, final WordCallback callback, final CharSequence prevWordForBigrams, final WordCallback callback,
final ProximityInfo proximityInfo) { final ProximityInfo proximityInfo) {
syncReloadDictionaryIfRequired(); syncReloadDictionaryIfRequired();
getWordsInner(codes, prevWordForBigrams, callback, proximityInfo); return getWordsInner(codes, prevWordForBigrams, callback, proximityInfo);
} }
@Override @Override