use of com.android.inputmethod.latin.NgramContext in project android_packages_inputmethods_LatinIME by CyanogenMod.
the class InputLogic method unlearnWord.
void unlearnWord(final String word, final SettingsValues settingsValues, final int eventType) {
final NgramContext ngramContext = mConnection.getNgramContextFromNthPreviousWord(settingsValues.mSpacingAndPunctuations, 2);
final long timeStampInSeconds = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
mDictionaryFacilitator.unlearnFromUserHistory(word, ngramContext, timeStampInSeconds, eventType);
}
use of com.android.inputmethod.latin.NgramContext in project android_packages_inputmethods_LatinIME by CyanogenMod.
the class NgramContextUtils method getNgramContextFromNthPreviousWord.
// Get context information from nth word before the cursor. n = 1 retrieves the words
// immediately before the cursor, n = 2 retrieves the words before that, and so on. This splits
// on whitespace only.
// Also, it won't return words that end in a separator (if the nth word before the cursor
// ends in a separator, it returns information representing beginning-of-sentence).
// Example (when Constants.MAX_PREV_WORD_COUNT_FOR_N_GRAM is 2):
// (n = 1) "abc def|" -> abc, def
// (n = 1) "abc def |" -> abc, def
// (n = 1) "abc 'def|" -> empty, 'def
// (n = 1) "abc def. |" -> beginning-of-sentence
// (n = 1) "abc def . |" -> beginning-of-sentence
// (n = 2) "abc def|" -> beginning-of-sentence, abc
// (n = 2) "abc def |" -> beginning-of-sentence, abc
// (n = 2) "abc 'def|" -> empty. The context is different from "abc def", but we cannot
// represent this situation using NgramContext. See TODO in the method.
// TODO: The next example's result should be "abc, def". This have to be fixed before we
// retrieve the prior context of Beginning-of-Sentence.
// (n = 2) "abc def. |" -> beginning-of-sentence, abc
// (n = 2) "abc def . |" -> abc, def
// (n = 2) "abc|" -> beginning-of-sentence
// (n = 2) "abc |" -> beginning-of-sentence
// (n = 2) "abc. def|" -> beginning-of-sentence
@Nonnull
public static NgramContext getNgramContextFromNthPreviousWord(final CharSequence prev, final SpacingAndPunctuations spacingAndPunctuations, final int n) {
if (prev == null)
return NgramContext.EMPTY_PREV_WORDS_INFO;
final String[] lines = NEWLINE_REGEX.split(prev);
if (lines.length == 0) {
return new NgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO);
}
final String[] w = SPACE_REGEX.split(lines[lines.length - 1]);
final WordInfo[] prevWordsInfo = new WordInfo[DecoderSpecificConstants.MAX_PREV_WORD_COUNT_FOR_N_GRAM];
Arrays.fill(prevWordsInfo, WordInfo.EMPTY_WORD_INFO);
for (int i = 0; i < prevWordsInfo.length; i++) {
final int focusedWordIndex = w.length - n - i;
// Referring to the word after the focused word.
if ((focusedWordIndex + 1) >= 0 && (focusedWordIndex + 1) < w.length) {
final String wordFollowingTheNthPrevWord = w[focusedWordIndex + 1];
if (!wordFollowingTheNthPrevWord.isEmpty()) {
final char firstChar = wordFollowingTheNthPrevWord.charAt(0);
if (spacingAndPunctuations.isWordConnector(firstChar)) {
// TODO: Return meaningful context for this case.
break;
}
}
}
// If we can't find (n + i) words, the context is beginning-of-sentence.
if (focusedWordIndex < 0) {
prevWordsInfo[i] = WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO;
break;
}
final String focusedWord = w[focusedWordIndex];
// If the word is empty, the context is beginning-of-sentence.
final int length = focusedWord.length();
if (length <= 0) {
prevWordsInfo[i] = WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO;
break;
}
// If the word ends in a sentence terminator, the context is beginning-of-sentence.
final char lastChar = focusedWord.charAt(length - 1);
if (spacingAndPunctuations.isSentenceTerminator(lastChar)) {
prevWordsInfo[i] = WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO;
break;
}
// TODO: Return meaningful context for this case.
if (spacingAndPunctuations.isWordSeparator(lastChar) || spacingAndPunctuations.isWordConnector(lastChar)) {
break;
}
prevWordsInfo[i] = new WordInfo(focusedWord);
}
return new NgramContext(prevWordsInfo);
}
use of com.android.inputmethod.latin.NgramContext in project android_packages_inputmethods_LatinIME by CyanogenMod.
the class WordInputEventForPersonalization method createInputEventFrom.
// Process a list of words and return a list of {@link WordInputEventForPersonalization}
// objects.
public static ArrayList<WordInputEventForPersonalization> createInputEventFrom(final List<String> tokens, final int timestamp, final SpacingAndPunctuations spacingAndPunctuations, final Locale locale) {
final ArrayList<WordInputEventForPersonalization> inputEvents = new ArrayList<>();
final int N = tokens.size();
NgramContext ngramContext = NgramContext.EMPTY_PREV_WORDS_INFO;
for (int i = 0; i < N; ++i) {
final String tempWord = tokens.get(i);
if (StringUtils.isEmptyStringOrWhiteSpaces(tempWord)) {
// just skip this token
if (DEBUG_TOKEN) {
Log.d(TAG, "--- isEmptyStringOrWhiteSpaces: \"" + tempWord + "\"");
}
continue;
}
if (!DictionaryInfoUtils.looksValidForDictionaryInsertion(tempWord, spacingAndPunctuations)) {
if (DEBUG_TOKEN) {
Log.d(TAG, "--- not looksValidForDictionaryInsertion: \"" + tempWord + "\"");
}
// Sentence terminator found. Split.
// TODO: Detect whether the context is beginning-of-sentence.
ngramContext = NgramContext.EMPTY_PREV_WORDS_INFO;
continue;
}
if (DEBUG_TOKEN) {
Log.d(TAG, "--- word: \"" + tempWord + "\"");
}
final WordInputEventForPersonalization inputEvent = detectWhetherVaildWordOrNotAndGetInputEvent(ngramContext, tempWord, timestamp, locale);
if (inputEvent == null) {
continue;
}
inputEvents.add(inputEvent);
ngramContext = ngramContext.getNextNgramContext(new NgramContext.WordInfo(tempWord));
}
return inputEvents;
}
use of com.android.inputmethod.latin.NgramContext in project android_packages_inputmethods_LatinIME by CyanogenMod.
the class AndroidSpellCheckerSession method fixWronglyInvalidatedWordWithSingleQuote.
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private SentenceSuggestionsInfo fixWronglyInvalidatedWordWithSingleQuote(TextInfo ti, SentenceSuggestionsInfo ssi) {
final CharSequence typedText = TextInfoCompatUtils.getCharSequenceOrString(ti);
if (!typedText.toString().contains(AndroidSpellCheckerService.SINGLE_QUOTE)) {
return null;
}
final int N = ssi.getSuggestionsCount();
final ArrayList<Integer> additionalOffsets = new ArrayList<>();
final ArrayList<Integer> additionalLengths = new ArrayList<>();
final ArrayList<SuggestionsInfo> additionalSuggestionsInfos = new ArrayList<>();
CharSequence currentWord = null;
for (int i = 0; i < N; ++i) {
final SuggestionsInfo si = ssi.getSuggestionsInfoAt(i);
final int flags = si.getSuggestionsAttributes();
if ((flags & SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY) == 0) {
continue;
}
final int offset = ssi.getOffsetAt(i);
final int length = ssi.getLengthAt(i);
final CharSequence subText = typedText.subSequence(offset, offset + length);
final NgramContext ngramContext = new NgramContext(new NgramContext.WordInfo(currentWord));
currentWord = subText;
if (!subText.toString().contains(AndroidSpellCheckerService.SINGLE_QUOTE)) {
continue;
}
// Split preserving spans.
final CharSequence[] splitTexts = SpannableStringUtils.split(subText, AndroidSpellCheckerService.SINGLE_QUOTE, true);
if (splitTexts == null || splitTexts.length <= 1) {
continue;
}
final int splitNum = splitTexts.length;
for (int j = 0; j < splitNum; ++j) {
final CharSequence splitText = splitTexts[j];
if (TextUtils.isEmpty(splitText)) {
continue;
}
if (mSuggestionsCache.getSuggestionsFromCache(splitText.toString()) == null) {
continue;
}
final int newLength = splitText.length();
// Neither RESULT_ATTR_IN_THE_DICTIONARY nor RESULT_ATTR_LOOKS_LIKE_TYPO
final int newFlags = 0;
final SuggestionsInfo newSi = new SuggestionsInfo(newFlags, EMPTY_STRING_ARRAY);
newSi.setCookieAndSequence(si.getCookie(), si.getSequence());
if (DBG) {
Log.d(TAG, "Override and remove old span over: " + splitText + ", " + offset + "," + newLength);
}
additionalOffsets.add(offset);
additionalLengths.add(newLength);
additionalSuggestionsInfos.add(newSi);
}
}
final int additionalSize = additionalOffsets.size();
if (additionalSize <= 0) {
return null;
}
final int suggestionsSize = N + additionalSize;
final int[] newOffsets = new int[suggestionsSize];
final int[] newLengths = new int[suggestionsSize];
final SuggestionsInfo[] newSuggestionsInfos = new SuggestionsInfo[suggestionsSize];
int i;
for (i = 0; i < N; ++i) {
newOffsets[i] = ssi.getOffsetAt(i);
newLengths[i] = ssi.getLengthAt(i);
newSuggestionsInfos[i] = ssi.getSuggestionsInfoAt(i);
}
for (; i < suggestionsSize; ++i) {
newOffsets[i] = additionalOffsets.get(i - N);
newLengths[i] = additionalLengths.get(i - N);
newSuggestionsInfos[i] = additionalSuggestionsInfos.get(i - N);
}
return new SentenceSuggestionsInfo(newSuggestionsInfos, newOffsets, newLengths);
}
use of com.android.inputmethod.latin.NgramContext in project android_packages_inputmethods_LatinIME by CyanogenMod.
the class AndroidSpellCheckerSession method onGetSuggestionsMultiple.
@Override
public SuggestionsInfo[] onGetSuggestionsMultiple(TextInfo[] textInfos, int suggestionsLimit, boolean sequentialWords) {
long ident = Binder.clearCallingIdentity();
try {
final int length = textInfos.length;
final SuggestionsInfo[] retval = new SuggestionsInfo[length];
for (int i = 0; i < length; ++i) {
final CharSequence prevWord;
if (sequentialWords && i > 0) {
final TextInfo prevTextInfo = textInfos[i - 1];
final CharSequence prevWordCandidate = TextInfoCompatUtils.getCharSequenceOrString(prevTextInfo);
// Note that an empty string would be used to indicate the initial word
// in the future.
prevWord = TextUtils.isEmpty(prevWordCandidate) ? null : prevWordCandidate;
} else {
prevWord = null;
}
final NgramContext ngramContext = new NgramContext(new NgramContext.WordInfo(prevWord));
final TextInfo textInfo = textInfos[i];
retval[i] = onGetSuggestionsInternal(textInfo, ngramContext, suggestionsLimit);
retval[i].setCookieAndSequence(textInfo.getCookie(), textInfo.getSequence());
}
return retval;
} finally {
Binder.restoreCallingIdentity(ident);
}
}
Aggregations