From 5920ab3ce88b98a33ecdddc04bda736c255391f7 Mon Sep 17 00:00:00 2001 From: ovadmin Date: Mon, 2 Oct 2017 00:59:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=8F=E5=B9=85=E9=87=8D=E6=A7=8B=E9=87=8D?= =?UTF-8?q?=E8=A4=87=E7=9A=84=E7=A8=8B=E5=BC=8F=E7=A2=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/InputMethodController.mm | 69 +++++++++++++++++---------------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/Source/InputMethodController.mm b/Source/InputMethodController.mm index 1084192e..7fadd3c6 100644 --- a/Source/InputMethodController.mm +++ b/Source/InputMethodController.mm @@ -142,6 +142,36 @@ public: } }; +static const double kEpsilon = 0.000001; + +static double FindHighestScore(const vector& 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& nodes, const string& candidateValue, bool fixed, double floatingNodeOverrideScore) { + for (auto ni = nodes.begin(), ne = nodes.end(); ni != ne; ++ni) { + const vector& 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((*ni).node)->selectCandidateAtIndex(i); + } else { + const_cast((*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 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::iterator ni = nodes.begin(), ne = nodes.end(); ni != ne; ++ni) { - const vector& 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((*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 nodes = _builder->grid().nodesCrossingOrEndingAt(cursorIndex); - for (vector::iterator ni = nodes.begin(), ne = nodes.end(); ni != ne; ++ni) { - const vector& 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((*ni).node)->selectCandidateAtIndex(i); - break; - } - } - } - + OverrideCandidate(nodes, selectedValue, true, 0.0); [_candidates removeAllObjects]; [self walk];