aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/latin
diff options
context:
space:
mode:
Diffstat (limited to 'java/src/com/android/inputmethod/latin')
-rw-r--r--java/src/com/android/inputmethod/latin/LatinIME.java39
-rw-r--r--java/src/com/android/inputmethod/latin/SettingsValues.java10
-rw-r--r--java/src/com/android/inputmethod/latin/Suggest.java74
-rw-r--r--java/src/com/android/inputmethod/latin/SuggestedWords.java152
4 files changed, 116 insertions, 159 deletions
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index f41972e8b..9f5931de9 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -924,17 +924,17 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
}
final List<SuggestedWords.SuggestedWordInfo> applicationSuggestedWords =
- SuggestedWords.Builder.getFromApplicationSpecifiedCompletions(
+ SuggestedWords.getFromApplicationSpecifiedCompletions(
applicationSpecifiedCompletions);
- SuggestedWords.Builder builder = new SuggestedWords.Builder(applicationSuggestedWords,
+ final SuggestedWords suggestedWords = new SuggestedWords(
+ applicationSuggestedWords,
false /* typedWordValid */,
- false /* hasMinimalSuggestion */,
+ false /* hasAutoCorrectionCandidate */,
false /* allowsToBeAutoCorrected */,
false /* isPunctuationSuggestions */);
// When in fullscreen mode, show completions generated by the application
- final SuggestedWords words = builder.build();
final boolean isAutoCorrection = false;
- setSuggestions(words, isAutoCorrection);
+ setSuggestions(suggestedWords, isAutoCorrection);
setAutoCorrectionIndicator(isAutoCorrection);
// TODO: is this the right thing to do? What should we auto-correct to in
// this case? This says to keep whatever the user typed.
@@ -1767,7 +1767,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
final CharSequence typedWord = mWordComposer.getTypedWord();
// getSuggestedWordBuilder handles gracefully a null value of prevWord
- final SuggestedWords.Builder builder = mSuggest.getSuggestedWordBuilder(mWordComposer,
+ final SuggestedWords suggestedWords = mSuggest.getSuggestedWords(mWordComposer,
prevWord, mKeyboardSwitcher.getKeyboard().getProximityInfo(), mCorrectionMode);
// Basically, we update the suggestion strip only when suggestion count > 1. However,
@@ -1776,24 +1776,25 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
// in most cases, suggestion count is 1 when typed word's length is 1, but we do always
// need to clear the previous state when the user starts typing a word (i.e. typed word's
// length == 1).
- if (builder.size() > 1 || typedWord.length() == 1 || !builder.allowsToBeAutoCorrected()
+ if (suggestedWords.size() > 1 || typedWord.length() == 1
+ || !suggestedWords.mAllowsToBeAutoCorrected
|| mSuggestionsView.isShowingAddToDictionaryHint()) {
- showSuggestions(builder.build(), typedWord);
+ showSuggestions(suggestedWords, typedWord);
} else {
SuggestedWords previousSuggestions = mSuggestionsView.getSuggestions();
if (previousSuggestions == mSettingsValues.mSuggestPuncList) {
previousSuggestions = SuggestedWords.EMPTY;
}
final ArrayList<SuggestedWords.SuggestedWordInfo> typedWordAndPreviousSuggestions =
- SuggestedWords.Builder.getTypedWordAndPreviousSuggestions(
+ SuggestedWords.getTypedWordAndPreviousSuggestions(
typedWord, previousSuggestions);
- final SuggestedWords.Builder obsoleteSuggestionsBuilder =
- new SuggestedWords.Builder(typedWordAndPreviousSuggestions,
+ final SuggestedWords obsoleteSuggestedWords =
+ new SuggestedWords(typedWordAndPreviousSuggestions,
false /* typedWordValid */,
- false /* hasMinimalSuggestion */,
+ false /* hasAutoCorrectionCandidate */,
false /* allowsToBeAutoCorrected */,
false /* isPunctuationSuggestions */);
- showSuggestions(obsoleteSuggestionsBuilder.build(), typedWord);
+ showSuggestions(obsoleteSuggestedWords, typedWord);
}
}
@@ -1973,23 +1974,23 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
return;
}
- final SuggestedWords.Builder builder;
+ final SuggestedWords suggestedWords;
if (mCorrectionMode == Suggest.CORRECTION_FULL_BIGRAM) {
final CharSequence prevWord = EditingUtils.getThisWord(getCurrentInputConnection(),
mSettingsValues.mWordSeparators);
if (!TextUtils.isEmpty(prevWord)) {
- builder = mSuggest.getBigramPredictionWordBuilder(prevWord);
+ suggestedWords = mSuggest.getBigramPredictions(prevWord);
} else {
- builder = null;
+ suggestedWords = null;
}
} else {
- builder = null;
+ suggestedWords = null;
}
- if (null != builder && builder.size() > 0) {
+ if (null != suggestedWords && suggestedWords.size() > 0) {
// Explicitly supply an empty typed word (the no-second-arg version of
// showSuggestions will retrieve the word near the cursor, we don't want that here)
- showSuggestions(builder.build(), "");
+ showSuggestions(suggestedWords, "");
} else {
if (!isShowingPunctuationList()) setPunctuationSuggestions();
}
diff --git a/java/src/com/android/inputmethod/latin/SettingsValues.java b/java/src/com/android/inputmethod/latin/SettingsValues.java
index 0a4aea140..d12b9c428 100644
--- a/java/src/com/android/inputmethod/latin/SettingsValues.java
+++ b/java/src/com/android/inputmethod/latin/SettingsValues.java
@@ -183,12 +183,11 @@ public class SettingsValues {
KeySpecParser.getLabel(puncSpec)));
}
}
- final SuggestedWords.Builder builder = new SuggestedWords.Builder(puncList,
+ return new SuggestedWords(puncList,
false /* typedWordValid */,
- false /* hasMinimalSuggestion */,
+ false /* hasAutoCorrectionCandidate */,
false /* allowsToBeAutoCorrected */,
true /* isPunctuationSuggestions */);
- return builder.build();
}
private static SuggestedWords createSuggestPuncOutputTextList(final String[] puncs) {
@@ -205,12 +204,11 @@ public class SettingsValues {
}
}
}
- final SuggestedWords.Builder builder = new SuggestedWords.Builder(puncOutputTextList,
+ return new SuggestedWords(puncOutputTextList,
false /* typedWordValid */,
- false /* hasMinimalSuggestion */,
+ false /* hasAutoCorrectionCandidate */,
false /* allowsToBeAutoCorrected */,
true /* isPunctuationSuggestions */);
- return builder.build();
}
private static String createWordSeparators(final String weakSpaceStrippers,
diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java
index 28d3b4437..3089625e7 100644
--- a/java/src/com/android/inputmethod/latin/Suggest.java
+++ b/java/src/com/android/inputmethod/latin/Suggest.java
@@ -239,7 +239,7 @@ public class Suggest implements Dictionary.WordCallback {
}
private static final WordComposer sEmptyWordComposer = new WordComposer();
- public SuggestedWords.Builder getBigramPredictionWordBuilder(CharSequence prevWordForBigram) {
+ public SuggestedWords getBigramPredictions(CharSequence prevWordForBigram) {
LatinImeLogger.onStartSuggestion(prevWordForBigram);
mIsFirstCharCapitalized = false;
mIsAllUpperCase = false;
@@ -269,16 +269,15 @@ public class Suggest implements Dictionary.WordCallback {
StringUtils.removeDupes(mSuggestions);
- return new SuggestedWords.Builder(
- SuggestedWords.Builder.getFromCharSequenceList(mSuggestions),
+ return new SuggestedWords(SuggestedWords.getFromCharSequenceList(mSuggestions),
false /* typedWordValid */,
- false /* hasMinimalSuggestion */,
+ false /* hasAutoCorrectionCandidate */,
false /* allowsToBeAutoCorrected */,
false /* isPunctuationSuggestions */);
}
// TODO: cleanup dictionaries looking up and suggestions building with SuggestedWords.Builder
- public SuggestedWords.Builder getSuggestedWordBuilder(
+ public SuggestedWords getSuggestedWords(
final WordComposer wordComposer, CharSequence prevWordForBigram,
final ProximityInfo proximityInfo, final int correctionMode) {
LatinImeLogger.onStartSuggestion(prevWordForBigram);
@@ -344,21 +343,22 @@ public class Suggest implements Dictionary.WordCallback {
}
} else if (wordComposer.size() > 1) {
+ final WordComposer wordComposerForLookup;
+ if (mTrailingSingleQuotesCount > 0) {
+ wordComposerForLookup = new WordComposer(wordComposer);
+ for (int i = mTrailingSingleQuotesCount - 1; i >= 0; --i) {
+ wordComposerForLookup.deleteLast();
+ }
+ } else {
+ wordComposerForLookup = wordComposer;
+ }
// At second character typed, search the unigrams (scores being affected by bigrams)
for (final String key : mUnigramDictionaries.keySet()) {
// Skip UserUnigramDictionary and WhitelistDictionary to lookup
if (key.equals(DICT_KEY_USER_UNIGRAM) || key.equals(DICT_KEY_WHITELIST))
continue;
final Dictionary dictionary = mUnigramDictionaries.get(key);
- if (mTrailingSingleQuotesCount > 0) {
- final WordComposer tmpWordComposer = new WordComposer(wordComposer);
- for (int i = mTrailingSingleQuotesCount - 1; i >= 0; --i) {
- tmpWordComposer.deleteLast();
- }
- dictionary.getWords(tmpWordComposer, this, proximityInfo);
- } else {
- dictionary.getWords(wordComposer, this, proximityInfo);
- }
+ dictionary.getWords(wordComposerForLookup, this, proximityInfo);
}
}
@@ -392,42 +392,36 @@ public class Suggest implements Dictionary.WordCallback {
mSuggestions.add(0, typedWord);
StringUtils.removeDupes(mSuggestions);
- final SuggestedWords.Builder builder;
- final ArrayList<SuggestedWords.SuggestedWordInfo> scoreInfoList;
+ final ArrayList<SuggestedWords.SuggestedWordInfo> suggestionsList;
if (DBG) {
// TODO: this doesn't take into account the fact that removing dupes from mSuggestions
// may have made mScores[] and mSuggestions out of sync.
final CharSequence autoCorrectionSuggestion = mSuggestions.get(0);
- final int autoCorrectionSuggestionScore = mScores[0];
double normalizedScore = BinaryDictionary.calcNormalizedScore(
- typedWord, autoCorrectionSuggestion.toString(),
- autoCorrectionSuggestionScore);
- scoreInfoList = new ArrayList<SuggestedWords.SuggestedWordInfo>();
- scoreInfoList.add(new SuggestedWords.SuggestedWordInfo(autoCorrectionSuggestion, "+",
+ typedWord, autoCorrectionSuggestion.toString(), mScores[0]);
+ suggestionsList = new ArrayList<SuggestedWords.SuggestedWordInfo>();
+ suggestionsList.add(new SuggestedWords.SuggestedWordInfo(autoCorrectionSuggestion, "+",
false));
final int suggestionsSize = mSuggestions.size();
// Note: i here is the index in mScores[], but the index in mSuggestions is one more
// than i because we added the typed word to mSuggestions without touching mScores.
for (int i = 0; i < mScores.length && i < suggestionsSize - 1; ++i) {
+ final String scoreInfoString;
if (normalizedScore > 0) {
- final String scoreThreshold = String.format("%d (%4.2f)", mScores[i],
- normalizedScore);
- scoreInfoList.add(
- new SuggestedWords.SuggestedWordInfo(mSuggestions.get(i + 1),
- scoreThreshold, false));
+ scoreInfoString = String.format("%d (%4.2f)", mScores[i], normalizedScore);
normalizedScore = 0.0;
} else {
- final String score = Integer.toString(mScores[i]);
- scoreInfoList.add(new SuggestedWords.SuggestedWordInfo(mSuggestions.get(i + 1),
- score, false));
+ scoreInfoString = Integer.toString(mScores[i]);
}
+ suggestionsList.add(new SuggestedWords.SuggestedWordInfo(mSuggestions.get(i + 1),
+ scoreInfoString, false));
}
for (int i = mScores.length; i < suggestionsSize; ++i) {
- scoreInfoList.add(new SuggestedWords.SuggestedWordInfo(mSuggestions.get(i),
+ suggestionsList.add(new SuggestedWords.SuggestedWordInfo(mSuggestions.get(i),
"--", false));
}
} else {
- scoreInfoList = SuggestedWords.Builder.getFromCharSequenceList(mSuggestions);
+ suggestionsList = SuggestedWords.getFromCharSequenceList(mSuggestions);
}
boolean autoCorrectionAvailable = hasAutoCorrection;
@@ -437,16 +431,20 @@ public class Suggest implements Dictionary.WordCallback {
}
// Don't auto-correct words with multiple capital letter
autoCorrectionAvailable &= !wordComposer.isMostlyCaps();
- builder = new SuggestedWords.Builder(scoreInfoList,
+ final boolean shouldBlockAutoCorrectionBySatefyNet;
+ if (allowsToBeAutoCorrected && suggestionsList.size() > 1 && mAutoCorrectionThreshold > 0
+ && Suggest.shouldBlockAutoCorrectionBySafetyNet(typedWord,
+ suggestionsList.get(1).mWord)) {
+ shouldBlockAutoCorrectionBySatefyNet = true;
+ } else {
+ shouldBlockAutoCorrectionBySatefyNet = false;
+ }
+ return new SuggestedWords(suggestionsList,
!allowsToBeAutoCorrected /* typedWordValid */,
- autoCorrectionAvailable /* hasMinimalSuggestion */,
+ autoCorrectionAvailable & !shouldBlockAutoCorrectionBySatefyNet
+ /* hasAutoCorrectionCandidate */,
allowsToBeAutoCorrected /* allowsToBeAutoCorrected */,
false /* isPunctuationSuggestions */);
- if (allowsToBeAutoCorrected && builder.size() > 1 && mAutoCorrectionThreshold > 0
- && Suggest.shouldBlockAutoCorrectionBySafetyNet(typedWord, builder.getWord(1))) {
- builder.setShouldBlockAutoCorrectionBySafetyNet();
- }
- return builder;
}
@Override
diff --git a/java/src/com/android/inputmethod/latin/SuggestedWords.java b/java/src/com/android/inputmethod/latin/SuggestedWords.java
index 144e67482..6b231f81c 100644
--- a/java/src/com/android/inputmethod/latin/SuggestedWords.java
+++ b/java/src/com/android/inputmethod/latin/SuggestedWords.java
@@ -16,31 +16,35 @@
package com.android.inputmethod.latin;
+import android.text.TextUtils;
import android.view.inputmethod.CompletionInfo;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
public class SuggestedWords {
- public static final SuggestedWords EMPTY = new SuggestedWords(false, false, false, false,
- Collections.<SuggestedWordInfo>emptyList());
+ public static final SuggestedWords EMPTY = new SuggestedWords(
+ Collections.<SuggestedWordInfo>emptyList(), false, false, false, false);
public final boolean mTypedWordValid;
public final boolean mHasAutoCorrectionCandidate;
public final boolean mIsPunctuationSuggestions;
+ public final boolean mAllowsToBeAutoCorrected;
private final List<SuggestedWordInfo> mSuggestedWordInfoList;
- SuggestedWords(boolean typedWordValid,
- boolean hasAutoCorrectionCandidate, boolean isPunctuationSuggestions,
- boolean shouldBlockAutoCorrectionBySafetyNet,
- List<SuggestedWordInfo> suggestedWordInfoList) {
+ public SuggestedWords(final List<SuggestedWordInfo> suggestedWordInfoList,
+ final boolean typedWordValid,
+ final boolean hasAutoCorrectionCandidate,
+ final boolean allowsToBeAutoCorrected,
+ final boolean isPunctuationSuggestions) {
+ mSuggestedWordInfoList = suggestedWordInfoList;
mTypedWordValid = typedWordValid;
- mHasAutoCorrectionCandidate = hasAutoCorrectionCandidate
- && !shouldBlockAutoCorrectionBySafetyNet;
+ mHasAutoCorrectionCandidate = hasAutoCorrectionCandidate;
+ mAllowsToBeAutoCorrected = allowsToBeAutoCorrected;
mIsPunctuationSuggestions = isPunctuationSuggestions;
- mSuggestedWordInfoList = suggestedWordInfoList;
}
public int size() {
@@ -69,104 +73,51 @@ public class SuggestedWords {
return "SuggestedWords:"
+ " mTypedWordValid=" + mTypedWordValid
+ " mHasAutoCorrectionCandidate=" + mHasAutoCorrectionCandidate
- + " mIsPunctuationSuggestions=" + mIsPunctuationSuggestions;
+ + " mAllowsToBeAutoCorrected=" + mAllowsToBeAutoCorrected
+ + " mIsPunctuationSuggestions=" + mIsPunctuationSuggestions
+ + " words=" + Arrays.toString(mSuggestedWordInfoList.toArray());
}
- public static class Builder {
- private final boolean mTypedWordValid;
- private final boolean mHasMinimalSuggestion;
- private final boolean mIsPunctuationSuggestions;
- private boolean mShouldBlockAutoCorrectionBySafetyNet;
- private final boolean mAllowsToBeAutoCorrected;
- private final List<SuggestedWordInfo> mSuggestedWordInfoList;
-
- public Builder(final List<SuggestedWordInfo> suggestedWordInfoList,
- final boolean typedWordValid,
- final boolean hasMinimalSuggestion,
- final boolean allowsToBeAutoCorrected,
- final boolean isPunctuationSuggestions) {
- mSuggestedWordInfoList = suggestedWordInfoList;
- mTypedWordValid = typedWordValid;
- mHasMinimalSuggestion = hasMinimalSuggestion;
- mAllowsToBeAutoCorrected = allowsToBeAutoCorrected;
- mIsPunctuationSuggestions = isPunctuationSuggestions;
- }
-
- public static ArrayList<SuggestedWordInfo> getFromCharSequenceList(
- final List<CharSequence> wordList) {
- final ArrayList<SuggestedWordInfo> result = new ArrayList<SuggestedWordInfo>();
- for (CharSequence word : wordList) {
- if (null != word) result.add(new SuggestedWordInfo(word, null, false));
- }
- return result;
- }
-
- public static List<SuggestedWordInfo> getFromApplicationSpecifiedCompletions(
- final CompletionInfo[] infos) {
- final ArrayList<SuggestedWordInfo> result = new ArrayList<SuggestedWordInfo>();
- for (CompletionInfo info : infos) {
- if (null != info) result.add(new SuggestedWordInfo(info.getText(), null, false));
- }
- return result;
+ public static ArrayList<SuggestedWordInfo> getFromCharSequenceList(
+ final List<CharSequence> wordList) {
+ final ArrayList<SuggestedWordInfo> result = new ArrayList<SuggestedWordInfo>();
+ for (CharSequence word : wordList) {
+ if (null != word) result.add(new SuggestedWordInfo(word, null, false));
}
+ return result;
+ }
- public Builder setShouldBlockAutoCorrectionBySafetyNet() {
- mShouldBlockAutoCorrectionBySafetyNet = true;
- return this;
+ public static List<SuggestedWordInfo> getFromApplicationSpecifiedCompletions(
+ final CompletionInfo[] infos) {
+ final ArrayList<SuggestedWordInfo> result = new ArrayList<SuggestedWordInfo>();
+ for (CompletionInfo info : infos) {
+ if (null != info) result.add(new SuggestedWordInfo(info.getText(), null, false));
}
+ return result;
+ }
- // Should get rid of the first one (what the user typed previously) from suggestions
- // and replace it with what the user currently typed.
- public static ArrayList<SuggestedWordInfo> getTypedWordAndPreviousSuggestions(
- final CharSequence typedWord, final SuggestedWords previousSuggestions) {
- final ArrayList<SuggestedWordInfo> suggestionsList = new ArrayList<SuggestedWordInfo>();
- final HashSet<String> alreadySeen = new HashSet<String>();
- suggestionsList.add(new SuggestedWordInfo(typedWord, null, false));
- alreadySeen.add(typedWord.toString());
- final int previousSize = previousSuggestions.size();
- for (int pos = 1; pos < previousSize; pos++) {
- final String prevWord = previousSuggestions.getWord(pos).toString();
- // Filter out duplicate suggestion.
- if (!alreadySeen.contains(prevWord)) {
- suggestionsList.add(new SuggestedWordInfo(prevWord, null, true));
- alreadySeen.add(prevWord);
- }
+ // Should get rid of the first one (what the user typed previously) from suggestions
+ // and replace it with what the user currently typed.
+ public static ArrayList<SuggestedWordInfo> getTypedWordAndPreviousSuggestions(
+ final CharSequence typedWord, final SuggestedWords previousSuggestions) {
+ final ArrayList<SuggestedWordInfo> suggestionsList = new ArrayList<SuggestedWordInfo>();
+ final HashSet<String> alreadySeen = new HashSet<String>();
+ suggestionsList.add(new SuggestedWordInfo(typedWord, null, false));
+ alreadySeen.add(typedWord.toString());
+ final int previousSize = previousSuggestions.size();
+ for (int pos = 1; pos < previousSize; pos++) {
+ final String prevWord = previousSuggestions.getWord(pos).toString();
+ // Filter out duplicate suggestion.
+ if (!alreadySeen.contains(prevWord)) {
+ suggestionsList.add(new SuggestedWordInfo(prevWord, null, true));
+ alreadySeen.add(prevWord);
}
- return suggestionsList;
- }
-
- public SuggestedWords build() {
- return new SuggestedWords(mTypedWordValid, mHasMinimalSuggestion,
- mIsPunctuationSuggestions, mShouldBlockAutoCorrectionBySafetyNet,
- mSuggestedWordInfoList);
- }
-
- public int size() {
- return mSuggestedWordInfoList.size();
- }
-
- public CharSequence getWord(int pos) {
- return mSuggestedWordInfoList.get(pos).mWord;
- }
-
- public boolean allowsToBeAutoCorrected() {
- return mAllowsToBeAutoCorrected;
- }
-
- @Override
- public String toString() {
- // Pretty-print method to help debug
- return "SuggestedWords.Builder:"
- + " mTypedWordValid=" + mTypedWordValid
- + " mHasMinimalSuggestion=" + mHasMinimalSuggestion
- + " mIsPunctuationSuggestions=" + mIsPunctuationSuggestions
- + " mShouldBlockAutoCorrectionBySafetyNet="
- + mShouldBlockAutoCorrectionBySafetyNet;
}
+ return suggestionsList;
}
public static class SuggestedWordInfo {
- private final CharSequence mWord;
+ public final CharSequence mWord;
private final CharSequence mDebugString;
private final boolean mPreviousSuggestedWord;
@@ -194,5 +145,14 @@ public class SuggestedWords {
public boolean isObsoleteSuggestedWord () {
return mPreviousSuggestedWord;
}
+
+ @Override
+ public String toString() {
+ if (TextUtils.isEmpty(mDebugString)) {
+ return mWord.toString();
+ } else {
+ return mWord.toString() + " (" + mDebugString.toString() + ")";
+ }
+ }
}
}