From bc3c4136b3316b12a14ea5a83ac206cedbcc179f Mon Sep 17 00:00:00 2001 From: Hiroyuki Komatsu Date: Mon, 9 Dec 2024 05:24:07 +0000 Subject: [PATCH] Remove the segments_size param of ConverterInterface::ResizeSegment(). * Also renamed one of overloaded ResizeSegment functions to ResizeSegments. #codehealth PiperOrigin-RevId: 704137708 --- src/converter/converter.cc | 9 ++-- src/converter/converter.h | 4 +- src/converter/converter_interface.h | 4 +- src/converter/converter_main.cc | 13 ++++-- src/converter/converter_mock.h | 4 +- src/converter/converter_test.cc | 42 +++++++----------- src/engine/minimal_engine.cc | 6 +-- .../user_boundary_history_rewriter.cc | 4 +- .../user_boundary_history_rewriter_test.cc | 44 ++++++++----------- 9 files changed, 59 insertions(+), 71 deletions(-) diff --git a/src/converter/converter.cc b/src/converter/converter.cc index de38d7032..990bdedce 100644 --- a/src/converter/converter.cc +++ b/src/converter/converter.cc @@ -525,11 +525,10 @@ bool Converter::ResizeSegment(Segments *segments, return true; } -bool Converter::ResizeSegment(Segments *segments, - const ConversionRequest &request, - size_t start_segment_index, - size_t unused_segments_size, - absl::Span new_size_array) const { +bool Converter::ResizeSegments(Segments *segments, + const ConversionRequest &request, + size_t start_segment_index, + absl::Span new_size_array) const { if (request.request_type() != ConversionRequest::CONVERSION) { return false; } diff --git a/src/converter/converter.h b/src/converter/converter.h index 3ffc23a45..d9514013d 100644 --- a/src/converter/converter.h +++ b/src/converter/converter.h @@ -106,9 +106,9 @@ class Converter final : public ConverterInterface { const ConversionRequest &request, size_t segment_index, int offset_length) const override; - ABSL_MUST_USE_RESULT bool ResizeSegment( + ABSL_MUST_USE_RESULT bool ResizeSegments( Segments *segments, const ConversionRequest &request, - size_t start_segment_index, size_t unused_segments_size, + size_t start_segment_index, absl::Span new_size_array) const override; // Execute ImmutableConverter, Rewriters, SuppressionDictionary. diff --git a/src/converter/converter_interface.h b/src/converter/converter_interface.h index 703a35d20..2cdd8bd08 100644 --- a/src/converter/converter_interface.h +++ b/src/converter/converter_interface.h @@ -144,9 +144,9 @@ class ConverterInterface { // Resize [start_segment_index, start_segment_index + segment_size] // segments with the new size in new_size_array. - ABSL_MUST_USE_RESULT virtual bool ResizeSegment( + ABSL_MUST_USE_RESULT virtual bool ResizeSegments( Segments *segments, const ConversionRequest &request, - size_t start_segment_index, size_t segments_size, + size_t start_segment_index, absl::Span new_size_array) const = 0; protected: diff --git a/src/converter/converter_main.cc b/src/converter/converter_main.cc index 2d98d9191..676e85b91 100644 --- a/src/converter/converter_main.cc +++ b/src/converter/converter_main.cc @@ -388,15 +388,20 @@ bool ExecCommand(const ConverterInterface &converter, const std::string &line, return converter.ResizeSegment(segments, conversion_request, NumberUtil::SimpleAtoi(fields[1]), NumberUtil::SimpleAtoi(fields[2])); - } else if (fields.size() > 3) { + } + } else if (func == "resizesegments" || func == "resizes") { + options.request_type = ConversionRequest::CONVERSION; + const ConversionRequest conversion_request = ConversionRequest( + composer, request, context, *config, std::move(options)); + if (fields.size() > 3) { std::vector new_arrays; - for (size_t i = 3; i < fields.size(); ++i) { + for (size_t i = 2; i < fields.size(); ++i) { new_arrays.push_back( static_cast(NumberUtil::SimpleAtoi(fields[i]))); } - return converter.ResizeSegment( + return converter.ResizeSegments( segments, conversion_request, NumberUtil::SimpleAtoi(fields[1]), - NumberUtil::SimpleAtoi(fields[2]), new_arrays); + new_arrays); } } else if (func == "disableuserhistory") { config->set_history_learning_level(config::Config::NO_HISTORY); diff --git a/src/converter/converter_mock.h b/src/converter/converter_mock.h index cfd6cee57..c2bd8e3b3 100644 --- a/src/converter/converter_mock.h +++ b/src/converter/converter_mock.h @@ -84,9 +84,9 @@ class StrictMockConverter : public ConverterInterface { (Segments * segments, const ConversionRequest &request, size_t segment_index, int offset_length), (const, override)); - MOCK_METHOD(bool, ResizeSegment, + MOCK_METHOD(bool, ResizeSegments, (Segments * segments, const ConversionRequest &request, - size_t start_segment_index, size_t segments_size, + size_t start_segment_index, absl::Span new_size_array), (const, override)); }; diff --git a/src/converter/converter_test.cc b/src/converter/converter_test.cc index e0785515a..0323cf33f 100644 --- a/src/converter/converter_test.cc +++ b/src/converter/converter_test.cc @@ -2062,7 +2062,7 @@ TEST_F(ConverterTest, ResizeSegmentWithOffset) { } } -TEST_F(ConverterTest, ResizeSegmentWithArray) { +TEST_F(ConverterTest, ResizeSegmentsWithArray) { constexpr Segment::SegmentType kFixedBoundary = Segment::FIXED_BOUNDARY; constexpr Segment::SegmentType kFree = Segment::FREE; @@ -2076,11 +2076,10 @@ TEST_F(ConverterTest, ResizeSegmentWithArray) { AddSegment("あいうえ", kFree, segments); const ConversionRequest convreq; const int start_segment_index = 0; - const int segments_size = 1; const std::array size_array = {3, 1}; EXPECT_EQ(segments.conversion_segments_size(), 1); - EXPECT_TRUE(converter->ResizeSegment( - &segments, convreq, start_segment_index, segments_size, size_array)); + EXPECT_TRUE(converter->ResizeSegments(&segments, convreq, + start_segment_index, size_array)); ASSERT_EQ(segments.conversion_segments_size(), 2); EXPECT_EQ(segments.conversion_segment(0).key(), "あいう"); EXPECT_EQ(segments.conversion_segment(1).key(), "え"); @@ -2100,11 +2099,10 @@ TEST_F(ConverterTest, ResizeSegmentWithArray) { AddSegment("あいうえ", Segment::FREE, segments); const ConversionRequest convreq; const int start_segment_index = 0; - const int segments_size = 1; const std::array size_array = {3, 1}; EXPECT_EQ(segments.conversion_segments_size(), 1); - EXPECT_TRUE(converter->ResizeSegment( - &segments, convreq, start_segment_index, segments_size, size_array)); + EXPECT_TRUE(converter->ResizeSegments(&segments, convreq, + start_segment_index, size_array)); ASSERT_EQ(segments.conversion_segments_size(), 2); EXPECT_EQ(segments.conversion_segment(0).key(), "あいう"); EXPECT_EQ(segments.conversion_segment(1).key(), "え"); @@ -2119,11 +2117,10 @@ TEST_F(ConverterTest, ResizeSegmentWithArray) { AddSegment("あいうえ", Segment::FREE, segments); const ConversionRequest convreq; const int start_segment_index = 0; - const int segments_size = 1; const std::array size_array = {3, 1, 0, 0, 0, 0, 0, 0}; EXPECT_EQ(segments.conversion_segments_size(), 1); - EXPECT_TRUE(converter->ResizeSegment( - &segments, convreq, start_segment_index, segments_size, size_array)); + EXPECT_TRUE(converter->ResizeSegments(&segments, convreq, + start_segment_index, size_array)); ASSERT_EQ(segments.conversion_segments_size(), 2); EXPECT_EQ(segments.conversion_segment(0).key(), "あいう"); EXPECT_EQ(segments.conversion_segment(1).key(), "え"); @@ -2137,11 +2134,10 @@ TEST_F(ConverterTest, ResizeSegmentWithArray) { AddSegment("あいうえおかきくけ", Segment::FREE, segments); const ConversionRequest convreq; const int start_segment_index = 0; - const int segments_size = 1; const std::array size_array = {2, 2, 1, 2, 2}; EXPECT_EQ(segments.conversion_segments_size(), 1); - EXPECT_TRUE(converter->ResizeSegment( - &segments, convreq, start_segment_index, segments_size, size_array)); + EXPECT_TRUE(converter->ResizeSegments(&segments, convreq, + start_segment_index, size_array)); ASSERT_EQ(segments.conversion_segments_size(), 5); EXPECT_EQ(segments.conversion_segment(0).key(), "あい"); EXPECT_EQ(segments.conversion_segment(1).key(), "うえ"); @@ -2165,11 +2161,10 @@ TEST_F(ConverterTest, ResizeSegmentWithArray) { AddSegment("くけ", Segment::FREE, segments); const ConversionRequest convreq; const int start_segment_index = 0; - const int segments_size = 4; const std::array size_array = {4, 1, 4}; EXPECT_EQ(segments.conversion_segments_size(), 4); - EXPECT_TRUE(converter->ResizeSegment( - &segments, convreq, start_segment_index, segments_size, size_array)); + EXPECT_TRUE(converter->ResizeSegments(&segments, convreq, + start_segment_index, size_array)); ASSERT_EQ(segments.conversion_segments_size(), 3); EXPECT_EQ(segments.conversion_segment(0).key(), "あいうえ"); EXPECT_EQ(segments.conversion_segment(1).key(), "お"); @@ -2189,11 +2184,10 @@ TEST_F(ConverterTest, ResizeSegmentWithArray) { AddSegment("くけ", Segment::FREE, segments); const ConversionRequest convreq; const int start_segment_index = 0; - const int segments_size = 2; const std::array size_array = {4, 1}; EXPECT_EQ(segments.conversion_segments_size(), 4); - EXPECT_TRUE(converter->ResizeSegment( - &segments, convreq, start_segment_index, segments_size, size_array)); + EXPECT_TRUE(converter->ResizeSegments(&segments, convreq, + start_segment_index, size_array)); ASSERT_EQ(segments.conversion_segments_size(), 4); EXPECT_EQ(segments.conversion_segment(0).key(), "あいうえ"); EXPECT_EQ(segments.conversion_segment(1).key(), "お"); @@ -2211,11 +2205,10 @@ TEST_F(ConverterTest, ResizeSegmentWithArray) { AddSegment("あいうえ", Segment::FREE, segments); const ConversionRequest convreq; const int start_segment_index = 0; - const int segments_size = 1; const std::array size_array = {3}; EXPECT_EQ(segments.conversion_segments_size(), 1); - EXPECT_TRUE(converter->ResizeSegment( - &segments, convreq, start_segment_index, segments_size, size_array)); + EXPECT_TRUE(converter->ResizeSegments(&segments, convreq, + start_segment_index, size_array)); ASSERT_EQ(segments.conversion_segments_size(), 2); EXPECT_EQ(segments.conversion_segment(0).key(), "あいう"); EXPECT_EQ(segments.conversion_segment(1).key(), "え"); @@ -2235,11 +2228,10 @@ TEST_F(ConverterTest, ResizeSegmentWithArray) { AddSegment("くけ", Segment::FREE, segments); const ConversionRequest convreq; const int start_segment_index = 2; - const int segments_size = 2; const std::array size_array = {4}; EXPECT_EQ(segments.conversion_segments_size(), 4); - EXPECT_TRUE(converter->ResizeSegment( - &segments, convreq, start_segment_index, segments_size, size_array)); + EXPECT_TRUE(converter->ResizeSegments(&segments, convreq, + start_segment_index, size_array)); // Since {"あいう", "えお"} may be modified too, the segment index for // "かきくけ" may be different from 2. diff --git a/src/engine/minimal_engine.cc b/src/engine/minimal_engine.cc index 07511d8c0..14c096763 100644 --- a/src/engine/minimal_engine.cc +++ b/src/engine/minimal_engine.cc @@ -146,9 +146,9 @@ class MinimalConverter : public ConverterInterface { return true; } - bool ResizeSegment(Segments *segments, const ConversionRequest &request, - size_t start_segment_index, size_t segments_size, - absl::Span new_size_array) const override { + bool ResizeSegments(Segments *segments, const ConversionRequest &request, + size_t start_segment_index, + absl::Span new_size_array) const override { return true; } }; diff --git a/src/rewriter/user_boundary_history_rewriter.cc b/src/rewriter/user_boundary_history_rewriter.cc index 07371670a..f86ef3b7f 100644 --- a/src/rewriter/user_boundary_history_rewriter.cc +++ b/src/rewriter/user_boundary_history_rewriter.cc @@ -312,8 +312,8 @@ bool UserBoundaryHistoryRewriter::Resize( MOZC_VLOG(2) << "ResizeSegment key: " << key << " segments: [" << seg_idx << ", " << seg_size << "] " << "resize: [" << absl::StrJoin(updated_array, " ") << "]"; - if (parent_converter_->ResizeSegment(&segments, request, seg_idx, - seg_size, updated_array)) { + if (parent_converter_->ResizeSegments(&segments, request, seg_idx, + updated_array)) { result = true; } else { LOG(WARNING) << "ResizeSegment failed for key: " << key; diff --git a/src/rewriter/user_boundary_history_rewriter_test.cc b/src/rewriter/user_boundary_history_rewriter_test.cc index 5a05b1581..36db950ce 100644 --- a/src/rewriter/user_boundary_history_rewriter_test.cc +++ b/src/rewriter/user_boundary_history_rewriter_test.cc @@ -142,10 +142,9 @@ TEST_F(UserBoundaryHistoryRewriterTest, SplitSegmentByHistory) { // TODO(noriyukit): The current implementation always sets the length array // size to 8 with padded zeros. Better to set the actual length. Segments segments = MakeSegments({"たんぽぽ"}, Segment::FREE); - EXPECT_CALL(converter, ResizeSegment(&segments, Ref(convreq), - /*start_segment_index=*/0, - /*segments_size=*/1, - ElementsAre(2, 2, 0, 0, 0, 0, 0, 0))) + EXPECT_CALL(converter, ResizeSegments(&segments, Ref(convreq), + /*start_segment_index=*/0, + ElementsAre(2, 2, 0, 0, 0, 0, 0, 0))) .WillOnce(Return(true)); EXPECT_TRUE(rewriter.Rewrite(convreq, &segments)); } @@ -175,10 +174,9 @@ TEST_F(UserBoundaryHistoryRewriterTest, JoinSegmentsByHistory) { // TODO(noriyukit): The current implementation always sets the length array // size to 8 with padded zeros. Better to set the actual length. Segments segments = MakeSegments({"たん", "ぽぽ"}, Segment::FREE); - EXPECT_CALL(converter, ResizeSegment(&segments, Ref(convreq), - /*start_segment_index=*/0, - /*segments_size=*/2, - ElementsAre(4, 0, 0, 0, 0, 0, 0, 0))) + EXPECT_CALL(converter, ResizeSegments(&segments, Ref(convreq), + /*start_segment_index=*/0, + ElementsAre(4, 0, 0, 0, 0, 0, 0, 0))) .WillOnce(Return(true)); EXPECT_TRUE(rewriter.Rewrite(convreq, &segments)); } @@ -416,13 +414,11 @@ TEST_F(UserBoundaryHistoryRewriterTest, FailureOfSplitIsNotFatal) { { const ConversionRequest convreq = CreateConversionRequest(); Segments segments = MakeSegments({"たんぽぽ", "わたげ"}, Segment::FREE); - EXPECT_CALL(converter, ResizeSegment(&segments, Ref(convreq), - /*start_segment_index=*/0, - /*segments_size=*/1, _)) + EXPECT_CALL(converter, ResizeSegments(&segments, Ref(convreq), + /*start_segment_index=*/0, _)) .WillOnce(Return(false)); - EXPECT_CALL(converter, ResizeSegment(&segments, Ref(convreq), - /*start_segment_index=*/1, - /*segments_size=*/1, _)) + EXPECT_CALL(converter, ResizeSegments(&segments, Ref(convreq), + /*start_segment_index=*/1, _)) .WillOnce(Return(false)); EXPECT_FALSE(rewriter.Rewrite(convreq, &segments)); } @@ -431,13 +427,11 @@ TEST_F(UserBoundaryHistoryRewriterTest, FailureOfSplitIsNotFatal) { Segments segments = MakeSegments({"たんぽぽ", "わたげ"}, Segment::FREE); const Segments resized = MakeSegments({"たん", "ぽぽ", "わたげ"}, Segment::FREE); - EXPECT_CALL(converter, ResizeSegment(&segments, Ref(convreq), - /*start_segment_index=*/0, - /*segments_size=*/1, _)) + EXPECT_CALL(converter, ResizeSegments(&segments, Ref(convreq), + /*start_segment_index=*/0, _)) .WillOnce(DoAll(SetArgPointee<0>(resized), Return(true))); - EXPECT_CALL(converter, ResizeSegment(&segments, Ref(convreq), - /*start_segment_index=*/1, - /*segments_size=*/1, _)) + EXPECT_CALL(converter, ResizeSegments(&segments, Ref(convreq), + /*start_segment_index=*/1, _)) .WillOnce(Return(false)); EXPECT_TRUE(rewriter.Rewrite(convreq, &segments)); } @@ -446,13 +440,11 @@ TEST_F(UserBoundaryHistoryRewriterTest, FailureOfSplitIsNotFatal) { Segments segments = MakeSegments({"たんぽぽ", "わたげ"}, Segment::FREE); const Segments resized = MakeSegments({"たんぽぽ", "わた", "げ"}, Segment::FREE); - EXPECT_CALL(converter, ResizeSegment(&segments, Ref(convreq), - /*start_segment_index=*/0, - /*segments_size=*/1, _)) + EXPECT_CALL(converter, ResizeSegments(&segments, Ref(convreq), + /*start_segment_index=*/0, _)) .WillOnce(Return(false)); - EXPECT_CALL(converter, ResizeSegment(&segments, Ref(convreq), - /*start_segment_index=*/1, - /*segments_size=*/1, _)) + EXPECT_CALL(converter, ResizeSegments(&segments, Ref(convreq), + /*start_segment_index=*/1, _)) .WillOnce(DoAll(SetArgPointee<0>(resized), Return(true))); EXPECT_TRUE(rewriter.Rewrite(convreq, &segments)); }