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 static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
22 
23 import androidx.test.InstrumentationRegistry;
24 import androidx.test.filters.SmallTest;
25 import androidx.test.runner.AndroidJUnit4;
26 
27 import com.android.inputmethod.latin.NgramContext.WordInfo;
28 import com.android.inputmethod.latin.settings.SpacingAndPunctuations;
29 import com.android.inputmethod.latin.utils.NgramContextUtils;
30 
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 
34 @SmallTest
35 @RunWith(AndroidJUnit4.class)
36 public class NgramContextTests {
37 
38     @Test
testConstruct()39     public void testConstruct() {
40         assertEquals(new NgramContext(new WordInfo("a")), new NgramContext(new WordInfo("a")));
41         assertEquals(new NgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO),
42                 new NgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO));
43         assertEquals(new NgramContext(WordInfo.EMPTY_WORD_INFO),
44                 new NgramContext(WordInfo.EMPTY_WORD_INFO));
45         assertEquals(new NgramContext(WordInfo.EMPTY_WORD_INFO),
46                 new NgramContext(WordInfo.EMPTY_WORD_INFO));
47     }
48 
49     @Test
testIsBeginningOfSentenceContext()50     public void testIsBeginningOfSentenceContext() {
51         assertFalse(new NgramContext().isBeginningOfSentenceContext());
52         assertTrue(new NgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO)
53                 .isBeginningOfSentenceContext());
54         assertTrue(NgramContext.BEGINNING_OF_SENTENCE.isBeginningOfSentenceContext());
55         assertFalse(new NgramContext(new WordInfo("a")).isBeginningOfSentenceContext());
56         assertFalse(new NgramContext(new WordInfo("")).isBeginningOfSentenceContext());
57         assertFalse(new NgramContext(WordInfo.EMPTY_WORD_INFO).isBeginningOfSentenceContext());
58         assertTrue(new NgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO, new WordInfo("a"))
59                 .isBeginningOfSentenceContext());
60         assertFalse(new NgramContext(new WordInfo("a"), WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO)
61                 .isBeginningOfSentenceContext());
62         assertFalse(new NgramContext(
63                 WordInfo.EMPTY_WORD_INFO, WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO)
64                 .isBeginningOfSentenceContext());
65     }
66 
67     @Test
testGetNextNgramContext()68     public void testGetNextNgramContext() {
69         final NgramContext ngramContext_a = new NgramContext(new WordInfo("a"));
70         final NgramContext ngramContext_b_a =
71                 ngramContext_a.getNextNgramContext(new WordInfo("b"));
72         assertEquals("b", ngramContext_b_a.getNthPrevWord(1));
73         assertEquals("a", ngramContext_b_a.getNthPrevWord(2));
74         final NgramContext ngramContext_bos_b =
75                 ngramContext_b_a.getNextNgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO);
76         assertTrue(ngramContext_bos_b.isBeginningOfSentenceContext());
77         assertEquals("b", ngramContext_bos_b.getNthPrevWord(2));
78         final NgramContext ngramContext_c_bos =
79                 ngramContext_b_a.getNextNgramContext(new WordInfo("c"));
80         assertEquals("c", ngramContext_c_bos.getNthPrevWord(1));
81     }
82 
83     @Test
testExtractPrevWordsContextTest()84     public void testExtractPrevWordsContextTest() {
85         final NgramContext ngramContext_bos =
86                 new NgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO);
87         assertEquals("<S>", ngramContext_bos.extractPrevWordsContext());
88         final NgramContext ngramContext_a = new NgramContext(new WordInfo("a"));
89         final NgramContext ngramContext_b_a =
90                 ngramContext_a.getNextNgramContext(new WordInfo("b"));
91         assertEquals("b", ngramContext_b_a.getNthPrevWord(1));
92         assertEquals("a", ngramContext_b_a.getNthPrevWord(2));
93         assertEquals("a b", ngramContext_b_a.extractPrevWordsContext());
94 
95         final NgramContext ngramContext_bos_b =
96                 ngramContext_b_a.getNextNgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO);
97         assertTrue(ngramContext_bos_b.isBeginningOfSentenceContext());
98         assertEquals("b", ngramContext_bos_b.getNthPrevWord(2));
99         assertEquals("a b <S>", ngramContext_bos_b.extractPrevWordsContext());
100 
101         final NgramContext ngramContext_empty = new NgramContext(WordInfo.EMPTY_WORD_INFO);
102         assertEquals("", ngramContext_empty.extractPrevWordsContext());
103         final NgramContext ngramContext_a_empty =
104                 ngramContext_empty.getNextNgramContext(new WordInfo("a"));
105         assertEquals("a", ngramContext_a_empty.getNthPrevWord(1));
106         assertEquals("a", ngramContext_a_empty.extractPrevWordsContext());
107     }
108 
109     @Test
testExtractPrevWordsContextArray()110     public void testExtractPrevWordsContextArray() {
111         final NgramContext ngramContext_bos =
112                 new NgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO);
113         assertEquals("<S>", ngramContext_bos.extractPrevWordsContext());
114         assertEquals(1, ngramContext_bos.extractPrevWordsContextArray().length);
115         final NgramContext ngramContext_a = new NgramContext(new WordInfo("a"));
116         final NgramContext ngramContext_b_a =
117                 ngramContext_a.getNextNgramContext(new WordInfo("b"));
118         assertEquals(2, ngramContext_b_a.extractPrevWordsContextArray().length);
119         assertEquals("b", ngramContext_b_a.getNthPrevWord(1));
120         assertEquals("a", ngramContext_b_a.getNthPrevWord(2));
121         assertEquals("a", ngramContext_b_a.extractPrevWordsContextArray()[0]);
122         assertEquals("b", ngramContext_b_a.extractPrevWordsContextArray()[1]);
123 
124         final NgramContext ngramContext_bos_b =
125                 ngramContext_b_a.getNextNgramContext(WordInfo.BEGINNING_OF_SENTENCE_WORD_INFO);
126         assertTrue(ngramContext_bos_b.isBeginningOfSentenceContext());
127         assertEquals(3, ngramContext_bos_b.extractPrevWordsContextArray().length);
128         assertEquals("b", ngramContext_bos_b.getNthPrevWord(2));
129         assertEquals("a", ngramContext_bos_b.extractPrevWordsContextArray()[0]);
130         assertEquals("b", ngramContext_bos_b.extractPrevWordsContextArray()[1]);
131         assertEquals("<S>", ngramContext_bos_b.extractPrevWordsContextArray()[2]);
132 
133         final NgramContext ngramContext_empty = new NgramContext(WordInfo.EMPTY_WORD_INFO);
134         assertEquals(0, ngramContext_empty.extractPrevWordsContextArray().length);
135         final NgramContext ngramContext_a_empty =
136                 ngramContext_empty.getNextNgramContext(new WordInfo("a"));
137         assertEquals(1, ngramContext_a_empty.extractPrevWordsContextArray().length);
138         assertEquals("a", ngramContext_a_empty.extractPrevWordsContextArray()[0]);
139     }
140 
141     @Test
testGetNgramContextFromNthPreviousWord()142     public void testGetNgramContextFromNthPreviousWord() {
143         SpacingAndPunctuations spacingAndPunctuations = new SpacingAndPunctuations(
144                 InstrumentationRegistry.getTargetContext().getResources());
145         assertEquals("<S>", NgramContextUtils.getNgramContextFromNthPreviousWord("",
146                 spacingAndPunctuations, 1).extractPrevWordsContext());
147         assertEquals("<S> b", NgramContextUtils.getNgramContextFromNthPreviousWord("a. b ",
148                 spacingAndPunctuations, 1).extractPrevWordsContext());
149         assertEquals("<S> b", NgramContextUtils.getNgramContextFromNthPreviousWord("a? b ",
150                 spacingAndPunctuations, 1).extractPrevWordsContext());
151         assertEquals("<S> b", NgramContextUtils.getNgramContextFromNthPreviousWord("a! b ",
152                 spacingAndPunctuations, 1).extractPrevWordsContext());
153         assertEquals("<S> b", NgramContextUtils.getNgramContextFromNthPreviousWord("a\nb ",
154                 spacingAndPunctuations, 1).extractPrevWordsContext());
155         assertEquals("<S> a b", NgramContextUtils.getNgramContextFromNthPreviousWord("a b ",
156                 spacingAndPunctuations, 1).extractPrevWordsContext());
157         assertFalse(NgramContextUtils
158                 .getNgramContextFromNthPreviousWord("a b c d e", spacingAndPunctuations, 1)
159                 .extractPrevWordsContext().startsWith("<S>"));
160     }
161 }
162