Merge "Pass blockOffensiveWords as a parameter"
This commit is contained in:
commit
83757d439e
11 changed files with 43 additions and 29 deletions
|
@ -107,13 +107,16 @@ public final class BinaryDictionary extends Dictionary {
|
|||
|
||||
@Override
|
||||
public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
|
||||
final String prevWord, final ProximityInfo proximityInfo) {
|
||||
return getSuggestionsWithSessionId(composer, prevWord, proximityInfo, 0);
|
||||
final String prevWord, final ProximityInfo proximityInfo,
|
||||
final boolean blockOffensiveWords) {
|
||||
return getSuggestionsWithSessionId(composer, prevWord, proximityInfo, blockOffensiveWords,
|
||||
0 /* sessionId */);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<SuggestedWordInfo> getSuggestionsWithSessionId(final WordComposer composer,
|
||||
final String prevWord, final ProximityInfo proximityInfo, int sessionId) {
|
||||
final String prevWord, final ProximityInfo proximityInfo,
|
||||
final boolean blockOffensiveWords, final int sessionId) {
|
||||
if (!isValidDictionary()) return null;
|
||||
|
||||
Arrays.fill(mInputCodePoints, Constants.NOT_A_CODE);
|
||||
|
@ -139,8 +142,6 @@ public final class BinaryDictionary extends Dictionary {
|
|||
inputSize, 0 /* commitPoint */, isGesture, prevWordCodePointArray,
|
||||
mUseFullEditDistance, mOutputCodePoints, mOutputScores, mSpaceIndices,
|
||||
mOutputTypes);
|
||||
final boolean blockPotentiallyOffensive =
|
||||
Settings.getInstance().getBlockPotentiallyOffensive();
|
||||
final ArrayList<SuggestedWordInfo> suggestions = CollectionUtils.newArrayList();
|
||||
for (int j = 0; j < count; ++j) {
|
||||
final int start = j * MAX_WORD_LENGTH;
|
||||
|
@ -150,7 +151,7 @@ public final class BinaryDictionary extends Dictionary {
|
|||
}
|
||||
if (len > 0) {
|
||||
final int flags = mOutputTypes[j] & SuggestedWordInfo.KIND_MASK_FLAGS;
|
||||
if (blockPotentiallyOffensive
|
||||
if (blockOffensiveWords
|
||||
&& 0 != (flags & SuggestedWordInfo.KIND_FLAG_POSSIBLY_OFFENSIVE)
|
||||
&& 0 == (flags & SuggestedWordInfo.KIND_FLAG_EXACT_MATCH)) {
|
||||
// If we block potentially offensive words, and if the word is possibly
|
||||
|
|
|
@ -51,18 +51,21 @@ public abstract class Dictionary {
|
|||
* @param composer the key sequence to match with coordinate info, as a WordComposer
|
||||
* @param prevWord the previous word, or null if none
|
||||
* @param proximityInfo the object for key proximity. May be ignored by some implementations.
|
||||
* @param blockOffensiveWords whether to block potentially offensive words
|
||||
* @return the list of suggestions (possibly null if none)
|
||||
*/
|
||||
// TODO: pass more context than just the previous word, to enable better suggestions (n-gram
|
||||
// and more)
|
||||
abstract public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
|
||||
final String prevWord, final ProximityInfo proximityInfo);
|
||||
final String prevWord, final ProximityInfo proximityInfo,
|
||||
final boolean blockOffensiveWords);
|
||||
|
||||
// The default implementation of this method ignores sessionId.
|
||||
// Subclasses that want to use sessionId need to override this method.
|
||||
public ArrayList<SuggestedWordInfo> getSuggestionsWithSessionId(final WordComposer composer,
|
||||
final String prevWord, final ProximityInfo proximityInfo, final int sessionId) {
|
||||
return getSuggestions(composer, prevWord, proximityInfo);
|
||||
final String prevWord, final ProximityInfo proximityInfo,
|
||||
final boolean blockOffensiveWords, final int sessionId) {
|
||||
return getSuggestions(composer, prevWord, proximityInfo, blockOffensiveWords);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -56,18 +56,19 @@ public final class DictionaryCollection extends Dictionary {
|
|||
|
||||
@Override
|
||||
public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
|
||||
final String prevWord, final ProximityInfo proximityInfo) {
|
||||
final String prevWord, final ProximityInfo proximityInfo,
|
||||
final boolean blockOffensiveWords) {
|
||||
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).getSuggestions(composer,
|
||||
prevWord, proximityInfo);
|
||||
prevWord, proximityInfo, blockOffensiveWords);
|
||||
if (null == suggestions) suggestions = CollectionUtils.newArrayList();
|
||||
final int length = dictionaries.size();
|
||||
for (int i = 1; i < length; ++ i) {
|
||||
final ArrayList<SuggestedWordInfo> sugg = dictionaries.get(i).getSuggestions(composer,
|
||||
prevWord, proximityInfo);
|
||||
prevWord, proximityInfo, blockOffensiveWords);
|
||||
if (null != sugg) suggestions.addAll(sugg);
|
||||
}
|
||||
return suggestions;
|
||||
|
|
|
@ -200,12 +200,14 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
|
|||
|
||||
@Override
|
||||
public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
|
||||
final String prevWord, final ProximityInfo proximityInfo) {
|
||||
final String prevWord, final ProximityInfo proximityInfo,
|
||||
final boolean blockOffensiveWords) {
|
||||
asyncReloadDictionaryIfRequired();
|
||||
if (mLocalDictionaryController.tryLock()) {
|
||||
try {
|
||||
if (mBinaryDictionary != null) {
|
||||
return mBinaryDictionary.getSuggestions(composer, prevWord, proximityInfo);
|
||||
return mBinaryDictionary.getSuggestions(composer, prevWord, proximityInfo,
|
||||
blockOffensiveWords);
|
||||
}
|
||||
} finally {
|
||||
mLocalDictionaryController.unlock();
|
||||
|
|
|
@ -253,7 +253,8 @@ public class ExpandableDictionary extends Dictionary {
|
|||
|
||||
@Override
|
||||
public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
|
||||
final String prevWord, final ProximityInfo proximityInfo) {
|
||||
final String prevWord, final ProximityInfo proximityInfo,
|
||||
final boolean blockOffensiveWords) {
|
||||
if (reloadDictionaryIfRequired()) return null;
|
||||
if (composer.size() > 1) {
|
||||
if (composer.size() >= Constants.Dictionary.MAX_WORD_LENGTH) {
|
||||
|
|
|
@ -2185,6 +2185,7 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
|
|||
mConnection.getNthPreviousWord(mSettings.getCurrent().mWordSeparators,
|
||||
mWordComposer.isComposingWord() ? 2 : 1);
|
||||
return mSuggest.getSuggestedWords(mWordComposer, prevWord, keyboard.getProximityInfo(),
|
||||
mSettings.getBlockPotentiallyOffensive(),
|
||||
mSettings.getCurrent().mCorrectionEnabled, sessionId);
|
||||
}
|
||||
|
||||
|
|
|
@ -174,21 +174,22 @@ public final class Suggest {
|
|||
|
||||
public SuggestedWords getSuggestedWords(final WordComposer wordComposer,
|
||||
final String prevWordForBigram, final ProximityInfo proximityInfo,
|
||||
final boolean isCorrectionEnabled, final int sessionId) {
|
||||
final boolean blockOffensiveWords, final boolean isCorrectionEnabled,
|
||||
final int sessionId) {
|
||||
LatinImeLogger.onStartSuggestion(prevWordForBigram);
|
||||
if (wordComposer.isBatchMode()) {
|
||||
return getSuggestedWordsForBatchInput(
|
||||
wordComposer, prevWordForBigram, proximityInfo, sessionId);
|
||||
wordComposer, prevWordForBigram, proximityInfo, blockOffensiveWords, sessionId);
|
||||
} else {
|
||||
return getSuggestedWordsForTypingInput(wordComposer, prevWordForBigram, proximityInfo,
|
||||
isCorrectionEnabled);
|
||||
blockOffensiveWords, isCorrectionEnabled);
|
||||
}
|
||||
}
|
||||
|
||||
// Retrieves suggestions for the typing input.
|
||||
private SuggestedWords getSuggestedWordsForTypingInput(final WordComposer wordComposer,
|
||||
final String prevWordForBigram, final ProximityInfo proximityInfo,
|
||||
final boolean isCorrectionEnabled) {
|
||||
final boolean blockOffensiveWords, final boolean isCorrectionEnabled) {
|
||||
final int trailingSingleQuotesCount = wordComposer.trailingSingleQuotesCount();
|
||||
final BoundedTreeSet suggestionsSet = new BoundedTreeSet(sSuggestedWordInfoComparator,
|
||||
MAX_SUGGESTIONS);
|
||||
|
@ -212,7 +213,7 @@ public final class Suggest {
|
|||
for (final String key : mDictionaries.keySet()) {
|
||||
final Dictionary dictionary = mDictionaries.get(key);
|
||||
suggestionsSet.addAll(dictionary.getSuggestions(
|
||||
wordComposerForLookup, prevWordForBigram, proximityInfo));
|
||||
wordComposerForLookup, prevWordForBigram, proximityInfo, blockOffensiveWords));
|
||||
}
|
||||
|
||||
final String whitelistedWord;
|
||||
|
@ -301,7 +302,7 @@ public final class Suggest {
|
|||
// Retrieves suggestions for the batch input.
|
||||
private SuggestedWords getSuggestedWordsForBatchInput(final WordComposer wordComposer,
|
||||
final String prevWordForBigram, final ProximityInfo proximityInfo,
|
||||
final int sessionId) {
|
||||
final boolean blockOffensiveWords, final int sessionId) {
|
||||
final BoundedTreeSet suggestionsSet = new BoundedTreeSet(sSuggestedWordInfoComparator,
|
||||
MAX_SUGGESTIONS);
|
||||
|
||||
|
@ -314,8 +315,8 @@ public final class Suggest {
|
|||
continue;
|
||||
}
|
||||
final Dictionary dictionary = mDictionaries.get(key);
|
||||
suggestionsSet.addAll(dictionary.getSuggestionsWithSessionId(
|
||||
wordComposer, prevWordForBigram, proximityInfo, sessionId));
|
||||
suggestionsSet.addAll(dictionary.getSuggestionsWithSessionId(wordComposer,
|
||||
prevWordForBigram, proximityInfo, blockOffensiveWords, sessionId));
|
||||
}
|
||||
|
||||
for (SuggestedWordInfo wordInfo : suggestionsSet) {
|
||||
|
|
|
@ -33,9 +33,10 @@ public final class SynchronouslyLoadedContactsBinaryDictionary extends ContactsB
|
|||
|
||||
@Override
|
||||
public synchronized ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer codes,
|
||||
final String prevWordForBigrams, final ProximityInfo proximityInfo) {
|
||||
final String prevWordForBigrams, final ProximityInfo proximityInfo,
|
||||
final boolean blockOffensiveWords) {
|
||||
syncReloadDictionaryIfRequired();
|
||||
return super.getSuggestions(codes, prevWordForBigrams, proximityInfo);
|
||||
return super.getSuggestions(codes, prevWordForBigrams, proximityInfo, blockOffensiveWords);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -36,9 +36,10 @@ public final class SynchronouslyLoadedUserBinaryDictionary extends UserBinaryDic
|
|||
|
||||
@Override
|
||||
public synchronized ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer codes,
|
||||
final String prevWordForBigrams, final ProximityInfo proximityInfo) {
|
||||
final String prevWordForBigrams, final ProximityInfo proximityInfo,
|
||||
final boolean blockOffensiveWords) {
|
||||
syncReloadDictionaryIfRequired();
|
||||
return super.getSuggestions(codes, prevWordForBigrams, proximityInfo);
|
||||
return super.getSuggestions(codes, prevWordForBigrams, proximityInfo, blockOffensiveWords);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -306,9 +306,10 @@ public abstract class AndroidWordLevelSpellCheckerSession extends Session {
|
|||
if (!DictionaryPool.isAValidDictionary(dictInfo)) {
|
||||
return AndroidSpellCheckerService.getNotInDictEmptySuggestions();
|
||||
}
|
||||
// TODO: make a spell checker option to block offensive words or not
|
||||
final ArrayList<SuggestedWordInfo> suggestions =
|
||||
dictInfo.mDictionary.getSuggestions(composer, prevWord,
|
||||
dictInfo.mProximityInfo);
|
||||
dictInfo.mProximityInfo, true /* blockOffensiveWords */);
|
||||
for (final SuggestedWordInfo suggestion : suggestions) {
|
||||
final String suggestionStr = suggestion.mWord;
|
||||
suggestionsGatherer.addWord(suggestionStr.toCharArray(), null, 0,
|
||||
|
|
|
@ -51,7 +51,8 @@ public final class DictionaryPool extends LinkedBlockingQueue<DictAndProximity>
|
|||
new Dictionary(Dictionary.TYPE_MAIN) {
|
||||
@Override
|
||||
public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
|
||||
final String prevWord, final ProximityInfo proximityInfo) {
|
||||
final String prevWord, final ProximityInfo proximityInfo,
|
||||
final boolean blockOffensiveWords) {
|
||||
return noSuggestions;
|
||||
}
|
||||
@Override
|
||||
|
|
Loading…
Reference in a new issue