am 148aab12
: Merge "Add util method in ngramcontext for context array"
* commit '148aab122883d5a53c890fd7795039f4aa087302': Add util method in ngramcontext for context array
This commit is contained in:
commit
1d22e76800
2 changed files with 65 additions and 0 deletions
|
@ -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<String> 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();
|
||||
}
|
||||
|
|
|
@ -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("<S>", 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 <S>", ngramContext_bos_b.extractPrevWordsContext());
|
||||
}
|
||||
|
||||
public void testExtractPrevWordsContextArray() {
|
||||
final NgramContext ngramContext_bos =
|
||||
new NgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO);
|
||||
assertEquals("<S>", 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("<S>", ngramContext_bos_b.extractPrevWordsContextArray()[2]);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue