Home | History | Annotate | Download | only in latin
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.inputmethod.latin;
     18 
     19 import com.android.inputmethod.latin.NgramContext.WordInfo;
     20 import com.android.inputmethod.latin.settings.SpacingAndPunctuations;
     21 import com.android.inputmethod.latin.utils.NgramContextUtils;
     22 
     23 import android.test.AndroidTestCase;
     24 import android.test.suitebuilder.annotation.SmallTest;
     25 
     26 @SmallTest
     27 public class NgramContextTests extends AndroidTestCase {
     28     public void testConstruct() {
     29         assertEquals(new NgramContext(new WordInfo("a")), new NgramContext(new WordInfo("a")));
     30         assertEquals(new NgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO),
     31                 new NgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO));
     32         assertEquals(new NgramContext(WordInfo.EMPTY_WORD_INFO),
     33                 new NgramContext(WordInfo.EMPTY_WORD_INFO));
     34         assertEquals(new NgramContext(WordInfo.EMPTY_WORD_INFO),
     35                 new NgramContext(WordInfo.EMPTY_WORD_INFO));
     36     }
     37 
     38     public void testIsBeginningOfSentenceContext() {
     39         assertFalse(new NgramContext().isBeginningOfSentenceContext());
     40         assertTrue(new NgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO)
     41                 .isBeginningOfSentenceContext());
     42         assertTrue(NgramContext.BEGINNING_OF_SENTENCE.isBeginningOfSentenceContext());
     43         assertFalse(new NgramContext(new WordInfo("a")).isBeginningOfSentenceContext());
     44         assertFalse(new NgramContext(new WordInfo("")).isBeginningOfSentenceContext());
     45         assertFalse(new NgramContext(WordInfo.EMPTY_WORD_INFO).isBeginningOfSentenceContext());
     46         assertTrue(new NgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO, new WordInfo("a"))
     47                 .isBeginningOfSentenceContext());
     48         assertFalse(new NgramContext(new WordInfo("a"), WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO)
     49                 .isBeginningOfSentenceContext());
     50         assertFalse(new NgramContext(
     51                 WordInfo.EMPTY_WORD_INFO, WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO)
     52                 .isBeginningOfSentenceContext());
     53     }
     54 
     55     public void testGetNextNgramContext() {
     56         final NgramContext ngramContext_a = new NgramContext(new WordInfo("a"));
     57         final NgramContext ngramContext_b_a =
     58                 ngramContext_a.getNextNgramContext(new WordInfo("b"));
     59         assertEquals("b", ngramContext_b_a.getNthPrevWord(1));
     60         assertEquals("a", ngramContext_b_a.getNthPrevWord(2));
     61         final NgramContext ngramContext_bos_b =
     62                 ngramContext_b_a.getNextNgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO);
     63         assertTrue(ngramContext_bos_b.isBeginningOfSentenceContext());
     64         assertEquals("b", ngramContext_bos_b.getNthPrevWord(2));
     65         final NgramContext ngramContext_c_bos =
     66                 ngramContext_b_a.getNextNgramContext(new WordInfo("c"));
     67         assertEquals("c", ngramContext_c_bos.getNthPrevWord(1));
     68     }
     69 
     70     public void testExtractPrevWordsContextTest() {
     71         final NgramContext ngramContext_bos =
     72                 new NgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO);
     73         assertEquals("<S>", ngramContext_bos.extractPrevWordsContext());
     74         final NgramContext ngramContext_a = new NgramContext(new WordInfo("a"));
     75         final NgramContext ngramContext_b_a =
     76                 ngramContext_a.getNextNgramContext(new WordInfo("b"));
     77         assertEquals("b", ngramContext_b_a.getNthPrevWord(1));
     78         assertEquals("a", ngramContext_b_a.getNthPrevWord(2));
     79         assertEquals("a b", ngramContext_b_a.extractPrevWordsContext());
     80 
     81         final NgramContext ngramContext_bos_b =
     82                 ngramContext_b_a.getNextNgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO);
     83         assertTrue(ngramContext_bos_b.isBeginningOfSentenceContext());
     84         assertEquals("b", ngramContext_bos_b.getNthPrevWord(2));
     85         assertEquals("a b <S>", ngramContext_bos_b.extractPrevWordsContext());
     86 
     87         final NgramContext ngramContext_empty = new NgramContext(WordInfo.EMPTY_WORD_INFO);
     88         assertEquals("", ngramContext_empty.extractPrevWordsContext());
     89         final NgramContext ngramContext_a_empty =
     90                 ngramContext_empty.getNextNgramContext(new WordInfo("a"));
     91         assertEquals("a", ngramContext_a_empty.getNthPrevWord(1));
     92         assertEquals("a", ngramContext_a_empty.extractPrevWordsContext());
     93     }
     94 
     95     public void testExtractPrevWordsContextArray() {
     96         final NgramContext ngramContext_bos =
     97                 new NgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO);
     98         assertEquals("<S>", ngramContext_bos.extractPrevWordsContext());
     99         assertEquals(1, ngramContext_bos.extractPrevWordsContextArray().length);
    100         final NgramContext ngramContext_a = new NgramContext(new WordInfo("a"));
    101         final NgramContext ngramContext_b_a =
    102                 ngramContext_a.getNextNgramContext(new WordInfo("b"));
    103         assertEquals(2, ngramContext_b_a.extractPrevWordsContextArray().length);
    104         assertEquals("b", ngramContext_b_a.getNthPrevWord(1));
    105         assertEquals("a", ngramContext_b_a.getNthPrevWord(2));
    106         assertEquals("a", ngramContext_b_a.extractPrevWordsContextArray()[0]);
    107         assertEquals("b", ngramContext_b_a.extractPrevWordsContextArray()[1]);
    108 
    109         final NgramContext ngramContext_bos_b =
    110                 ngramContext_b_a.getNextNgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO);
    111         assertTrue(ngramContext_bos_b.isBeginningOfSentenceContext());
    112         assertEquals(3, ngramContext_bos_b.extractPrevWordsContextArray().length);
    113         assertEquals("b", ngramContext_bos_b.getNthPrevWord(2));
    114         assertEquals("a", ngramContext_bos_b.extractPrevWordsContextArray()[0]);
    115         assertEquals("b", ngramContext_bos_b.extractPrevWordsContextArray()[1]);
    116         assertEquals("<S>", ngramContext_bos_b.extractPrevWordsContextArray()[2]);
    117 
    118         final NgramContext ngramContext_empty = new NgramContext(WordInfo.EMPTY_WORD_INFO);
    119         assertEquals(0, ngramContext_empty.extractPrevWordsContextArray().length);
    120         final NgramContext ngramContext_a_empty =
    121                 ngramContext_empty.getNextNgramContext(new WordInfo("a"));
    122         assertEquals(1, ngramContext_a_empty.extractPrevWordsContextArray().length);
    123         assertEquals("a", ngramContext_a_empty.extractPrevWordsContextArray()[0]);
    124     }
    125 
    126     public void testGetNgramContextFromNthPreviousWord() {
    127         SpacingAndPunctuations spacingAndPunctuations = new SpacingAndPunctuations(
    128                 mContext.getResources());
    129         assertEquals("<S>", NgramContextUtils.getNgramContextFromNthPreviousWord("",
    130                 spacingAndPunctuations, 1).extractPrevWordsContext());
    131         assertEquals("<S> b", NgramContextUtils.getNgramContextFromNthPreviousWord("a. b ",
    132                 spacingAndPunctuations, 1).extractPrevWordsContext());
    133         assertEquals("<S> b", NgramContextUtils.getNgramContextFromNthPreviousWord("a? b ",
    134                 spacingAndPunctuations, 1).extractPrevWordsContext());
    135         assertEquals("<S> b", NgramContextUtils.getNgramContextFromNthPreviousWord("a! b ",
    136                 spacingAndPunctuations, 1).extractPrevWordsContext());
    137         assertEquals("<S> b", NgramContextUtils.getNgramContextFromNthPreviousWord("a\nb ",
    138                 spacingAndPunctuations, 1).extractPrevWordsContext());
    139         assertEquals("<S> a b", NgramContextUtils.getNgramContextFromNthPreviousWord("a b ",
    140                 spacingAndPunctuations, 1).extractPrevWordsContext());
    141         assertFalse(NgramContextUtils
    142                 .getNgramContextFromNthPreviousWord("a b c d e", spacingAndPunctuations, 1)
    143                 .extractPrevWordsContext().startsWith("<S>"));
    144     }
    145 }
    146