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
main
Chieu Nguyen 2015-03-09 18:54:39 -07:00
parent 375035905d
commit 21a28abc6a
2 changed files with 19 additions and 5 deletions

View File

@ -142,8 +142,7 @@ public class NgramContext {
} }
} }
} }
return terms.size() == 0 ? BEGINNING_OF_SENTENCE_TAG return TextUtils.join(CONTEXT_SEPARATOR, terms);
: TextUtils.join(CONTEXT_SEPARATOR, terms);
} }
/** /**
@ -166,9 +165,7 @@ public class NgramContext {
} }
} }
} }
final String[] contextStringArray = prevTermList.size() == 0 ? final String[] contextStringArray = prevTermList.toArray(new String[prevTermList.size()]);
new String[] { BEGINNING_OF_SENTENCE_TAG }
: prevTermList.toArray(new String[prevTermList.size()]);
return contextStringArray; return contextStringArray;
} }

View File

@ -81,15 +81,24 @@ public class NgramContextTests extends AndroidTestCase {
assertTrue(ngramContext_bos_b.isBeginningOfSentenceContext()); assertTrue(ngramContext_bos_b.isBeginningOfSentenceContext());
assertEquals("b", ngramContext_bos_b.getNthPrevWord(2)); assertEquals("b", ngramContext_bos_b.getNthPrevWord(2));
assertEquals("a b <S>", ngramContext_bos_b.extractPrevWordsContext()); 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() { public void testExtractPrevWordsContextArray() {
final NgramContext ngramContext_bos = final NgramContext ngramContext_bos =
new NgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO); new NgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO);
assertEquals("<S>", ngramContext_bos.extractPrevWordsContext()); assertEquals("<S>", ngramContext_bos.extractPrevWordsContext());
assertEquals(1, ngramContext_bos.extractPrevWordsContextArray().length);
final NgramContext ngramContext_a = new NgramContext(new WordInfo("a")); final NgramContext ngramContext_a = new NgramContext(new WordInfo("a"));
final NgramContext ngramContext_b_a = final NgramContext ngramContext_b_a =
ngramContext_a.getNextNgramContext(new WordInfo("b")); ngramContext_a.getNextNgramContext(new WordInfo("b"));
assertEquals(2, ngramContext_b_a.extractPrevWordsContextArray().length);
assertEquals("b", ngramContext_b_a.getNthPrevWord(1)); assertEquals("b", ngramContext_b_a.getNthPrevWord(1));
assertEquals("a", ngramContext_b_a.getNthPrevWord(2)); assertEquals("a", ngramContext_b_a.getNthPrevWord(2));
assertEquals("a", ngramContext_b_a.extractPrevWordsContextArray()[0]); assertEquals("a", ngramContext_b_a.extractPrevWordsContextArray()[0]);
@ -98,9 +107,17 @@ public class NgramContextTests extends AndroidTestCase {
final NgramContext ngramContext_bos_b = final NgramContext ngramContext_bos_b =
ngramContext_b_a.getNextNgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO); ngramContext_b_a.getNextNgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO);
assertTrue(ngramContext_bos_b.isBeginningOfSentenceContext()); assertTrue(ngramContext_bos_b.isBeginningOfSentenceContext());
assertEquals(3, ngramContext_bos_b.extractPrevWordsContextArray().length);
assertEquals("b", ngramContext_bos_b.getNthPrevWord(2)); assertEquals("b", ngramContext_bos_b.getNthPrevWord(2));
assertEquals("a", ngramContext_bos_b.extractPrevWordsContextArray()[0]); assertEquals("a", ngramContext_bos_b.extractPrevWordsContextArray()[0]);
assertEquals("b", ngramContext_bos_b.extractPrevWordsContextArray()[1]); assertEquals("b", ngramContext_bos_b.extractPrevWordsContextArray()[1]);
assertEquals("<S>", ngramContext_bos_b.extractPrevWordsContextArray()[2]); 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]);
} }
} }