Add getMaxFrequencyOfExactMatches() to Dictionary.

Bug: 13142176
Bug: 15428247
Change-Id: I93b44ef40cafb6b811c68fa79f3a4971fc3916a5
main
Keisuke Kuroyanagi 2014-06-05 19:05:58 +09:00
parent 9d4d61f9c1
commit d988fcc93d
4 changed files with 64 additions and 10 deletions

View File

@ -95,6 +95,10 @@ public abstract class Dictionary {
return NOT_A_PROBABILITY; return NOT_A_PROBABILITY;
} }
public int getMaxFrequencyOfExactMatches(final String word) {
return NOT_A_PROBABILITY;
}
/** /**
* Compares the contents of the character array with the typed word and returns true if they * Compares the contents of the character array with the typed word and returns true if they
* are the same. * are the same.

View File

@ -63,7 +63,7 @@ public class DictionaryFacilitator {
private final Object mLock = new Object(); private final Object mLock = new Object();
private final DistracterFilter mDistracterFilter; private final DistracterFilter mDistracterFilter;
private static final String[] DICT_TYPES_ORDERED_TO_GET_SUGGESTION = private static final String[] DICT_TYPES_ORDERED_TO_GET_SUGGESTIONS =
new String[] { new String[] {
Dictionary.TYPE_MAIN, Dictionary.TYPE_MAIN,
Dictionary.TYPE_USER_HISTORY, Dictionary.TYPE_USER_HISTORY,
@ -89,8 +89,8 @@ public class DictionaryFacilitator {
new Class[] { Context.class, Locale.class, File.class }; new Class[] { Context.class, Locale.class, File.class };
private static final String[] SUB_DICT_TYPES = private static final String[] SUB_DICT_TYPES =
Arrays.copyOfRange(DICT_TYPES_ORDERED_TO_GET_SUGGESTION, 1 /* start */, Arrays.copyOfRange(DICT_TYPES_ORDERED_TO_GET_SUGGESTIONS, 1 /* start */,
DICT_TYPES_ORDERED_TO_GET_SUGGESTION.length); DICT_TYPES_ORDERED_TO_GET_SUGGESTIONS.length);
/** /**
* Class contains dictionaries for a locale. * Class contains dictionaries for a locale.
@ -333,7 +333,7 @@ public class DictionaryFacilitator {
dictionaries = mDictionaries; dictionaries = mDictionaries;
mDictionaries = new Dictionaries(); mDictionaries = new Dictionaries();
} }
for (final String dictType : DICT_TYPES_ORDERED_TO_GET_SUGGESTION) { for (final String dictType : DICT_TYPES_ORDERED_TO_GET_SUGGESTIONS) {
dictionaries.closeDict(dictType); dictionaries.closeDict(dictType);
} }
mDistracterFilter.close(); mDistracterFilter.close();
@ -469,7 +469,7 @@ public class DictionaryFacilitator {
final SuggestionResults suggestionResults = final SuggestionResults suggestionResults =
new SuggestionResults(dictionaries.mLocale, SuggestedWords.MAX_SUGGESTIONS); new SuggestionResults(dictionaries.mLocale, SuggestedWords.MAX_SUGGESTIONS);
final float[] languageWeight = new float[] { Dictionary.NOT_A_LANGUAGE_WEIGHT }; final float[] languageWeight = new float[] { Dictionary.NOT_A_LANGUAGE_WEIGHT };
for (final String dictType : DICT_TYPES_ORDERED_TO_GET_SUGGESTION) { for (final String dictType : DICT_TYPES_ORDERED_TO_GET_SUGGESTIONS) {
final Dictionary dictionary = dictionaries.getDict(dictType); final Dictionary dictionary = dictionaries.getDict(dictType);
if (null == dictionary) continue; if (null == dictionary) continue;
final ArrayList<SuggestedWordInfo> dictionarySuggestions = final ArrayList<SuggestedWordInfo> dictionarySuggestions =
@ -502,7 +502,7 @@ public class DictionaryFacilitator {
return false; return false;
} }
final String lowerCasedWord = word.toLowerCase(dictionaries.mLocale); final String lowerCasedWord = word.toLowerCase(dictionaries.mLocale);
for (final String dictType : DICT_TYPES_ORDERED_TO_GET_SUGGESTION) { for (final String dictType : DICT_TYPES_ORDERED_TO_GET_SUGGESTIONS) {
final Dictionary dictionary = dictionaries.getDict(dictType); final Dictionary dictionary = dictionaries.getDict(dictType);
// Ideally the passed map would come out of a {@link java.util.concurrent.Future} and // Ideally the passed map would come out of a {@link java.util.concurrent.Future} and
// would be immutable once it's finished initializing, but concretely a null test is // would be immutable once it's finished initializing, but concretely a null test is
@ -516,16 +516,22 @@ public class DictionaryFacilitator {
return false; return false;
} }
public int getFrequency(final String word) { private int getFrequencyInternal(final String word,
final boolean isGettingMaxFrequencyOfExactMatches) {
if (TextUtils.isEmpty(word)) { if (TextUtils.isEmpty(word)) {
return Dictionary.NOT_A_PROBABILITY; return Dictionary.NOT_A_PROBABILITY;
} }
int maxFreq = -1; int maxFreq = Dictionary.NOT_A_PROBABILITY;
final Dictionaries dictionaries = mDictionaries; final Dictionaries dictionaries = mDictionaries;
for (final String dictType : DICT_TYPES_ORDERED_TO_GET_SUGGESTION) { for (final String dictType : DICT_TYPES_ORDERED_TO_GET_SUGGESTIONS) {
final Dictionary dictionary = dictionaries.getDict(dictType); final Dictionary dictionary = dictionaries.getDict(dictType);
if (dictionary == null) continue; if (dictionary == null) continue;
final int tempFreq = dictionary.getFrequency(word); final int tempFreq;
if (isGettingMaxFrequencyOfExactMatches) {
tempFreq = dictionary.getMaxFrequencyOfExactMatches(word);
} else {
tempFreq = dictionary.getFrequency(word);
}
if (tempFreq >= maxFreq) { if (tempFreq >= maxFreq) {
maxFreq = tempFreq; maxFreq = tempFreq;
} }
@ -533,6 +539,14 @@ public class DictionaryFacilitator {
return maxFreq; return maxFreq;
} }
public int getFrequency(final String word) {
return getFrequencyInternal(word, false /* isGettingMaxFrequencyOfExactMatches */);
}
public int getMaxFrequencyOfExactMatches(final String word) {
return getFrequencyInternal(word, true /* isGettingMaxFrequencyOfExactMatches */);
}
public void clearUserHistoryDictionary() { public void clearUserHistoryDictionary() {
final ExpandableBinaryDictionary userHistoryDict = final ExpandableBinaryDictionary userHistoryDict =
mDictionaries.getSubDict(Dictionary.TYPE_USER_HISTORY); mDictionaries.getSubDict(Dictionary.TYPE_USER_HISTORY);

View File

@ -441,6 +441,30 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
return mBinaryDictionary.isValidWord(word); return mBinaryDictionary.isValidWord(word);
} }
@Override
public int getMaxFrequencyOfExactMatches(final String word) {
reloadDictionaryIfRequired();
boolean lockAcquired = false;
try {
lockAcquired = mLock.readLock().tryLock(
TIMEOUT_FOR_READ_OPS_IN_MILLISECONDS, TimeUnit.MILLISECONDS);
if (lockAcquired) {
if (mBinaryDictionary == null) {
return NOT_A_PROBABILITY;
}
return mBinaryDictionary.getMaxFrequencyOfExactMatches(word);
}
} catch (final InterruptedException e) {
Log.e(TAG, "Interrupted tryLock() in getMaxFrequencyOfExactMatches().", e);
} finally {
if (lockAcquired) {
mLock.readLock().unlock();
}
}
return NOT_A_PROBABILITY;
}
protected boolean isValidNgramLocked(final PrevWordsInfo prevWordsInfo, final String word) { protected boolean isValidNgramLocked(final PrevWordsInfo prevWordsInfo, final String word) {
if (mBinaryDictionary == null) return false; if (mBinaryDictionary == null) return false;
return mBinaryDictionary.isValidNgram(prevWordsInfo, word); return mBinaryDictionary.isValidNgram(prevWordsInfo, word);

View File

@ -101,6 +101,18 @@ public final class ReadOnlyBinaryDictionary extends Dictionary {
return NOT_A_PROBABILITY; return NOT_A_PROBABILITY;
} }
@Override
public int getMaxFrequencyOfExactMatches(final String word) {
if (mLock.readLock().tryLock()) {
try {
return mBinaryDictionary.getMaxFrequencyOfExactMatches(word);
} finally {
mLock.readLock().unlock();
}
}
return NOT_A_PROBABILITY;
}
@Override @Override
public void close() { public void close() {
mLock.writeLock().lock(); mLock.writeLock().lock();