diff options
author | 2014-06-25 14:14:37 +0900 | |
---|---|---|
committer | 2014-06-25 14:14:37 +0900 | |
commit | e708b1bc2e11285ad404133b8de21719ce08acb5 (patch) | |
tree | 40789514163f738b73d8aa8b6c753334f53702b2 /java/src/com/android/inputmethod/latin/PrevWordsInfo.java | |
parent | 9bbc3aa02ab61d27cdbe2d9a7c0501433460223c (diff) | |
download | latinime-e708b1bc2e11285ad404133b8de21719ce08acb5.tar.gz latinime-e708b1bc2e11285ad404133b8de21719ce08acb5.tar.xz latinime-e708b1bc2e11285ad404133b8de21719ce08acb5.zip |
Make PrevWordsInfo have multiple words' information.
Bug: 14425059
Change-Id: I2bd6a872904a44b80f638a13d91a97559217cc1a
Diffstat (limited to 'java/src/com/android/inputmethod/latin/PrevWordsInfo.java')
-rw-r--r-- | java/src/com/android/inputmethod/latin/PrevWordsInfo.java | 119 |
1 files changed, 97 insertions, 22 deletions
diff --git a/java/src/com/android/inputmethod/latin/PrevWordsInfo.java b/java/src/com/android/inputmethod/latin/PrevWordsInfo.java index 42b311c69..5dda44445 100644 --- a/java/src/com/android/inputmethod/latin/PrevWordsInfo.java +++ b/java/src/com/android/inputmethod/latin/PrevWordsInfo.java @@ -16,47 +16,122 @@ package com.android.inputmethod.latin; +import java.util.Arrays; + +import com.android.inputmethod.latin.utils.StringUtils; + /** * Class to represent information of previous words. This class is used to add n-gram entries * into binary dictionaries, to get predictions, and to get suggestions. */ -// TODO: Support multiple previous words for n-gram. public class PrevWordsInfo { - public static final PrevWordsInfo EMPTY_PREV_WORDS_INFO = new PrevWordsInfo(null); + public static final PrevWordsInfo EMPTY_PREV_WORDS_INFO = + new PrevWordsInfo(WordInfo.EMPTY_WORD_INFO); public static final PrevWordsInfo BEGINNING_OF_SENTENCE = new PrevWordsInfo(); - // The word immediately before the considered word. null means we don't have any context - // including the "beginning of sentence context" - we just don't know what to predict. - // An example of that is after a comma. - // For simplicity of implementation, this may also be null transiently after the WordComposer - // was reset and before starting a new composing word, but we should never be calling - // getSuggetions* in this situation. - // This is an empty string when mIsBeginningOfSentence is true. - public final String mPrevWord; + /** + * Word information used to represent previous words information. + */ + public static class WordInfo { + public static final WordInfo EMPTY_WORD_INFO = new WordInfo(null); + public static final WordInfo BEGINNING_OF_SENTENCE = new WordInfo(); + + // This is an empty string when mIsBeginningOfSentence is true. + public final String mWord; + // TODO: Have sentence separator. + // Whether the current context is beginning of sentence or not. This is true when composing + // at the beginning of an input field or composing a word after a sentence separator. + public final boolean mIsBeginningOfSentence; + + // Beginning of sentence. + public WordInfo() { + mWord = ""; + mIsBeginningOfSentence = true; + } + + public WordInfo(final String word) { + mWord = word; + mIsBeginningOfSentence = false; + } + + public boolean isValid() { + return mWord != null; + } + } - // TODO: Have sentence separator. - // Whether the current context is beginning of sentence or not. This is true when composing at - // the beginning of an input field or composing a word after a sentence separator. - public final boolean mIsBeginningOfSentence; + // The words immediately before the considered word. EMPTY_WORD_INFO element means we don't + // have any context for that previous word including the "beginning of sentence context" - we + // just don't know what to predict using the information. An example of that is after a comma. + // For simplicity of implementation, elements may also be EMPTY_WORD_INFO transiently after the + // WordComposer was reset and before starting a new composing word, but we should never be + // calling getSuggetions* in this situation. + public WordInfo[] mPrevWordsInfo = new WordInfo[Constants.MAX_PREV_WORD_COUNT_FOR_N_GRAM]; // Beginning of sentence. public PrevWordsInfo() { - mPrevWord = ""; - mIsBeginningOfSentence = true; + mPrevWordsInfo[0] = WordInfo.BEGINNING_OF_SENTENCE; + Arrays.fill(mPrevWordsInfo, 1 /* start */, mPrevWordsInfo.length, WordInfo.EMPTY_WORD_INFO); } - public PrevWordsInfo(final String prevWord) { - mPrevWord = prevWord; - mIsBeginningOfSentence = false; + // Construct from the previous word information. + public PrevWordsInfo(final WordInfo prevWordInfo) { + mPrevWordsInfo[0] = prevWordInfo; + Arrays.fill(mPrevWordsInfo, 1 /* start */, mPrevWordsInfo.length, WordInfo.EMPTY_WORD_INFO); + } + + // Construct from WordInfo array. n-th element represents (n+1)-th previous word's information. + public PrevWordsInfo(final WordInfo[] prevWordsInfo) { + for (int i = 0; i < Constants.MAX_PREV_WORD_COUNT_FOR_N_GRAM; i++) { + mPrevWordsInfo[i] = + (prevWordsInfo.length > i) ? prevWordsInfo[i] : WordInfo.EMPTY_WORD_INFO; + } + } + + // Create next prevWordsInfo using current prevWordsInfo. + public PrevWordsInfo getNextPrevWordsInfo(final WordInfo wordInfo) { + final WordInfo[] prevWordsInfo = new WordInfo[Constants.MAX_PREV_WORD_COUNT_FOR_N_GRAM]; + prevWordsInfo[0] = wordInfo; + for (int i = 1; i < prevWordsInfo.length; i++) { + prevWordsInfo[i] = mPrevWordsInfo[i - 1]; + } + return new PrevWordsInfo(prevWordsInfo); } public boolean isValid() { - return mPrevWord != null; + return mPrevWordsInfo[0].isValid(); + } + + public void outputToArray(final int[][] codePointArrays, + final boolean[] isBeginningOfSentenceArray) { + for (int i = 0; i < mPrevWordsInfo.length; i++) { + final WordInfo wordInfo = mPrevWordsInfo[i]; + if (wordInfo == null || !wordInfo.isValid()) { + codePointArrays[i] = new int[0]; + isBeginningOfSentenceArray[i] = false; + continue; + } + codePointArrays[i] = StringUtils.toCodePointArray(wordInfo.mWord); + isBeginningOfSentenceArray[i] = wordInfo.mIsBeginningOfSentence; + } } @Override public String toString() { - return "PrevWord: " + mPrevWord + ", isBeginningOfSentence: " - + mIsBeginningOfSentence + "."; + final StringBuffer builder = new StringBuffer(); + for (int i = 0; i < mPrevWordsInfo.length; i++) { + final WordInfo wordInfo = mPrevWordsInfo[i]; + builder.append("PrevWord["); + builder.append(i); + builder.append("]: "); + if (!wordInfo.isValid()) { + builder.append("Empty. "); + continue; + } + builder.append(wordInfo.mWord); + builder.append(", isBeginningOfSentence: "); + builder.append(wordInfo.mIsBeginningOfSentence); + builder.append(". "); + } + return builder.toString(); } } |