am 79e2e9fa: Merge "Add Dictionary.isInDictionary()."

* commit '79e2e9fa8c08ea1ad1b1e51f83bdd28fa88660dd':
  Add Dictionary.isInDictionary().
main
Keisuke Kuroyanagi 2014-06-09 02:42:30 +00:00 committed by Android Git Automerger
commit e3d55cc5b6
12 changed files with 38 additions and 49 deletions

View File

@ -345,7 +345,7 @@ public final class BinaryDictionary extends Dictionary {
}
@Override
public boolean isValidWord(final String word) {
public boolean isInDictionary(final String word) {
return getFrequency(word) != NOT_A_PROBABILITY;
}

View File

@ -16,6 +16,7 @@
package com.android.inputmethod.latin;
import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.keyboard.ProximityInfo;
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
@ -85,11 +86,19 @@ public abstract class Dictionary {
final int sessionId, final float[] inOutLanguageWeight);
/**
* Checks if the given word occurs in the dictionary
* Checks if the given word has to be treated as a valid word. Please note that some
* dictionaries have entries that should be treated as invalid words.
* @param word the word to search for. The search should be case-insensitive.
* @return true if the word exists, false otherwise
* @return true if the word is valid, false otherwise
*/
abstract public boolean isValidWord(final String word);
public boolean isValidWord(final String word) {
return isInDictionary(word);
}
/**
* Checks if the given word is in the dictionary regardless of it being valid or not.
*/
abstract public boolean isInDictionary(final String word);
public int getFrequency(final String word) {
return NOT_A_PROBABILITY;
@ -165,7 +174,7 @@ public abstract class Dictionary {
}
@Override
public boolean isValidWord(String word) {
public boolean isInDictionary(String word) {
return false;
}
}

View File

@ -78,9 +78,9 @@ public final class DictionaryCollection extends Dictionary {
}
@Override
public boolean isValidWord(final String word) {
public boolean isInDictionary(final String word) {
for (int i = mDictionaries.size() - 1; i >= 0; --i)
if (mDictionaries.get(i).isValidWord(word)) return true;
if (mDictionaries.get(i).isInDictionary(word)) return true;
return false;
}

View File

@ -414,7 +414,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
}
@Override
public boolean isValidWord(final String word) {
public boolean isInDictionary(final String word) {
reloadDictionaryIfRequired();
boolean lockAcquired = false;
try {
@ -424,10 +424,10 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
if (mBinaryDictionary == null) {
return false;
}
return isValidWordLocked(word);
return isInDictionaryLocked(word);
}
} catch (final InterruptedException e) {
Log.e(TAG, "Interrupted tryLock() in isValidWord().", e);
Log.e(TAG, "Interrupted tryLock() in isInDictionary().", e);
} finally {
if (lockAcquired) {
mLock.readLock().unlock();
@ -436,9 +436,9 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
return false;
}
protected boolean isValidWordLocked(final String word) {
protected boolean isInDictionaryLocked(final String word) {
if (mBinaryDictionary == null) return false;
return mBinaryDictionary.isValidWord(word);
return mBinaryDictionary.isInDictionary(word);
}
@Override
@ -590,20 +590,6 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
});
}
// TODO: Implement BinaryDictionary.isInDictionary().
@UsedForTesting
public boolean isInUnderlyingBinaryDictionaryForTests(final String word) {
mLock.readLock().lock();
try {
if (mBinaryDictionary != null && mDictType == Dictionary.TYPE_USER_HISTORY) {
return mBinaryDictionary.isValidWord(word);
}
return false;
} finally {
mLock.readLock().unlock();
}
}
@UsedForTesting
public void waitAllTasksForTests() {
final CountDownLatch countDownLatch = new CountDownLatch(1);

View File

@ -66,10 +66,10 @@ public final class ReadOnlyBinaryDictionary extends Dictionary {
}
@Override
public boolean isValidWord(final String word) {
public boolean isInDictionary(final String word) {
if (mLock.readLock().tryLock()) {
try {
return mBinaryDictionary.isValidWord(word);
return mBinaryDictionary.isInDictionary(word);
} finally {
mLock.readLock().unlock();
}

View File

@ -80,4 +80,10 @@ public abstract class DecayingExpandableBinaryDictionaryBase extends ExpandableB
/* package */ void runGCIfRequired() {
runGCIfRequired(false /* mindsBlockByGC */);
}
@Override
public boolean isValidWord(final String word) {
// Strings out of this dictionary should not be considered existing words.
return false;
}
}

View File

@ -38,10 +38,4 @@ public class PersonalizationDictionary extends DecayingExpandableBinaryDictionar
final Locale locale, final File dictFile) {
return PersonalizationHelper.getPersonalizationDictionary(context, locale);
}
@Override
public boolean isValidWord(final String word) {
// Strings out of this dictionary should not be considered existing words.
return false;
}
}

View File

@ -47,12 +47,6 @@ public class UserHistoryDictionary extends DecayingExpandableBinaryDictionaryBas
return PersonalizationHelper.getUserHistoryDictionary(context, locale);
}
@Override
public boolean isValidWord(final String word) {
// Strings out of this dictionary should not be considered existing words.
return false;
}
/**
* Add a word to the user history dictionary.
*

View File

@ -58,7 +58,7 @@ public final class DictionaryPool extends LinkedBlockingQueue<DictAndKeyboard> {
return noSuggestions;
}
@Override
public boolean isValidWord(final String word) {
public boolean isInDictionary(final String word) {
// This is never called. However if for some strange reason it ever gets
// called, returning true is less destructive (it will not underline the
// word in red).

View File

@ -47,9 +47,9 @@ public final class SynchronouslyLoadedContactsBinaryDictionary extends ContactsB
}
@Override
public boolean isValidWord(final String word) {
public boolean isInDictionary(final String word) {
synchronized (mLock) {
return super.isValidWord(word);
return super.isInDictionary(word);
}
}
}

View File

@ -52,9 +52,9 @@ public final class SynchronouslyLoadedUserBinaryDictionary extends UserBinaryDic
}
@Override
public boolean isValidWord(final String word) {
public boolean isInDictionary(final String word) {
synchronized (mLock) {
return super.isValidWord(word);
return super.isInDictionary(word);
}
}
}

View File

@ -134,7 +134,7 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
dict.waitAllTasksForTests();
for (int i = 0; i < numberOfWords; ++i) {
final String word = words.get(i);
assertTrue(dict.isInUnderlyingBinaryDictionaryForTests(word));
assertTrue(dict.isInDictionary(word));
}
}
// write to file.
@ -268,19 +268,19 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
DistracterFilter.EMPTY_DISTRACTER_FILTER);
prevWordsInfo = new PrevWordsInfo(word);
dict.waitAllTasksForTests();
assertTrue(dict.isInUnderlyingBinaryDictionaryForTests(word));
assertTrue(dict.isInDictionary(word));
}
forcePassingShortTime();
dict.runGCIfRequired();
dict.waitAllTasksForTests();
for (final String word : words) {
assertTrue(dict.isInUnderlyingBinaryDictionaryForTests(word));
assertTrue(dict.isInDictionary(word));
}
forcePassingLongTime();
dict.runGCIfRequired();
dict.waitAllTasksForTests();
for (final String word : words) {
assertFalse(dict.isInUnderlyingBinaryDictionaryForTests(word));
assertFalse(dict.isInDictionary(word));
}
}
}