aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/lite/kernels/skip_gram_test.cc
blob: 185b64cb44969b57588ea5d0b40f55b6ddf8e11f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include <vector>

#include <gtest/gtest.h>
#include "tensorflow/contrib/lite/interpreter.h"
#include "tensorflow/contrib/lite/kernels/register.h"
#include "tensorflow/contrib/lite/kernels/test_util.h"
#include "tensorflow/contrib/lite/model.h"
#include "tensorflow/contrib/lite/string_util.h"

namespace tflite {
namespace {

using ::testing::ElementsAre;

static char kSentence[] = "The quick\t brown fox\n jumps over\n the lazy dog!";

class SkipGramOp : public SingleOpModel {
 public:
  SkipGramOp(int ngram_size, int max_skip_size, bool include_all_ngrams) {
    input_ = AddInput(TensorType_STRING);
    output_ = AddOutput(TensorType_STRING);

    SetBuiltinOp(BuiltinOperator_SKIP_GRAM, BuiltinOptions_SkipGramOptions,
                 CreateSkipGramOptions(builder_, ngram_size, max_skip_size,
                                       include_all_ngrams)
                     .Union());
    BuildInterpreter({{1}});
  }
  void SetInput(const string& content) {
    PopulateStringTensor(input_, {content});
  }

  std::vector<string> GetOutput() {
    std::vector<string> ans;
    TfLiteTensor* tensor = interpreter_->tensor(output_);

    int num = GetStringCount(tensor);
    for (int i = 0; i < num; i++) {
      StringRef strref = GetString(tensor, i);
      ans.push_back(string(strref.str, strref.len));
    }
    return ans;
  }

