Skip to content

Commit

Permalink
Use Modules for the constructors of DictionaryPredictionAggregator.
Browse files Browse the repository at this point in the history
#codehealth

PiperOrigin-RevId: 602641139
  • Loading branch information
hiroyuki-komatsu committed Jan 30, 2024
1 parent 49aaf9c commit 02d69fc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 62 deletions.
1 change: 1 addition & 0 deletions src/prediction/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
34 changes: 10 additions & 24 deletions src/prediction/dictionary_prediction_aggregator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -551,41 +551,27 @@ 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<SingleKanjiPredictionAggregator>(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)
: DictionaryPredictionAggregator(
data_manager, converter, immutable_converter, dictionary,
suffix_dictionary, pos_matcher,
std::make_unique<SingleKanjiPredictionAggregator>(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<PredictionAggregatorInterface>
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;
Expand Down
12 changes: 1 addition & 11 deletions src/prediction/dictionary_prediction_aggregator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Result> AggregateResults(const ConversionRequest &request,
const Segments &segments) const override;

Expand Down Expand Up @@ -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<PredictionAggregatorInterface>
single_kanji_prediction_aggregator);

Expand Down
52 changes: 25 additions & 27 deletions src/prediction/dictionary_prediction_aggregator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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<PredictionAggregatorInterface>
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;

Expand Down Expand Up @@ -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<DictionaryInterface> suffix_dictionary = nullptr) {
auto dictionary = std::make_unique<MockDictionary>();
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<MockSingleKanjiPredictionAggregator>();
single_kanji_prediction_aggregator_ = kanji_aggregator.get();

aggregator_ = std::make_unique<DictionaryPredictionAggregatorTestPeer>(
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_; }
Expand All @@ -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_;
}
Expand All @@ -435,9 +435,7 @@ class MockDataAndAggregator {
const testing::MockDataManager data_manager_;
MockConverter converter_;
MockImmutableConverter mock_immutable_converter_;
std::unique_ptr<const DictionaryInterface> dictionary_;
std::unique_ptr<const DictionaryInterface> suffix_dictionary_;
PosMatcher pos_matcher_;
engine::Modules modules_;

MockDictionary *mock_dictionary_;
MockSingleKanjiPredictionAggregator *single_kanji_prediction_aggregator_;
Expand Down Expand Up @@ -1788,7 +1786,7 @@ class TestSuffixDictionary : public DictionaryInterface {

TEST_F(DictionaryPredictionAggregatorTest, AggregateSuffixPrediction) {
auto data_and_aggregator = std::make_unique<MockDataAndAggregator>();
data_and_aggregator->Init(new TestSuffixDictionary());
data_and_aggregator->Init(std::make_unique<TestSuffixDictionary>());

const DictionaryPredictionAggregatorTestPeer &aggregator =
data_and_aggregator->aggregator();
Expand Down Expand Up @@ -1825,7 +1823,7 @@ TEST_F(DictionaryPredictionAggregatorTest, AggregateSuffixPrediction) {

TEST_F(DictionaryPredictionAggregatorTest, AggregateZeroQuerySuffixPrediction) {
auto data_and_aggregator = std::make_unique<MockDataAndAggregator>();
data_and_aggregator->Init(new TestSuffixDictionary());
data_and_aggregator->Init(std::make_unique<TestSuffixDictionary>());

const DictionaryPredictionAggregatorTestPeer &aggregator =
data_and_aggregator->aggregator();
Expand Down

0 comments on commit 02d69fc

Please sign in to comment.