@@ -85,15 +85,30 @@ constexpr size_t kSuggestionMaxResultsSize = 256;
85
85
constexpr size_t kPredictionMaxResultsSize = 100000 ;
86
86
87
87
// Returns true if the |target| may be redundant result.
88
- bool MaybeRedundant (const absl::string_view reference,
89
- const absl::string_view target) {
88
+ bool MaybeRedundant (const Result &reference_result,
89
+ const Result &target_result) {
90
+ const absl::string_view reference = reference_result.value ;
91
+ const absl::string_view target = target_result.value ;
92
+
93
+ // Same value means the result is redundant.
94
+ if (reference == target) {
95
+ return true ;
96
+ }
97
+
98
+ // If the key is the same, the target is not redundant as value is different.
99
+ if (reference_result.key == target_result.key ) {
100
+ return false ;
101
+ }
102
+
103
+ // target is not an appended value of the reference.
90
104
if (!target.starts_with (reference)) {
91
105
return false ;
92
106
}
107
+
108
+ // If the suffix is Emoji or unknown script, the result is not redundant.
109
+ // For example, if the reference is "東京", "東京🗼" is not redundant, but
110
+ // "東京タワー" is redundant.
93
111
const absl::string_view suffix = target.substr (reference.size ());
94
- if (suffix.empty ()) {
95
- return true ;
96
- }
97
112
const Util::ScriptType script_type = Util::GetScriptType (suffix);
98
113
return (script_type != Util::EMOJI && script_type != Util::UNKNOWN_SCRIPT);
99
114
}
@@ -949,17 +964,18 @@ void DictionaryPredictionAggregator::AggregateUnigramForMixedConversion(
949
964
950
965
// Traverse all remaining elements and check if each result is redundant.
951
966
for (Iter iter = min_iter; iter != max_iter;) {
952
- // - We do not filter user dictionary word.
953
- const bool should_check_redundant =
954
- !(iter->candidate_attributes & converter::Candidate::USER_DICTIONARY);
955
- if (should_check_redundant &&
956
- MaybeRedundant (reference_result.value , iter->value )) {
957
- // Swap out the redundant result.
967
+ // We do not filter user dictionary word.
968
+ if (iter->candidate_attributes & converter::Candidate::USER_DICTIONARY) {
969
+ ++iter;
970
+ continue ;
971
+ }
972
+ // If the result is redundant, swap it out.
973
+ if (MaybeRedundant (reference_result, *iter)) {
958
974
--max_iter;
959
975
std::iter_swap (iter, max_iter);
960
- } else {
961
- ++iter;
976
+ continue ;
962
977
}
978
+ ++iter;
963
979
}
964
980
}
965
981
0 commit comments