Skip to content

Commit

Permalink
refactor Translation::Compare() (#624)
Browse files Browse the repository at this point in the history
---------

Signed-off-by: shewer <[email protected]>
Co-authored-by: Qijia Liu <[email protected]>
  • Loading branch information
shewer and eagleoflqj authored Sep 26, 2023
1 parent f8528dd commit e849f28
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
17 changes: 17 additions & 0 deletions src/rime/candidate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,21 @@ vector<of<Candidate>> Candidate::GetGenuineCandidates(
return result;
}

int Candidate::compare(const Candidate& other) {
// the one nearer to the beginning of segment comes first
int k = start_ - other.start_;
if (k != 0)
return k;
// then the longer comes first
k = end_ - other.end_;
if (k != 0)
return -k;
// compare quality
double qdiff = quality_ - other.quality_;
if (qdiff != 0.)
return (qdiff > 0.) ? -1 : 1;
// draw
return 0;
}

} // namespace rime
1 change: 1 addition & 0 deletions src/rime/candidate.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Candidate {

static an<Candidate> GetGenuineCandidate(const an<Candidate>& cand);
static vector<of<Candidate>> GetGenuineCandidates(const an<Candidate>& cand);
int compare(const Candidate& other);

// recognized by translators in learning phase
const string& type() const { return type_; }
Expand Down
16 changes: 1 addition & 15 deletions src/rime/translation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,7 @@ int Translation::Compare(an<Translation> other,
auto theirs = other->Peek();
if (!ours || !theirs)
return 1;
int k = 0;
// the one nearer to the beginning of segment comes first
k = ours->start() - theirs->start();
if (k != 0)
return k;
// then the longer comes first
k = ours->end() - theirs->end();
if (k != 0)
return -k;
// compare quality
double qdiff = ours->quality() - theirs->quality();
if (qdiff != 0.)
return (qdiff > 0.) ? -1 : 1;
// draw
return 0;
return ours->compare(*theirs);
}

bool UniqueTranslation::Next() {
Expand Down

0 comments on commit e849f28

Please sign in to comment.