1 // Copyright 2010-2018, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 #include <memory>
31 #include <string>
32 #include <vector>
33
34 #include "base/file_stream.h"
35 #include "base/file_util.h"
36 #include "base/util.h"
37 #include "data_manager/testing/mock_data_manager.h"
38 #include "dictionary/dictionary_token.h"
39 #include "dictionary/pos_matcher.h"
40 #include "dictionary/text_dictionary_loader.h"
41 #include "testing/base/public/googletest.h"
42 #include "testing/base/public/gunit.h"
43
44 using std::unique_ptr;
45
46 namespace mozc {
47 namespace dictionary {
48 namespace {
49
50 const char kTextLines[] =
51 "key_test1\t0\t0\t1\tvalue_test1\n"
52 "foo\t1\t2\t3\tbar\n"
53 "buz\t10\t20\t30\tfoobar\n";
54
55 const char kReadingCorrectionLines[] =
56 "bar\tfoo\tfoo_correct\n"
57 "foobar\tfoobar_error\tfoobar_correct\n";
58
59 } // namespace
60
61 class TextDictionaryLoaderTest : public ::testing::Test {
62 protected:
63 // Explicitly define constructor to prevent Visual C++ from
64 // considering this class as POD.
TextDictionaryLoaderTest()65 TextDictionaryLoaderTest() {}
66
SetUp()67 void SetUp() override {
68 pos_matcher_.Set(mock_data_manager_.GetPOSMatcherData());
69 }
70
CreateTextDictionaryLoader()71 TextDictionaryLoader *CreateTextDictionaryLoader() {
72 return new TextDictionaryLoader(pos_matcher_);
73 }
74
75 POSMatcher pos_matcher_;
76
77 private:
78 const testing::MockDataManager mock_data_manager_;
79 };
80
TEST_F(TextDictionaryLoaderTest,BasicTest)81 TEST_F(TextDictionaryLoaderTest, BasicTest) {
82 {
83 unique_ptr<TextDictionaryLoader> loader(CreateTextDictionaryLoader());
84 std::vector<Token *> tokens;
85 loader->CollectTokens(&tokens);
86 EXPECT_TRUE(tokens.empty());
87 }
88
89 const string filename = FileUtil::JoinPath(FLAGS_test_tmpdir, "test.tsv");
90 {
91 OutputFileStream ofs(filename.c_str());
92 ofs << kTextLines;
93 }
94
95 {
96 unique_ptr<TextDictionaryLoader> loader(CreateTextDictionaryLoader());
97 loader->Load(filename, "");
98 const std::vector<Token *> &tokens = loader->tokens();
99
100 EXPECT_EQ(3, tokens.size());
101
102 EXPECT_EQ("key_test1", tokens[0]->key);
103 EXPECT_EQ("value_test1", tokens[0]->value);
104 EXPECT_EQ(0, tokens[0]->lid);
105 EXPECT_EQ(0, tokens[0]->rid);
106 EXPECT_EQ(1, tokens[0]->cost);
107
108 EXPECT_EQ("foo", tokens[1]->key);
109 EXPECT_EQ("bar", tokens[1]->value);
110 EXPECT_EQ(1, tokens[1]->lid);
111 EXPECT_EQ(2, tokens[1]->rid);
112 EXPECT_EQ(3, tokens[1]->cost);
113
114 EXPECT_EQ("buz", tokens[2]->key);
115 EXPECT_EQ("foobar", tokens[2]->value);
116 EXPECT_EQ(10, tokens[2]->lid);
117 EXPECT_EQ(20, tokens[2]->rid);
118 EXPECT_EQ(30, tokens[2]->cost);
119
120 loader->Clear();
121 EXPECT_TRUE(loader->tokens().empty());
122 }
123
124 {
125 unique_ptr<TextDictionaryLoader> loader(CreateTextDictionaryLoader());
126 loader->LoadWithLineLimit(filename, "", 2);
127 const std::vector<Token *> &tokens = loader->tokens();
128
129 EXPECT_EQ(2, tokens.size());
130
131 EXPECT_EQ("key_test1", tokens[0]->key);
132 EXPECT_EQ("value_test1", tokens[0]->value);
133 EXPECT_EQ(0, tokens[0]->lid);
134 EXPECT_EQ(0, tokens[0]->rid);
135 EXPECT_EQ(1, tokens[0]->cost);
136
137 EXPECT_EQ("foo", tokens[1]->key);
138 EXPECT_EQ("bar", tokens[1]->value);
139 EXPECT_EQ(1, tokens[1]->lid);
140 EXPECT_EQ(2, tokens[1]->rid);
141 EXPECT_EQ(3, tokens[1]->cost);
142
143 loader->Clear();
144 EXPECT_TRUE(loader->tokens().empty());
145 }
146
147 {
148 unique_ptr<TextDictionaryLoader> loader(CreateTextDictionaryLoader());
149 // open twice -- tokens are cleared everytime
150 loader->Load(filename, "");
151 loader->Load(filename, "");
152 const std::vector<Token *> &tokens = loader->tokens();
153 EXPECT_EQ(3, tokens.size());
154 }
155
156 FileUtil::Unlink(filename);
157 }
158
TEST_F(TextDictionaryLoaderTest,RewriteSpecialTokenTest)159 TEST_F(TextDictionaryLoaderTest, RewriteSpecialTokenTest) {
160 unique_ptr<TextDictionaryLoader> loader(CreateTextDictionaryLoader());
161 {
162 Token token;
163 token.lid = 100;
164 token.rid = 200;
165 EXPECT_TRUE(loader->RewriteSpecialToken(&token, ""));
166 EXPECT_EQ(100, token.lid);
167 EXPECT_EQ(200, token.rid);
168 EXPECT_EQ(Token::NONE, token.attributes);
169 }
170
171 {
172 Token token;
173 token.lid = 100;
174 token.rid = 200;
175 EXPECT_TRUE(loader->RewriteSpecialToken(&token, "SPELLING_CORRECTION"));
176 EXPECT_EQ(100, token.lid);
177 EXPECT_EQ(200, token.rid);
178 EXPECT_EQ(Token::SPELLING_CORRECTION, token.attributes);
179 }
180
181 {
182 Token token;
183 token.lid = 100;
184 token.rid = 200;
185 EXPECT_TRUE(loader->RewriteSpecialToken(&token, "ZIP_CODE"));
186 EXPECT_EQ(pos_matcher_.GetZipcodeId(), token.lid);
187 EXPECT_EQ(pos_matcher_.GetZipcodeId(), token.rid);
188 EXPECT_EQ(Token::NONE, token.attributes);
189 }
190
191 {
192 Token token;
193 token.lid = 100;
194 token.rid = 200;
195 EXPECT_TRUE(loader->RewriteSpecialToken(&token, "ENGLISH:RATED"));
196 EXPECT_EQ(pos_matcher_.GetIsolatedWordId(), token.lid);
197 EXPECT_EQ(pos_matcher_.GetIsolatedWordId(), token.rid);
198 EXPECT_EQ(Token::NONE, token.attributes);
199 }
200
201 {
202 Token token;
203 token.lid = 100;
204 token.rid = 200;
205 EXPECT_FALSE(loader->RewriteSpecialToken(&token, "foo"));
206 EXPECT_EQ(100, token.lid);
207 EXPECT_EQ(200, token.rid);
208 EXPECT_EQ(Token::NONE, token.attributes);
209 }
210 }
211
TEST_F(TextDictionaryLoaderTest,LoadMultipleFilesTest)212 TEST_F(TextDictionaryLoaderTest, LoadMultipleFilesTest) {
213 const string filename1 = FileUtil::JoinPath(FLAGS_test_tmpdir, "test1.tsv");
214 const string filename2 = FileUtil::JoinPath(FLAGS_test_tmpdir, "test2.tsv");
215 const string filename = filename1 + "," + filename2;
216
217 {
218 OutputFileStream ofs(filename1.c_str());
219 ofs << kTextLines;
220 }
221 {
222 OutputFileStream ofs(filename2.c_str());
223 ofs << kTextLines;
224 }
225
226 {
227 unique_ptr<TextDictionaryLoader> loader(CreateTextDictionaryLoader());
228 loader->Load(filename, "");
229 EXPECT_EQ(6, loader->tokens().size());
230 }
231
232 FileUtil::Unlink(filename1);
233 FileUtil::Unlink(filename2);
234 }
235
TEST_F(TextDictionaryLoaderTest,ReadingCorrectionTest)236 TEST_F(TextDictionaryLoaderTest, ReadingCorrectionTest) {
237 unique_ptr<TextDictionaryLoader> loader(CreateTextDictionaryLoader());
238
239 const string dic_filename =
240 FileUtil::JoinPath(FLAGS_test_tmpdir, "test.tsv");
241 const string reading_correction_filename =
242 FileUtil::JoinPath(FLAGS_test_tmpdir, "reading_correction.tsv");
243
244 {
245 OutputFileStream ofs(dic_filename.c_str());
246 ofs << kTextLines;
247 }
248 {
249 OutputFileStream ofs(reading_correction_filename.c_str());
250 ofs << kReadingCorrectionLines;
251 }
252
253 loader->Load(dic_filename, reading_correction_filename);
254 const std::vector<Token *> &tokens = loader->tokens();
255 ASSERT_EQ(tokens.size(), 4);
256 EXPECT_EQ("foobar_error", tokens[3]->key);
257 EXPECT_EQ("foobar", tokens[3]->value);
258 EXPECT_EQ(10, tokens[3]->lid);
259 EXPECT_EQ(20, tokens[3]->rid);
260 EXPECT_EQ(30 + 2302, tokens[3]->cost);
261 }
262
263 } // namespace dictionary
264 } // namespace mozc
265