Handle empty previous ngram context correctly.
Fixes bug where empty previous ngram context not at the
beginning of a sentence incorrectly returned the
beginning-of-sentence marker.
Bug: 19612336
Change-Id: I5ddf3408e20dcedf70487b85c983cd3fc12550e7
diff --git a/java/src/com/android/inputmethod/latin/NgramContext.java b/java/src/com/android/inputmethod/latin/NgramContext.java
index c935158..aeeff61 100644
--- a/java/src/com/android/inputmethod/latin/NgramContext.java
+++ b/java/src/com/android/inputmethod/latin/NgramContext.java
@@ -142,8 +142,7 @@
}
}
}
- return terms.size() == 0 ? BEGINNING_OF_SENTENCE_TAG
- : TextUtils.join(CONTEXT_SEPARATOR, terms);
+ return TextUtils.join(CONTEXT_SEPARATOR, terms);
}
/**
@@ -166,9 +165,7 @@
}
}
}
- final String[] contextStringArray = prevTermList.size() == 0 ?
- new String[] { BEGINNING_OF_SENTENCE_TAG }
- : prevTermList.toArray(new String[prevTermList.size()]);
+ final String[] contextStringArray = prevTermList.toArray(new String[prevTermList.size()]);
return contextStringArray;
}
diff --git a/tests/src/com/android/inputmethod/latin/NgramContextTests.java b/tests/src/com/android/inputmethod/latin/NgramContextTests.java
index 97143a3..7749bff 100644
--- a/tests/src/com/android/inputmethod/latin/NgramContextTests.java
+++ b/tests/src/com/android/inputmethod/latin/NgramContextTests.java
@@ -81,15 +81,24 @@
assertTrue(ngramContext_bos_b.isBeginningOfSentenceContext());
assertEquals("b", ngramContext_bos_b.getNthPrevWord(2));
assertEquals("a b <S>", ngramContext_bos_b.extractPrevWordsContext());
+
+ final NgramContext ngramContext_empty = new NgramContext(WordInfo.EMPTY_WORD_INFO);
+ assertEquals("", ngramContext_empty.extractPrevWordsContext());
+ final NgramContext ngramContext_a_empty =
+ ngramContext_empty.getNextNgramContext(new WordInfo("a"));
+ assertEquals("a", ngramContext_a_empty.getNthPrevWord(1));
+ assertEquals("a", ngramContext_a_empty.extractPrevWordsContext());
}
public void testExtractPrevWordsContextArray() {
final NgramContext ngramContext_bos =
new NgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO);
assertEquals("<S>", ngramContext_bos.extractPrevWordsContext());
+ assertEquals(1, ngramContext_bos.extractPrevWordsContextArray().length);
final NgramContext ngramContext_a = new NgramContext(new WordInfo("a"));
final NgramContext ngramContext_b_a =
ngramContext_a.getNextNgramContext(new WordInfo("b"));
+ assertEquals(2, ngramContext_b_a.extractPrevWordsContextArray().length);
assertEquals("b", ngramContext_b_a.getNthPrevWord(1));
assertEquals("a", ngramContext_b_a.getNthPrevWord(2));
assertEquals("a", ngramContext_b_a.extractPrevWordsContextArray()[0]);
@@ -98,9 +107,17 @@
final NgramContext ngramContext_bos_b =
ngramContext_b_a.getNextNgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO);
assertTrue(ngramContext_bos_b.isBeginningOfSentenceContext());
+ assertEquals(3, ngramContext_bos_b.extractPrevWordsContextArray().length);
assertEquals("b", ngramContext_bos_b.getNthPrevWord(2));
assertEquals("a", ngramContext_bos_b.extractPrevWordsContextArray()[0]);
assertEquals("b", ngramContext_bos_b.extractPrevWordsContextArray()[1]);
assertEquals("<S>", ngramContext_bos_b.extractPrevWordsContextArray()[2]);
+
+ final NgramContext ngramContext_empty = new NgramContext(WordInfo.EMPTY_WORD_INFO);
+ assertEquals(0, ngramContext_empty.extractPrevWordsContextArray().length);
+ final NgramContext ngramContext_a_empty =
+ ngramContext_empty.getNextNgramContext(new WordInfo("a"));
+ assertEquals(1, ngramContext_a_empty.extractPrevWordsContextArray().length);
+ assertEquals("a", ngramContext_a_empty.extractPrevWordsContextArray()[0]);
}
}