From a568e0acb4a45707e554f63aede917bfa46b9dba Mon Sep 17 00:00:00 2001 From: Jatin Matani Date: Tue, 3 Mar 2015 13:08:06 -0800 Subject: [PATCH] Add util method in ngramcontext for context array Change-Id: I2e3e2ac7706be50ed2055b0c69b30e25a516a879 --- .../inputmethod/latin/NgramContext.java | 26 +++++++++++++ .../inputmethod/latin/NgramContextTests.java | 39 +++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/java/src/com/android/inputmethod/latin/NgramContext.java b/java/src/com/android/inputmethod/latin/NgramContext.java index 53bec6e59..c9351586e 100644 --- a/java/src/com/android/inputmethod/latin/NgramContext.java +++ b/java/src/com/android/inputmethod/latin/NgramContext.java @@ -146,6 +146,32 @@ public class NgramContext { : TextUtils.join(CONTEXT_SEPARATOR, terms); } + /** + * Extracts the previous words context. + * + * @return a String array with the previous words. + */ + public String[] extractPrevWordsContextArray() { + final ArrayList prevTermList = 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) { + prevTermList.add(BEGINNING_OF_SENTENCE_TAG); + } else { + final String term = wordInfo.mWord.toString(); + if (!term.isEmpty()) { + prevTermList.add(term); + } + } + } + } + final String[] contextStringArray = prevTermList.size() == 0 ? + new String[] { BEGINNING_OF_SENTENCE_TAG } + : prevTermList.toArray(new String[prevTermList.size()]); + return contextStringArray; + } + public boolean isValid() { return mPrevWordsCount > 0 && mPrevWordsInfo[0].isValid(); } diff --git a/tests/src/com/android/inputmethod/latin/NgramContextTests.java b/tests/src/com/android/inputmethod/latin/NgramContextTests.java index ab1819d0b..97143a3b6 100644 --- a/tests/src/com/android/inputmethod/latin/NgramContextTests.java +++ b/tests/src/com/android/inputmethod/latin/NgramContextTests.java @@ -64,4 +64,43 @@ public class NgramContextTests extends AndroidTestCase { ngramContext_b_a.getNextNgramContext(new WordInfo("c")); assertEquals("c", ngramContext_c_bos.getNthPrevWord(1)); } + + public void testExtractPrevWordsContextTest() { + final NgramContext ngramContext_bos = + new NgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO); + assertEquals("", ngramContext_bos.extractPrevWordsContext()); + final NgramContext ngramContext_a = new NgramContext(new WordInfo("a")); + final NgramContext ngramContext_b_a = + ngramContext_a.getNextNgramContext(new WordInfo("b")); + assertEquals("b", ngramContext_b_a.getNthPrevWord(1)); + assertEquals("a", ngramContext_b_a.getNthPrevWord(2)); + assertEquals("a b", ngramContext_b_a.extractPrevWordsContext()); + + final NgramContext ngramContext_bos_b = + ngramContext_b_a.getNextNgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO); + assertTrue(ngramContext_bos_b.isBeginningOfSentenceContext()); + assertEquals("b", ngramContext_bos_b.getNthPrevWord(2)); + assertEquals("a b ", ngramContext_bos_b.extractPrevWordsContext()); + } + + public void testExtractPrevWordsContextArray() { + final NgramContext ngramContext_bos = + new NgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO); + assertEquals("", ngramContext_bos.extractPrevWordsContext()); + final NgramContext ngramContext_a = new NgramContext(new WordInfo("a")); + final NgramContext ngramContext_b_a = + ngramContext_a.getNextNgramContext(new WordInfo("b")); + assertEquals("b", ngramContext_b_a.getNthPrevWord(1)); + assertEquals("a", ngramContext_b_a.getNthPrevWord(2)); + assertEquals("a", ngramContext_b_a.extractPrevWordsContextArray()[0]); + assertEquals("b", ngramContext_b_a.extractPrevWordsContextArray()[1]); + + final NgramContext ngramContext_bos_b = + ngramContext_b_a.getNextNgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO); + assertTrue(ngramContext_bos_b.isBeginningOfSentenceContext()); + assertEquals("b", ngramContext_bos_b.getNthPrevWord(2)); + assertEquals("a", ngramContext_bos_b.extractPrevWordsContextArray()[0]); + assertEquals("b", ngramContext_bos_b.extractPrevWordsContextArray()[1]); + assertEquals("", ngramContext_bos_b.extractPrevWordsContextArray()[2]); + } }