 private:
  int input_;
  int output_;
};

TEST(SkipGramTest, TestUnigram) {
  SkipGramOp m(1, 0, false);

  m.SetInput(kSentence);
  m.Invoke();
  EXPECT_THAT(m.GetOutput(), testing::UnorderedElementsAreArray(
                                 {"The", "quick", "brown", "fox", "jumps",
                                  "over", "the", "lazy", "dog!"}));
}

TEST(SkipGramTest, TestBigram) {
  SkipGramOp m(2, 0, false);
  m.SetInput(kSentence);
  m.Invoke();
  EXPECT_THAT(m.GetOutput(),
              testing::UnorderedElementsAreArray(
                  {"The quick", "quick brown", "brown fox", "fox jumps",
                   "jumps over", "over the", "the lazy", "lazy dog!"}));
}

TEST(SkipGramTest, TestAllBigram) {
  SkipGramOp m(2, 0, true);
  m.SetInput(kSentence);
  m.Invoke();
  EXPECT_THAT(m.GetOutput(),
              testing::UnorderedElementsAreArray(
                  {// Unigram
                   "The", "quick", "brown", "fox", "jumps", "over", "the",
                   "lazy", "dog!",
                   //  Bigram
                   "The quick", "quick brown", "brown fox", "fox jumps",
                   "jumps over", "over the", "the lazy", "lazy dog!"}));
}

TEST(SkipGramTest, TestAllTrigram) {
  SkipGramOp m(3, 0, true);
  m.SetInput(kSentence);
  m.Invoke();
  EXPECT_THAT(m.GetOutput(),
              testing::UnorderedElementsAreArray(
                  {// Unigram
                   "The", "quick", "brown", "fox", "jumps", "over", "the",
                   "lazy", "dog!",
                   // Bigram
                   "The quick", "quick brown", "brown fox", "fox jumps",
                   "jumps over", "over the", "the lazy", "lazy dog!",
                   // Trigram
                   "The quick brown", "quick brown fox", "brown fox jumps",
                   "fox jumps over", "jumps over the", "over the lazy",
                   "the lazy dog!"}));
}

TEST(SkipGramTest, TestSkip1Bigram) {
  SkipGramOp m(2, 1, false);
  m.SetInput(kSentence);
  m.Invoke();
  EXPECT_THAT(
      m.GetOutput(),
      testing::UnorderedElementsAreArray(
          {"The quick", "The brown", "quick brown", "quick fox", "brown fox",
           "brown jumps", "fox jumps", "fox over", "jumps over", "jumps the",
           "over the", "over lazy", "the lazy", "the dog!", "lazy dog!"}));
}

TEST(SkipGramTest, TestSkip2Bigram) {
  SkipGramOp m(2, 2, false);
  m.SetInput(kSentence);
  m.Invoke();
  EXPECT_THAT(m.GetOutput(),
              testing::UnorderedElementsAreArray(
                  {"The quick",  "The brown",   "The fox",    "quick brown",
                   "quick fox",  "quick jumps", "brown fox",  "brown jumps",
                   "brown over", "fox jumps",   "fox over",   "fox the",
                   "jumps over", "jumps the",   "jumps lazy", "over the",
                   "over lazy",  "over dog!",   "the lazy",   "the dog!",
                   "lazy dog!"}));
}

TEST(SkipGramTest, TestSkip1Trigram) {
  SkipGramOp m(3, 1, false);
  m.SetInput(kSentence);
  m.Invoke();
  EXPECT_THAT(m.GetOutput(),
              testing::UnorderedElementsAreArray(
                  {"The quick brown", "The quick fox",    "The brown fox",
                   "The brown jumps", "quick brown fox",  "quick brown jumps",
                   "quick fox jumps", "quick fox over",   "brown fox jumps",
                   "brown fox over",  "brown jumps over", "brown jumps the",
                   "fox jumps over",  "fox jumps the",    "fox over the",
                   "fox over lazy",   "jumps over the",   "jumps over lazy",
                   "jumps the lazy",  "jumps the dog!",   "over the lazy",
                   "over the dog!",   "over lazy dog!",   "the lazy dog!"}));
}

TEST(SkipGramTest, TestSkip2Trigram) {
  SkipGramOp m(3, 2, false);
  m.SetInput(kSentence);
  m.Invoke();
  EXPECT_THAT(m.GetOutput(),
              testing::UnorderedElementsAreArray(
                  {"The quick brown",  "The quick fox",     "The quick jumps",
                   "The brown fox",    "The brown jumps",   "The brown over",
                   "The fox jumps",    "The fox over",      "The fox the",
                   "quick brown fox",  "quick brown jumps", "quick brown over",
                   "quick fox jumps",  "quick fox over",    "quick fox the",
                   "quick jumps over", "quick jumps the",   "quick jumps lazy",
                   "brown fox jumps",  "brown fox over",    "brown fox the",
                   "brown jumps over", "brown jumps the",   "brown jumps lazy",
                   "brown over the",   "brown over lazy",   "brown over dog!",
                   "fox jumps over",   "fox jumps the",     "fox jumps lazy",
                   "fox over the",     "fox over lazy",     "fox over dog!",
                   "fox the lazy",     "fox the dog!",      "jumps over the",
                   "jumps over lazy",  "jumps over dog!",   "jumps the lazy",
                   "jumps the dog!",   "jumps lazy dog!",   "over the lazy",
                   "over the dog!",    "over lazy dog!",    "the lazy dog!"}));
}

TEST(SkipGramTest, TestAllSkip2Trigram) {
  SkipGramOp m(3, 2, true);
  m.SetInput(kSentence);
  m.Invoke();
  EXPECT_THAT(
      m.GetOutput(),
      testing::UnorderedElementsAreArray(
          {// Unigram
           "The", "quick", "brown", "fox", "jumps", "over", "the", "lazy",
           "dog!",
           // Bigram
           "The quick", "The brown", "The fox", "quick brown", "quick fox",
           "quick jumps", "brown fox", "brown jumps", "brown over", "fox jumps",
           "fox over", "fox the", "jumps over", "jumps the", "jumps lazy",
           "over the", "over lazy", "over dog!", "the lazy", "the dog!",
           "lazy dog!",
           // Trigram
           "The quick brown", "The quick fox", "The quick jumps",
           "The brown fox", "The brown jumps", "The brown over",
           "The fox jumps", "The fox over", "The fox the", "quick brown fox",
           "quick brown jumps", "quick brown over", "quick fox jumps",
           "quick fox over", "quick fox the", "quick jumps over",
           "quick jumps the", "quick jumps lazy", "brown fox jumps",
           "brown fox over", "brown fox the", "brown jumps over",
           "brown jumps the", "brown jumps lazy", "brown over the",
           "brown over lazy", "brown over dog!", "fox jumps over",
           "fox jumps the", "fox jumps lazy", "fox over the", "fox over lazy",
           "fox over dog!", "fox the lazy", "fox the dog!", "jumps over the",
           "jumps over lazy", "jumps over dog!", "jumps the lazy",
           "jumps the dog!", "jumps lazy dog!", "over the lazy",
           "over the dog!", "over lazy dog!", "the lazy dog!"}));
}

TEST(SkipGramTest, TestSingleWord) {
  SkipGramOp m(1, 1, false);
  m.SetInput("Hi");
  m.Invoke();
  EXPECT_THAT(m.GetOutput(), ElementsAre("Hi"));
}

TEST(SkipGramTest, TestWordsLessThanGram) {
  SkipGramOp m(3, 1, false);
  m.SetInput("Hi hi");
  m.Invoke();
  EXPECT_THAT(m.GetOutput(), std::vector<string>());
}

TEST(SkipGramTest, TestEmptyInput) {
  SkipGramOp m(1, 1, false);
  m.SetInput("");
  m.Invoke();
  EXPECT_THAT(m.GetOutput(), ElementsAre());
}

TEST(SkipGramTest, TestWhitespaceInput) {
  SkipGramOp m(1, 1, false);
  m.SetInput("    ");
  m.Invoke();
  EXPECT_THAT(m.GetOutput(), ElementsAre());
}

TEST(SkipGramTest, TestInputWithExtraSpace) {
  SkipGramOp m(1, 1, false);
  m.SetInput("   Hello   world    !  ");
  m.Invoke();
  EXPECT_THAT(m.GetOutput(), ElementsAre("Hello", "world", "!"));
}

}  // namespace
}  // namespace tflite

int main(int argc, char** argv) {
  ::tflite::LogToStderr();
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}