From f7425bb15be6514bb2daedbc62760ca5d52c08cf Mon Sep 17 00:00:00 2001
From: satok <satok@google.com>
Date: Wed, 5 Jan 2011 16:37:53 +0900
Subject: [PATCH] Supress overflow at mulitplying demotion rate

Change-Id: I2003c5f88a5062b11e2f21522095bb94b1eb4efd
---
 native/src/unigram_dictionary.cpp | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

diff --git a/native/src/unigram_dictionary.cpp b/native/src/unigram_dictionary.cpp
index 5e1212a5d7..af2cc97fce 100644
--- a/native/src/unigram_dictionary.cpp
+++ b/native/src/unigram_dictionary.cpp
@@ -269,6 +269,14 @@ void UnigramDictionary::getSuggestionCandidates(const int skipPos,
     }
 }
 
+inline static void multiplyRate(const int rate, int *freq) {
+    if (rate > 1000000) {
+        *freq = (*freq / 100) * rate;
+    } else {
+        *freq = *freq * rate / 100;
+    }
+}
+
 bool UnigramDictionary::getMissingSpaceWords(const int inputLength, const int missingSpacePos) {
     if (missingSpacePos <= 0 || missingSpacePos >= inputLength
             || inputLength >= MAX_WORD_LENGTH) return false;
@@ -294,7 +302,7 @@ bool UnigramDictionary::getMissingSpaceWords(const int inputLength, const int mi
 
     int pairFreq = ((firstFreq + secondFreq) / 2);
     for (int i = 0; i < inputLength; ++i) pairFreq *= TYPED_LETTER_MULTIPLIER;
-    pairFreq = pairFreq * WORDS_WITH_MISSING_SPACE_CHARACTER_DEMOTION_RATE / 100;
+    multiplyRate(WORDS_WITH_MISSING_SPACE_CHARACTER_DEMOTION_RATE, &pairFreq);
     addWord(word, newWordLength, pairFreq);
     return true;
 }
@@ -345,14 +353,13 @@ inline int UnigramDictionary::calculateFinalFreq(const int inputIndex, const int
         const bool sameLength) {
     // TODO: Demote by edit distance
     int finalFreq = freq * snr;
-    if (skipPos >= 0) finalFreq = finalFreq * WORDS_WITH_MISSING_CHARACTER_DEMOTION_RATE / 100;
-    if (transposedPos >= 0) finalFreq = finalFreq
-            * WORDS_WITH_TRANSPOSED_CHARACTERS_DEMOTION_RATE / 100;
+    if (skipPos >= 0) multiplyRate(WORDS_WITH_MISSING_CHARACTER_DEMOTION_RATE, &finalFreq);
+    if (transposedPos >= 0) multiplyRate(
+            WORDS_WITH_TRANSPOSED_CHARACTERS_DEMOTION_RATE, &finalFreq);
     if (excessivePos >= 0) {
-        finalFreq = finalFreq * WORDS_WITH_EXCESSIVE_CHARACTER_DEMOTION_RATE / 100;
+        multiplyRate(WORDS_WITH_EXCESSIVE_CHARACTER_DEMOTION_RATE, &finalFreq);
         if (!existsAdjacentProximityChars(inputIndex, mInputLength)) {
-            finalFreq = finalFreq
-                    * WORDS_WITH_EXCESSIVE_CHARACTER_OUT_OF_PROXIMITY_DEMOTION_RATE / 100;
+            multiplyRate(WORDS_WITH_EXCESSIVE_CHARACTER_OUT_OF_PROXIMITY_DEMOTION_RATE, &finalFreq);
         }
     }
     if (sameLength && skipPos < 0) finalFreq *= FULL_WORD_MULTIPLIER;
-- 
GitLab