Make UserHistoryDictionary use updateEntriesForWordWithNgramContext()

Bug: 14425059
Change-Id: I409ebf90f68f4a0f1638128ac5ee6af859323d30
main
Keisuke Kuroyanagi 2014-10-06 15:50:38 +09:00
parent a9089f9a57
commit 9290d0a4eb
4 changed files with 52 additions and 49 deletions

View File

@ -42,6 +42,8 @@ import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import javax.annotation.Nonnull;
/** /**
* Implements a static, compacted, binary dictionary of standard words. * Implements a static, compacted, binary dictionary of standard words.
*/ */
@ -495,8 +497,7 @@ public final class BinaryDictionary extends Dictionary {
} }
// Update entries for the word occurrence with the ngramContext. // Update entries for the word occurrence with the ngramContext.
@UsedForTesting public boolean updateEntriesForWordWithNgramContext(@Nonnull final NgramContext ngramContext,
public boolean updateEntriesForWordWithNgramContext(final NgramContext ngramContext,
final String word, final boolean isValidWord, final int count, final int timestamp) { final String word, final boolean isValidWord, final int count, final int timestamp) {
if (TextUtils.isEmpty(word)) { if (TextUtils.isEmpty(word)) {
return false; return false;

View File

@ -46,6 +46,8 @@ import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock;
import javax.annotation.Nonnull;
/** /**
* Abstract base class for an expandable dictionary that can be created and updated dynamically * Abstract base class for an expandable dictionary that can be created and updated dynamically
* during runtime. When updated it automatically generates a new binary dictionary to handle future * during runtime. When updated it automatically generates a new binary dictionary to handle future
@ -292,13 +294,9 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
} }
} }
/** private void updateDictionaryWithWriteLockIfWordIsNotADistracter(
* Adds unigram information of a word to the dictionary. May overwrite an existing entry. @Nonnull final Runnable updateTask,
*/ @Nonnull final String word, @Nonnull final DistracterFilter distracterFilter) {
public void addUnigramEntryWithCheckingDistracter(final String word, final int frequency,
final String shortcutTarget, final int shortcutFreq, final boolean isNotAWord,
final boolean isBlacklisted, final int timestamp,
final DistracterFilter distracterFilter) {
reloadDictionaryIfRequired(); reloadDictionaryIfRequired();
asyncPreCheckAndExecuteTaskWithWriteLock( asyncPreCheckAndExecuteTaskWithWriteLock(
new Callable<Boolean>() { new Callable<Boolean>() {
@ -315,10 +313,25 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
return; return;
} }
runGCIfRequiredLocked(true /* mindsBlockByGC */); runGCIfRequiredLocked(true /* mindsBlockByGC */);
updateTask.run();
}
});
}
/**
* Adds unigram information of a word to the dictionary. May overwrite an existing entry.
*/
public void addUnigramEntryWithCheckingDistracter(final String word, final int frequency,
final String shortcutTarget, final int shortcutFreq, final boolean isNotAWord,
final boolean isBlacklisted, final int timestamp,
@Nonnull final DistracterFilter distracterFilter) {
updateDictionaryWithWriteLockIfWordIsNotADistracter(new Runnable() {
@Override
public void run() {
addUnigramLocked(word, frequency, shortcutTarget, shortcutFreq, addUnigramLocked(word, frequency, shortcutTarget, shortcutFreq,
isNotAWord, isBlacklisted, timestamp); isNotAWord, isBlacklisted, timestamp);
} }
}); }, word, distracterFilter);
} }
protected void addUnigramLocked(final String word, final int frequency, protected void addUnigramLocked(final String word, final int frequency,
@ -354,7 +367,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
/** /**
* Adds n-gram information of a word to the dictionary. May overwrite an existing entry. * Adds n-gram information of a word to the dictionary. May overwrite an existing entry.
*/ */
public void addNgramEntry(final NgramContext ngramContext, final String word, public void addNgramEntry(@Nonnull final NgramContext ngramContext, final String word,
final int frequency, final int timestamp) { final int frequency, final int timestamp) {
reloadDictionaryIfRequired(); reloadDictionaryIfRequired();
asyncExecuteTaskWithWriteLock(new Runnable() { asyncExecuteTaskWithWriteLock(new Runnable() {
@ -369,7 +382,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
}); });
} }
protected void addNgramEntryLocked(final NgramContext ngramContext, final String word, protected void addNgramEntryLocked(@Nonnull final NgramContext ngramContext, final String word,
final int frequency, final int timestamp) { final int frequency, final int timestamp) {
if (!mBinaryDictionary.addNgramEntry(ngramContext, word, frequency, timestamp)) { if (!mBinaryDictionary.addNgramEntry(ngramContext, word, frequency, timestamp)) {
if (DEBUG) { if (DEBUG) {
@ -383,7 +396,8 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
* Dynamically remove the n-gram entry in the dictionary. * Dynamically remove the n-gram entry in the dictionary.
*/ */
@UsedForTesting @UsedForTesting
public void removeNgramDynamically(final NgramContext ngramContext, final String word) { public void removeNgramDynamically(@Nonnull final NgramContext ngramContext,
final String word) {
reloadDictionaryIfRequired(); reloadDictionaryIfRequired();
asyncExecuteTaskWithWriteLock(new Runnable() { asyncExecuteTaskWithWriteLock(new Runnable() {
@Override @Override
@ -402,6 +416,26 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
}); });
} }
/**
* Update dictionary for the word with the ngramContext if the word is not a distracter.
*/
public void updateEntriesForWordWithCheckingDistracter(@Nonnull final NgramContext ngramContext,
final String word, final boolean isValidWord, final int count, final int timestamp,
@Nonnull final DistracterFilter distracterFilter) {
updateDictionaryWithWriteLockIfWordIsNotADistracter(new Runnable() {
@Override
public void run() {
if (!mBinaryDictionary.updateEntriesForWordWithNgramContext(ngramContext, word,
isValidWord, count, timestamp)) {
if (DEBUG) {
Log.e(TAG, "Cannot update counter. word: " + word
+ " context: "+ ngramContext.toString());
}
}
}
}, word, distracterFilter);
}
public interface AddMultipleDictionaryEntriesCallback { public interface AddMultipleDictionaryEntriesCallback {
public void onFinished(); public void onFinished();
} }
@ -410,7 +444,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
* Dynamically add multiple entries to the dictionary. * Dynamically add multiple entries to the dictionary.
*/ */
public void addMultipleDictionaryEntriesDynamically( public void addMultipleDictionaryEntriesDynamically(
final ArrayList<LanguageModelParam> languageModelParams, @Nonnull final ArrayList<LanguageModelParam> languageModelParams,
final AddMultipleDictionaryEntriesCallback callback) { final AddMultipleDictionaryEntriesCallback callback) {
reloadDictionaryIfRequired(); reloadDictionaryIfRequired();
asyncExecuteTaskWithWriteLock(new Runnable() { asyncExecuteTaskWithWriteLock(new Runnable() {

View File

@ -158,11 +158,6 @@ public class NgramContext {
} }
} }
public NgramContext getTrimmedNgramContext(final int maxPrevWordCount) {
final int newSize = Math.min(maxPrevWordCount, mPrevWordsCount);
return new NgramContext(this /* prevWordsInfo */, newSize);
}
public int getPrevWordCount() { public int getPrevWordCount() {
return mPrevWordsCount; return mPrevWordsCount;
} }

View File

@ -65,34 +65,7 @@ public class UserHistoryDictionary extends DecayingExpandableBinaryDictionaryBas
if (word.length() > Constants.DICTIONARY_MAX_WORD_LENGTH) { if (word.length() > Constants.DICTIONARY_MAX_WORD_LENGTH) {
return; return;
} }
final int frequency = isValid ? userHistoryDictionary.updateEntriesForWordWithCheckingDistracter(ngramContext, word,
FREQUENCY_FOR_WORDS_IN_DICTS : FREQUENCY_FOR_WORDS_NOT_IN_DICTS; isValid, 1 /* count */, timestamp, distracterFilter);
userHistoryDictionary.addUnigramEntryWithCheckingDistracter(word, frequency,
null /* shortcutTarget */, 0 /* shortcutFreq */, false /* isNotAWord */,
false /* isBlacklisted */, timestamp, distracterFilter);
final boolean isBeginningOfSentenceContext = ngramContext.isBeginningOfSentenceContext();
final NgramContext ngramContextToBeSaved =
ngramContext.getTrimmedNgramContext(SUPPORTED_NGRAM - 1);
for (int i = 0; i < ngramContextToBeSaved.getPrevWordCount(); i++) {
final CharSequence prevWord = ngramContextToBeSaved.getNthPrevWord(1 /* n */);
if (prevWord == null || (prevWord.length() > Constants.DICTIONARY_MAX_WORD_LENGTH)) {
return;
}
// Do not insert a word as a bigram of itself
if (i == 0 && TextUtils.equals(word, prevWord)) {
return;
}
if (isBeginningOfSentenceContext) {
// Beginning-of-Sentence n-gram entry is added as an n-gram entry of an OOV word.
userHistoryDictionary.addNgramEntry(
ngramContextToBeSaved.getTrimmedNgramContext(i + 1), word,
FREQUENCY_FOR_WORDS_NOT_IN_DICTS, timestamp);
} else {
userHistoryDictionary.addNgramEntry(
ngramContextToBeSaved.getTrimmedNgramContext(i + 1), word, frequency,
timestamp);
}
}
} }
} }