小幅重構重複的程式碼

This commit is contained in:
Mengjuei 2017-10-02 00:59:20 +08:00
parent 0aa20badfc
commit 07b68ff0ec
1 changed files with 35 additions and 34 deletions

View File

@ -142,6 +142,36 @@ public:
}
};
static const double kEpsilon = 0.000001;
static double FindHighestScore(const vector<NodeAnchor>& nodes, double epsilon) {
double highestScore = 0.0;
for (auto ni = nodes.begin(), ne = nodes.end(); ni != ne; ++ni) {
double score = ni->node->highestUnigramScore();
if (score > highestScore) {
highestScore = score;
}
}
return highestScore + epsilon;
}
static void OverrideCandidate(const vector<NodeAnchor>& nodes, const string& candidateValue, bool fixed, double floatingNodeOverrideScore) {
for (auto ni = nodes.begin(), ne = nodes.end(); ni != ne; ++ni) {
const vector<KeyValuePair>& candidates = (*ni).node->candidates();
for (size_t i = 0, c = candidates.size(); i < c; ++i) {
if (candidates[i].value == candidateValue) {
// found our node
if (fixed) {
const_cast<Node*>((*ni).node)->selectCandidateAtIndex(i);
} else {
const_cast<Node*>((*ni).node)->selectFloatingCandidateAtIndex(i, floatingNodeOverrideScore);
}
return;
}
}
}
}
@implementation McBopomofoInputMethodController
- (void)dealloc
{
@ -616,32 +646,14 @@ public:
[self popOverflowComposingTextAndWalk:client];
// get user override model suggestion
string overrideCandidate =
string overrideValue =
(_inputMode == kPlainBopomofoModeIdentifier) ? "" :
_uom->suggest(_walkedNodes, _builder->cursorIndex(), [[NSDate date] timeIntervalSince1970]);
if (!overrideCandidate.empty()) {
if (!overrideValue.empty()) {
size_t cursorIndex = [self actualCandidateCursorIndex];
vector<NodeAnchor> nodes = _builder->grid().nodesCrossingOrEndingAt(cursorIndex);
double highestScore = 0.0;
for (auto ni = nodes.begin(), ne = nodes.end(); ni != ne; ++ni) {
double score = ni->node->highestUnigramScore();
if (score > highestScore) {
highestScore = score;
}
}
highestScore += 0.00001;
for (vector<NodeAnchor>::iterator ni = nodes.begin(), ne = nodes.end(); ni != ne; ++ni) {
const vector<KeyValuePair>& candidates = (*ni).node->candidates();
for (size_t i = 0, c = candidates.size(); i < c; ++i) {
if (candidates[i].value == overrideCandidate) {
// found our node
const_cast<Node*>((*ni).node)->selectFloatingCandidateAtIndex(i, highestScore);
break;
}
}
}
double highestScore = FindHighestScore(nodes, kEpsilon);
OverrideCandidate(nodes, overrideValue, false, highestScore);
}
// then update the text
@ -1356,18 +1368,7 @@ public:
}
vector<NodeAnchor> nodes = _builder->grid().nodesCrossingOrEndingAt(cursorIndex);
for (vector<NodeAnchor>::iterator ni = nodes.begin(), ne = nodes.end(); ni != ne; ++ni) {
const vector<KeyValuePair>& candidates = (*ni).node->candidates();
for (size_t i = 0, c = candidates.size(); i < c; ++i) {
if (candidates[i].value == selectedValue) {
// found our node
const_cast<Node*>((*ni).node)->selectCandidateAtIndex(i);
break;
}
}
}
OverrideCandidate(nodes, selectedValue, true, 0.0);
[_candidates removeAllObjects];
[self walk];