Skip to content

Commit

Permalink
Remove StartSuggestion, StartPartialPrediction, and StartPartialSugge…
Browse files Browse the repository at this point in the history
…stion from ConverterInterface.

* Use StartPrediction covers all functionality.
* ConversionRequest::RequestType controls the behavior.

For example, instead of using StartPartialSuggestion, please use StartPrediction with ConversionRequest with RequestType set to PARTIAL_SUGGESTION.

Before:
const ConversionRequest convreq = ConversionRequestBuilder()
    ...
    .Build();
converter.StartPartialSuggestion(convreq, *segment);

After:
const ConversionRequest convreq = ConversionRequestBuilder()
    .SetRequestType(ConversionRequest::PARTIAL_SUGGESTION)
    ....
    .Build();
converter.StartPrediction(convreq, *segment);
PiperOrigin-RevId: 699945320
  • Loading branch information
hiroyuki-komatsu committed Nov 25, 2024
1 parent ef3688d commit a81137f
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 97 deletions.
31 changes: 0 additions & 31 deletions src/converter/converter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -455,37 +455,6 @@ bool Converter::StartPrediction(const ConversionRequest &request,
return Predict(request, segments);
}

bool Converter::StartSuggestion(const ConversionRequest &original_request,
Segments *segments) const {
const ConversionRequest request = CreateConversionRequestWithType(
original_request, ConversionRequest::SUGGESTION);
return Predict(request, segments);
}

bool Converter::StartPartialSuggestion(
const ConversionRequest &original_request, Segments *segments) const {
const size_t cursor = original_request.composer().GetCursor();
if (cursor == 0 || cursor == original_request.composer().GetLength()) {
return StartSuggestion(original_request, segments);
}

const ConversionRequest request = CreateConversionRequestWithType(
original_request, ConversionRequest::PARTIAL_SUGGESTION);
return Predict(request, segments);
}

bool Converter::StartPartialPrediction(
const ConversionRequest &original_request, Segments *segments) const {
const size_t cursor = original_request.composer().GetCursor();
if (cursor == 0 || cursor == original_request.composer().GetLength()) {
return StartPrediction(original_request, segments);
}

const ConversionRequest request = CreateConversionRequestWithType(
original_request, ConversionRequest::PARTIAL_PREDICTION);
return Predict(request, segments);
}

