Skip to content

Commit

Permalink
fix(uniquifier): half of the duplicate candidates remain after dedup [C…
Browse files Browse the repository at this point in the history
…loses #114]
  • Loading branch information
lotem committed Jan 16, 2017
1 parent 9c4fa8a commit 2ab76bc
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions src/rime/gear/uniquifier.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,35 @@ bool UniquifiedTranslation::Next() {
return CacheTranslation::Next() && Uniquify();
}

bool UniquifiedTranslation::Uniquify() {
if (exhausted()) {
return false;
static CandidateList::iterator find_text_match(const an<Candidate>& target,
CandidateList::iterator begin,
CandidateList::iterator end) {
for (auto iter = begin; iter != end; ++iter) {
if ((*iter)->text() == target->text()) {
return iter;
}
}
auto next = Peek();
for (auto& c : *candidates_) {
if (c->text() == next->text()) {
auto u = As<UniquifiedCandidate>(c);
if (!u) {
u = New<UniquifiedCandidate>(c, "uniquified");
c = u;
}
u->Append(next);
return CacheTranslation::Next();
return end;
}

bool UniquifiedTranslation::Uniquify() {
while (!exhausted()) {
auto next = Peek();
CandidateList::iterator previous = find_text_match(
next, candidates_->begin(), candidates_->end());
if (previous == candidates_->end()) {
// Encountered a unique candidate.
return true;
}
auto uniquified = As<UniquifiedCandidate>(*previous);
if (!uniquified) {
*previous = uniquified =
New<UniquifiedCandidate>(*previous, "uniquified");
}
uniquified->Append(next);
CacheTranslation::Next();
}
return true;
return false;
}

// Uniquifier
Expand Down

0 comments on commit 2ab76bc

Please sign in to comment.