Merge "Pass blockOffensiveWords as a parameter"

main
Jean Chalard 2013-05-01 11:57:36 +00:00 committed by Android (Google) Code Review
commit 83757d439e
11 changed files with 43 additions and 29 deletions

View File

@ -107,13 +107,16 @@ public final class BinaryDictionary extends Dictionary {
@Override @Override
public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer, public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
final String prevWord, final ProximityInfo proximityInfo) { final String prevWord, final ProximityInfo proximityInfo,
return getSuggestionsWithSessionId(composer, prevWord, proximityInfo, 0); final boolean blockOffensiveWords) {
return getSuggestionsWithSessionId(composer, prevWord, proximityInfo, blockOffensiveWords,
0 /* sessionId */);
} }
@Override @Override
public ArrayList<SuggestedWordInfo> getSuggestionsWithSessionId(final WordComposer composer, 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; if (!isValidDictionary()) return null;
Arrays.fill(mInputCodePoints, Constants.NOT_A_CODE); Arrays.fill(mInputCodePoints, Constants.NOT_A_CODE);
@ -139,8 +142,6 @@ public final class BinaryDictionary extends Dictionary {
inputSize, 0 /* commitPoint */, isGesture, prevWordCodePointArray, inputSize, 0 /* commitPoint */, isGesture, prevWordCodePointArray,
mUseFullEditDistance, mOutputCodePoints, mOutputScores, mSpaceIndices, mUseFullEditDistance, mOutputCodePoints, mOutputScores, mSpaceIndices,
mOutputTypes); mOutputTypes);
final boolean blockPotentiallyOffensive =
Settings.getInstance().getBlockPotentiallyOffensive();
final ArrayList<SuggestedWordInfo> suggestions = CollectionUtils.newArrayList(); final ArrayList<SuggestedWordInfo> suggestions = CollectionUtils.newArrayList();
for (int j = 0; j < count; ++j) { for (int j = 0; j < count; ++j) {
final int start = j * MAX_WORD_LENGTH; final int start = j * MAX_WORD_LENGTH;
@ -150,7 +151,7 @@ public final class BinaryDictionary extends Dictionary {
} }
if (len > 0) { if (len > 0) {
final int flags = mOutputTypes[j] & SuggestedWordInfo.KIND_MASK_FLAGS; 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_POSSIBLY_OFFENSIVE)
&& 0 == (flags & SuggestedWordInfo.KIND_FLAG_EXACT_MATCH)) { && 0 == (flags & SuggestedWordInfo.KIND_FLAG_EXACT_MATCH)) {
// If we block potentially offensive words, and if the word is possibly // If we block potentially offensive words, and if the word is possibly

View File

@ -51,18 +51,21 @@ public abstract class Dictionary {
* @param composer the key sequence to match with coordinate info, as a WordComposer * @param composer the key sequence to match with coordinate info, as a WordComposer
* @param prevWord the previous word, or null if none * @param prevWord the previous word, or null if none
* @param proximityInfo the object for key proximity. May be ignored by some implementations. * @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) * @return the list of suggestions (possibly null if none)
*/ */
// TODO: pass more context than just the previous word, to enable better suggestions (n-gram // TODO: pass more context than just the previous word, to enable better suggestions (n-gram
// and more) // and more)
abstract public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer, 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. // The default implementation of this method ignores sessionId.
// Subclasses that want to use sessionId need to override this method. // Subclasses that want to use sessionId need to override this method.
public ArrayList<SuggestedWordInfo> getSuggestionsWithSessionId(final WordComposer composer, public ArrayList<SuggestedWordInfo> getSuggestionsWithSessionId(final WordComposer composer,
final String prevWord, final ProximityInfo proximityInfo, final int sessionId) { final String prevWord, final ProximityInfo proximityInfo,
return getSuggestions(composer, prevWord, proximityInfo); final boolean blockOffensiveWords, final int sessionId) {
return getSuggestions(composer, prevWord, proximityInfo, blockOffensiveWords);
} }
/** /**

View File

@ -56,18 +56,19 @@ public final class DictionaryCollection extends Dictionary {
@Override @Override
public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer, 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; final CopyOnWriteArrayList<Dictionary> dictionaries = mDictionaries;
if (dictionaries.isEmpty()) return null; if (dictionaries.isEmpty()) return null;
// To avoid creating unnecessary objects, we get the list out of the first // 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) // dictionary and add the rest to it if not null, hence the get(0)
ArrayList<SuggestedWordInfo> suggestions = dictionaries.get(0).getSuggestions(composer, ArrayList<SuggestedWordInfo> suggestions = dictionaries.get(0).getSuggestions(composer,
prevWord, proximityInfo); prevWord, proximityInfo, blockOffensiveWords);
if (null == suggestions) suggestions = CollectionUtils.newArrayList(); if (null == suggestions) suggestions = CollectionUtils.newArrayList();
final int length = dictionaries.size(); final int length = dictionaries.size();
for (int i = 1; i < length; ++ i) { for (int i = 1; i < length; ++ i) {
final ArrayList<SuggestedWordInfo> sugg = dictionaries.get(i).getSuggestions(composer, final ArrayList<SuggestedWordInfo> sugg = dictionaries.get(i).getSuggestions(composer,
prevWord, proximityInfo); prevWord, proximityInfo, blockOffensiveWords);
if (null != sugg) suggestions.addAll(sugg); if (null != sugg) suggestions.addAll(sugg);
} }
return suggestions; return suggestions;

View File

@ -200,12 +200,14 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
@Override @Override
public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer, public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
final String prevWord, final ProximityInfo proximityInfo) { final String prevWord, final ProximityInfo proximityInfo,
final boolean blockOffensiveWords) {
asyncReloadDictionaryIfRequired(); asyncReloadDictionaryIfRequired();
if (mLocalDictionaryController.tryLock()) { if (mLocalDictionaryController.tryLock()) {
try { try {
if (mBinaryDictionary != null) { if (mBinaryDictionary != null) {
return mBinaryDictionary.getSuggestions(composer, prevWord, proximityInfo); return mBinaryDictionary.getSuggestions(composer, prevWord, proximityInfo,
blockOffensiveWords);
} }
} finally { } finally {
mLocalDictionaryController.unlock(); mLocalDictionaryController.unlock();

View File

@ -253,7 +253,8 @@ public class ExpandableDictionary extends Dictionary {
@Override @Override
public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer, 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 (reloadDictionaryIfRequired()) return null;
if (composer.size() > 1) { if (composer.size() > 1) {
if (composer.size() >= Constants.Dictionary.MAX_WORD_LENGTH) { if (composer.size() >= Constants.Dictionary.MAX_WORD_LENGTH) {

View File

@ -2185,6 +2185,7 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
mConnection.getNthPreviousWord(mSettings.getCurrent().mWordSeparators, mConnection.getNthPreviousWord(mSettings.getCurrent().mWordSeparators,
mWordComposer.isComposingWord() ? 2 : 1); mWordComposer.isComposingWord() ? 2 : 1);
return mSuggest.getSuggestedWords(mWordComposer, prevWord, keyboard.getProximityInfo(), return mSuggest.getSuggestedWords(mWordComposer, prevWord, keyboard.getProximityInfo(),
mSettings.getBlockPotentiallyOffensive(),
mSettings.getCurrent().mCorrectionEnabled, sessionId); mSettings.getCurrent().mCorrectionEnabled, sessionId);
} }

View File

@ -174,21 +174,22 @@ public final class Suggest {
public SuggestedWords getSuggestedWords(final WordComposer wordComposer, public SuggestedWords getSuggestedWords(final WordComposer wordComposer,
final String prevWordForBigram, final ProximityInfo proximityInfo, final String prevWordForBigram, final ProximityInfo proximityInfo,
final boolean isCorrectionEnabled, final int sessionId) { final boolean blockOffensiveWords, final boolean isCorrectionEnabled,
final int sessionId) {
LatinImeLogger.onStartSuggestion(prevWordForBigram); LatinImeLogger.onStartSuggestion(prevWordForBigram);
if (wordComposer.isBatchMode()) { if (wordComposer.isBatchMode()) {
return getSuggestedWordsForBatchInput( return getSuggestedWordsForBatchInput(
wordComposer, prevWordForBigram, proximityInfo, sessionId); wordComposer, prevWordForBigram, proximityInfo, blockOffensiveWords, sessionId);
} else { } else {
return getSuggestedWordsForTypingInput(wordComposer, prevWordForBigram, proximityInfo, return getSuggestedWordsForTypingInput(wordComposer, prevWordForBigram, proximityInfo,
isCorrectionEnabled); blockOffensiveWords, isCorrectionEnabled);
} }
} }
// Retrieves suggestions for the typing input. // Retrieves suggestions for the typing input.
private SuggestedWords getSuggestedWordsForTypingInput(final WordComposer wordComposer, private SuggestedWords getSuggestedWordsForTypingInput(final WordComposer wordComposer,
final String prevWordForBigram, final ProximityInfo proximityInfo, final String prevWordForBigram, final ProximityInfo proximityInfo,
final boolean isCorrectionEnabled) { final boolean blockOffensiveWords, final boolean isCorrectionEnabled) {
final int trailingSingleQuotesCount = wordComposer.trailingSingleQuotesCount(); final int trailingSingleQuotesCount = wordComposer.trailingSingleQuotesCount();
final BoundedTreeSet suggestionsSet = new BoundedTreeSet(sSuggestedWordInfoComparator, final BoundedTreeSet suggestionsSet = new BoundedTreeSet(sSuggestedWordInfoComparator,
MAX_SUGGESTIONS); MAX_SUGGESTIONS);
@ -212,7 +213,7 @@ public final class Suggest {
for (final String key : mDictionaries.keySet()) { for (final String key : mDictionaries.keySet()) {
final Dictionary dictionary = mDictionaries.get(key); final Dictionary dictionary = mDictionaries.get(key);
suggestionsSet.addAll(dictionary.getSuggestions( suggestionsSet.addAll(dictionary.getSuggestions(
wordComposerForLookup, prevWordForBigram, proximityInfo)); wordComposerForLookup, prevWordForBigram, proximityInfo, blockOffensiveWords));
} }
final String whitelistedWord; final String whitelistedWord;
@ -301,7 +302,7 @@ public final class Suggest {
// Retrieves suggestions for the batch input. // Retrieves suggestions for the batch input.
private SuggestedWords getSuggestedWordsForBatchInput(final WordComposer wordComposer, private SuggestedWords getSuggestedWordsForBatchInput(final WordComposer wordComposer,
final String prevWordForBigram, final ProximityInfo proximityInfo, final String prevWordForBigram, final ProximityInfo proximityInfo,
final int sessionId) { final boolean blockOffensiveWords, final int sessionId) {
final BoundedTreeSet suggestionsSet = new BoundedTreeSet(sSuggestedWordInfoComparator, final BoundedTreeSet suggestionsSet = new BoundedTreeSet(sSuggestedWordInfoComparator,
MAX_SUGGESTIONS); MAX_SUGGESTIONS);
@ -314,8 +315,8 @@ public final class Suggest {
continue; continue;
} }
final Dictionary dictionary = mDictionaries.get(key); final Dictionary dictionary = mDictionaries.get(key);
suggestionsSet.addAll(dictionary.getSuggestionsWithSessionId( suggestionsSet.addAll(dictionary.getSuggestionsWithSessionId(wordComposer,
wordComposer, prevWordForBigram, proximityInfo, sessionId)); prevWordForBigram, proximityInfo, blockOffensiveWords, sessionId));
} }
for (SuggestedWordInfo wordInfo : suggestionsSet) { for (SuggestedWordInfo wordInfo : suggestionsSet) {

View File

@ -33,9 +33,10 @@ public final class SynchronouslyLoadedContactsBinaryDictionary extends ContactsB
@Override @Override
public synchronized ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer codes, public synchronized ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer codes,
final String prevWordForBigrams, final ProximityInfo proximityInfo) { final String prevWordForBigrams, final ProximityInfo proximityInfo,
final boolean blockOffensiveWords) {
syncReloadDictionaryIfRequired(); syncReloadDictionaryIfRequired();
return super.getSuggestions(codes, prevWordForBigrams, proximityInfo); return super.getSuggestions(codes, prevWordForBigrams, proximityInfo, blockOffensiveWords);
} }
@Override @Override

View File

@ -36,9 +36,10 @@ public final class SynchronouslyLoadedUserBinaryDictionary extends UserBinaryDic
@Override @Override
public synchronized ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer codes, public synchronized ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer codes,
final String prevWordForBigrams, final ProximityInfo proximityInfo) { final String prevWordForBigrams, final ProximityInfo proximityInfo,
final boolean blockOffensiveWords) {
syncReloadDictionaryIfRequired(); syncReloadDictionaryIfRequired();
return super.getSuggestions(codes, prevWordForBigrams, proximityInfo); return super.getSuggestions(codes, prevWordForBigrams, proximityInfo, blockOffensiveWords);
} }
@Override @Override

View File

@ -306,9 +306,10 @@ public abstract class AndroidWordLevelSpellCheckerSession extends Session {
if (!DictionaryPool.isAValidDictionary(dictInfo)) { if (!DictionaryPool.isAValidDictionary(dictInfo)) {
return AndroidSpellCheckerService.getNotInDictEmptySuggestions(); return AndroidSpellCheckerService.getNotInDictEmptySuggestions();
} }
// TODO: make a spell checker option to block offensive words or not
final ArrayList<SuggestedWordInfo> suggestions = final ArrayList<SuggestedWordInfo> suggestions =
dictInfo.mDictionary.getSuggestions(composer, prevWord, dictInfo.mDictionary.getSuggestions(composer, prevWord,
dictInfo.mProximityInfo); dictInfo.mProximityInfo, true /* blockOffensiveWords */);
for (final SuggestedWordInfo suggestion : suggestions) { for (final SuggestedWordInfo suggestion : suggestions) {
final String suggestionStr = suggestion.mWord; final String suggestionStr = suggestion.mWord;
suggestionsGatherer.addWord(suggestionStr.toCharArray(), null, 0, suggestionsGatherer.addWord(suggestionStr.toCharArray(), null, 0,

View File

@ -51,7 +51,8 @@ public final class DictionaryPool extends LinkedBlockingQueue<DictAndProximity>
new Dictionary(Dictionary.TYPE_MAIN) { new Dictionary(Dictionary.TYPE_MAIN) {
@Override @Override
public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer, public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
final String prevWord, final ProximityInfo proximityInfo) { final String prevWord, final ProximityInfo proximityInfo,
final boolean blockOffensiveWords) {
return noSuggestions; return noSuggestions;
} }
@Override @Override