am 1616d6d4: Merge "Make UserHistoryDictionary use updateEntriesForWordWithNgramContext()"
* commit '1616d6d44098ce9fd8c65383af21c88799927b17': Make UserHistoryDictionary use updateEntriesForWordWithNgramContext()main
commit
00da9ac5b4
|
@ -42,6 +42,8 @@ import java.util.HashMap;
|
|||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
@UsedForTesting
|
||||
public boolean updateEntriesForWordWithNgramContext(final NgramContext ngramContext,
|
||||
public boolean updateEntriesForWordWithNgramContext(@Nonnull final NgramContext ngramContext,
|
||||
final String word, final boolean isValidWord, final int count, final int timestamp) {
|
||||
if (TextUtils.isEmpty(word)) {
|
||||
return false;
|
||||
|
|
|
@ -46,6 +46,8 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
|||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
@ -292,13 +294,9 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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,
|
||||
final DistracterFilter distracterFilter) {
|
||||
private void updateDictionaryWithWriteLockIfWordIsNotADistracter(
|
||||
@Nonnull final Runnable updateTask,
|
||||
@Nonnull final String word, @Nonnull final DistracterFilter distracterFilter) {
|
||||
reloadDictionaryIfRequired();
|
||||
asyncPreCheckAndExecuteTaskWithWriteLock(
|
||||
new Callable<Boolean>() {
|
||||
|
@ -315,12 +313,27 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
|
|||
return;
|
||||
}
|
||||
runGCIfRequiredLocked(true /* mindsBlockByGC */);
|
||||
addUnigramLocked(word, frequency, shortcutTarget, shortcutFreq,
|
||||
isNotAWord, isBlacklisted, timestamp);
|
||||
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,
|
||||
isNotAWord, isBlacklisted, timestamp);
|
||||
}
|
||||
}, word, distracterFilter);
|
||||
}
|
||||
|
||||
protected void addUnigramLocked(final String word, final int frequency,
|
||||
final String shortcutTarget, final int shortcutFreq, final boolean isNotAWord,
|
||||
final boolean isBlacklisted, final int timestamp) {
|
||||
|
@ -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.
|
||||
*/
|
||||
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) {
|
||||
reloadDictionaryIfRequired();
|
||||
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) {
|
||||
if (!mBinaryDictionary.addNgramEntry(ngramContext, word, frequency, timestamp)) {
|
||||
if (DEBUG) {
|
||||
|
@ -383,7 +396,8 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
|
|||
* Dynamically remove the n-gram entry in the dictionary.
|
||||
*/
|
||||
@UsedForTesting
|
||||
public void removeNgramDynamically(final NgramContext ngramContext, final String word) {
|
||||
public void removeNgramDynamically(@Nonnull final NgramContext ngramContext,
|
||||
final String word) {
|
||||
reloadDictionaryIfRequired();
|
||||
asyncExecuteTaskWithWriteLock(new Runnable() {
|
||||
@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 void onFinished();
|
||||
}
|
||||
|
@ -410,7 +444,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
|
|||
* Dynamically add multiple entries to the dictionary.
|
||||
*/
|
||||
public void addMultipleDictionaryEntriesDynamically(
|
||||
final ArrayList<LanguageModelParam> languageModelParams,
|
||||
@Nonnull final ArrayList<LanguageModelParam> languageModelParams,
|
||||
final AddMultipleDictionaryEntriesCallback callback) {
|
||||
reloadDictionaryIfRequired();
|
||||
asyncExecuteTaskWithWriteLock(new Runnable() {
|
||||
|
|
|
@ -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() {
|
||||
return mPrevWordsCount;
|
||||
}
|
||||
|
|
|
@ -65,34 +65,7 @@ public class UserHistoryDictionary extends DecayingExpandableBinaryDictionaryBas
|
|||
if (word.length() > Constants.DICTIONARY_MAX_WORD_LENGTH) {
|
||||
return;
|
||||
}
|
||||
final int frequency = isValid ?
|
||||
FREQUENCY_FOR_WORDS_IN_DICTS : FREQUENCY_FOR_WORDS_NOT_IN_DICTS;
|
||||
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);
|
||||
}
|
||||
}
|
||||
userHistoryDictionary.updateEntriesForWordWithCheckingDistracter(ngramContext, word,
|
||||
isValid, 1 /* count */, timestamp, distracterFilter);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue