1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #include <memory>
17 #include <string>
18 #include <vector>
19
20 #include <gtest/gtest.h>
21 #include "flatbuffers/flatbuffers.h" // from @flatbuffers
22 #include "tensorflow/lite/interpreter.h"
23 #include "tensorflow/lite/kernels/test_util.h"
24 #include "tensorflow/lite/schema/schema_generated.h"
25 #include "tensorflow/lite/string_type.h"
26 #include "tensorflow/lite/string_util.h"
27
28 namespace tflite {
29 namespace {
30
31 using ::testing::ElementsAre;
32
33 static char kSentence[] = "The quick\t brown fox\n jumps over\n the lazy dog!";
34
35 class SkipGramOp : public SingleOpModel {
36 public:
SkipGramOp(int ngram_size,int max_skip_size,bool include_all_ngrams)37 SkipGramOp(int ngram_size, int max_skip_size, bool include_all_ngrams) {
38 input_ = AddInput(TensorType_STRING);
39 output_ = AddOutput(TensorType_STRING);
40
41 SetBuiltinOp(BuiltinOperator_SKIP_GRAM, BuiltinOptions_SkipGramOptions,
42 CreateSkipGramOptions(builder_, ngram_size, max_skip_size,
43 include_all_ngrams)
44 .Union());
45 BuildInterpreter({{1}});
46 }
SetInput(const string & content)47 void SetInput(const string& content) {
48 PopulateStringTensor(input_, {content});
49 }
50
GetOutput()51 std::vector<string> GetOutput() {
52 std::vector<string> ans;
53 TfLiteTensor* tensor = interpreter_->tensor(output_);
54
55 int num = GetStringCount(tensor);
56 for (int i = 0; i < num; i++) {
57 StringRef strref = GetString(tensor, i);
58 ans.push_back(string(strref.str, strref.len));
59 }
60 return ans;
61 }
62
63 private:
64 int input_;
65 int output_;
66 };
67
TEST(SkipGramTest,TestUnigram)68 TEST(SkipGramTest, TestUnigram) {
69 SkipGramOp m(1, 0, false);
70
71 m.SetInput(kSentence);
72 ASSERT_EQ(m.Invoke(), kTfLiteOk);
73 EXPECT_THAT(m.GetOutput(), testing::UnorderedElementsAreArray(
74 {"The", "quick", "brown", "fox", "jumps",
75 "over", "the", "lazy", "dog!"}));
76 }
77
TEST(SkipGramTest,TestBigram)78 TEST(SkipGramTest, TestBigram) {
79 SkipGramOp m(2, 0, false);
80 m.SetInput(kSentence);
81 ASSERT_EQ(m.Invoke(), kTfLiteOk);
82 EXPECT_THAT(m.GetOutput(),
83 testing::UnorderedElementsAreArray(
84 {"The quick", "quick brown", "brown fox", "fox jumps",
85 "jumps over", "over the", "the lazy", "lazy dog!"}));
86 }
87
TEST(SkipGramTest,TestAllBigram)88 TEST(SkipGramTest, TestAllBigram) {
89 SkipGramOp m(2, 0, true);
90 m.SetInput(kSentence);
91 ASSERT_EQ(m.Invoke(), kTfLiteOk);
92 EXPECT_THAT(m.GetOutput(),
93 testing::UnorderedElementsAreArray(
94 {// Unigram
95 "The", "quick", "brown", "fox", "jumps", "over", "the",
96 "lazy", "dog!",
97 // Bigram
98 "The quick", "quick brown", "brown fox", "fox jumps",
99 "jumps over", "over the", "the lazy", "lazy dog!"}));
100 }
101
TEST(SkipGramTest,TestAllTrigram)102 TEST(SkipGramTest, TestAllTrigram) {
103 SkipGramOp m(3, 0, true);
104 m.SetInput(kSentence);
105 ASSERT_EQ(m.Invoke(), kTfLiteOk);
106 EXPECT_THAT(m.GetOutput(),
107 testing::UnorderedElementsAreArray(
108 {// Unigram
109 "The", "quick", "brown", "fox", "jumps", "over", "the",
110 "lazy", "dog!",
111 // Bigram
112 "The quick", "quick brown", "brown fox", "fox jumps",
113 "jumps over", "over the", "the lazy", "lazy dog!",
114 // Trigram
115 "The quick brown", "quick brown fox", "brown fox jumps",
116 "fox jumps over", "jumps over the", "over the lazy",
117 "the lazy dog!"}));
118 }
119
TEST(SkipGramTest,TestSkip1Bigram)120 TEST(SkipGramTest, TestSkip1Bigram) {
121 SkipGramOp m(2, 1, false);
122 m.SetInput(kSentence);
123 ASSERT_EQ(m.Invoke(), kTfLiteOk);
124 EXPECT_THAT(
125 m.GetOutput(),
126 testing::UnorderedElementsAreArray(
127 {"The quick", "The brown", "quick brown", "quick fox", "brown fox",
128 "brown jumps", "fox jumps", "fox over", "jumps over", "jumps the",
129 "over the", "over lazy", "the lazy", "the dog!", "lazy dog!"}));
130 }
131
TEST(SkipGramTest,TestSkip2Bigram)132 TEST(SkipGramTest, TestSkip2Bigram) {
133 SkipGramOp m(2, 2, false);
134 m.SetInput(kSentence);
135 ASSERT_EQ(m.Invoke(), kTfLiteOk);
136 EXPECT_THAT(m.GetOutput(),
137 testing::UnorderedElementsAreArray(
138 {"The quick", "The brown", "The fox", "quick brown",
139 "quick fox", "quick jumps", "brown fox", "brown jumps",
140 "brown over", "fox jumps", "fox over", "fox the",
141 "jumps over", "jumps the", "jumps lazy", "over the",
142 "over lazy", "over dog!", "the lazy", "the dog!",
143 "lazy dog!"}));
144 }
145
TEST(SkipGramTest,TestSkip1Trigram)146 TEST(SkipGramTest, TestSkip1Trigram) {
147 SkipGramOp m(3, 1, false);
148 m.SetInput(kSentence);
149 ASSERT_EQ(m.Invoke(), kTfLiteOk);
150 EXPECT_THAT(m.GetOutput(),
151 testing::UnorderedElementsAreArray(
152 {"The quick brown", "The quick fox", "The brown fox",
153 "The brown jumps", "quick brown fox", "quick brown jumps",
154 "quick fox jumps", "quick fox over", "brown fox jumps",
155 "brown fox over", "brown jumps over", "brown jumps the",
156 "fox jumps over", "fox jumps the", "fox over the",
157 "fox over lazy", "jumps over the", "jumps over lazy",
158 "jumps the lazy", "jumps the dog!", "over the lazy",
159 "over the dog!", "over lazy dog!", "the lazy dog!"}));
160 }
161
TEST(SkipGramTest,TestSkip2Trigram)162 TEST(SkipGramTest, TestSkip2Trigram) {
163 SkipGramOp m(3, 2, false);
164 m.SetInput(kSentence);
165 ASSERT_EQ(m.Invoke(), kTfLiteOk);
166 EXPECT_THAT(m.GetOutput(),
167 testing::UnorderedElementsAreArray(
168 {"The quick brown", "The quick fox", "The quick jumps",
169 "The brown fox", "The brown jumps", "The brown over",
170 "The fox jumps", "The fox over", "The fox the",
171 "quick brown fox", "quick brown jumps", "quick brown over",
172 "quick fox jumps", "quick fox over", "quick fox the",
173 "quick jumps over", "quick jumps the", "quick jumps lazy",
174 "brown fox jumps", "brown fox over", "brown fox the",
175 "brown jumps over", "brown jumps the", "brown jumps lazy",
176 "brown over the", "brown over lazy", "brown over dog!",
177 "fox jumps over", "fox jumps the", "fox jumps lazy",
178 "fox over the", "fox over lazy", "fox over dog!",
179 "fox the lazy", "fox the dog!", "jumps over the",
180 "jumps over lazy", "jumps over dog!", "jumps the lazy",
181 "jumps the dog!", "jumps lazy dog!", "over the lazy",
182 "over the dog!", "over lazy dog!", "the lazy dog!"}));
183 }
184
TEST(SkipGramTest,TestAllSkip2Trigram)185 TEST(SkipGramTest, TestAllSkip2Trigram) {
186 SkipGramOp m(3, 2, true);
187 m.SetInput(kSentence);
188 ASSERT_EQ(m.Invoke(), kTfLiteOk);
189 EXPECT_THAT(
190 m.GetOutput(),
191 testing::UnorderedElementsAreArray(
192 {// Unigram
193 "The", "quick", "brown", "fox", "jumps", "over", "the", "lazy",
194 "dog!",
195 // Bigram
196 "The quick", "The brown", "The fox", "quick brown", "quick fox",
197 "quick jumps", "brown fox", "brown jumps", "brown over", "fox jumps",
198 "fox over", "fox the", "jumps over", "jumps the", "jumps lazy",
199 "over the", "over lazy", "over dog!", "the lazy", "the dog!",
200 "lazy dog!",
201 // Trigram
202 "The quick brown", "The quick fox", "The quick jumps",
203 "The brown fox", "The brown jumps", "The brown over",
204 "The fox jumps", "The fox over", "The fox the", "quick brown fox",
205 "quick brown jumps", "quick brown over", "quick fox jumps",
206 "quick fox over", "quick fox the", "quick jumps over",
207 "quick jumps the", "quick jumps lazy", "brown fox jumps",
208 "brown fox over", "brown fox the", "brown jumps over",
209 "brown jumps the", "brown jumps lazy", "brown over the",
210 "brown over lazy", "brown over dog!", "fox jumps over",
211 "fox jumps the", "fox jumps lazy", "fox over the", "fox over lazy",
212 "fox over dog!", "fox the lazy", "fox the dog!", "jumps over the",
213 "jumps over lazy", "jumps over dog!", "jumps the lazy",
214 "jumps the dog!", "jumps lazy dog!", "over the lazy",
215 "over the dog!", "over lazy dog!", "the lazy dog!"}));
216 }
217
TEST(SkipGramTest,TestSingleWord)218 TEST(SkipGramTest, TestSingleWord) {
219 SkipGramOp m(1, 1, false);
220 m.SetInput("Hi");
221 ASSERT_EQ(m.Invoke(), kTfLiteOk);
222 EXPECT_THAT(m.GetOutput(), ElementsAre("Hi"));
223 }
224
TEST(SkipGramTest,TestWordsLessThanGram)225 TEST(SkipGramTest, TestWordsLessThanGram) {
226 SkipGramOp m(3, 1, false);
227 m.SetInput("Hi hi");
228 ASSERT_EQ(m.Invoke(), kTfLiteOk);
229 EXPECT_THAT(m.GetOutput(), std::vector<string>());
230 }
231
TEST(SkipGramTest,TestEmptyInput)232 TEST(SkipGramTest, TestEmptyInput) {
233 SkipGramOp m(1, 1, false);
234 m.SetInput("");
235 ASSERT_EQ(m.Invoke(), kTfLiteOk);
236 EXPECT_THAT(m.GetOutput(), ElementsAre());
237 }
238
TEST(SkipGramTest,TestWhitespaceInput)239 TEST(SkipGramTest, TestWhitespaceInput) {
240 SkipGramOp m(1, 1, false);
241 m.SetInput(" ");
242 ASSERT_EQ(m.Invoke(), kTfLiteOk);
243 EXPECT_THAT(m.GetOutput(), ElementsAre());
244 }
245
TEST(SkipGramTest,TestInputWithExtraSpace)246 TEST(SkipGramTest, TestInputWithExtraSpace) {
247 SkipGramOp m(1, 1, false);
248 m.SetInput(" Hello world ! ");
249 ASSERT_EQ(m.Invoke(), kTfLiteOk);
250 EXPECT_THAT(m.GetOutput(), ElementsAre("Hello", "world", "!"));
251 }
252
253 } // namespace
254 } // namespace tflite
255