void Converter::FinishConversion(const ConversionRequest &request,
Segments *segments) const {
CommitUsageStats(segments, segments->history_segments_size(),
Expand Down
9 changes: 0 additions & 9 deletions src/converter/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,6 @@ class Converter final : public ConverterInterface {
ABSL_MUST_USE_RESULT
bool StartPrediction(const ConversionRequest &request,
Segments *segments) const override;
ABSL_MUST_USE_RESULT
bool StartSuggestion(const ConversionRequest &request,
Segments *segments) const override;
ABSL_MUST_USE_RESULT
bool StartPartialPrediction(const ConversionRequest &request,
Segments *segments) const override;
ABSL_MUST_USE_RESULT
bool StartPartialSuggestion(const ConversionRequest &request,
Segments *segments) const override;

void FinishConversion(const ConversionRequest &request,
Segments *segments) const override;
Expand Down
15 changes: 0 additions & 15 deletions src/converter/converter_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,6 @@ class ConverterInterface {
virtual bool StartPrediction(const ConversionRequest &request,
Segments *segments) const = 0;

// Starts suggestion for given request.
ABSL_MUST_USE_RESULT
virtual bool StartSuggestion(const ConversionRequest &request,
Segments *segments) const = 0;

// Starts partial prediction for given request.
ABSL_MUST_USE_RESULT
virtual bool StartPartialPrediction(const ConversionRequest &request,
Segments *segments) const = 0;

// Starts partial suggestion for given request.
ABSL_MUST_USE_RESULT
virtual bool StartPartialSuggestion(const ConversionRequest &request,
Segments *segments) const = 0;

// Finish conversion.
// Segments are cleared. Context is not cleared
virtual void FinishConversion(const ConversionRequest &request,
Expand Down
24 changes: 20 additions & 4 deletions src/converter/converter_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,12 @@ bool ExecCommand(const ConverterInterface &converter, const std::string &line,
absl::GetFlag(FLAGS_max_conversion_candidates_size),
.create_partial_candidates = request.auto_partial_suggestion(),
};
const ConversionRequest conversion_request = ConversionRequest(
composer, request, context, *config, std::move(options));

const std::string &func = fields[0];
if (func == "startconversion" || func == "start" || func == "s") {
options.request_type = ConversionRequest::CONVERSION;
const ConversionRequest conversion_request = ConversionRequest(
composer, request, context, *config, std::move(options));
CHECK_FIELDS_LENGTH(2);
composer.SetPreeditTextForTestOnly(fields[1]);
return converter.StartConversion(conversion_request, segments);
Expand All @@ -325,20 +326,29 @@ bool ExecCommand(const ConverterInterface &converter, const std::string &line,
CHECK_FIELDS_LENGTH(2);
return converter.StartReverseConversion(segments, fields[1]);
} else if (func == "startprediction" || func == "predict" || func == "p") {
options.request_type = ConversionRequest::PREDICTION;
const ConversionRequest conversion_request = ConversionRequest(
composer, request, context, *config, std::move(options));
if (fields.size() >= 2) {
composer.SetPreeditTextForTestOnly(fields[1]);
return converter.StartPrediction(conversion_request, segments);
} else {
return converter.StartPrediction(conversion_request, segments);
}
} else if (func == "startsuggestion" || func == "suggest") {
options.request_type = ConversionRequest::SUGGESTION;
const ConversionRequest conversion_request = ConversionRequest(
composer, request, context, *config, std::move(options));
if (fields.size() >= 2) {
composer.SetPreeditTextForTestOnly(fields[1]);
return converter.StartSuggestion(conversion_request, segments);
return converter.StartPrediction(conversion_request, segments);
} else {
return converter.StartSuggestion(conversion_request, segments);
return converter.StartPrediction(conversion_request, segments);
}
} else if (func == "finishconversion" || func == "finish") {
options.request_type = ConversionRequest::CONVERSION;
const ConversionRequest conversion_request = ConversionRequest(
composer, request, context, *config, std::move(options));
converter.FinishConversion(conversion_request, segments);
return true;
} else if (func == "resetconversion" || func == "reset") {
Expand All @@ -359,6 +369,9 @@ bool ExecCommand(const ConverterInterface &converter, const std::string &line,
if (!(converter.CommitSegmentValue(segments, i, 0))) return false;
}
}
options.request_type = ConversionRequest::CONVERSION;
const ConversionRequest conversion_request = ConversionRequest(
composer, request, context, *config, std::move(options));
converter.FinishConversion(conversion_request, segments);
return true;
} else if (func == "focussegmentvalue" || func == "focus") {
Expand All @@ -372,6 +385,9 @@ bool ExecCommand(const ConverterInterface &converter, const std::string &line,
singleton_vector.push_back(NumberUtil::SimpleAtoi(fields[1]));
return converter.CommitSegments(segments, singleton_vector);
} else if (func == "resizesegment" || func == "resize") {
options.request_type = ConversionRequest::CONVERSION;
const ConversionRequest conversion_request = ConversionRequest(
composer, request, context, *config, std::move(options));
if (fields.size() == 3) {
return converter.ResizeSegment(segments, conversion_request,
NumberUtil::SimpleAtoi(fields[1]),
Expand Down
9 changes: 0 additions & 9 deletions src/converter/converter_mock.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,6 @@ class StrictMockConverter : public ConverterInterface {
MOCK_METHOD(bool, StartPrediction,
(const ConversionRequest &request, Segments *segments),
(const, override));
MOCK_METHOD(bool, StartSuggestion,
(const ConversionRequest &request, Segments *segments),
(const, override));
MOCK_METHOD(bool, StartPartialPrediction,
(const ConversionRequest &request, Segments *segments),
(const, override));
MOCK_METHOD(bool, StartPartialSuggestion,
(const ConversionRequest &request, Segments *segments),
(const, override));
MOCK_METHOD(void, FinishConversion,
(const ConversionRequest &request, Segments *segments),
(const, override));
Expand Down
16 changes: 8 additions & 8 deletions src/converter/converter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ TEST_F(ConverterTest, StartSuggestion) {
std::move(options));

Segments segments;
EXPECT_TRUE(converter->StartSuggestion(request, &segments));
EXPECT_TRUE(converter->StartPrediction(request, &segments));
EXPECT_EQ(segments.segments_size(), 1);
ASSERT_TRUE(segments.segment(0).meta_candidates_size() >=
transliteration::HALF_ASCII);
Expand All @@ -1043,7 +1043,7 @@ TEST_F(ConverterTest, StartSuggestion) {
std::move(options));

Segments segments;
EXPECT_TRUE(converter->StartSuggestion(request, &segments));
EXPECT_TRUE(converter->StartPrediction(request, &segments));
EXPECT_EQ(segments.segments_size(), 1);
ASSERT_TRUE(segments.segment(0).meta_candidates_size() >=
transliteration::HALF_ASCII);
Expand All @@ -1059,7 +1059,7 @@ TEST_F(ConverterTest, StartPartialPrediction) {
ConverterInterface *converter = engine->GetConverter();
CHECK(converter);
Segments segments;
EXPECT_TRUE(converter->StartPartialPrediction(
EXPECT_TRUE(converter->StartPrediction(
ConvReq("わたしは", ConversionRequest::PARTIAL_PREDICTION), &segments));
EXPECT_EQ(segments.segments_size(), 1);
EXPECT_EQ(segments.segment(0).candidate(0).key, "わたしは");
Expand All @@ -1072,7 +1072,7 @@ TEST_F(ConverterTest, StartPartialSuggestion) {
ConverterInterface *converter = engine->GetConverter();
CHECK(converter);
Segments segments;
EXPECT_TRUE(converter->StartPartialSuggestion(
EXPECT_TRUE(converter->StartPrediction(
ConvReq("わたしは", ConversionRequest::PARTIAL_SUGGESTION), &segments));
EXPECT_EQ(segments.segments_size(), 1);
EXPECT_EQ(segments.segment(0).candidate(0).key, "わたしは");
Expand All @@ -1084,7 +1084,7 @@ TEST_F(ConverterTest, StartPartialPredictionMobile) {
ConverterInterface *converter = engine->GetConverter();
CHECK(converter);
Segments segments;
EXPECT_TRUE(converter->StartPartialPrediction(
EXPECT_TRUE(converter->StartPrediction(
ConvReq("わたしは", ConversionRequest::PARTIAL_PREDICTION), &segments));
EXPECT_EQ(segments.segments_size(), 1);
EXPECT_EQ(segments.segment(0).candidate(0).key, "わたしは");
Expand All @@ -1096,7 +1096,7 @@ TEST_F(ConverterTest, StartPartialSuggestionMobile) {
ConverterInterface *converter = engine->GetConverter();
CHECK(converter);
Segments segments;
EXPECT_TRUE(converter->StartPartialSuggestion(
EXPECT_TRUE(converter->StartPrediction(
ConvReq("わたしは", ConversionRequest::PARTIAL_SUGGESTION), &segments));
EXPECT_EQ(segments.segments_size(), 1);
EXPECT_EQ(segments.segment(0).candidate(0).key, "わたしは");
Expand Down Expand Up @@ -1245,7 +1245,7 @@ TEST_F(ConverterTest, VariantExpansionForSuggestion) {
Segments segments;
{
// Dictionary suggestion
EXPECT_TRUE(converter.StartSuggestion(
EXPECT_TRUE(converter.StartPrediction(
ConvReq("てすと", ConversionRequest::SUGGESTION), &segments));
EXPECT_EQ(segments.conversion_segments_size(), 1);
EXPECT_LE(1, segments.conversion_segment(0).candidates_size());
Expand All @@ -1256,7 +1256,7 @@ TEST_F(ConverterTest, VariantExpansionForSuggestion) {
{
// Realtime conversion
segments.Clear();
EXPECT_TRUE(converter.StartSuggestion(
EXPECT_TRUE(converter.StartPrediction(
ConvReq("てすとの", ConversionRequest::SUGGESTION), &segments));
EXPECT_EQ(segments.conversion_segments_size(), 1);
EXPECT_LE(1, segments.conversion_segment(0).candidates_size());
Expand Down
12 changes: 6 additions & 6 deletions src/converter/quality_regression_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,9 @@ absl::StatusOr<bool> QualityRegressionUtil::ConvertAndTest(
const ConversionRequest conv_req(
composer, request_, context, config_,
{.request_type = ConversionRequest::SUGGESTION});
if (!converter_->StartSuggestion(conv_req, &segments_)) {
return absl::UnknownError(
absl::StrCat("StartSuggestion failed: ", item.OutputAsTSV()));
if (!converter_->StartPrediction(conv_req, &segments_)) {
return absl::UnknownError(absl::StrCat(
"StartPrediction for suggestion failed: ", item.OutputAsTSV()));
}
} else if (command == kZeroQueryExpect || command == kZeroQueryNotExpect) {
commands::Request request = request_;
Expand All @@ -298,9 +298,9 @@ absl::StatusOr<bool> QualityRegressionUtil::ConvertAndTest(
composer, request, context, config_,
{.request_type = ConversionRequest::SUGGESTION,
.max_conversion_candidates_size = 10});
if (!converter_->StartSuggestion(conv_req, &segments_)) {
return absl::UnknownError(
absl::StrCat("StartSuggestion failed: ", item.OutputAsTSV()));
if (!converter_->StartPrediction(conv_req, &segments_)) {
return absl::UnknownError(absl::StrCat(
"StartSuggestion for suggestion failed: ", item.OutputAsTSV()));
}
if (!converter_->CommitSegmentValue(&segments_, 0, 0)) {
return absl::UnknownError(
Expand Down
15 changes: 0 additions & 15 deletions src/engine/minimal_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,6 @@ class MinimalConverter : public ConverterInterface {
return AddAsIsCandidate(request, segments);
}

bool StartSuggestion(const ConversionRequest &request,
Segments *segments) const override {
return AddAsIsCandidate(request, segments);
}

bool StartPartialPrediction(const ConversionRequest &request,
Segments *segments) const override {
return false;
}

bool StartPartialSuggestion(const ConversionRequest &request,
Segments *segments) const override {
return false;
}

void FinishConversion(const ConversionRequest &request,
Segments *segments) const override {}

Expand Down

0 comments on commit a81137f

Please sign in to comment.