From 02d69fc748a2befccd542e6c7cf33319a7d957ab Mon Sep 17 00:00:00 2001 From: Hiroyuki Komatsu Date: Tue, 30 Jan 2024 09:12:11 +0000 Subject: [PATCH] Use Modules for the constructors of DictionaryPredictionAggregator. #codehealth PiperOrigin-RevId: 602641139 --- src/prediction/BUILD.bazel | 1 + .../dictionary_prediction_aggregator.cc | 34 ++++-------- .../dictionary_prediction_aggregator.h | 12 +---- .../dictionary_prediction_aggregator_test.cc | 52 +++++++++---------- 4 files changed, 37 insertions(+), 62 deletions(-) diff --git a/src/prediction/BUILD.bazel b/src/prediction/BUILD.bazel index aa3019444..f905d8bd1 100644 --- a/src/prediction/BUILD.bazel +++ b/src/prediction/BUILD.bazel @@ -288,6 +288,7 @@ mozc_cc_test( "//dictionary:dictionary_token", "//dictionary:pos_matcher", "//dictionary:suffix_dictionary", + "//engine:modules", "//engine:spellchecker_interface", "//protocol:commands_cc_proto", "//protocol:config_cc_proto", diff --git a/src/prediction/dictionary_prediction_aggregator.cc b/src/prediction/dictionary_prediction_aggregator.cc index 987e50c3a..4618edd03 100644 --- a/src/prediction/dictionary_prediction_aggregator.cc +++ b/src/prediction/dictionary_prediction_aggregator.cc @@ -551,8 +551,7 @@ DictionaryPredictionAggregator::DictionaryPredictionAggregator( const ImmutableConverterInterface *immutable_converter, const engine::Modules &modules) : DictionaryPredictionAggregator( - data_manager, converter, immutable_converter, modules.GetDictionary(), - modules.GetSuffixDictionary(), modules.GetPosMatcher(), + data_manager, converter, immutable_converter, modules, std::make_unique(data_manager)) { } @@ -560,32 +559,19 @@ DictionaryPredictionAggregator::DictionaryPredictionAggregator( const DataManagerInterface &data_manager, const ConverterInterface *converter, const ImmutableConverterInterface *immutable_converter, - const dictionary::DictionaryInterface *dictionary, - const dictionary::DictionaryInterface *suffix_dictionary, - const dictionary::PosMatcher *pos_matcher) - : DictionaryPredictionAggregator( - data_manager, converter, immutable_converter, dictionary, - suffix_dictionary, pos_matcher, - std::make_unique(data_manager)) {} - -DictionaryPredictionAggregator::DictionaryPredictionAggregator( - const DataManagerInterface &data_manager, - const ConverterInterface *converter, - const ImmutableConverterInterface *immutable_converter, - const dictionary::DictionaryInterface *dictionary, - const dictionary::DictionaryInterface *suffix_dictionary, - const dictionary::PosMatcher *pos_matcher, + const engine::Modules &modules, std::unique_ptr single_kanji_prediction_aggregator) : converter_(converter), immutable_converter_(immutable_converter), - dictionary_(dictionary), - suffix_dictionary_(suffix_dictionary), - counter_suffix_word_id_(pos_matcher->GetCounterSuffixWordId()), - kanji_number_id_(pos_matcher->GetKanjiNumberId()), - zip_code_id_(pos_matcher->GetZipcodeId()), - number_id_(pos_matcher->GetNumberId()), - unknown_id_(pos_matcher->GetUnknownId()), + dictionary_(modules.GetDictionary()), + suffix_dictionary_(modules.GetSuffixDictionary()), + counter_suffix_word_id_( + modules.GetPosMatcher()->GetCounterSuffixWordId()), + kanji_number_id_(modules.GetPosMatcher()->GetKanjiNumberId()), + zip_code_id_(modules.GetPosMatcher()->GetZipcodeId()), + number_id_(modules.GetPosMatcher()->GetNumberId()), + unknown_id_(modules.GetPosMatcher()->GetUnknownId()), single_kanji_prediction_aggregator_( std::move(single_kanji_prediction_aggregator)) { absl::string_view zero_query_token_array_data; diff --git a/src/prediction/dictionary_prediction_aggregator.h b/src/prediction/dictionary_prediction_aggregator.h index 91de46ba2..0aa4b09c3 100644 --- a/src/prediction/dictionary_prediction_aggregator.h +++ b/src/prediction/dictionary_prediction_aggregator.h @@ -71,14 +71,6 @@ class DictionaryPredictionAggregator : public PredictionAggregatorInterface { const ImmutableConverterInterface *immutable_converter, const engine::Modules &modules); - DictionaryPredictionAggregator( - const DataManagerInterface &data_manager, - const ConverterInterface *converter, - const ImmutableConverterInterface *immutable_converter, - const dictionary::DictionaryInterface *dictionary, - const dictionary::DictionaryInterface *suffix_dictionary, - const dictionary::PosMatcher *pos_matcher); - std::vector AggregateResults(const ConversionRequest &request, const Segments &segments) const override; @@ -122,9 +114,7 @@ class DictionaryPredictionAggregator : public PredictionAggregatorInterface { const DataManagerInterface &data_manager, const ConverterInterface *converter, const ImmutableConverterInterface *immutable_converter, - const dictionary::DictionaryInterface *dictionary, - const dictionary::DictionaryInterface *suffix_dictionary, - const dictionary::PosMatcher *pos_matcher, + const engine::Modules &modules, std::unique_ptr single_kanji_prediction_aggregator); diff --git a/src/prediction/dictionary_prediction_aggregator_test.cc b/src/prediction/dictionary_prediction_aggregator_test.cc index a7bac21e8..f00b01a85 100644 --- a/src/prediction/dictionary_prediction_aggregator_test.cc +++ b/src/prediction/dictionary_prediction_aggregator_test.cc @@ -62,6 +62,7 @@ #include "dictionary/dictionary_token.h" #include "dictionary/pos_matcher.h" #include "dictionary/suffix_dictionary.h" +#include "engine/modules.h" #include "engine/spellchecker_interface.h" #include "prediction/prediction_aggregator_interface.h" #include "prediction/result.h" @@ -87,13 +88,10 @@ class DictionaryPredictionAggregatorTestPeer { const DataManagerInterface &data_manager, const ConverterInterface *converter, const ImmutableConverterInterface *immutable_converter, - const dictionary::DictionaryInterface *dictionary, - const dictionary::DictionaryInterface *suffix_dictionary, - const dictionary::PosMatcher *pos_matcher, + const engine::Modules &modules, std::unique_ptr single_kanji_prediction_aggregator) - : aggregator_(data_manager, converter, immutable_converter, dictionary, - suffix_dictionary, pos_matcher, + : aggregator_(data_manager, converter, immutable_converter, modules, std::move(single_kanji_prediction_aggregator)) {} virtual ~DictionaryPredictionAggregatorTestPeer() = default; @@ -390,25 +388,27 @@ class MockDataAndAggregator { // Initializes aggregator with the given suffix_dictionary. When // nullptr is passed to the |suffix_dictionary|, MockDataManager's suffix // dictionary is used. - // Note that |suffix_dictionary| is owned by this class. - void Init(const DictionaryInterface *suffix_dictionary = nullptr) { - pos_matcher_.Set(data_manager_.GetPosMatcherData()); - mock_dictionary_ = new MockDictionary; - single_kanji_prediction_aggregator_ = - new MockSingleKanjiPredictionAggregator; - dictionary_.reset(mock_dictionary_); - if (!suffix_dictionary) { - suffix_dictionary_.reset( - CreateSuffixDictionaryFromDataManager(data_manager_)); - } else { - suffix_dictionary_.reset(suffix_dictionary); + // Note that |suffix_dictionary| is owned by Modules. + void Init(std::unique_ptr suffix_dictionary = nullptr) { + auto dictionary = std::make_unique(); + mock_dictionary_ = dictionary.get(); + modules_.PresetDictionary(std::move(dictionary)); + + if (suffix_dictionary) { + modules_.PresetSuffixDictionary(std::move(suffix_dictionary)); } - CHECK(suffix_dictionary_.get()); + + absl::Status init = modules_.Init(&data_manager_); + CHECK_OK(init); + CHECK_NE(modules_.GetSuffixDictionary(), nullptr); + + auto kanji_aggregator = + std::make_unique(); + single_kanji_prediction_aggregator_ = kanji_aggregator.get(); aggregator_ = std::make_unique( - data_manager_, &converter_, &mock_immutable_converter_, - dictionary_.get(), suffix_dictionary_.get(), &pos_matcher_, - absl::WrapUnique(single_kanji_prediction_aggregator_)); + data_manager_, &converter_, &mock_immutable_converter_, modules_, + std::move(kanji_aggregator)); } MockDictionary *mutable_dictionary() { return mock_dictionary_; } @@ -420,7 +420,7 @@ class MockDataAndAggregator { mutable_single_kanji_prediction_aggregator() { return single_kanji_prediction_aggregator_; } - const PosMatcher &pos_matcher() const { return pos_matcher_; } + const PosMatcher &pos_matcher() const { return *modules_.GetPosMatcher(); } const DictionaryPredictionAggregatorTestPeer &aggregator() { return *aggregator_; } @@ -435,9 +435,7 @@ class MockDataAndAggregator { const testing::MockDataManager data_manager_; MockConverter converter_; MockImmutableConverter mock_immutable_converter_; - std::unique_ptr dictionary_; - std::unique_ptr suffix_dictionary_; - PosMatcher pos_matcher_; + engine::Modules modules_; MockDictionary *mock_dictionary_; MockSingleKanjiPredictionAggregator *single_kanji_prediction_aggregator_; @@ -1788,7 +1786,7 @@ class TestSuffixDictionary : public DictionaryInterface { TEST_F(DictionaryPredictionAggregatorTest, AggregateSuffixPrediction) { auto data_and_aggregator = std::make_unique(); - data_and_aggregator->Init(new TestSuffixDictionary()); + data_and_aggregator->Init(std::make_unique()); const DictionaryPredictionAggregatorTestPeer &aggregator = data_and_aggregator->aggregator(); @@ -1825,7 +1823,7 @@ TEST_F(DictionaryPredictionAggregatorTest, AggregateSuffixPrediction) { TEST_F(DictionaryPredictionAggregatorTest, AggregateZeroQuerySuffixPrediction) { auto data_and_aggregator = std::make_unique(); - data_and_aggregator->Init(new TestSuffixDictionary()); + data_and_aggregator->Init(std::make_unique()); const DictionaryPredictionAggregatorTestPeer &aggregator = data_and_aggregator->aggregator();