Skip to content

Commit 3267df5

Browse files
Refactor to extract a logic to create an alternative candidate.
* The behavior should not be changed. * Add CreateAlternativeCandidate to extract a logic of a new candidate creation from RewriteSegment. #codehealth PiperOrigin-RevId: 722525552
1 parent 2a2f6cf commit 3267df5

File tree

2 files changed

+96
-77
lines changed

2 files changed

+96
-77
lines changed

src/rewriter/variants_rewriter.cc

Lines changed: 86 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,75 @@ VariantsRewriter::GetFormTypesFromStringPair(absl::string_view input1,
385385
return std::make_pair(output_form1, output_form2);
386386
}
387387

388+
VariantsRewriter::AlternativeCandidateResult
389+
VariantsRewriter::CreateAlternativeCandidate(
390+
const Segment::Candidate &original_candidate) const {
391+
std::string primary_value, secondary_value;
392+
std::string primary_content_value, secondary_content_value;
393+
std::vector<uint32_t> primary_inner_segment_boundary;
394+
std::vector<uint32_t> secondary_inner_segment_boundary;
395+
396+
AlternativeCandidateResult result;
397+
if (!GenerateAlternatives(
398+
original_candidate, &primary_value, &secondary_value,
399+
&primary_content_value, &secondary_content_value,
400+
&primary_inner_segment_boundary, &secondary_inner_segment_boundary)) {
401+
return result;
402+
}
403+
404+
auto get_description_type = [](Util::FormType form) {
405+
switch (form) {
406+
case Util::FULL_WIDTH:
407+
return VariantsRewriter::FULL_WIDTH;
408+
case Util::HALF_WIDTH:
409+
return VariantsRewriter::HALF_WIDTH;
410+
default:
411+
return VariantsRewriter::FULL_HALF_WIDTH;
412+
}
413+
};
414+
415+
// auto = std::pair<Util::FormType, Util::FormType>
416+
const auto [primary_form, secondary_form] =
417+
GetFormTypesFromStringPair(primary_value, secondary_value);
418+
const bool is_primary_half_width = (primary_form == Util::HALF_WIDTH);
419+
const bool is_secondary_half_width = (secondary_form == Util::HALF_WIDTH);
420+
const int primary_description_type =
421+
(get_description_type(primary_form) | CHARACTER_FORM | ZIPCODE |
422+
SPELLING_CORRECTION);
423+
const int secondary_description_type =
424+
(get_description_type(secondary_form) | CHARACTER_FORM | ZIPCODE |
425+
SPELLING_CORRECTION);
426+
427+
auto new_candidate = std::make_unique<Segment::Candidate>(original_candidate);
428+
429+
if (original_candidate.value == primary_value) {
430+
result.is_original_candidate_primary = true;
431+
result.original_candidate_description_type = primary_description_type;
432+
433+
new_candidate->value = std::move(secondary_value);
434+
new_candidate->content_value = std::move(secondary_content_value);
435+
new_candidate->inner_segment_boundary =
436+
std::move(secondary_inner_segment_boundary);
437+
new_candidate->style =
438+
GetStyle(original_candidate.style, is_secondary_half_width);
439+
SetDescription(pos_matcher_, secondary_description_type,
440+
new_candidate.get());
441+
} else {
442+
result.is_original_candidate_primary = false;
443+
result.original_candidate_description_type = secondary_description_type;
444+
445+
new_candidate->value = std::move(primary_value);
446+
new_candidate->content_value = std::move(primary_content_value);
447+
new_candidate->inner_segment_boundary =
448+
std::move(primary_inner_segment_boundary);
449+
new_candidate->style =
450+
GetStyle(original_candidate.style, is_primary_half_width);
451+
SetDescription(pos_matcher_, primary_description_type, new_candidate.get());
452+
}
453+
result.alternative_candidate = std::move(new_candidate);
454+
return result;
455+
}
456+
388457
bool VariantsRewriter::RewriteSegment(RewriteType type, Segment *seg) const {
389458
CHECK(seg);
390459
bool modified = false;
@@ -400,22 +469,7 @@ bool VariantsRewriter::RewriteSegment(RewriteType type, Segment *seg) const {
400469
SetDescriptionForTransliteration(pos_matcher_, candidate);
401470
}
402471

403-
auto get_description_type = [](Util::FormType form) {
404-
switch (form) {
405-
case Util::FULL_WIDTH:
406-
return VariantsRewriter::FULL_WIDTH;
407-
case Util::HALF_WIDTH:
408-
return VariantsRewriter::HALF_WIDTH;
409-
default:
410-
return VariantsRewriter::FULL_HALF_WIDTH;
411-
}
412-
};
413-
414472
// Regular Candidate
415-
std::string primary_value, secondary_value;
416-
std::string primary_content_value, secondary_content_value;
417-
std::vector<uint32_t> primary_inner_segment_boundary;
418-
std::vector<uint32_t> secondary_inner_segment_boundary;
419473
for (size_t i = 0; i < seg->candidates_size(); ++i) {
420474
Segment::Candidate *original_candidate = seg->mutable_candidate(i);
421475
DCHECK(original_candidate);
@@ -432,80 +486,36 @@ bool VariantsRewriter::RewriteSegment(RewriteType type, Segment *seg) const {
432486
continue;
433487
}
434488

435-
if (!GenerateAlternatives(*original_candidate, &primary_value,
436-
&secondary_value, &primary_content_value,
437-
&secondary_content_value,
438-
&primary_inner_segment_boundary,
439-
&secondary_inner_segment_boundary)) {
489+
AlternativeCandidateResult result =
490+
CreateAlternativeCandidate(*original_candidate);
491+
if (result.alternative_candidate == nullptr) {
440492
SetDescriptionForCandidate(pos_matcher_, original_candidate);
441493
continue;
442494
}
443495

444-
// auto = std::pair<Util::FormType, Util::FormType>
445-
const auto [primary_form, secondary_form] =
446-
GetFormTypesFromStringPair(primary_value, secondary_value);
447-
const bool is_primary_half_width = (primary_form == Util::HALF_WIDTH);
448-
const bool is_secondary_half_width =
449-
(secondary_form == Util::HALF_WIDTH);
450-
const int primary_description_type =
451-
(get_description_type(primary_form) | CHARACTER_FORM | ZIPCODE |
452-
SPELLING_CORRECTION);
453-
const int secondary_description_type =
454-
(get_description_type(secondary_form) | CHARACTER_FORM | ZIPCODE |
455-
SPELLING_CORRECTION);
456-
457496
if (type == EXPAND_VARIANT) {
458497
// Insert default candidate to position |i| and
459498
// rewrite original(|i+1|) to alternative
460-
auto alt_candidate =
461-
std::make_unique<Segment::Candidate>(*original_candidate);
462-
463-
if (original_candidate->value == primary_value) {
464-
SetDescription(pos_matcher_, primary_description_type,
499+
if (result.is_original_candidate_primary) {
500+
SetDescription(pos_matcher_, result.original_candidate_description_type,
465501
original_candidate);
466502

467-
alt_candidate->value = std::move(secondary_value);
468-
alt_candidate->content_value = std::move(secondary_content_value);
469-
alt_candidate->inner_segment_boundary =
470-
std::move(secondary_inner_segment_boundary);
471-
alt_candidate->style =
472-
GetStyle(original_candidate->style, is_secondary_half_width);
473-
SetDescription(pos_matcher_, secondary_description_type,
474-
alt_candidate.get());
475-
seg->insert_candidate(i + 1, std::move(alt_candidate));
503+
seg->insert_candidate(i + 1, std::move(result.alternative_candidate));
476504
} else {
477-
alt_candidate->value = std::move(primary_value);
478-
alt_candidate->content_value = std::move(primary_content_value);
479-
alt_candidate->inner_segment_boundary =
480-
std::move(primary_inner_segment_boundary);
481-
alt_candidate->style =
482-
GetStyle(original_candidate->style, is_primary_half_width);
483-
SetDescription(pos_matcher_, primary_description_type,
484-
alt_candidate.get());
485-
486-
SetDescription(pos_matcher_, secondary_description_type,
505+
SetDescription(pos_matcher_, result.original_candidate_description_type,
487506
original_candidate);
488-
seg->insert_candidate(i, std::move(alt_candidate));
507+
seg->insert_candidate(i, std::move(result.alternative_candidate));
489508
}
490509
++i; // skip inserted candidate
491510
} else if (type == SELECT_VARIANT) {
492-
if (original_candidate->value == primary_value) {
493-
SetDescription(pos_matcher_, primary_description_type,
511+
if (result.is_original_candidate_primary) {
512+
SetDescription(pos_matcher_, result.original_candidate_description_type,
494513
original_candidate);
514+
// new_candidate is not used.
495515
} else {
496-
auto alt_candidate =
497-
std::make_unique<Segment::Candidate>(*original_candidate);
498516
// Replace the original candidate to the alternative candidate.
499-
alt_candidate->value = std::move(primary_value);
500-
alt_candidate->content_value = std::move(primary_content_value);
501-
alt_candidate->inner_segment_boundary =
502-
std::move(primary_inner_segment_boundary);
503-
alt_candidate->style =
504-
GetStyle(original_candidate->style, is_primary_half_width);
505-
SetDescription(pos_matcher_, primary_description_type,
506-
alt_candidate.get());
507517
seg->erase_candidate(i);
508-
seg->insert_candidate(i, std::move(alt_candidate));
518+
seg->insert_candidate(i, std::move(result.alternative_candidate));
509519
}
510520
}
511521
modified = true;
@@ -546,9 +556,9 @@ bool VariantsRewriter::GenerateAlternatives(
546556
return false;
547557
}
548558
if (original.value != original.content_value) {
549-
manager->ConvertConversionStringWithAlternative(
550-
original.content_value, primary_content_value,
551-
secondary_content_value);
559+
manager->ConvertConversionStringWithAlternative(original.content_value,
560+
primary_content_value,
561+
secondary_content_value);
552562
} else {
553563
*primary_content_value = *primary_value;
554564
*secondary_content_value = *secondary_value;
@@ -571,7 +581,7 @@ bool VariantsRewriter::GenerateAlternatives(
571581
inner_primary_value.assign(iter.GetValue().data(),
572582
iter.GetValue().size());
573583
inner_secondary_value.assign(iter.GetValue().data(),
574-
iter.GetValue().size());
584+
iter.GetValue().size());
575585
} else {
576586
at_least_one_modified = true;
577587
}
@@ -595,8 +605,7 @@ bool VariantsRewriter::GenerateAlternatives(
595605
secondary_inner_segment_boundary->push_back(
596606
Segment::Candidate::EncodeLengths(
597607
iter.GetKey().size(), inner_secondary_value.size(),
598-
iter.GetContentKey().size(),
599-
inner_secondary_content_value.size()));
608+
iter.GetContentKey().size(), inner_secondary_content_value.size()));
600609
}
601610
return at_least_one_modified;
602611
}

src/rewriter/variants_rewriter.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#define MOZC_REWRITER_VARIANTS_REWRITER_H_
3232

3333
#include <cstdint>
34+
#include <memory>
3435
#include <string>
3536
#include <utility>
3637
#include <vector>
@@ -158,6 +159,15 @@ class VariantsRewriter : public RewriterInterface {
158159
std::vector<uint32_t> *primary_inner_segment_boundary,
159160
std::vector<uint32_t> *secondary_inner_segment_boundary) const;
160161

162+
// Returns an alternative candidate and information for the base candidate.
163+
struct AlternativeCandidateResult {
164+
bool is_original_candidate_primary;
165+
int original_candidate_description_type;
166+
std::unique_ptr<Segment::Candidate> alternative_candidate = nullptr;
167+
};
168+
AlternativeCandidateResult CreateAlternativeCandidate(
169+
const Segment::Candidate &original_candidate) const;
170+
161171
const dictionary::PosMatcher pos_matcher_;
162172
};
163173

0 commit comments

Comments
 (0)