From bb57090f1da9d1fc5a0eda9b627d3f8c8b25ab42 Mon Sep 17 00:00:00 2001
From: Jean Chalard <jchalard@google.com>
Date: Mon, 30 Sep 2013 21:39:43 +0900
Subject: [PATCH] Create the wiring for auto-commit confidence computation.

Bug: 9059617
Change-Id: I7e4e05ffa8c68cfebbe362a977fd176555cad12a
---
 .../inputmethod/latin/BinaryDictionary.java       |  3 ++-
 ...android_inputmethod_latin_BinaryDictionary.cpp | 12 ++++++++++--
 native/jni/src/defines.h                          |  3 +++
 .../src/suggest/core/dictionary/dictionary.cpp    |  7 ++++---
 .../jni/src/suggest/core/dictionary/dictionary.h  |  2 +-
 native/jni/src/suggest/core/suggest.cpp           | 15 ++++++++++++---
 native/jni/src/suggest/core/suggest.h             |  7 +++++--
 native/jni/src/suggest/core/suggest_interface.h   |  2 +-
 8 files changed, 38 insertions(+), 13 deletions(-)

diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionary.java b/java/src/com/android/inputmethod/latin/BinaryDictionary.java
index 29c6c0451d..b7b08cd974 100644
--- a/java/src/com/android/inputmethod/latin/BinaryDictionary.java
+++ b/java/src/com/android/inputmethod/latin/BinaryDictionary.java
@@ -62,7 +62,8 @@ public final class BinaryDictionary extends Dictionary {
     private final int[] mSpaceIndices = new int[MAX_RESULTS];
     private final int[] mOutputScores = new int[MAX_RESULTS];
     private final int[] mOutputTypes = new int[MAX_RESULTS];
-    private final int[] mOutputAutoCommitFirstWordConfidence = new int[MAX_RESULTS];
+    // Only one result is ever used
+    private final int[] mOutputAutoCommitFirstWordConfidence = new int[1];
 
     private final NativeSuggestOptions mNativeSuggestOptions = new NativeSuggestOptions();
 
diff --git a/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp b/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp
index c5ef264fc4..21c15ea67f 100644
--- a/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp
+++ b/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp
@@ -142,7 +142,7 @@ static int latinime_BinaryDictionary_getSuggestions(JNIEnv *env, jclass clazz, j
         jintArray inputCodePointsArray, jint inputSize, jint commitPoint, jintArray suggestOptions,
         jintArray prevWordCodePointsForBigrams, jintArray outputCodePointsArray,
         jintArray scoresArray, jintArray spaceIndicesArray, jintArray outputTypesArray,
-        jintArray outputAutoCommitFirstWordConfidence) {
+        jintArray outputAutoCommitFirstWordConfidenceArray) {
     Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
     if (!dictionary) return 0;
     ProximityInfo *pInfo = reinterpret_cast<ProximityInfo *>(proximityInfo);
@@ -196,17 +196,23 @@ static int latinime_BinaryDictionary_getSuggestions(JNIEnv *env, jclass clazz, j
     int spaceIndices[spaceIndicesLength];
     const jsize outputTypesLength = env->GetArrayLength(outputTypesArray);
     int outputTypes[outputTypesLength];
+    const jsize outputAutoCommitFirstWordConfidenceLength =
+            env->GetArrayLength(outputAutoCommitFirstWordConfidenceArray);
+    // We only use the first result, as obviously we will only ever autocommit the first one
+    ASSERT(outputAutoCommitFirstWordConfidenceLength == 1);
+    int outputAutoCommitFirstWordConfidence[outputAutoCommitFirstWordConfidenceLength];
     memset(outputCodePoints, 0, sizeof(outputCodePoints));
     memset(scores, 0, sizeof(scores));
     memset(spaceIndices, 0, sizeof(spaceIndices));
     memset(outputTypes, 0, sizeof(outputTypes));
+    memset(outputAutoCommitFirstWordConfidence, 0, sizeof(outputAutoCommitFirstWordConfidence));
 
     int count;
     if (givenSuggestOptions.isGesture() || inputSize > 0) {
         count = dictionary->getSuggestions(pInfo, traverseSession, xCoordinates, yCoordinates,
                 times, pointerIds, inputCodePoints, inputSize, prevWordCodePoints,
                 prevWordCodePointsLength, commitPoint, &givenSuggestOptions, outputCodePoints,
-                scores, spaceIndices, outputTypes);
+                scores, spaceIndices, outputTypes, outputAutoCommitFirstWordConfidence);
     } else {
         count = dictionary->getBigrams(prevWordCodePoints, prevWordCodePointsLength,
                 outputCodePoints, scores, outputTypes);
@@ -217,6 +223,8 @@ static int latinime_BinaryDictionary_getSuggestions(JNIEnv *env, jclass clazz, j
     env->SetIntArrayRegion(scoresArray, 0, scoresLength, scores);
     env->SetIntArrayRegion(spaceIndicesArray, 0, spaceIndicesLength, spaceIndices);
     env->SetIntArrayRegion(outputTypesArray, 0, outputTypesLength, outputTypes);
+    env->SetIntArrayRegion(outputAutoCommitFirstWordConfidenceArray, 0,
+            outputAutoCommitFirstWordConfidenceLength, outputAutoCommitFirstWordConfidence);
 
     return count;
 }
diff --git a/native/jni/src/defines.h b/native/jni/src/defines.h
index c2aa8ba0e9..c920f64b45 100644
--- a/native/jni/src/defines.h
+++ b/native/jni/src/defines.h
@@ -298,6 +298,9 @@ static inline void prof_out(void) {
 #define NOT_AN_INDEX (-1)
 #define NOT_A_PROBABILITY (-1)
 #define NOT_A_DICT_POS (S_INT_MIN)
+// A special value to mean the first word confidence makes no sense in this case,
+// e.g. this is not a multi-word suggestion.
+#define NOT_A_FIRST_WORD_CONFIDENCE (S_INT_MIN)
 
 #define KEYCODE_SPACE ' '
 #define KEYCODE_SINGLE_QUOTE '\''
diff --git a/native/jni/src/suggest/core/dictionary/dictionary.cpp b/native/jni/src/suggest/core/dictionary/dictionary.cpp
index b1d01ed86e..5969b31cc8 100644
--- a/native/jni/src/suggest/core/dictionary/dictionary.cpp
+++ b/native/jni/src/suggest/core/dictionary/dictionary.cpp
@@ -55,14 +55,14 @@ int Dictionary::getSuggestions(ProximityInfo *proximityInfo, DicTraverseSession
         int *xcoordinates, int *ycoordinates, int *times, int *pointerIds, int *inputCodePoints,
         int inputSize, int *prevWordCodePoints, int prevWordLength, int commitPoint,
         const SuggestOptions *const suggestOptions, int *outWords, int *frequencies,
-        int *spaceIndices, int *outputTypes) const {
+        int *spaceIndices, int *outputTypes, int *outputAutoCommitFirstWordConfidence) const {
     int result = 0;
     if (suggestOptions->isGesture()) {
         DicTraverseSession::initSessionInstance(
                 traverseSession, this, prevWordCodePoints, prevWordLength, suggestOptions);
         result = mGestureSuggest->getSuggestions(proximityInfo, traverseSession, xcoordinates,
                 ycoordinates, times, pointerIds, inputCodePoints, inputSize, commitPoint, outWords,
-                frequencies, spaceIndices, outputTypes);
+                frequencies, spaceIndices, outputTypes, outputAutoCommitFirstWordConfidence);
         if (DEBUG_DICT) {
             DUMP_RESULT(outWords, frequencies);
         }
@@ -72,7 +72,8 @@ int Dictionary::getSuggestions(ProximityInfo *proximityInfo, DicTraverseSession
                 traverseSession, this, prevWordCodePoints, prevWordLength, suggestOptions);
         result = mTypingSuggest->getSuggestions(proximityInfo, traverseSession, xcoordinates,
                 ycoordinates, times, pointerIds, inputCodePoints, inputSize, commitPoint,
-                outWords, frequencies, spaceIndices, outputTypes);
+                outWords, frequencies, spaceIndices, outputTypes,
+                outputAutoCommitFirstWordConfidence);
         if (DEBUG_DICT) {
             DUMP_RESULT(outWords, frequencies);
         }
diff --git a/native/jni/src/suggest/core/dictionary/dictionary.h b/native/jni/src/suggest/core/dictionary/dictionary.h
index d8a0f3e580..43d3b964d8 100644
--- a/native/jni/src/suggest/core/dictionary/dictionary.h
+++ b/native/jni/src/suggest/core/dictionary/dictionary.h
@@ -60,7 +60,7 @@ class Dictionary {
             int *xcoordinates, int *ycoordinates, int *times, int *pointerIds, int *inputCodePoints,
             int inputSize, int *prevWordCodePoints, int prevWordLength, int commitPoint,
             const SuggestOptions *const suggestOptions, int *outWords, int *frequencies,
-            int *spaceIndices, int *outputTypes) const;
+            int *spaceIndices, int *outputTypes, int *outputAutoCommitFirstWordConfidence) const;
 
     int getBigrams(const int *word, int length, int *outWords, int *frequencies,
             int *outputTypes) const;
diff --git a/native/jni/src/suggest/core/suggest.cpp b/native/jni/src/suggest/core/suggest.cpp
index e20bc497a5..51cfba17a7 100644
--- a/native/jni/src/suggest/core/suggest.cpp
+++ b/native/jni/src/suggest/core/suggest.cpp
@@ -49,7 +49,7 @@ const float Suggest::AUTOCORRECT_CLASSIFICATION_THRESHOLD = 0.33f;
 int Suggest::getSuggestions(ProximityInfo *pInfo, void *traverseSession,
         int *inputXs, int *inputYs, int *times, int *pointerIds, int *inputCodePoints,
         int inputSize, int commitPoint, int *outWords, int *frequencies, int *outputIndices,
-        int *outputTypes) const {
+        int *outputTypes, int *outputAutoCommitFirstWordConfidence) const {
     PROF_OPEN;
     PROF_START(0);
     const float maxSpatialDistance = TRAVERSAL->getMaxSpatialDistance();
@@ -70,7 +70,8 @@ int Suggest::getSuggestions(ProximityInfo *pInfo, void *traverseSession,
     }
     PROF_END(1);
     PROF_START(2);
-    const int size = outputSuggestions(tSession, frequencies, outWords, outputIndices, outputTypes);
+    const int size = outputSuggestions(tSession, frequencies, outWords, outputIndices, outputTypes,
+            outputAutoCommitFirstWordConfidence);
     PROF_END(2);
     PROF_CLOSE;
     return size;
@@ -117,7 +118,8 @@ void Suggest::initializeSearch(DicTraverseSession *traverseSession, int commitPo
  * Outputs the final list of suggestions (i.e., terminal nodes).
  */
 int Suggest::outputSuggestions(DicTraverseSession *traverseSession, int *frequencies,
-        int *outputCodePoints, int *outputIndicesToPartialCommit, int *outputTypes) const {
+        int *outputCodePoints, int *outputIndicesToPartialCommit, int *outputTypes,
+        int *outputAutoCommitFirstWordConfidence) const {
 #if DEBUG_EVALUATE_MOST_PROBABLE_STRING
     const int terminalSize = 0;
 #else
@@ -164,6 +166,8 @@ int Suggest::outputSuggestions(DicTraverseSession *traverseSession, int *frequen
     // TODO: have partial commit work even with multiple pointers.
     const bool outputSecondWordFirstLetterInputIndex =
             traverseSession->isOnlyOnePointerUsed(0 /* pointerId */);
+    outputAutoCommitFirstWordConfidence[0] = computeFirstWordConfidence();
+
     // Output suggestion results here
     for (int terminalIndex = 0; terminalIndex < terminalSize && outputWordIndex < MAX_RESULTS;
             ++terminalIndex) {
@@ -251,6 +255,11 @@ int Suggest::outputSuggestions(DicTraverseSession *traverseSession, int *frequen
     return outputWordIndex;
 }
 
+int Suggest::computeFirstWordConfidence() const {
+    // TODO: implement this.
+    return NOT_A_FIRST_WORD_CONFIDENCE;
+}
+
 /**
  * Expands the dicNodes in the current search priority queue by advancing to the possible child
  * nodes based on the next touch point(s) (or no touch points for lookahead)
diff --git a/native/jni/src/suggest/core/suggest.h b/native/jni/src/suggest/core/suggest.h
index b24019632f..0e8bd11958 100644
--- a/native/jni/src/suggest/core/suggest.h
+++ b/native/jni/src/suggest/core/suggest.h
@@ -48,14 +48,17 @@ class Suggest : public SuggestInterface {
     AK_FORCE_INLINE virtual ~Suggest() {}
     int getSuggestions(ProximityInfo *pInfo, void *traverseSession, int *inputXs, int *inputYs,
             int *times, int *pointerIds, int *inputCodePoints, int inputSize, int commitPoint,
-            int *outWords, int *frequencies, int *outputIndices, int *outputTypes) const;
+            int *outWords, int *frequencies, int *outputIndices, int *outputTypes,
+            int *outputAutoCommitFirstWordConfidence) const;
 
  private:
     DISALLOW_IMPLICIT_CONSTRUCTORS(Suggest);
     void createNextWordDicNode(DicTraverseSession *traverseSession, DicNode *dicNode,
             const bool spaceSubstitution) const;
     int outputSuggestions(DicTraverseSession *traverseSession, int *frequencies,
-            int *outputCodePoints, int *outputIndicesToPartialCommit, int *outputTypes) const;
+            int *outputCodePoints, int *outputIndicesToPartialCommit, int *outputTypes,
+            int *outputAutoCommitFirstWordConfidence) const;
+    int computeFirstWordConfidence() const;
     void initializeSearch(DicTraverseSession *traverseSession, int commitPoint) const;
     void expandCurrentDicNodes(DicTraverseSession *traverseSession) const;
     void processTerminalDicNode(DicTraverseSession *traverseSession, DicNode *dicNode) const;
diff --git a/native/jni/src/suggest/core/suggest_interface.h b/native/jni/src/suggest/core/suggest_interface.h
index 0bb85d7e57..4deb4d9248 100644
--- a/native/jni/src/suggest/core/suggest_interface.h
+++ b/native/jni/src/suggest/core/suggest_interface.h
@@ -28,7 +28,7 @@ class SuggestInterface {
     virtual int getSuggestions(ProximityInfo *pInfo, void *traverseSession, int *inputXs,
             int *inputYs, int *times, int *pointerIds, int *inputCodePoints, int inputSize,
             int commitPoint, int *outWords, int *frequencies, int *outputIndices,
-            int *outputTypes) const = 0;
+            int *outputTypes, int *outputAutoCommitFirstWordConfidence) const = 0;
     SuggestInterface() {}
     virtual ~SuggestInterface() {}
  private:
-- 
GitLab