aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/latin/NgramContext.java
diff options
context:
space:
mode:
authorMohammadinamul Sheik <inamul@google.com>2015-02-05 02:10:56 +0000
committerAndroid Git Automerger <android-git-automerger@android.com>2015-02-05 02:10:56 +0000
commit124039cfe1c10721a36ea31c01ec3a8a3fa93c49 (patch)
treea66a91b32a838f782d059b5f0b8b871e9bd58c57 /java/src/com/android/inputmethod/latin/NgramContext.java
parentbaf0bd90a3497afc665d23c8ebe1592278ab9699 (diff)
parentbae6848434c0618887bd3fd441dd5ce16a2375fd (diff)
downloadlatinime-124039cfe1c10721a36ea31c01ec3a8a3fa93c49.tar.gz
latinime-124039cfe1c10721a36ea31c01ec3a8a3fa93c49.tar.xz
latinime-124039cfe1c10721a36ea31c01ec3a8a3fa93c49.zip
am bae68484: Merge "AOSP changes to use KeyboardLayout to support the new DictionaryFacilitator"
* commit 'bae6848434c0618887bd3fd441dd5ce16a2375fd': AOSP changes to use KeyboardLayout to support the new DictionaryFacilitator
Diffstat (limited to 'java/src/com/android/inputmethod/latin/NgramContext.java')
-rw-r--r--java/src/com/android/inputmethod/latin/NgramContext.java30
1 files changed, 30 insertions, 0 deletions
diff --git a/java/src/com/android/inputmethod/latin/NgramContext.java b/java/src/com/android/inputmethod/latin/NgramContext.java
index b47731229..2d66fb000 100644
--- a/java/src/com/android/inputmethod/latin/NgramContext.java
+++ b/java/src/com/android/inputmethod/latin/NgramContext.java
@@ -22,6 +22,7 @@ import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.latin.common.Constants;
import com.android.inputmethod.latin.common.StringUtils;
+import java.util.ArrayList;
import java.util.Arrays;
import javax.annotation.Nonnull;
@@ -38,6 +39,10 @@ public class NgramContext {
public static final NgramContext BEGINNING_OF_SENTENCE =
new NgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO);
+ public static final String BEGINNING_OF_SENTENCE_TAG = "<S>";
+
+ public static final String CONTEXT_SEPARATOR = " ";
+
/**
* Word information used to represent previous words information.
*/
@@ -114,6 +119,31 @@ public class NgramContext {
return new NgramContext(prevWordsInfo);
}
+
+ /**
+ * Extracts the previous words context.
+ *
+ * @return a String with the previous words separated by white space.
+ */
+ public String extractPrevWordsContext() {
+ final ArrayList<String> terms = new ArrayList<>();
+ for (int i = mPrevWordsInfo.length - 1; i >= 0; --i) {
+ if (mPrevWordsInfo[i] != null && mPrevWordsInfo[i].isValid()) {
+ final NgramContext.WordInfo wordInfo = mPrevWordsInfo[i];
+ if (wordInfo.mIsBeginningOfSentence) {
+ terms.add(BEGINNING_OF_SENTENCE_TAG);
+ } else {
+ final String term = wordInfo.mWord.toString();
+ if (!term.isEmpty()) {
+ terms.add(term);
+ }
+ }
+ }
+ }
+ return terms.size() == 0 ? BEGINNING_OF_SENTENCE_TAG
+ : TextUtils.join(CONTEXT_SEPARATOR, terms);
+ }
+
public boolean isValid() {
return mPrevWordsCount > 0 && mPrevWordsInfo[0].isValid();
}