Search in sources :

Example 1 with WordInfo

use of com.android.inputmethod.latin.NgramContext.WordInfo in project android_packages_inputmethods_LatinIME by CyanogenMod.

the class NgramContextTests method testIsBeginningOfSentenceContext.

public void testIsBeginningOfSentenceContext() {
    assertFalse(new NgramContext().isBeginningOfSentenceContext());
    assertTrue(new NgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO).isBeginningOfSentenceContext());
    assertTrue(NgramContext.BEGINNING_OF_SENTENCE.isBeginningOfSentenceContext());
    assertFalse(new NgramContext(new WordInfo("a")).isBeginningOfSentenceContext());
    assertFalse(new NgramContext(new WordInfo("")).isBeginningOfSentenceContext());
    assertFalse(new NgramContext(WordInfo.EMPTY_WORD_INFO).isBeginningOfSentenceContext());
    assertTrue(new NgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO, new WordInfo("a")).isBeginningOfSentenceContext());
    assertFalse(new NgramContext(new WordInfo("a"), WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO).isBeginningOfSentenceContext());
    assertFalse(new NgramContext(WordInfo.EMPTY_WORD_INFO, WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO).isBeginningOfSentenceContext());
}
Also used : WordInfo(com.android.inputmethod.latin.NgramContext.WordInfo)

Example 2 with WordInfo

use of com.android.inputmethod.latin.NgramContext.WordInfo in project android_packages_inputmethods_LatinIME by CyanogenMod.

the class NgramContextUtils method getNgramContextFromNthPreviousWord.

// Get context information from nth word before the cursor. n = 1 retrieves the words
// immediately before the cursor, n = 2 retrieves the words before that, and so on. This splits
// on whitespace only.
// Also, it won't return words that end in a separator (if the nth word before the cursor
// ends in a separator, it returns information representing beginning-of-sentence).
// Example (when Constants.MAX_PREV_WORD_COUNT_FOR_N_GRAM is 2):
// (n = 1) "abc def|" -> abc, def
// (n = 1) "abc def |" -> abc, def
// (n = 1) "abc 'def|" -> empty, 'def
// (n = 1) "abc def. |" -> beginning-of-sentence
// (n = 1) "abc def . |" -> beginning-of-sentence
// (n = 2) "abc def|" -> beginning-of-sentence, abc
// (n = 2) "abc def |" -> beginning-of-sentence, abc
// (n = 2) "abc 'def|" -> empty. The context is different from "abc def", but we cannot
// represent this situation using NgramContext. See TODO in the method.
// TODO: The next example's result should be "abc, def". This have to be fixed before we
// retrieve the prior context of Beginning-of-Sentence.
// (n = 2) "abc def. |" -> beginning-of-sentence, abc
// (n = 2) "abc def . |" -> abc, def
// (n = 2) "abc|" -> beginning-of-sentence
// (n = 2) "abc |" -> beginning-of-sentence
// (n = 2) "abc. def|" -> beginning-of-sentence
@Nonnull
public static NgramContext getNgramContextFromNthPreviousWord(final CharSequence prev, final SpacingAndPunctuations spacingAndPunctuations, final int n) {
    if (prev == null)
        return NgramContext.EMPTY_PREV_WORDS_INFO;
    final String[] lines = NEWLINE_REGEX.split(prev);
    if (lines.length == 0) {
        return new NgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO);
    }
    final String[] w = SPACE_REGEX.split(lines[lines.length - 1]);
    final WordInfo[] prevWordsInfo = new WordInfo[DecoderSpecificConstants.MAX_PREV_WORD_COUNT_FOR_N_GRAM];
    Arrays.fill(prevWordsInfo, WordInfo.EMPTY_WORD_INFO);
    for (int i = 0; i < prevWordsInfo.length; i++) {
        final int focusedWordIndex = w.length - n - i;
        // Referring to the word after the focused word.
        if ((focusedWordIndex + 1) >= 0 && (focusedWordIndex + 1) < w.length) {
            final String wordFollowingTheNthPrevWord = w[focusedWordIndex + 1];
            if (!wordFollowingTheNthPrevWord.isEmpty()) {
                final char firstChar = wordFollowingTheNthPrevWord.charAt(0);
                if (spacingAndPunctuations.isWordConnector(firstChar)) {
                    // TODO: Return meaningful context for this case.
                    break;
                }
            }
        }
        // If we can't find (n + i) words, the context is beginning-of-sentence.
        if (focusedWordIndex < 0) {
            prevWordsInfo[i] = WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO;
            break;
        }
        final String focusedWord = w[focusedWordIndex];
        // If the word is empty, the context is beginning-of-sentence.
        final int length = focusedWord.length();
        if (length <= 0) {
            prevWordsInfo[i] = WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO;
            break;
        }
        // If the word ends in a sentence terminator, the context is beginning-of-sentence.
        final char lastChar = focusedWord.charAt(length - 1);
        if (spacingAndPunctuations.isSentenceTerminator(lastChar)) {
            prevWordsInfo[i] = WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO;
            break;
        }
        // TODO: Return meaningful context for this case.
        if (spacingAndPunctuations.isWordSeparator(lastChar) || spacingAndPunctuations.isWordConnector(lastChar)) {
            break;
        }
        prevWordsInfo[i] = new WordInfo(focusedWord);
    }
    return new NgramContext(prevWordsInfo);
}
Also used : NgramContext(com.android.inputmethod.latin.NgramContext) WordInfo(com.android.inputmethod.latin.NgramContext.WordInfo) Nonnull(javax.annotation.Nonnull)

Example 3 with WordInfo

use of com.android.inputmethod.latin.NgramContext.WordInfo in project android_packages_inputmethods_LatinIME by CyanogenMod.

the class UserHistoryDictionaryTestsHelper method addWordsToDictionary.

private static void addWordsToDictionary(final UserHistoryDictionary dict, final List<String> words, final int timestamp) {
    NgramContext ngramContext = NgramContext.getEmptyPrevWordsContext(BinaryDictionary.MAX_PREV_WORD_COUNT_FOR_N_GRAM);
    for (final String word : words) {
        UserHistoryDictionary.addToDictionary(dict, ngramContext, word, true, timestamp);
        ngramContext = ngramContext.getNextNgramContext(new WordInfo(word));
    }
}
Also used : NgramContext(com.android.inputmethod.latin.NgramContext) WordInfo(com.android.inputmethod.latin.NgramContext.WordInfo)

Example 4 with WordInfo

use of com.android.inputmethod.latin.NgramContext.WordInfo in project android_packages_inputmethods_LatinIME by CyanogenMod.

the class NgramContextTests method testExtractPrevWordsContextTest.

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());
    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());
}
Also used : WordInfo(com.android.inputmethod.latin.NgramContext.WordInfo)

Example 5 with WordInfo

use of com.android.inputmethod.latin.NgramContext.WordInfo in project android_packages_inputmethods_LatinIME by CyanogenMod.

the class NgramContextTests method testExtractPrevWordsContextArray.

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]);
    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(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]);
}
Also used : WordInfo(com.android.inputmethod.latin.NgramContext.WordInfo)

Aggregations

WordInfo (com.android.inputmethod.latin.NgramContext.WordInfo)6 NgramContext (com.android.inputmethod.latin.NgramContext)2 Nonnull (javax.annotation.Nonnull)1