Codebase list mozc / fa02514
More removals of GET_CONFIG. This CL is a preparation of Singleton<Config> removal. GET_CONFIG relies on process global Singleton<Config>, which has caused many troubles. With this CL, many conversion-related methods will be refactored to receive ConversionRequest to access settings. Hopefully this can help to address or at least mitigate test flakiness like #317. BUG=#317 TEST=unittest REF_BUG=19010851,19188911 REF_CL=87318105 Hiroyuki Komatsu authored 8 years ago Yohei Yukawa committed 8 years ago
31 changed file(s) with 536 addition(s) and 367 deletion(s). Raw diff Collapse all Expand all
134134 }
135135 KeyCorrectedNodeListBuilder builder(pos, key, key_corrector,
136136 lattice->node_allocator());
137 dictionary->LookupPrefix(
138 StringPiece(str, length),
139 request.IsKanaModifierInsensitiveConversion(),
140 &builder);
137 dictionary->LookupPrefix(StringPiece(str, length), request, &builder);
141138 if (builder.tail() != NULL) {
142139 builder.tail()->bnext = NULL;
143140 }
775772 BaseNodeListBuilder builder(
776773 lattice->node_allocator(),
777774 lattice->node_allocator()->max_nodes_size());
778 dictionary_->LookupReverse(StringPiece(begin, len), &builder);
775 dictionary_->LookupReverse(StringPiece(begin, len), request, &builder);
779776 result_node = builder.result();
780777 } else {
781778 if (is_prediction) {
782779 NodeListBuilderWithCacheEnabled builder(
783780 lattice->node_allocator(),
784781 lattice->cache_info(begin_pos) + 1);
785 dictionary_->LookupPrefix(
786 StringPiece(begin, len),
787 request.IsKanaModifierInsensitiveConversion(),
788 &builder);
782 dictionary_->LookupPrefix(StringPiece(begin, len), request, &builder);
789783 result_node = builder.result();
790784 lattice->SetCacheInfo(begin_pos, len);
791785 } else {
793787 BaseNodeListBuilder builder(
794788 lattice->node_allocator(),
795789 lattice->node_allocator()->max_nodes_size());
796 dictionary_->LookupPrefix(
797 StringPiece(begin, len),
798 request.IsKanaModifierInsensitiveConversion(),
799 &builder);
790 dictionary_->LookupPrefix(StringPiece(begin, len), request, &builder);
800791 result_node = builder.result();
801792 }
802793 }
11031094 // Note that, the average number of lid/rid variation is less than 30 in
11041095 // most cases. So, in order to avoid too many allocations for internal
11051096 // nodes of std::map, we use vector of key-value pairs.
1106 typedef vector<pair<int, pair<int, Node*> > > BestMap;
1097 typedef vector<pair<int, pair<int, Node*>>> BestMap;
11071098 typedef OrderBy<FirstKey, Less> OrderByFirst;
11081099 BestMap lbest, rbest;
11091100 lbest.reserve(128);
12661257 lattice->node_allocator()->max_nodes_size(),
12671258 pos_matcher_);
12681259 suffix_dictionary_->LookupPredictive(
1269 StringPiece(key.data() + pos, key.size() - pos),
1270 request.IsKanaModifierInsensitiveConversion(), &builder);
1260 StringPiece(key.data() + pos, key.size() - pos), request, &builder);
12711261 if (builder.result() != NULL) {
12721262 lattice->Insert(pos, builder.result());
12731263 }
12971287 lattice->node_allocator()->max_nodes_size(),
12981288 pos_matcher_);
12991289 dictionary_->LookupPredictive(
1300 StringPiece(key.data() + pos, key.size() - pos),
1301 request.IsKanaModifierInsensitiveConversion(), &builder);
1290 StringPiece(key.data() + pos, key.size() - pos), request, &builder);
13021291 if (builder.result() != NULL) {
13031292 lattice->Insert(pos, builder.result());
13041293 }
15831572 std::unique_ptr<KeyCorrector> key_corrector;
15841573 if (is_conversion && !segments.resized()) {
15851574 KeyCorrector::InputMode mode = KeyCorrector::ROMAN;
1586 if (GET_CONFIG(preedit_method) != config::Config::ROMAN) {
1575 if (request.config().preedit_method() != config::Config::ROMAN) {
15871576 mode = KeyCorrector::KANA;
15881577 }
15891578 key_corrector.reset(new KeyCorrector(key, mode, history_key.size()));
175175
176176 } // namespace
177177
178 class ImmutableConverterTest : public ::testing::Test {
179 protected:
180 virtual void SetUp() {
181 SystemUtil::SetUserProfileDirectory(FLAGS_test_tmpdir);
182 config::ConfigHandler::GetDefaultConfig(&default_config_);
183 config::ConfigHandler::SetConfig(default_config_);
184 }
185
186 virtual void TearDown() {
187 config::ConfigHandler::SetConfig(default_config_);
188 }
189
190 private:
191 config::Config default_config_;
192 };
193
194 TEST_F(ImmutableConverterTest, KeepKeyForPrediction) {
178 TEST(ImmutableConverterTest, KeepKeyForPrediction) {
195179 std::unique_ptr<MockDataAndImmutableConverter> data_and_converter(
196180 new MockDataAndImmutableConverter);
197181 Segments segments;
209193 EXPECT_EQ(kRequestKey, segments.segment(0).key());
210194 }
211195
212 TEST_F(ImmutableConverterTest, DummyCandidatesCost) {
196 TEST(ImmutableConverterTest, DummyCandidatesCost) {
213197 std::unique_ptr<MockDataAndImmutableConverter> data_and_converter(
214198 new MockDataAndImmutableConverter);
215199 Segment segment;
221205 EXPECT_LT(segment.candidate(0).wcost, segment.candidate(2).wcost);
222206 }
223207
224 TEST_F(ImmutableConverterTest, DummyCandidatesInnerSegmentBoundary) {
208 TEST(ImmutableConverterTest, DummyCandidatesInnerSegmentBoundary) {
225209 std::unique_ptr<MockDataAndImmutableConverter> data_and_converter(
226210 new MockDataAndImmutableConverter);
227211 Segment segment;
251235 virtual bool HasValue(StringPiece value) const { return false; }
252236
253237 virtual void LookupPredictive(
254 StringPiece key, bool use_kana_modifier_insensitive_looukp,
238 StringPiece key,
239 const ConversionRequest &convreq,
255240 Callback *callback) const {
256241 if (key == target_query_) {
257242 received_target_query_ = true;
260245
261246 virtual void LookupPrefix(
262247 StringPiece key,
263 bool use_kana_modifier_insensitive_looukp,
248 const ConversionRequest &convreq,
264249 Callback *callback) const {
265250 // No check
266251 }
267252
268 virtual void LookupExact(StringPiece key, Callback *callback) const {
253 virtual void LookupExact(StringPiece key,
254 const ConversionRequest &convreq,
255 Callback *callback) const {
269256 // No check
270257 }
271258
272 virtual void LookupReverse(StringPiece str, Callback *callback) const {
259 virtual void LookupReverse(StringPiece str,
260 const ConversionRequest &convreq,
261 Callback *callback) const {
273262 // No check
274263 }
275264
283272 };
284273 } // namespace
285274
286 TEST_F(ImmutableConverterTest, PredictiveNodesOnlyForConversionKey) {
275 TEST(ImmutableConverterTest, PredictiveNodesOnlyForConversionKey) {
287276 Segments segments;
288277 {
289278 Segment *segment = segments.add_segment();
326315 EXPECT_FALSE(dictionary->received_target_query());
327316 }
328317
329 TEST_F(ImmutableConverterTest, AddPredictiveNodes) {
318 TEST(ImmutableConverterTest, AddPredictiveNodes) {
330319 Segments segments;
331320 {
332321 Segment *segment = segments.add_segment();
355344 EXPECT_TRUE(dictionary->received_target_query());
356345 }
357346
358 TEST_F(ImmutableConverterTest, InnerSegmenBoundaryForPrediction) {
347 TEST(ImmutableConverterTest, InnerSegmenBoundaryForPrediction) {
359348 std::unique_ptr<MockDataAndImmutableConverter> data_and_converter(
360349 new MockDataAndImmutableConverter);
361350 Segments segments;
408397 EXPECT_EQ("\xe4\xb8\xad\xe3\x83\x8e", content_values[2]);
409398 }
410399
411 TEST_F(ImmutableConverterTest, NoInnerSegmenBoundaryForConversion) {
400 TEST(ImmutableConverterTest, NoInnerSegmenBoundaryForConversion) {
412401 std::unique_ptr<MockDataAndImmutableConverter> data_and_converter(
413402 new MockDataAndImmutableConverter);
414403 Segments segments;
429418 }
430419 }
431420
432 TEST_F(ImmutableConverterTest, NotConnectedTest) {
421 TEST(ImmutableConverterTest, NotConnectedTest) {
433422 std::unique_ptr<MockDataAndImmutableConverter> data_and_converter(
434423 new MockDataAndImmutableConverter);
435424 ImmutableConverterImpl *converter = data_and_converter->GetConverter();
477466 EXPECT_TRUE(tested);
478467 }
479468
480 TEST_F(ImmutableConverterTest, HistoryKeyLengthIsVeryLong) {
469 TEST(ImmutableConverterTest, HistoryKeyLengthIsVeryLong) {
481470 // "あ..." (100 times)
482471 const string kA100 =
483472 "\xE3\x81\x82\xE3\x81\x82\xE3\x81\x82\xE3\x81\x82\xE3\x81\x82"
562551 }
563552 } // namespace
564553
565 TEST_F(ImmutableConverterTest, EnableAutoPartialSuggestion) {
554 TEST(ImmutableConverterTest, EnableAutoPartialSuggestion) {
566555 const commands::Request request;
567 ConversionRequest conversion_request(NULL, &request);
556 ConversionRequest conversion_request;
557 conversion_request.set_request(&request);
568558 conversion_request.set_create_partial_candidates(true);
569559
570560 EXPECT_TRUE(AutoPartialSuggestionTestHelper(conversion_request));
571561 }
572562
573 TEST_F(ImmutableConverterTest, DisableAutoPartialSuggestion) {
563 TEST(ImmutableConverterTest, DisableAutoPartialSuggestion) {
574564 const commands::Request request;
575 ConversionRequest conversion_request(NULL, &request);
565 ConversionRequest conversion_request;
566 conversion_request.set_request(&request);
576567 conversion_request.set_create_partial_candidates(false);
577568
578569 EXPECT_FALSE(AutoPartialSuggestionTestHelper(conversion_request));
579570 }
580571
581 TEST_F(ImmutableConverterTest, AutoPartialSuggestionDefault) {
572 TEST(ImmutableConverterTest, AutoPartialSuggestionDefault) {
582573 const commands::Request request;
583 ConversionRequest conversion_request(NULL, &request);
574 ConversionRequest conversion_request;
575 conversion_request.set_request(&request);
584576
585577 EXPECT_FALSE(AutoPartialSuggestionTestHelper(conversion_request));
586578 }
587579
588 TEST_F(ImmutableConverterTest, AutoPartialSuggestionForSingleSegment) {
580 TEST(ImmutableConverterTest, AutoPartialSuggestionForSingleSegment) {
589581 const commands::Request request;
590 ConversionRequest conversion_request(NULL, &request);
582 ConversionRequest conversion_request;
583 conversion_request.set_request(&request);
591584 conversion_request.set_create_partial_candidates(true);
592585
593586 std::unique_ptr<MockDataAndImmutableConverter> data_and_converter(
3333 #include "base/logging.h"
3434 #include "base/string_piece.h"
3535 #include "base/util.h"
36 #include "config/config_handler.h"
3736 #include "dictionary/dictionary_interface.h"
3837 #include "dictionary/dictionary_token.h"
3938 #include "dictionary/pos_matcher.h"
146145
147146 void DictionaryImpl::LookupPredictive(
148147 StringPiece key,
149 bool use_kana_modifier_insensitive_lookup,
150 Callback *callback) const {
151 CallbackWithFilter callback_with_filter(
152 GET_CONFIG(use_spelling_correction),
153 GET_CONFIG(use_zip_code_conversion),
154 GET_CONFIG(use_t13n_conversion),
148 const ConversionRequest &conversion_request,
149 Callback *callback) const {
150 CallbackWithFilter callback_with_filter(
151 conversion_request.config().use_spelling_correction(),
152 conversion_request.config().use_zip_code_conversion(),
153 conversion_request.config().use_t13n_conversion(),
155154 pos_matcher_,
156155 suppression_dictionary_,
157156 callback);
158157 for (size_t i = 0; i < dics_.size(); ++i) {
159158 dics_[i]->LookupPredictive(
160 key, use_kana_modifier_insensitive_lookup, &callback_with_filter);
159 key,
160 conversion_request,
161 &callback_with_filter);
161162 }
162163 }
163164
164165 void DictionaryImpl::LookupPrefix(
165166 StringPiece key,
166 bool use_kana_modifier_insensitive_lookup,
167 Callback *callback) const {
168 CallbackWithFilter callback_with_filter(
169 GET_CONFIG(use_spelling_correction),
170 GET_CONFIG(use_zip_code_conversion),
171 GET_CONFIG(use_t13n_conversion),
167 const ConversionRequest &conversion_request,
168 Callback *callback) const {
169 CallbackWithFilter callback_with_filter(
170 conversion_request.config().use_spelling_correction(),
171 conversion_request.config().use_zip_code_conversion(),
172 conversion_request.config().use_t13n_conversion(),
172173 pos_matcher_,
173174 suppression_dictionary_,
174175 callback);
175176 for (size_t i = 0; i < dics_.size(); ++i) {
176177 dics_[i]->LookupPrefix(
177 key, use_kana_modifier_insensitive_lookup, &callback_with_filter);
178 }
179 }
180
181 void DictionaryImpl::LookupExact(StringPiece key, Callback *callback) const {
182 CallbackWithFilter callback_with_filter(
183 GET_CONFIG(use_spelling_correction),
184 GET_CONFIG(use_zip_code_conversion),
185 GET_CONFIG(use_t13n_conversion),
186 pos_matcher_,
187 suppression_dictionary_,
188 callback);
189 for (size_t i = 0; i < dics_.size(); ++i) {
190 dics_[i]->LookupExact(key, &callback_with_filter);
191 }
192 }
193
194 void DictionaryImpl::LookupReverse(StringPiece str,
195 Callback *callback) const {
196 CallbackWithFilter callback_with_filter(
197 GET_CONFIG(use_spelling_correction),
198 GET_CONFIG(use_zip_code_conversion),
199 GET_CONFIG(use_t13n_conversion),
200 pos_matcher_,
201 suppression_dictionary_,
202 callback);
203 for (size_t i = 0; i < dics_.size(); ++i) {
204 dics_[i]->LookupReverse(str, &callback_with_filter);
178 key,
179 conversion_request,
180 &callback_with_filter);
181 }
182 }
183
184 void DictionaryImpl::LookupExact(
185 StringPiece key,
186 const ConversionRequest &conversion_request,
187 Callback *callback) const {
188 CallbackWithFilter callback_with_filter(
189 conversion_request.config().use_spelling_correction(),
190 conversion_request.config().use_zip_code_conversion(),
191 conversion_request.config().use_t13n_conversion(),
192 pos_matcher_,
193 suppression_dictionary_,
194 callback);
195 for (size_t i = 0; i < dics_.size(); ++i) {
196 dics_[i]->LookupExact(key, conversion_request, &callback_with_filter);
197 }
198 }
199
200 void DictionaryImpl::LookupReverse(
201 StringPiece str,
202 const ConversionRequest &conversion_request,
203 Callback *callback) const {
204 CallbackWithFilter callback_with_filter(
205 conversion_request.config().use_spelling_correction(),
206 conversion_request.config().use_zip_code_conversion(),
207 conversion_request.config().use_t13n_conversion(),
208 pos_matcher_,
209 suppression_dictionary_,
210 callback);
211 for (size_t i = 0; i < dics_.size(); ++i) {
212 dics_[i]->LookupReverse(str, conversion_request, &callback_with_filter);
205213 }
206214 }
207215
208216 bool DictionaryImpl::LookupComment(StringPiece key, StringPiece value,
217 const ConversionRequest &conversion_request,
209218 string *comment) const {
210219 // TODO(komatsu): UserDictionary should be treated as the highest priority.
211220 // In the current implementation, UserDictionary is the last node of dics_,
212221 // but the only dictionary which may return true.
213222 for (size_t i = 0; i < dics_.size(); ++i) {
214 if (dics_[i]->LookupComment(key, value, comment)) {
223 if (dics_[i]->LookupComment(key, value, conversion_request, comment)) {
215224 return true;
216225 }
217226 }
6262
6363 virtual bool HasKey(StringPiece key) const;
6464 virtual bool HasValue(StringPiece value) const;
65 virtual void LookupPredictive(
66 StringPiece key, bool use_kana_modifier_insensitive_lookup,
67 Callback *callback) const;
68 virtual void LookupPrefix(
69 StringPiece key, bool use_kana_modifier_insensitive_lookup,
70 Callback *callback) const;
71 virtual void LookupExact(StringPiece key, Callback *callback) const;
72 virtual void LookupReverse(StringPiece str, Callback *callback) const;
65 virtual void LookupPredictive(StringPiece key,
66 const ConversionRequest &conversion_request,
67 Callback *callback) const;
68 virtual void LookupPrefix(StringPiece key,
69 const ConversionRequest &conversion_request,
70 Callback *callback) const;
71
72 virtual void LookupExact(StringPiece key,
73 const ConversionRequest &conversion_request,
74 Callback *callback) const;
75
76 virtual void LookupReverse(StringPiece str,
77 const ConversionRequest &conversion_request,
78 Callback *callback) const;
79
7380 virtual bool LookupComment(StringPiece key, StringPiece value,
81 const ConversionRequest &conversion_request,
7482 string *comment) const;
7583 virtual bool Reload();
7684 virtual void PopulateReverseLookupCache(StringPiece str) const;
3636 #include "base/system_util.h"
3737 #include "base/util.h"
3838 #include "config/config_handler.h"
39 #include "request/conversion_request.h"
3940 #include "converter/node_allocator.h"
4041 #include "data_manager/testing/mock_data_manager.h"
4142 #include "dictionary/dictionary_interface.h"
193194 // Pair of DictionaryInterface's lookup method and query text.
194195 struct LookupMethodAndQuery {
195196 void (DictionaryInterface::*lookup_method)(
196 StringPiece, bool, DictionaryInterface::Callback *) const;
197 StringPiece,
198 const ConversionRequest &,
199 DictionaryInterface::Callback *) const;
197200 const char *query;
198201 };
202
203 ConversionRequest convreq_;
199204 };
200205
201206 TEST_F(DictionaryImplTest, WordSuppressionTest) {
225230 s->UnLock();
226231 for (size_t i = 0; i < arraysize(kTestPair); ++i) {
227232 CheckKeyValueExistenceCallback callback(kKey, kValue);
228 (d->*kTestPair[i].lookup_method)(kTestPair[i].query, false, &callback);
233 (d->*kTestPair[i].lookup_method)(kTestPair[i].query, convreq_, &callback);
229234 EXPECT_FALSE(callback.found());
230235 }
231236
235240 s->UnLock();
236241 for (size_t i = 0; i < arraysize(kTestPair); ++i) {
237242 CheckKeyValueExistenceCallback callback(kKey, kValue);
238 (d->*kTestPair[i].lookup_method)(kTestPair[i].query, false, &callback);
243 (d->*kTestPair[i].lookup_method)(kTestPair[i].query, convreq_, &callback);
239244 EXPECT_TRUE(callback.found());
240245 }
241246 }
262267 config::ConfigHandler::SetConfig(config);
263268 for (size_t i = 0; i < arraysize(kTestPair); ++i) {
264269 CheckSpellingExistenceCallback callback(kKey, kValue);
265 (d->*kTestPair[i].lookup_method)(kTestPair[i].query, false, &callback);
270 (d->*kTestPair[i].lookup_method)(kTestPair[i].query, convreq_, &callback);
266271 EXPECT_TRUE(callback.found());
267272 }
268273
271276 config::ConfigHandler::SetConfig(config);;
272277 for (size_t i = 0; i < arraysize(kTestPair); ++i) {
273278 CheckSpellingExistenceCallback callback(kKey, kValue);
274 (d->*kTestPair[i].lookup_method)(kTestPair[i].query, false, &callback);
279 (d->*kTestPair[i].lookup_method)(kTestPair[i].query, convreq_, &callback);
275280 EXPECT_FALSE(callback.found());
276281 }
277282 }
297302 config::ConfigHandler::SetConfig(config);
298303 for (size_t i = 0; i < arraysize(kTestPair); ++i) {
299304 CheckZipCodeExistenceCallback callback(kKey, kValue, data->pos_matcher);
300 (d->*kTestPair[i].lookup_method)(kTestPair[i].query, false, &callback);
305 (d->*kTestPair[i].lookup_method)(kTestPair[i].query, convreq_, &callback);
301306 EXPECT_TRUE(callback.found());
302307 }
303308
306311 config::ConfigHandler::SetConfig(config);;
307312 for (size_t i = 0; i < arraysize(kTestPair); ++i) {
308313 CheckZipCodeExistenceCallback callback(kKey, kValue, data->pos_matcher);
309 (d->*kTestPair[i].lookup_method)(kTestPair[i].query, false, &callback);
314 (d->*kTestPair[i].lookup_method)(kTestPair[i].query, convreq_, &callback);
310315 EXPECT_FALSE(callback.found());
311316 }
312317 }
334339 config::ConfigHandler::SetConfig(config);
335340 for (size_t i = 0; i < arraysize(kTestPair); ++i) {
336341 CheckEnglishT13nCallback callback(kKey, kValue);
337 (d->*kTestPair[i].lookup_method)(kTestPair[i].query, false, &callback);
342 (d->*kTestPair[i].lookup_method)(kTestPair[i].query, convreq_, &callback);
338343 EXPECT_TRUE(callback.found());
339344 }
340345
343348 config::ConfigHandler::SetConfig(config);;
344349 for (size_t i = 0; i < arraysize(kTestPair); ++i) {
345350 CheckEnglishT13nCallback callback(kKey, kValue);
346 (d->*kTestPair[i].lookup_method)(kTestPair[i].query, false, &callback);
351 (d->*kTestPair[i].lookup_method)(kTestPair[i].query, convreq_, &callback);
347352 EXPECT_FALSE(callback.found());
348353 }
349354 }
354359 NodeAllocator allocator;
355360
356361 string comment;
357 EXPECT_FALSE(d->LookupComment("key", "value", &comment));
362 EXPECT_FALSE(d->LookupComment("key", "value", convreq_, &comment));
358363 EXPECT_TRUE(comment.empty());
359364
360365 // If key or value is "comment", UserDictionaryStub returns
361366 // "UserDictionaryStub" as comment.
362 EXPECT_TRUE(d->LookupComment("key", "comment", &comment));
367 EXPECT_TRUE(d->LookupComment("key", "comment", convreq_, &comment));
363368 EXPECT_EQ("UserDictionaryStub", comment);
364369 }
365370
3535 #include "base/port.h"
3636 #include "base/string_piece.h"
3737 #include "dictionary/dictionary_token.h"
38 #include "request/conversion_request.h"
3839
3940 namespace mozc {
4041 namespace dictionary {
114115 // Returns true if the dictionary has an entry for the given value.
115116 virtual bool HasValue(StringPiece value) const = 0;
116117
117 virtual void LookupPredictive(
118 StringPiece key, bool use_kana_modifier_insensitive_lookup,
119 Callback *callback) const = 0;
118 virtual void LookupPredictive(StringPiece key,
119 const ConversionRequest &conversion_request,
120 Callback *callback) const = 0;
120121
121 virtual void LookupPrefix(
122 StringPiece key, bool use_kana_modifier_insensitive_lookup,
123 Callback *callback) const = 0;
122 virtual void LookupPrefix(StringPiece key,
123 const ConversionRequest &conversion_request,
124 Callback *callback) const = 0;
124125
125 virtual void LookupExact(StringPiece key, Callback *callback) const = 0;
126 virtual void LookupExact(StringPiece key,
127 const ConversionRequest &conversion_request,
128 Callback *callback) const = 0;
126129
127130 // For reverse lookup, the reading is stored in Token::value and the word
128131 // is stored in Token::key.
129 virtual void LookupReverse(StringPiece str, Callback *callback) const = 0;
132 virtual void LookupReverse(StringPiece str,
133 const ConversionRequest &conversion_request,
134 Callback *callback) const = 0;
130135
131136 // Looks up a user comment from a pair of key and value. When (key, value)
132137 // doesn't exist in this dictionary or user comment is empty, bool is
133138 // returned and string is kept as-is.
134139 virtual bool LookupComment(StringPiece key, StringPiece value,
140 const ConversionRequest &conversion_request,
135141 string *comment) const { return false; }
136142
137143 // Populates cache for LookupReverse().
4747
4848 bool HasKeyInternal(const map<string, vector<Token *>> &dic, StringPiece key) {
4949 typedef vector<Token *> TokenPtrVector;
50 for (map<string, vector<Token *> >::const_iterator map_it = dic.begin();
50 for (map<string, vector<Token *>>::const_iterator map_it = dic.begin();
5151 map_it != dic.end(); ++map_it) {
5252 const TokenPtrVector &v = map_it->second;
5353 for (TokenPtrVector::const_iterator it = v.begin(); it != v.end(); ++it) {
6262 bool HasValueInternal(const map<string, vector<Token *>> &dic,
6363 StringPiece value) {
6464 typedef vector<Token *> TokenPtrVector;
65 for (map<string, vector<Token *> >::const_iterator map_it = dic.begin();
65 for (map<string, vector<Token *>>::const_iterator map_it = dic.begin();
6666 map_it != dic.end(); ++map_it) {
6767 const TokenPtrVector &v = map_it->second;
6868 for (TokenPtrVector::const_iterator it = v.begin(); it != v.end(); ++it) {
8686 return token.release();
8787 }
8888
89 void DeletePtrs(map<string, vector<Token *> > *m) {
90 for (map<string, vector<Token *> >::iterator iter = m->begin();
89 void DeletePtrs(map<string, vector<Token *>> *m) {
90 for (map<string, vector<Token *>>::iterator iter = m->begin();
9191 iter != m->end(); ++iter) {
9292 STLDeleteElements(&iter->second);
9393 }
122122
123123 void DictionaryMock::LookupPredictive(
124124 StringPiece key,
125 bool, // use_kana_modifier_insensitive_lookup
126 Callback *callback) const {
127 map<string, vector<Token *> >::const_iterator vector_iter =
125 const ConversionRequest &conversion_request,
126 Callback *callback) const {
127 map<string, vector<Token *>>::const_iterator vector_iter =
128128 predictive_dictionary_.find(key.as_string());
129129 if (vector_iter == predictive_dictionary_.end()) {
130130 return;
143143
144144 void DictionaryMock::LookupPrefix(
145145 StringPiece key,
146 bool, // use_kana_modifier_insensitive_lookup
146 const ConversionRequest &conversion_request,
147147 Callback *callback) const {
148148 CHECK(!key.empty());
149149
150150 string prefix;
151151 for (size_t len = 1; len <= key.size(); ++len) {
152152 key.substr(0, len).CopyToString(&prefix);
153 map<string, vector<Token *> >::const_iterator iter =
153 map<string, vector<Token *>>::const_iterator iter =
154154 prefix_dictionary_.find(prefix);
155155 if (iter == prefix_dictionary_.end()) {
156156 continue;
187187 }
188188 }
189189
190 void DictionaryMock::LookupExact(StringPiece key, Callback *callback) const {
191 map<string, vector<Token *> >::const_iterator iter =
190 void DictionaryMock::LookupExact(
191 StringPiece key,
192 const ConversionRequest &conversion_request,
193 Callback *callback) const {
194 map<string, vector<Token *>>::const_iterator iter =
192195 exact_dictionary_.find(key.as_string());
193196 if (iter == exact_dictionary_.end()) {
194197 return;
205208 }
206209 }
207210
208 void DictionaryMock::LookupReverse(StringPiece str, Callback *callback) const {
211 void DictionaryMock::LookupReverse(
212 StringPiece str,
213 const ConversionRequest &conversion_request,
214 Callback *callback) const {
209215 CHECK(!str.empty());
210216
211217 for (int i = 1; i <= str.size(); ++i) {
212218 StringPiece prefix = str.substr(0, i);
213219
214 map<string, vector<Token *> >::const_iterator iter =
220 map<string, vector<Token *>>::const_iterator iter =
215221 reverse_dictionary_.find(prefix.as_string());
216222 if (iter == reverse_dictionary_.end()) {
217223 continue;
5959 #include "base/string_piece.h"
6060 #include "dictionary/dictionary_interface.h"
6161 #include "dictionary/dictionary_token.h"
62 #include "request/conversion_request.h"
6263
6364 namespace mozc {
6465 namespace dictionary {
7576 // DictionaryMock doesn't support a limitation. Note also that only the
7677 // tokens whose keys exactly match the registered key are looked up; see the
7778 // comment of AddLookupPredictive.
78 virtual void LookupPredictive(
79 StringPiece key, bool use_kana_modifier_insensitive_lookup,
80 Callback *callback) const;
79 virtual void LookupPredictive(StringPiece key,
80 const ConversionRequest &conversion_request,
81 Callback *callback) const;
8182
82 virtual void LookupPrefix(
83 StringPiece key, bool use_kana_modifier_insensitive_lookup,
84 Callback *callback) const;
83 virtual void LookupPrefix(StringPiece key,
84 const ConversionRequest &conversion_request,
85 Callback *callback) const;
8586
86 virtual void LookupExact(StringPiece key, Callback *callback) const;
87 virtual void LookupExact(StringPiece key,
88 const ConversionRequest &conversion_request,
89 Callback *callback) const;
8790
8891 // For reverse lookup, the reading is stored in Token::value and the word
8992 // is stored in Token::key.
90 virtual void LookupReverse(StringPiece str, Callback *callback) const;
93 virtual void LookupReverse(StringPiece str,
94 const ConversionRequest &conversion_request,
95 Callback *callback) const;
9196
9297 // Adds a string-result pair to the predictive search result.
9398 // LookupPrefix will return the result only when the search key exactly
120125 Token::AttributesBitfield token_attributes);
121126
122127 private:
123 map<string, vector<Token *> > reverse_dictionary_;
124 map<string, vector<Token *> > prefix_dictionary_;
125 map<string, vector<Token *> > exact_dictionary_;
126 map<string, vector<Token *> > predictive_dictionary_;
128 map<string, vector<Token *>> reverse_dictionary_;
129 map<string, vector<Token *>> prefix_dictionary_;
130 map<string, vector<Token *>> exact_dictionary_;
131 map<string, vector<Token *>> predictive_dictionary_;
127132
128133 DISALLOW_COPY_AND_ASSIGN(DictionaryMock);
129134 };
3434
3535 #include "base/logging.h"
3636 #include "base/util.h"
37 #include "request/conversion_request.h"
3738 #include "dictionary/dictionary_test_util.h"
3839 #include "dictionary/dictionary_token.h"
3940 #include "testing/base/public/gunit.h"
6364 const vector<Token> &tokens);
6465
6566 unique_ptr<DictionaryMock> mock_;
67 ConversionRequest convreq_;
6668 };
6769
6870 bool DictionaryMockTest::SearchMatchingToken(const string &key,
136138 dic->AddLookupPrefix(t1->key, t1->key, t1->value, Token::NONE);
137139
138140 CollectTokenCallback callback;
139 dic->LookupPrefix(t0->key, false, &callback);
141 dic->LookupPrefix(t0->key, convreq_, &callback);
140142 ASSERT_EQ(1, callback.tokens().size());
141143 EXPECT_TOKEN_EQ(*t0, callback.tokens()[0]);
142144
143145 callback.Clear();
144 dic->LookupPrefix(t1->key, false, &callback);
146 dic->LookupPrefix(t1->key, convreq_, &callback);
145147 ASSERT_EQ(2, callback.tokens().size());
146148 EXPECT_TOKEN_EQ(*t0, callback.tokens()[0]);
147149 EXPECT_TOKEN_EQ(*t1, callback.tokens()[1]);
148150
149151 callback.Clear();
150 dic->LookupPrefix("google", false, &callback);
152 dic->LookupPrefix("google", convreq_, &callback);
151153 EXPECT_TRUE(callback.tokens().empty());
152154 }
153155
174176 }
175177
176178 CollectTokenCallback callback;
177 dic->LookupReverse(k1, &callback);
179 dic->LookupReverse(k1, convreq_, &callback);
178180 const vector<Token> &result_tokens = callback.tokens();
179181 EXPECT_TRUE(SearchMatchingToken(t0->key, t0->value, 0, result_tokens))
180182 << "Failed to find: " << t0->key;
203205 }
204206
205207 CollectTokenCallback callback;
206 dic->LookupPredictive(k0, false, &callback);
208 dic->LookupPredictive(k0, convreq_, &callback);
207209 ASSERT_EQ(2, callback.tokens().size());
208210 EXPECT_TOKEN_EQ(*t1, callback.tokens()[0]);
209211 EXPECT_TOKEN_EQ(*t2, callback.tokens()[1]);
221223 GetMock()->AddLookupExact(t1->key, t1->key, t1->value, Token::NONE);
222224
223225 CollectTokenCallback callback;
224 dic->LookupExact(kKey, &callback);
226 dic->LookupExact(kKey, convreq_, &callback);
225227 ASSERT_EQ(2, callback.tokens().size());
226228 EXPECT_TOKEN_EQ(*t0, callback.tokens()[0]);
227229 EXPECT_TOKEN_EQ(*t1, callback.tokens()[1]);
228230
229231 callback.Clear();
230 dic->LookupExact("hoge", &callback);
232 dic->LookupExact("hoge", convreq_, &callback);
231233 EXPECT_TRUE(callback.tokens().empty());
232234
233235 callback.Clear();
234236 dic->LookupExact("\xE3\x81\xBB", // "ほ"
237 convreq_,
235238 &callback);
236239 EXPECT_TRUE(callback.tokens().empty());
237240 }
9090
9191 void SuffixDictionary::LookupPredictive(
9292 StringPiece key,
93 bool, // use_kana_modifier_insensitive_lookup
93 const ConversionRequest &conversion_request,
9494 Callback *callback) const {
9595 typedef IteratorAdapter<const SuffixToken *, SuffixTokenKeyAdapter> Iter;
9696 pair<Iter, Iter> range = std::equal_range(
126126 }
127127 }
128128
129 void SuffixDictionary::LookupPrefix(StringPiece key,
130 bool use_kana_modifier_insensitive_lookup,
131 Callback *callback) const {
129 void SuffixDictionary::LookupPrefix(
130 StringPiece key,
131 const ConversionRequest &conversion_request,
132 Callback *callback) const {
132133 }
133134
134 void SuffixDictionary::LookupReverse(StringPiece str,
135 Callback *callback) const {
135 void SuffixDictionary::LookupExact(
136 StringPiece key,
137 const ConversionRequest &conversion_request,
138 Callback *callback) const {
136139 }
137140
138 void SuffixDictionary::LookupExact(StringPiece key, Callback *callback) const {
141 void SuffixDictionary::LookupReverse(
142 StringPiece key,
143 const ConversionRequest &conversion_request,
144 Callback *callback) const {
139145 }
140146
141147 } // namespace dictionary
5959 virtual bool HasValue(StringPiece value) const;
6060
6161 // Kana modifier insensitive lookup is not supported.
62 virtual void LookupPredictive(
63 StringPiece key, bool use_kana_modifier_insensitive_lookup,
64 Callback *callback) const;
62 virtual void LookupPredictive(StringPiece key,
63 const ConversionRequest &conversion_request,
64 Callback *callback) const;
6565
6666 // SuffixDictionary doesn't support Prefix/Revese/Exact Lookup.
67 virtual void LookupPrefix(
68 StringPiece key, bool use_kana_modifier_insensitive_lookup,
69 Callback *callback) const;
70 virtual void LookupReverse(StringPiece str, Callback *callback) const;
71 virtual void LookupExact(StringPiece key, Callback *callback) const;
67 virtual void LookupPrefix(StringPiece key,
68 const ConversionRequest &conversion_request,
69 Callback *callback) const;
70
71 virtual void LookupExact(StringPiece key,
72 const ConversionRequest &conversion_request,
73 Callback *callback) const;
74
75 virtual void LookupReverse(StringPiece str,
76 const ConversionRequest &conversion_request,
77 Callback *callback) const;
7278
7379 private:
7480 const SuffixToken *const suffix_tokens_;
3535 #include "dictionary/dictionary_interface.h"
3636 #include "dictionary/dictionary_test_util.h"
3737 #include "dictionary/suffix_dictionary_token.h"
38 #include "request/conversion_request.h"
3839 #include "testing/base/public/gunit.h"
3940
4041 namespace mozc {
4344 TEST(SuffixDictionaryTest, LookupPredictive) {
4445 // Test SuffixDictionary with mock data.
4546 std::unique_ptr<const SuffixDictionary> dic;
47 ConversionRequest convreq;
4648 {
4749 const testing::MockDataManager manager;
4850 const SuffixToken *tokens = NULL;
5658 // Lookup with empty key. All tokens are looked up. Here, just verify the
5759 // result is nonempty and each token has valid data.
5860 CollectTokenCallback callback;
59 dic->LookupPredictive("", false, &callback);
61 dic->LookupPredictive("", convreq, &callback);
6062 EXPECT_FALSE(callback.tokens().empty());
6163 for (size_t i = 0; i < callback.tokens().size(); ++i) {
6264 const Token &token = callback.tokens()[i];
7173 // Non-empty prefix.
7274 const string kPrefix = "\xE3\x81\x9F"; // "た"
7375 CollectTokenCallback callback;
74 dic->LookupPredictive(kPrefix, false, &callback);
76 dic->LookupPredictive(kPrefix, convreq, &callback);
7577 EXPECT_FALSE(callback.tokens().empty());
7678 for (size_t i = 0; i < callback.tokens().size(); ++i) {
7779 const Token &token = callback.tokens()[i];
654654 }
655655
656656 void SystemDictionary::LookupPredictive(
657 StringPiece key, bool use_kana_modifier_insensitive_lookup,
657 StringPiece key,
658 const ConversionRequest &conversion_request,
658659 Callback *callback) const {
659660 // Do nothing for empty key, although looking up all the entries with empty
660661 // string seems natural.
668669 return;
669670 }
670671
671 const KeyExpansionTable &table = use_kana_modifier_insensitive_lookup
672 ? hiragana_expansion_table_
673 : KeyExpansionTable::GetDefaultInstance();
672 const KeyExpansionTable &table =
673 conversion_request.IsKanaModifierInsensitiveConversion() ?
674 hiragana_expansion_table_ : KeyExpansionTable::GetDefaultInstance();
674675
675676 // TODO(noriyukit): Lookup limit should be implemented at caller side by using
676677 // callback mechanism. This hard-coding limits the capability and generality
965966
966967 void SystemDictionary::LookupPrefix(
967968 StringPiece key,
968 bool use_kana_modifier_insensitive_lookup,
969 const ConversionRequest &conversion_request,
969970 Callback *callback) const {
970971 string encoded_key;
971972 codec_->EncodeKey(key, &encoded_key);
972973
973 if (!use_kana_modifier_insensitive_lookup) {
974 if (!conversion_request.IsKanaModifierInsensitiveConversion()) {
974975 RunCallbackOnEachPrefix(key_trie_, value_trie_, token_array_, codec_,
975976 frequent_pos_, key.data(), encoded_key, callback,
976977 SelectAllTokens());
986987 actual_key_buffer, &actual_prefix);
987988 }
988989
989 void SystemDictionary::LookupExact(StringPiece key, Callback *callback) const {
990 void SystemDictionary::LookupExact(
991 StringPiece key,
992 const ConversionRequest &conversion_request,
993 Callback *callback) const {
990994 // Find the key in the key trie.
991995 string encoded_key;
992996 codec_->EncodeKey(key, &encoded_key);
10091013 }
10101014 }
10111015
1012 void SystemDictionary::LookupReverse(StringPiece str,
1013 Callback *callback) const {
1016 void SystemDictionary::LookupReverse(
1017 StringPiece str,
1018 const ConversionRequest &conversion_request,
1019 Callback *callback) const {
10141020 // 1st step: Hiragana/Katakana are not in the value trie
10151021 // 2nd step: Reverse lookup in value trie
10161022 ReverseLookupCallbackWrapper callback_wrapper(callback);
5555 ],
5656 'dependencies': [
5757 '../../base/base.gyp:base_core',
58 '../../request/request.gyp:conversion_request',
5859 '../../storage/louds/louds.gyp:bit_vector_based_array',
5960 '../../storage/louds/louds.gyp:louds_trie',
6061 '../dictionary_base.gyp:text_dictionary_loader',
9898 // Implementation of DictionaryInterface.
9999 virtual bool HasKey(StringPiece key) const;
100100 virtual bool HasValue(StringPiece value) const;
101 virtual void LookupPredictive(
102 StringPiece key, bool use_kana_modifier_insensitive_lookup,
103 Callback *callback) const;
104 virtual void LookupPrefix(
105 StringPiece key, bool use_kana_modifier_insensitive_lookup,
106 Callback *callback) const;
107 virtual void LookupExact(StringPiece key, Callback *callback) const;
108 virtual void LookupReverse(StringPiece str, Callback *callback) const;
101
102 virtual void LookupPredictive(StringPiece key,
103 const ConversionRequest &converter_request,
104 Callback *callback) const;
105
106 virtual void LookupPrefix(StringPiece key,
107 const ConversionRequest &converter_request,
108 Callback *callback) const;
109
110 virtual void LookupExact(StringPiece key,
111 const ConversionRequest &converter_request,
112 Callback *callback) const;
113
114 virtual void LookupReverse(StringPiece str,
115 const ConversionRequest &converter_request,
116 Callback *callback) const;
117
109118 virtual void PopulateReverseLookupCache(StringPiece str) const;
110119 virtual void ClearReverseLookupCache() const;
111120
4141 #include "base/stl_util.h"
4242 #include "base/system_util.h"
4343 #include "base/util.h"
44 #include "config/config_handler.h"
4445 #include "data_manager/user_pos_manager.h"
4546 #include "dictionary/dictionary_test_util.h"
4647 #include "dictionary/dictionary_token.h"
4849 #include "dictionary/system/codec_interface.h"
4950 #include "dictionary/system/system_dictionary_builder.h"
5051 #include "dictionary/text_dictionary_loader.h"
52 #include "protocol/commands.pb.h"
53 #include "protocol/config.pb.h"
54 #include "request/conversion_request.h"
5155 #include "testing/base/public/googletest.h"
5256 #include "testing/base/public/gunit.h"
5357
8791
8892 namespace {
8993
90 const bool kEnableKanaModiferInsensitiveLookup = true;
91 const bool kDisableKanaModiferInsensitiveLookup = false;
92
94 // This function will be removed soon in the following change, which
95 // enables to inject config to ConversionRequest.
96 void config_set_use_kana_modifier_insensitive_conversion(bool is_enable) {
97 config::Config config;
98 config::ConfigHandler::GetDefaultConfig(&config);
99 config.set_use_kana_modifier_insensitive_conversion(is_enable);
100 config::ConfigHandler::SetConfig(config);
101 }
93102 } // namespace
94103
95104 class SystemDictionaryTest : public testing::Test {
101110 const string dic_path = FileUtil::JoinPath(FLAGS_test_srcdir,
102111 FLAGS_dictionary_source);
103112 text_dict_->LoadWithLineLimit(dic_path, "", FLAGS_dictionary_test_size);
113
114 convreq_.set_request(&request_);
104115 }
105116
106117 virtual void SetUp() {
110121 original_flags_min_key_length_to_use_small_cost_encoding_ =
111122 FLAGS_min_key_length_to_use_small_cost_encoding;
112123 FLAGS_min_key_length_to_use_small_cost_encoding = kint32max;
124
125 request_.Clear();
126
127 // This config initialization will be removed once ConversionRequest can
128 // take config as an injected argument.
129 config::Config config;
130 config::ConfigHandler::GetDefaultConfig(&config);
131 config::ConfigHandler::SetConfig(config);
113132 }
114133
115134 virtual void TearDown() {
116135 FLAGS_min_key_length_to_use_small_cost_encoding =
117136 original_flags_min_key_length_to_use_small_cost_encoding_;
137
138 // This config initialization will be removed once ConversionRequest can
139 // take config as an injected argument.
140 config::Config config;
141 config::ConfigHandler::GetDefaultConfig(&config);
142 config::ConfigHandler::SetConfig(config);
118143 }
119144
120145 void BuildSystemDictionary(const vector <Token *>& tokens,
124149 bool reverse) const;
125150
126151 unique_ptr<TextDictionaryLoader> text_dict_;
152
153 ConversionRequest convreq_;
154 commands::Request request_;
127155 const string dic_fn_;
128156 int original_flags_min_key_length_to_use_small_cost_encoding_;
129157 };
305333 CollectTokenCallback callback;
306334
307335 // Look up by exact key.
308 system_dic->LookupPrefix(t0->key, false, &callback);
336 system_dic->LookupPrefix(t0->key, convreq_, &callback);
309337 ASSERT_EQ(1, callback.tokens().size());
310338 EXPECT_TOKEN_EQ(*t0, callback.tokens().front());
311339
313341 callback.Clear();
314342 system_dic->LookupPrefix(
315343 "\xE3\x81\x82\xE3\x81\x84\xE3\x81\x86", // "あいう"
316 false, &callback);
344 convreq_, &callback);
317345 ASSERT_EQ(1, callback.tokens().size());
318346 EXPECT_TOKEN_EQ(*t0, callback.tokens().front());
319347
321349 callback.Clear();
322350 system_dic->LookupPrefix(
323351 "\xE3\x81\x8B\xE3\x81\x8D\xE3\x81\x8F", // "かきく"
324 false, &callback);
352 convreq_, &callback);
325353 EXPECT_TRUE(callback.tokens().empty());
326354 }
327355
366394 // All the tokens should be looked up.
367395 CollectTokenCallback callback;
368396 system_dic->LookupPrefix("\xe3\x81\x82", // "あ"
369 false, &callback);
397 convreq_, &callback);
370398 EXPECT_TOKENS_EQ_UNORDERED(source_tokens, callback.tokens());
371399 }
372400
382410 // All the tokens should be looked up.
383411 for (size_t i = 0; i < source_tokens.size(); ++i) {
384412 CheckTokenExistenceCallback callback(source_tokens[i]);
385 system_dic->LookupPrefix(source_tokens[i]->key, false, &callback);
413 system_dic->LookupPrefix(source_tokens[i]->key, convreq_, &callback);
386414 EXPECT_TRUE(callback.found())
387415 << "Token was not found: " << PrintToken(*source_tokens[i]);
388416 }
410438
411439 // |t0| should be looked up from |k1|.
412440 CheckTokenExistenceCallback callback(t0.get());
413 system_dic->LookupPrefix(k1, false, &callback);
441 system_dic->LookupPrefix(k1, convreq_, &callback);
414442 EXPECT_TRUE(callback.found());
415443 }
416444
435463 return TRAVERSE_CONTINUE;
436464 }
437465
438 const set<pair<string, string> > &result() const {
466 const set<pair<string, string>> &result() const {
439467 return result_;
440468 }
441469
442470 private:
443 set<pair<string, string> > result_;
471 set<pair<string, string>> result_;
444472 };
445473
446474 } // namespace
520548 {
521549 LookupPrefixTestCallback callback;
522550 system_dic->LookupPrefix("\xE3\x81\x82\xE3\x81\x84", // "あい"
523 false, &callback);
524 const set<pair<string, string> > &result = callback.result();
551 convreq_, &callback);
552 const set<pair<string, string>> &result = callback.result();
525553 // "あ" -- "あい" should be found.
526554 for (size_t i = 0; i < 5; ++i) {
527555 const pair<string, string> entry(
542570 LookupPrefixTestCallback callback;
543571 system_dic->LookupPrefix(
544572 "\xE3\x81\x8B\xE3\x81\x8D\xE3\x81\x8F", //"かきく"
545 false,
573 convreq_,
546574 &callback);
547 const set<pair<string, string> > &result = callback.result();
575 const set<pair<string, string>> &result = callback.result();
548576 // Only "か" should be found as the callback doesn't traverse the subtree of
549577 // "かき" due to culling request from LookupPrefixTestCallback::OnKey().
550578 for (size_t i = 0; i < kKeyValuesSize; ++i) {
560588 LookupPrefixTestCallback callback;
561589 system_dic->LookupPrefix(
562590 "\xE3\x81\x95\xE3\x81\x97\xE3\x81\x99", // "さしす"
563 false,
591 convreq_,
564592 &callback);
565 const set<pair<string, string> > &result = callback.result();
593 const set<pair<string, string>> &result = callback.result();
566594 // Only "さし" should be found as tokens for "さ" is skipped (see
567595 // LookupPrefixTestCallback::OnKey()).
568596 for (size_t i = 0; i < kKeyValuesSize; ++i) {
578606 LookupPrefixTestCallback callback;
579607 system_dic->LookupPrefix(
580608 "\xE3\x81\x9F\xE3\x81\xA1\xE3\x81\xA4", // "たちつ"
581 false,
609 convreq_,
582610 &callback);
583 const set<pair<string, string> > &result = callback.result();
611 const set<pair<string, string>> &result = callback.result();
584612 // Nothing should be found as the traversal is immediately done after seeing
585613 // "た"; see LookupPrefixTestCallback::OnKey().
586614 EXPECT_TRUE(result.empty());
589617 // Test for prefix lookup with key expansion.
590618 {
591619 LookupPrefixTestCallback callback;
620 // Use kana modifier insensitive lookup
621 request_.set_kana_modifier_insensitive_conversion(true);
622 config_set_use_kana_modifier_insensitive_conversion(true);
592623 system_dic->LookupPrefix(
593624 "\xE3\x81\xAF\xE3\x81\xB2", // "はひ"
594 true, // Use kana modifier insensitive lookup
625 convreq_,
595626 &callback);
596 const set<pair<string, string> > &result = callback.result();
627 const set<pair<string, string>> &result = callback.result();
597628 const char *kExpectedKeys[] = {
598629 "\xE3\x81\xAF", // "は"
599630 "\xE3\x81\xB0", // "ば"
616647
617648 TEST_F(SystemDictionaryTest, LookupPredictive) {
618649 vector<Token *> tokens;
619 ScopedElementsDeleter<vector<Token *> > deleter(&tokens);
650 ScopedElementsDeleter<vector<Token *>> deleter(&tokens);
620651
621652 // "まみむめもや" -> "value0"
622653 tokens.push_back(CreateToken("\xe3\x81\xbe\xe3\x81\xbf\xe3\x82\x80"
642673 const char *kMamimumemo =
643674 "\xe3\x81\xbe\xe3\x81\xbf\xe3\x82\x80\xe3\x82\x81\xe3\x82\x82";
644675 CheckMultiTokensExistenceCallback callback(tokens);
645 system_dic->LookupPredictive(kMamimumemo, false, &callback);
676 system_dic->LookupPredictive(kMamimumemo, convreq_, &callback);
646677 EXPECT_TRUE(callback.AreAllFound());
647678 }
648679
649680 TEST_F(SystemDictionaryTest, LookupPredictive_KanaModifierInsensitiveLookup) {
650681 vector<Token *> tokens;
651 ScopedElementsDeleter<vector<Token *> > deleter(&tokens);
682 ScopedElementsDeleter<vector<Token *>> deleter(&tokens);
652683
653684 // "がっこう" -> "学校"
654685 tokens.push_back(CreateToken(
670701
671702 // Without Kana modifier insensitive lookup flag, nothing is looked up.
672703 CollectTokenCallback callback;
673 system_dic->LookupPredictive(kKey, false, &callback);
704 request_.set_kana_modifier_insensitive_conversion(false);
705 config_set_use_kana_modifier_insensitive_conversion(false);
706 system_dic->LookupPredictive(kKey, convreq_, &callback);
674707 EXPECT_TRUE(callback.tokens().empty());
675708
676709 // With Kana modifier insensitive lookup flag, every token is looked up.
677710 callback.Clear();
678 system_dic->LookupPredictive(kKey, true, &callback);
711 request_.set_kana_modifier_insensitive_conversion(true);
712 config_set_use_kana_modifier_insensitive_conversion(true);
713 system_dic->LookupPredictive(kKey, convreq_, &callback);
679714 EXPECT_TOKENS_EQ_UNORDERED(tokens, callback.tokens());
680715 }
681716
682717 TEST_F(SystemDictionaryTest, LookupPredictive_CutOffEmulatingBFS) {
683718 vector<Token *> tokens;
684 ScopedElementsDeleter<vector<Token *> > deleter(&tokens);
719 ScopedElementsDeleter<vector<Token *>> deleter(&tokens);
685720
686721 // "あい" -> "ai"
687722 tokens.push_back(CreateToken("\xe3\x81\x82\xe3\x81\x84", "ai"));
705740 // mechanism. However, "あい" is looked up as it's short.
706741 CheckMultiTokensExistenceCallback callback(tokens);
707742 system_dic->LookupPredictive("\xe3\x81\x82", // "あ"
708 false, &callback);
743 convreq_, &callback);
709744 EXPECT_TRUE(callback.IsFound(tokens[0]));
710745 EXPECT_FALSE(callback.IsFound(tokens[1]));
711746 }
733768
734769 // |t0| should not be looked up from |k1|.
735770 CheckTokenExistenceCallback callback0(t0.get());
736 system_dic->LookupExact(k1, &callback0);
771 system_dic->LookupExact(k1, convreq_, &callback0);
737772 EXPECT_FALSE(callback0.found());
738773 // But |t1| should be found.
739774 CheckTokenExistenceCallback callback1(t1.get());
740 system_dic->LookupExact(k1, &callback1);
775 system_dic->LookupExact(k1, convreq_, &callback1);
741776 EXPECT_TRUE(callback1.found());
742777
743778 // Nothing should be found from "hoge".
744779 CollectTokenCallback callback_hoge;
745 system_dic->LookupExact("hoge", &callback_hoge);
780 system_dic->LookupExact("hoge", convreq_, &callback_hoge);
746781 EXPECT_TRUE(callback_hoge.tokens().empty());
747782 }
748783
847882 for (size_t source_index = 0; source_index < test_size; ++source_index) {
848883 const Token &source_token = *source_tokens[source_index];
849884 CollectTokenCallback callback;
850 system_dic->LookupReverse(source_token.value, &callback);
885 system_dic->LookupReverse(source_token.value, convreq_, &callback);
851886 const vector<Token> &tokens = callback.tokens();
852887
853888 bool found = false;
886921 // append "が"
887922 const string key = t7->value + "\xe3\x81\x8c";
888923 CollectTokenCallback callback;
889 system_dic->LookupReverse(key, &callback);
924 system_dic->LookupReverse(key, convreq_, &callback);
890925 const vector<Token> &tokens = callback.tokens();
891926 bool found = false;
892927 for (size_t i = 0; i < tokens.size(); ++i) {
922957 size > 0 && it != source_tokens.end(); ++it, --size) {
923958 const Token &t = **it;
924959 CollectTokenCallback callback1, callback2;
925 system_dic_without_index->LookupReverse(t.value, &callback1);
926 system_dic_with_index->LookupReverse(t.value, &callback2);
960 system_dic_without_index->LookupReverse(t.value, convreq_, &callback1);
961 system_dic_with_index->LookupReverse(t.value, convreq_, &callback2);
927962
928963 const vector<Token> &tokens1 = callback1.tokens();
929964 const vector<Token> &tokens2 = callback2.tokens();
961996 << "Failed to open dictionary source:" << dic_fn_;
962997 system_dic->PopulateReverseLookupCache(kDoraemon);
963998 CheckTokenExistenceCallback callback(&target_token);
964 system_dic->LookupReverse(kDoraemon, &callback);
999 system_dic->LookupReverse(kDoraemon, convreq_, &callback);
9651000 EXPECT_TRUE(callback.found())
9661001 << "Could not find " << PrintToken(source_token);
9671002 system_dic->ClearReverseLookupCache();
10141049
10151050 for (size_t i = 0; i < source_tokens.size(); ++i) {
10161051 CheckTokenExistenceCallback callback(source_tokens[i]);
1017 system_dic->LookupPrefix(source_tokens[i]->key, false, &callback);
1052 system_dic->LookupPrefix(source_tokens[i]->key, convreq_, &callback);
10181053 EXPECT_TRUE(callback.found())
10191054 << "Token " << i << " was not found: " << PrintToken(*source_tokens[i]);
10201055 }
10511086 ASSERT_TRUE(system_dic.get() != NULL)
10521087 << "Failed to open dictionary source:" << dic_fn_;
10531088
1089 request_.set_kana_modifier_insensitive_conversion(true);
1090 config_set_use_kana_modifier_insensitive_conversion(true);
1091
10541092 // Prefix search
10551093 for (size_t i = 0; i < arraysize(tokens); ++i) {
10561094 CheckTokenExistenceCallback callback(tokens[i].get());
10571095 // "かつこう" -> "かつ", "かっこ", "かつこう", "かっこう" and "がっこう"
1058 system_dic->LookupPrefix(
1059 k2, kEnableKanaModiferInsensitiveLookup, &callback);
1096 system_dic->LookupPrefix(k2, convreq_, &callback);
10601097 EXPECT_TRUE(callback.found())
10611098 << "Token " << i << " was not found: " << PrintToken(*tokens[i]);
10621099 }
10691106 expected.push_back(tokens[i].get());
10701107 }
10711108 CheckMultiTokensExistenceCallback callback(expected);
1072 system_dic->LookupPredictive(
1073 k0, kEnableKanaModiferInsensitiveLookup, &callback);
1109 system_dic->LookupPredictive(k0, convreq_, &callback);
10741110 EXPECT_TRUE(callback.AreAllFound());
10751111 }
10761112 {
10801116 expected.push_back(tokens[3].get());
10811117 expected.push_back(tokens[4].get());
10821118 CheckMultiTokensExistenceCallback callback(expected);
1083 system_dic->LookupPredictive(
1084 k1, kEnableKanaModiferInsensitiveLookup, &callback);
1119 system_dic->LookupPredictive(k1, convreq_, &callback);
10851120 EXPECT_TRUE(callback.AreAllFound());
10861121 }
10871122 }
11131148 // Lookup |t0| from "ていすていんぐ"
11141149 const string k = "\xe3\x81\xa6\xe3\x81\x84\xe3\x81\x99\xe3\x81\xa6"
11151150 "\xe3\x81\x84\xe3\x82\x93\xe3\x81\x90";
1151 request_.set_kana_modifier_insensitive_conversion(true);
1152 config_set_use_kana_modifier_insensitive_conversion(true);
11161153 CheckTokenExistenceCallback callback(t0.get());
1117 system_dic->LookupPrefix(k, kEnableKanaModiferInsensitiveLookup,
1118 &callback);
1154 system_dic->LookupPrefix(k, convreq_, &callback);
11191155 EXPECT_TRUE(callback.found()) << "Not found: " << PrintToken(*t0);
11201156 }
11211157
11511187 SystemDictionary::Builder(dic_fn_).Build());
11521188 ASSERT_TRUE(system_dic.get() != NULL)
11531189 << "Failed to open dictionary source:" << dic_fn_;
1190
1191 request_.set_kana_modifier_insensitive_conversion(false);
1192 config_set_use_kana_modifier_insensitive_conversion(true);
11541193
11551194 // Prefix search
11561195 {
11641203 not_to_be_looked_up.push_back(t4.get());
11651204 for (size_t i = 0; i < to_be_looked_up.size(); ++i) {
11661205 CheckTokenExistenceCallback callback(to_be_looked_up[i]);
1167 system_dic->LookupPrefix(
1168 k3, kDisableKanaModiferInsensitiveLookup, &callback);
1206 system_dic->LookupPrefix(k3, convreq_, &callback);
11691207 EXPECT_TRUE(callback.found())
11701208 << "Token is not found: " << PrintToken(*to_be_looked_up[i]);
11711209 }
11721210 for (size_t i = 0; i < not_to_be_looked_up.size(); ++i) {
11731211 CheckTokenExistenceCallback callback(not_to_be_looked_up[i]);
1174 system_dic->LookupPrefix(
1175 k3, kDisableKanaModiferInsensitiveLookup, &callback);
1212 system_dic->LookupPrefix(k3, convreq_, &callback);
11761213 EXPECT_FALSE(callback.found())
11771214 << "Token should not be found: "
11781215 << PrintToken(*not_to_be_looked_up[i]);
11911228 not_to_be_looked_up.push_back(t4.get());
11921229 for (size_t i = 0; i < to_be_looked_up.size(); ++i) {
11931230 CheckTokenExistenceCallback callback(to_be_looked_up[i]);
1194 system_dic->LookupPredictive(
1195 k1, kDisableKanaModiferInsensitiveLookup, &callback);
1231 system_dic->LookupPredictive(k1, convreq_, &callback);
11961232 EXPECT_TRUE(callback.found())
11971233 << "Token is not found: " << PrintToken(*to_be_looked_up[i]);
11981234 }
11991235 for (size_t i = 0; i < not_to_be_looked_up.size(); ++i) {
12001236 CheckTokenExistenceCallback callback(not_to_be_looked_up[i]);
1201 system_dic->LookupPredictive(
1202 k3, kDisableKanaModiferInsensitiveLookup, &callback);
1237 system_dic->LookupPredictive(k3, convreq_, &callback);
12031238 EXPECT_FALSE(callback.found())
12041239 << "Token should not be found: "
12051240 << PrintToken(*not_to_be_looked_up[i]);
6464 ],
6565 'dependencies': [
6666 '../../base/base.gyp:base_core',
67 '../../config/config.gyp:config_handler',
6768 '../../data_manager/data_manager.gyp:user_pos_manager',
69 '../../request/request.gyp:conversion_request',
6870 '../../session/session_base.gyp:request_test_util',
6971 '../../testing/testing.gyp:gtest_main',
7072 '../dictionary.gyp:dictionary_test_util',
8587 'dependencies': [
8688 '../../base/base.gyp:base_core',
8789 '../../data_manager/data_manager.gyp:user_pos_manager',
90 '../../request/request.gyp:conversion_request',
8891 '../../testing/testing.gyp:gtest_main',
8992 '../dictionary.gyp:dictionary_test_util',
9093 'system_dictionary.gyp:system_dictionary_builder',
163163
164164 void ValueDictionary::LookupPredictive(
165165 StringPiece key,
166 bool, // use_kana_modifier_insensitive_lookup
166 const ConversionRequest &conversion_request,
167167 Callback *callback) const {
168168 // Do nothing for empty key, although looking up all the entries with empty
169169 // string seems natural.
213213 }
214214
215215 void ValueDictionary::LookupPrefix(
216 StringPiece key, bool use_kana_modifier_insensitive_lookup,
216 StringPiece key,
217 const ConversionRequest &conversion_request,
217218 Callback *callback) const {}
218219
219 void ValueDictionary::LookupExact(StringPiece key, Callback *callback) const {
220 void ValueDictionary::LookupExact(
221 StringPiece key,
222 const ConversionRequest &conversion_request,
223 Callback *callback) const {
220224 if (key.empty()) {
221225 // For empty string, return nothing for compatibility reason; see the
222226 // comment above.
236240 callback->OnToken(key, key, token);
237241 }
238242
239 void ValueDictionary::LookupReverse(StringPiece str,
240 Callback *callback) const {}
243 void ValueDictionary::LookupReverse(
244 StringPiece str,
245 const ConversionRequest &conversion_request,
246 Callback *callback) const {
247 }
241248
242249 } // namespace dictionary
243250 } // namespace mozc
6161 // Implementation of DictionaryInterface
6262 virtual bool HasKey(StringPiece key) const;
6363 virtual bool HasValue(StringPiece value) const;
64 virtual void LookupPredictive(
65 StringPiece key, bool use_kana_modifier_insensitive_lookup,
66 Callback *callback) const;
67 virtual void LookupPrefix(
68 StringPiece key, bool use_kana_modifier_insensitive_lookup,
69 Callback *callback) const;
70 virtual void LookupExact(StringPiece key, Callback *callback) const;
71 virtual void LookupReverse(StringPiece str, Callback *callback) const;
64 virtual void LookupPredictive(StringPiece key,
65 const ConversionRequest &conversion_request,
66 Callback *callback) const;
67 virtual void LookupPrefix(StringPiece key,
68 const ConversionRequest &conversion_request,
69 Callback *callback) const;
70 virtual void LookupExact(StringPiece key,
71 const ConversionRequest &conversion_request,
72 Callback *callback) const;
73 virtual void LookupReverse(StringPiece str,
74 const ConversionRequest &conversion_request,
75 Callback *callback) const;
7276
7377 private:
7478 explicit ValueDictionary(const POSMatcher& pos_matcher);
4040 #include "dictionary/dictionary_token.h"
4141 #include "dictionary/pos_matcher.h"
4242 #include "dictionary/system/system_dictionary_builder.h"
43 #include "request/conversion_request.h"
4344 #include "testing/base/public/googletest.h"
4445 #include "testing/base/public/gunit.h"
4546
8889
8990 const string dict_name_;
9091 const POSMatcher *pos_matcher_;
92 ConversionRequest convreq_;
9193
9294 private:
9395 vector<Token *> tokens_;
146148
147149 {
148150 CollectTokenCallback callback;
149 dictionary->LookupPredictive("", false, &callback);
151 dictionary->LookupPredictive("", convreq_, &callback);
150152 EXPECT_TRUE(callback.tokens().empty());
151153 }
152154 {
153155 CollectTokenCallback callback;
154 dictionary->LookupPredictive("w", false, &callback);
156 dictionary->LookupPredictive("w", convreq_, &callback);
155157 vector<Token *> expected;
156158 expected.push_back(&token_we);
157159 expected.push_back(&token_war);
161163 }
162164 {
163165 CollectTokenCallback callback;
164 dictionary->LookupPredictive("wo", false, &callback);
166 dictionary->LookupPredictive("wo", convreq_, &callback);
165167 vector<Token *> expected;
166168 expected.push_back(&token_word);
167169 expected.push_back(&token_world);
169171 }
170172 {
171173 CollectTokenCallback callback;
172 dictionary->LookupPredictive("ho", false, &callback);
174 dictionary->LookupPredictive("ho", convreq_, &callback);
173175 EXPECT_TRUE(callback.tokens().empty());
174176 }
175177 }
187189 ValueDictionary::CreateValueDictionaryFromFile(*pos_matcher_,
188190 dict_name_));
189191 CollectTokenCallback callback;
190 dictionary->LookupExact("war", &callback);
192 dictionary->LookupExact("war", convreq_, &callback);
191193 ASSERT_EQ(1, callback.tokens().size());
192194 EXPECT_EQ("war", callback.tokens()[0].value);
193195 }
4242 #include "base/stl_util.h"
4343 #include "base/thread.h"
4444 #include "base/util.h"
45 #include "config/config_handler.h"
4645 #include "dictionary/dictionary_token.h"
4746 #include "dictionary/pos_matcher.h"
4847 #include "dictionary/suppression_dictionary.h"
309308
310309 void UserDictionary::LookupPredictive(
311310 StringPiece key,
312 bool, // use_kana_modifier_insensitive_lookup
311 const ConversionRequest &conversion_request,
313312 Callback *callback) const {
314313 scoped_reader_lock l(mutex_.get());
315314
320319 if (tokens_->empty()) {
321320 return;
322321 }
323 if (GET_CONFIG(incognito_mode)) {
322 if (conversion_request.config().incognito_mode()) {
324323 return;
325324 }
326325
358357
359358 // UserDictionary doesn't support kana modifier insensitive lookup.
360359 void UserDictionary::LookupPrefix(
361 StringPiece key, bool /*use_kana_modifier_insensitive_lookup*/,
360 StringPiece key,
361 const ConversionRequest &conversion_request,
362362 Callback *callback) const {
363363 scoped_reader_lock l(mutex_.get());
364364
369369 if (tokens_->empty()) {
370370 return;
371371 }
372 if (GET_CONFIG(incognito_mode)) {
372 if (conversion_request.config().incognito_mode()) {
373373 return;
374374 }
375375
414414 }
415415 }
416416
417 void UserDictionary::LookupExact(StringPiece key, Callback *callback) const {
417 void UserDictionary::LookupExact(
418 StringPiece key,
419 const ConversionRequest &conversion_request,
420 Callback *callback) const {
418421 scoped_reader_lock l(mutex_.get());
419 if (key.empty() || tokens_->empty() || GET_CONFIG(incognito_mode)) {
422 if (key.empty() || tokens_->empty() ||
423 conversion_request.config().incognito_mode()) {
420424 return;
421425 }
422426 UserPOS::Token key_token;
444448 }
445449 }
446450
447 void UserDictionary::LookupReverse(StringPiece str,
448 Callback *callback) const {
449 if (GET_CONFIG(incognito_mode)) {
450 return;
451 }
451 void UserDictionary::LookupReverse(
452 StringPiece key,
453 const ConversionRequest &conversion_request,
454 Callback *callback) const {
452455 }
453456
454457 bool UserDictionary::LookupComment(StringPiece key, StringPiece value,
458 const ConversionRequest &conversion_request,
455459 string *comment) const {
456 if (key.empty() || GET_CONFIG(incognito_mode)) {
460 if (key.empty() || conversion_request.config().incognito_mode()) {
457461 return false;
458462 }
459463
519523
520524 bool UserDictionary::AddToAutoRegisteredDictionary(
521525 const string &key, const string &value,
526 const ConversionRequest &conversion_request,
522527 user_dictionary::UserDictionary::PosType pos) {
523528 if (reloader_->IsRunning()) {
524529 return false;
525530 }
526531
527532 FindValueCallback callback(value);
528 LookupExact(key, &callback);
533 LookupExact(key, conversion_request, &callback);
529534 if (callback.found()) {
530535 // Already registered.
531536 return false;
5858 virtual bool HasValue(StringPiece value) const;
5959 // Lookup methods don't support kana modifier insensitive lookup, i.e.,
6060 // Callback::OnActualKey() is never called.
61 virtual void LookupPredictive(
62 StringPiece key, bool use_kana_modifier_insensitive_lookup,
63 Callback *callback) const;
64 virtual void LookupPrefix(
65 StringPiece key, bool use_kana_modifier_insensitive_lookup,
66 Callback *callback) const;
67 virtual void LookupExact(StringPiece key, Callback *callback) const;
68 virtual void LookupReverse(StringPiece str, Callback *callback) const;
61 virtual void LookupPredictive(StringPiece key,
62 const ConversionRequest &conversion_request,
63 Callback *callback) const;
64
65 virtual void LookupPrefix(StringPiece key,
66 const ConversionRequest &conversion_request,
67 Callback *callback) const;
68
69 virtual void LookupExact(StringPiece key,
70 const ConversionRequest &conversion_request,
71 Callback *callback) const;
72
73 virtual void LookupReverse(StringPiece str,
74 const ConversionRequest &conversion_request,
75 Callback *callback) const;
6976
7077 // Looks up a user comment from a pair of key and value. When (key, value)
7178 // doesn't exist in this dictionary or user comment is empty, bool is
7279 // returned and string is kept as-is.
7380 virtual bool LookupComment(StringPiece key, StringPiece value,
81 const ConversionRequest &conversion_request,
7482 string *comment) const;
7583
7684 // Loads dictionary from UserDictionaryStorage.
9199 // is executed synchronously with user input.
92100 bool AddToAutoRegisteredDictionary(
93101 const string &key, const string &value,
102 const ConversionRequest &conversion_request,
94103 user_dictionary::UserDictionary::PosType pos);
95104
96105 // Sets user dicitonary filename for unittesting
4444 }
4545
4646 virtual void LookupPredictive(
47 StringPiece key, bool use_kana_modifier_insensitive_lookup,
47 StringPiece key,
48 const ConversionRequest &conversion_request,
4849 Callback *callback) const {}
4950
5051 virtual void LookupPrefix(
51 StringPiece key, bool use_kana_modifier_insensitive_lookup,
52 StringPiece key,
53 const ConversionRequest &conversion_request,
5254 Callback *callback) const {}
5355
54 virtual void LookupExact(StringPiece key, Callback *callback) const {}
56 virtual void LookupExact(
57 StringPiece key,
58 const ConversionRequest &conversion_request,
59 Callback *callback) const {}
5560
56 virtual void LookupReverse(StringPiece str, Callback *callback) const {}
61 virtual void LookupReverse(
62 StringPiece str,
63 const ConversionRequest &conversion_request,
64 Callback *callback) const {}
5765
58 virtual bool LookupComment(StringPiece key, StringPiece value,
59 string *comment) const {
66 virtual bool LookupComment(
67 StringPiece key,
68 StringPiece value,
69 const ConversionRequest &conversion_request,
70 string *comment) const {
6071 if (key == "comment" || value == "comment") {
6172 comment->assign("UserDictionaryStub");
6273 return true;
5252 #include "dictionary/user_pos.h"
5353 #include "dictionary/user_pos_interface.h"
5454 #include "protocol/config.pb.h"
55 #include "request/conversion_request.h"
5556 #include "testing/base/public/googletest.h"
5657 #include "testing/base/public/gunit.h"
5758 #include "usage_stats/usage_stats.h"
188189 return result;
189190 }
190191
192 // This function will be removed soon in the following change, which
193 // enables to inject config to ConversionRequest.
194 void config_set_incognito_mode(bool is_enabled) {
195 config::Config config;
196 config::ConfigHandler::GetDefaultConfig(&config);
197 config.set_incognito_mode(is_enabled);
198 config::ConfigHandler::SetConfig(config);
199 }
200
191201 class UserDictionaryTest : public ::testing::Test {
192202 protected:
193203 virtual void SetUp() {
195205 suppression_dictionary_.reset(new SuppressionDictionary);
196206
197207 mozc::usage_stats::UsageStats::ClearAllStatsForTest();
208
209 // This config initialization will be removed once ConversionRequest can
210 // take config as an injected argument.
211 config::Config config;
212 config::ConfigHandler::GetDefaultConfig(&config);
213 config::ConfigHandler::SetConfig(config);
198214 }
199215
200216 virtual void TearDown() {
201217 mozc::usage_stats::UsageStats::ClearAllStatsForTest();
218
219 // This config initialization will be removed once ConversionRequest can
220 // take config as an injected argument.
221 config::Config config;
222 config::ConfigHandler::GetDefaultConfig(&config);
223 config::ConfigHandler::SetConfig(config);
202224 }
203225
204226 // Workaround for the constructor of UserDictionary being protected.
248270 vector<Entry> entries_;
249271 };
250272
251 static void TestLookupPredictiveHelper(const Entry *expected,
252 size_t expected_size,
253 StringPiece key,
254 const UserDictionary &dic) {
273 void TestLookupPredictiveHelper(const Entry *expected,
274 size_t expected_size,
275 StringPiece key,
276 const UserDictionary &dic) {
255277 EntryCollector collector;
256 dic.LookupPredictive(key, false, &collector);
278 dic.LookupPredictive(key, convreq_, &collector);
257279
258280 if (expected == NULL || expected_size == 0) {
259281 EXPECT_TRUE(collector.entries().empty());
263285 }
264286 }
265287
266 static void TestLookupPrefixHelper(const Entry *expected,
267 size_t expected_size,
268 const char *key,
269 size_t key_size,
270 const UserDictionary &dic) {
288 void TestLookupPrefixHelper(const Entry *expected,
289 size_t expected_size,
290 const char *key,
291 size_t key_size,
292 const UserDictionary &dic) {
271293 EntryCollector collector;
272 dic.LookupPrefix(StringPiece(key, key_size), false, &collector);
294 dic.LookupPrefix(StringPiece(key, key_size), convreq_, &collector);
273295
274296 if (expected == NULL || expected_size == 0) {
275297 EXPECT_TRUE(collector.entries().empty());
279301 }
280302 }
281303
282 static void TestLookupExactHelper(const Entry *expected,
283 size_t expected_size,
284 const char *key,
285 size_t key_size,
286 const UserDictionary &dic) {
304 void TestLookupExactHelper(const Entry *expected,
305 size_t expected_size,
306 const char *key,
307 size_t key_size,
308 const UserDictionary &dic) {
287309 EntryCollector collector;
288 dic.LookupExact(StringPiece(key, key_size), &collector);
310 dic.LookupExact(StringPiece(key, key_size), convreq_, &collector);
289311
290312 if (expected == NULL || expected_size == 0) {
291313 EXPECT_TRUE(collector.entries().empty());
355377 }
356378
357379 // Helper function to lookup comment string from |dic|.
358 static string LookupComment(const UserDictionary& dic,
359 StringPiece key, StringPiece value) {
380 string LookupComment(const UserDictionary& dic,
381 StringPiece key, StringPiece value) {
360382 string comment;
361 dic.LookupComment(key, value, &comment);
383 dic.LookupComment(key, value, convreq_, &comment);
362384 return comment;
363385 }
364386
365387 unique_ptr<SuppressionDictionary> suppression_dictionary_;
388 ConversionRequest convreq_;
366389
367390 private:
368391 mozc::usage_stats::scoped_usage_stats_enabler usage_stats_enabler_;
559582 }
560583
561584 TEST_F(UserDictionaryTest, IncognitoModeTest) {
562 config::Config config;
563 config::ConfigHandler::GetConfig(&config);
564 config.set_incognito_mode(true);
565 config::ConfigHandler::SetConfig(config);
566
585 config_set_incognito_mode(true);
567586 unique_ptr<UserDictionary> dic(CreateDictionaryWithMockPos());
568587 // Wait for async reload called from the constructor.
569588 dic->WaitForReloader();
577596 TestLookupPrefixHelper(NULL, 0, "start", 4, *dic);
578597 TestLookupPredictiveHelper(NULL, 0, "s", *dic);
579598
580 config.set_incognito_mode(false);
581 config::ConfigHandler::SetConfig(config);
582
599 config_set_incognito_mode(false);
583600 {
584601 EntryCollector collector;
585 dic->LookupPrefix("start", false, &collector);
602 dic->LookupPrefix("start", convreq_, &collector);
586603 EXPECT_FALSE(collector.entries().empty());
587604 }
588605 {
589606 EntryCollector collector;
590 dic->LookupPredictive("s", false, &collector);
607 dic->LookupPredictive("s", convreq_, &collector);
591608 EXPECT_FALSE(collector.entries().empty());
592609 }
593610 }
633650 dic->Reload();
634651 for (int i = 0; i < 1000; ++i) {
635652 CollectTokenCallback callback;
636 dic->LookupPrefix(keys[i], false, &callback);
653 dic->LookupPrefix(keys[i], convreq_, &callback);
637654 }
638655 }
639656 dic->WaitForReloader();
664681 EXPECT_TRUE(dic->AddToAutoRegisteredDictionary(
665682 "key" + NumberUtil::SimpleItoa(i),
666683 "value" + NumberUtil::SimpleItoa(i),
684 convreq_,
667685 user_dictionary::UserDictionary::NOUN));
668686 dic->WaitForReloader();
669687 }
703721 dic->WaitForReloader();
704722 dic->SetUserDictionaryName(filename);
705723 EXPECT_TRUE(dic->AddToAutoRegisteredDictionary(
706 "key", "value", user_dictionary::UserDictionary::NOUN));
724 "key", "value", convreq_, user_dictionary::UserDictionary::NOUN));
707725 dic->WaitForReloader();
708726 // Duplicated one is not registered.
709727 EXPECT_FALSE(dic->AddToAutoRegisteredDictionary(
710 "key", "value", user_dictionary::UserDictionary::NOUN));
728 "key", "value", convreq_, user_dictionary::UserDictionary::NOUN));
711729 dic->WaitForReloader();
712730 }
713731
844862 {
845863 const char kKey[] = "key0123";
846864 CollectTokenCallback callback;
847 user_dic->LookupPrefix(kKey, false, &callback);
865 user_dic->LookupPrefix(kKey, convreq_, &callback);
848866 const vector<Token> &tokens = callback.tokens();
849867 for (size_t i = 0; i < tokens.size(); ++i) {
850868 EXPECT_EQ("default", tokens[i].value);
853871 {
854872 const char kKey[] = "key";
855873 CollectTokenCallback callback;
856 user_dic->LookupPredictive(kKey, false, &callback);
874 user_dic->LookupPredictive(kKey, convreq_, &callback);
857875 const vector<Token> &tokens = callback.tokens();
858876 for (size_t i = 0; i < tokens.size(); ++i) {
859877 EXPECT_TRUE(tokens[i].value == "suggest_only" ||
924942 // Entry is in user dictionary but has no comment.
925943 string comment;
926944 comment = "prev comment";
927 EXPECT_FALSE(dic->LookupComment("comment_key1", "comment_value2", &comment));
945 EXPECT_FALSE(dic->LookupComment(
946 "comment_key1", "comment_value2", convreq_, &comment));
928947 EXPECT_EQ("prev comment", comment);
929948
930949 // Usual case: single key-value pair with comment.
931 EXPECT_TRUE(dic->LookupComment("comment_key2", "comment_value2", &comment));
950 EXPECT_TRUE(dic->LookupComment(
951 "comment_key2", "comment_value2", convreq_, &comment));
932952 EXPECT_EQ("comment", comment);
933953
934954 // There exist two entries having the same key, value and POS. Since POS is
935955 // irrelevant to comment lookup, the first nonempty comment should be found.
936 EXPECT_TRUE(dic->LookupComment("comment_key3", "comment_value3", &comment));
956 EXPECT_TRUE(dic->LookupComment(
957 "comment_key3", "comment_value3", convreq_, &comment));
937958 EXPECT_EQ("comment1", comment);
938959
939960 // White-space only comments should be cleared.
940 EXPECT_FALSE(dic->LookupComment("comment_key4", "comment_value4", &comment));
961 EXPECT_FALSE(dic->LookupComment(
962 "comment_key4", "comment_value4", convreq_, &comment));
941963 // The previous comment should remain.
942964 EXPECT_EQ("comment1", comment);
943965
00 MAJOR=2
11 MINOR=17
2 BUILD=2229
2 BUILD=2230
33 REVISION=102
44 # NACL_DICTIONARY_VERSION is the target version of the system dictionary to be
55 # downloaded by NaCl Mozc.
4444 #include "base/number_util.h"
4545 #include "base/util.h"
4646 #include "composer/composer.h"
47 #include "config/config_handler.h"
4847 #include "converter/connector.h"
4948 #include "converter/converter_interface.h"
5049 #include "converter/immutable_converter_interface.h"
140139 return request.mixed_conversion() || FLAGS_enable_mixed_conversion;
141140 }
142141
143 bool IsTypingCorrectionEnabled() {
144 return GET_CONFIG(use_typing_correction) ||
142 bool IsTypingCorrectionEnabled(const ConversionRequest &request) {
143 return request.config().use_typing_correction() ||
145144 FLAGS_enable_typing_correction;
146145 }
147146
14391438 vector<Result> *results) const {
14401439 // Check that history_key/history_value are in the dictionary.
14411440 FindValueCallback find_history_callback(history_value);
1442 dictionary_->LookupPrefix(history_key, false, &find_history_callback);
1441 dictionary_->LookupPrefix(history_key, request, &find_history_callback);
14431442
14441443 // History value is not found in the dictionary.
14451444 // User may create this the history candidate from T13N or segment
14761475 history_value_size - 1, 1));
14771476 for (size_t i = prev_results_size; i < results->size(); ++i) {
14781477 CheckBigramResult(find_history_callback.token(), history_ctype,
1479 last_history_ctype, &(*results)[i]);
1478 last_history_ctype, request, &(*results)[i]);
14801479 }
14811480 }
14821481
14861485 const Token &history_token,
14871486 const Util::ScriptType history_ctype,
14881487 const Util::ScriptType last_history_ctype,
1488 const ConversionRequest &request,
14891489 Result *result) const {
14901490 DCHECK(result);
14911491
15471547 }
15481548
15491549 FindValueCallback callback(value);
1550 dictionary_->LookupPrefix(key, false, &callback);
1550 dictionary_->LookupPrefix(key, request, &callback);
15511551 if (!callback.found()) {
15521552 result->types = NO_PREDICTION;
15531553 return;
15701570 const bool is_zero_query = query_key.empty();
15711571 PredictiveLookupCallback callback(types, lookup_limit, input_key.size(),
15721572 NULL, is_zero_query, results);
1573 dictionary.LookupPredictive(input_key, false, &callback);
1573 dictionary.LookupPredictive(input_key, request, &callback);
15741574 return;
15751575 }
15761576
15881588 PredictiveLookupCallback callback(
15891589 types, lookup_limit, input_key.size(),
15901590 expanded.empty() ? NULL : &expanded, is_zero_query, results);
1591 dictionary.LookupPredictive(
1592 input_key, request.IsKanaModifierInsensitiveConversion(), &callback);
1591 dictionary.LookupPredictive(input_key, request, &callback);
15931592 }
15941593
15951594 void DictionaryPredictor::GetPredictiveResultsForBigram(
16101609 PredictiveBigramLookupCallback callback(
16111610 types, lookup_limit, input_key.size(), NULL, history_value,
16121611 is_zero_query, results);
1613 dictionary.LookupPredictive(input_key, false, &callback);
1612 dictionary.LookupPredictive(input_key, request, &callback);
16141613 return;
16151614 }
16161615
16291628 expanded.empty() ? NULL : &expanded,
16301629 history_value, is_zero_query,
16311630 results);
1632 dictionary.LookupPredictive(
1633 input_key, request.IsKanaModifierInsensitiveConversion(), &callback);
1631 dictionary.LookupPredictive(input_key, request, &callback);
16341632 }
16351633
16361634 void DictionaryPredictor::GetPredictiveResultsForEnglish(
16611659 Util::LowerString(&key);
16621660 PredictiveLookupCallback callback(types, lookup_limit, key.size(), NULL,
16631661 false, results);
1664 dictionary.LookupPredictive(key, false, &callback);
1662 dictionary.LookupPredictive(key, request, &callback);
16651663 for (size_t i = prev_results_size; i < results->size(); ++i) {
16661664 Util::UpperString(&results->at(i).value);
16671665 }
16721670 Util::LowerString(&key);
16731671 PredictiveLookupCallback callback(types, lookup_limit, key.size(), NULL,
16741672 false, results);
1675 dictionary.LookupPredictive(key, false, &callback);
1673 dictionary.LookupPredictive(key, request, &callback);
16761674 for (size_t i = prev_results_size; i < results->size(); ++i) {
16771675 Util::CapitalizeString(&results->at(i).value);
16781676 }
16801678 // For other cases (lower and as-is), just look up directly.
16811679 PredictiveLookupCallback callback(types, lookup_limit, input_key.size(),
16821680 NULL, false, results);
1683 dictionary.LookupPredictive(input_key, false, &callback);
1681 dictionary.LookupPredictive(input_key, request, &callback);
16841682 }
16851683 // If input mode is FULL_ASCII, then convert the results to full-width.
16861684 if (request.composer().GetInputMode() == transliteration::FULL_ASCII) {
17131711 PredictiveLookupCallback callback(
17141712 types, lookup_limit, input_key.size(),
17151713 query.expanded.empty() ? NULL : &query.expanded, false, results);
1716 dictionary.LookupPredictive(
1717 input_key, request.IsKanaModifierInsensitiveConversion(), &callback);
1714 dictionary.LookupPredictive(input_key, request, &callback);
17181715
17191716 for (size_t i = previous_results_size; i < results->size(); ++i) {
17201717 results->at(i).wcost += query.cost;
19621959
19631960 const bool zero_query_suggestion = request.request().zero_query_suggestion();
19641961 if (IsLatinInputMode(request) && !zero_query_suggestion) {
1965 if (GET_CONFIG(use_dictionary_suggest)) {
1962 if (request.config().use_dictionary_suggest()) {
19661963 // By following the dictionary_suggest config, enable English prediction.
19671964 result |= ENGLISH;
19681965 }
19721969 return result;
19731970 }
19741971
1975 if (!GET_CONFIG(use_dictionary_suggest) &&
1972 if (!request.config().use_dictionary_suggest() &&
19761973 segments.request_type() == Segments::SUGGESTION) {
19771974 VLOG(2) << "no_dictionary_suggest";
19781975 return result;
20222019 result |= SUFFIX;
20232020 }
20242021
2025 if (IsTypingCorrectionEnabled() && key_len >= 3) {
2022 if (IsTypingCorrectionEnabled(request) && key_len >= 3) {
20262023 result |= TYPING_CORRECTION;
20272024 }
20282025
20412038 }
20422039
20432040 return (segments.request_type() == Segments::PARTIAL_SUGGESTION ||
2044 GET_CONFIG(use_realtime_conversion) ||
2041 request.config().use_realtime_conversion() ||
20452042 IsMixedConversionEnabled(request.request()));
20462043 }
20472044
273273 void CheckBigramResult(const dictionary::Token &history_token,
274274 const Util::ScriptType history_ctype,
275275 const Util::ScriptType last_history_ctype,
276 const ConversionRequest &request,
276277 Result *result) const;
277278
278279 void GetPredictiveResults(const dictionary::DictionaryInterface &dictionary,
305305 bool(StringPiece));
306306 MOCK_CONST_METHOD3(LookupPredictive,
307307 void(StringPiece key,
308 bool use_kana_modifier_insensitive_lookup,
308 const ConversionRequest& convreq,
309309 Callback *callback));
310310 MOCK_CONST_METHOD3(LookupPrefix,
311311 void(StringPiece key,
312 bool use_kana_modifier_insensitive_lookup,
312 const ConversionRequest& convreq,
313313 Callback *callback));
314 MOCK_CONST_METHOD2(LookupExact,
315 void(StringPiece key, Callback *callback));
316 MOCK_CONST_METHOD2(LookupReverse,
317 void(StringPiece str, Callback *callback));
314 MOCK_CONST_METHOD3(LookupExact,
315 void(StringPiece key,
316 const ConversionRequest& convreq,
317 Callback *callback));
318 MOCK_CONST_METHOD3(LookupReverse,
319 void(StringPiece str,
320 const ConversionRequest& convreq,
321 Callback *callback));
318322 };
319323
320324 // Action to call the third argument of LookupPrefix with the token
629633 composer.GetQueryForPrediction(&query);
630634 segment->set_key(query);
631635
632 EXPECT_CALL(*check_dictionary, LookupPredictive(_, use_expansion, _));
636 EXPECT_CALL(*check_dictionary,
637 LookupPredictive(_, ::testing::Ref(conversion_request), _));
633638
634639 vector<TestableDictionaryPredictor::Result> results;
635640 predictor->AggregateUnigramPrediction(
687692 segment->set_key(query);
688693
689694 // History key and value should be in the dictionary.
690 EXPECT_CALL(*check_dictionary, LookupPrefix(_, _, _))
695 EXPECT_CALL(*check_dictionary,
696 LookupPrefix(_, ::testing::Ref(conversion_request), _))
691697 .WillOnce(LookupPrefixOneToken(
692698 // "ぐーぐる"
693699 "\xe3\x81\x90\xe3\x83\xbc\xe3\x81\x90\xe3\x82\x8b",
694700 // "グーグル"
695701 "\xe3\x82\xb0\xe3\x83\xbc\xe3\x82\xb0\xe3\x83\xab",
696702 1, 1));
697 EXPECT_CALL(*check_dictionary, LookupPredictive(_, use_expansion, _));
703 EXPECT_CALL(*check_dictionary,
704 LookupPredictive(_, ::testing::Ref(conversion_request), _));
698705
699706 vector<TestableDictionaryPredictor::Result> results;
700707 predictor->AggregateBigramPrediction(
735742 composer.GetQueryForPrediction(&query);
736743 segment->set_key(query);
737744
738 EXPECT_CALL(*check_dictionary, LookupPredictive(_, use_expansion, _));
745 EXPECT_CALL(*check_dictionary,
746 LookupPredictive(_, ::testing::Ref(conversion_request), _));
739747
740748 vector<TestableDictionaryPredictor::Result> results;
741749 predictor->AggregateSuffixPrediction(
17731781
17741782 virtual void LookupPredictive(
17751783 StringPiece key,
1776 bool use_kana_modifier_insensitive_lookup,
1784 const ConversionRequest &conversion_request,
17771785 Callback *callback) const {
17781786 Token token;
17791787 for (size_t i = 0; i < arraysize(kSuffixTokens); ++i) {
18041812 }
18051813
18061814 virtual void LookupPrefix(
1807 StringPiece key, bool use_kana_modifier_insensitive_lookup,
1815 StringPiece key,
1816 const ConversionRequest &conversion_request,
18081817 Callback *callback) const {}
18091818
1810 virtual void LookupExact(StringPiece key, Callback *callback) const {}
1811
1812 virtual void LookupReverse(StringPiece str, Callback *callback) const {}
1819 virtual void LookupExact(StringPiece key,
1820 const ConversionRequest &conversion_request,
1821 Callback *callback) const {}
1822
1823 virtual void LookupReverse(StringPiece str,
1824 const ConversionRequest &conversion_request,
1825 Callback *callback) const {}
18131826 };
18141827
18151828 } // namespace
7171 return *request_;
7272 }
7373
74 void ConversionRequest::set_request(const commands::Request *request) {
75 request_ = request;
76 }
77
7478 const config::Config &ConversionRequest::config() const {
7579 return config::ConfigHandler::GetConfig();
7680 }
9191 void set_composer_key_selection(ComposerKeySelection selection);
9292
9393 const commands::Request &request() const;
94 void set_request(const commands::Request *request);
9495
9596 const config::Config &config() const;
9697
192192 if (dictionary_ != NULL) {
193193 if (dictionary_->LookupComment(segment->candidate(j).content_key,
194194 segment->candidate(j).content_value,
195 request,
195196 &comment)) {
196197 Segment::Candidate *candidate = segment->mutable_candidate(j);
197198 candidate->usage_id = usage_id_for_user_comment;