From a829188f54c0fd2e26192d98d9e56e033d8f91aa Mon Sep 17 00:00:00 2001
From: Keisuke Kuroynagi <ksk@google.com>
Date: Thu, 4 Jul 2013 17:39:01 +0900
Subject: [PATCH] Allow force auto commit when the top is long multiword.

Bug: 9688196
Change-Id: I73ff2cd77b932bf7307b3541ea9da3fa4afa11b4
---
 .../jni/src/suggest/core/policy/traversal.h   |  2 +-
 native/jni/src/suggest/core/suggest.cpp       | 35 +++++++++++--------
 .../policyimpl/typing/typing_traversal.h      |  2 +-
 3 files changed, 22 insertions(+), 17 deletions(-)

diff --git a/native/jni/src/suggest/core/policy/traversal.h b/native/jni/src/suggest/core/policy/traversal.h
index c6f66f231e..f26d7149e1 100644
--- a/native/jni/src/suggest/core/policy/traversal.h
+++ b/native/jni/src/suggest/core/policy/traversal.h
@@ -45,7 +45,7 @@ class Traversal {
             const DicNode *const dicNode) const = 0;
     virtual bool needsToTraverseAllUserInput() const = 0;
     virtual float getMaxSpatialDistance() const = 0;
-    virtual bool allowPartialCommit() const = 0;
+    virtual bool autoCorrectsToMultiWordSuggestionIfTop() const = 0;
     virtual int getDefaultExpandDicNodeSize() const = 0;
     virtual int getMaxCacheSize() const = 0;
     virtual bool isPossibleOmissionChildNode(const DicTraverseSession *const traverseSession,
diff --git a/native/jni/src/suggest/core/suggest.cpp b/native/jni/src/suggest/core/suggest.cpp
index 3deeb3c581..9a0f10cd51 100644
--- a/native/jni/src/suggest/core/suggest.cpp
+++ b/native/jni/src/suggest/core/suggest.cpp
@@ -85,9 +85,9 @@ void Suggest::initializeSearch(DicTraverseSession *traverseSession, int commitPo
     if (!traverseSession->getProximityInfoState(0)->isUsed()) {
         return;
     }
-    if (TRAVERSAL->allowPartialCommit()) {
-        commitPoint = 0;
-    }
+
+    // Never auto partial commit for now.
+    commitPoint = 0;
 
     if (traverseSession->getInputSize() > MIN_CONTINUOUS_SUGGESTION_INPUT_SIZE
             && traverseSession->isContinuousSuggestionPossible()) {
@@ -151,6 +151,17 @@ int Suggest::outputSuggestions(DicTraverseSession *traverseSession, int *frequen
     int maxScore = S_INT_MIN;
     int bestExactMatchedNodeTerminalIndex = -1;
     int bestExactMatchedNodeOutputWordIndex = -1;
+    // Force autocorrection for obvious long multi-word suggestions when the top suggestion is
+    // a long multiple words suggestion.
+    // TODO: Implement a smarter auto-commit method for handling multi-word suggestions.
+    // traverseSession->isPartiallyCommited() always returns false because we never auto partial
+    // commit for now.
+    const bool forceCommitMultiWords = (terminalSize > 0) ?
+            TRAVERSAL->autoCorrectsToMultiWordSuggestionIfTop()
+                    && (traverseSession->isPartiallyCommited()
+                            || (traverseSession->getInputSize()
+                                    >= MIN_LEN_FOR_MULTI_WORD_AUTOCORRECT
+                                            && terminals[0].hasMultipleWords())) : false;
     // Output suggestion results here
     for (int terminalIndex = 0; terminalIndex < terminalSize && outputWordIndex < MAX_RESULTS;
             ++terminalIndex) {
@@ -180,22 +191,16 @@ int Suggest::outputSuggestions(DicTraverseSession *traverseSession, int *frequen
 
         // Increase output score of top typing suggestion to ensure autocorrection.
         // TODO: Better integration with java side autocorrection logic.
-        // Force autocorrection for obvious long multi-word suggestions.
-        const bool isForceCommitMultiWords = TRAVERSAL->allowPartialCommit()
-                && (traverseSession->isPartiallyCommited()
-                        || (traverseSession->getInputSize() >= MIN_LEN_FOR_MULTI_WORD_AUTOCORRECT
-                                && terminalDicNode->hasMultipleWords()));
-
         const int finalScore = SCORING->calculateFinalScore(
                 compoundDistance, traverseSession->getInputSize(),
-                isForceCommitMultiWords || (isValidWord && SCORING->doesAutoCorrectValidWord()));
+                (forceCommitMultiWords && terminalDicNode->hasMultipleWords())
+                        || (isValidWord && SCORING->doesAutoCorrectValidWord()));
         maxScore = max(maxScore, finalScore);
 
-        if (TRAVERSAL->allowPartialCommit()) {
-            // Index for top typing suggestion should be 0.
-            if (isValidWord && outputWordIndex == 0) {
-                terminalDicNode->outputSpacePositionsResult(spaceIndices);
-            }
+        // TODO: Implement a smarter auto-commit method for handling multi-word suggestions.
+        // Index for top typing suggestion should be 0.
+        if (isValidWord && outputWordIndex == 0) {
+            terminalDicNode->outputSpacePositionsResult(spaceIndices);
         }
 
         // Don't output invalid words. However, we still need to submit their shortcuts if any.
diff --git a/native/jni/src/suggest/policyimpl/typing/typing_traversal.h b/native/jni/src/suggest/policyimpl/typing/typing_traversal.h
index 76122ebe75..ef144e00a1 100644
--- a/native/jni/src/suggest/policyimpl/typing/typing_traversal.h
+++ b/native/jni/src/suggest/policyimpl/typing/typing_traversal.h
@@ -137,7 +137,7 @@ class TypingTraversal : public Traversal {
         return ScoringParams::MAX_SPATIAL_DISTANCE;
     }
 
-    AK_FORCE_INLINE bool allowPartialCommit() const {
+    AK_FORCE_INLINE bool autoCorrectsToMultiWordSuggestionIfTop() const {
         return true;
     }
 
-- 
GitLab