diff options
-rw-r--r-- | java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java | 11 | ||||
-rw-r--r-- | native/jni/src/correction.cpp | 2 | ||||
-rw-r--r-- | native/jni/src/correction.h | 154 | ||||
-rw-r--r-- | native/jni/src/proximity_info.h | 24 | ||||
-rw-r--r-- | native/jni/src/proximity_info_state.h | 7 | ||||
-rw-r--r-- | tests/src/com/android/inputmethod/latin/FusionDictionaryTests.java | 42 |
6 files changed, 144 insertions, 96 deletions
diff --git a/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java b/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java index 5864db28e..260155174 100644 --- a/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java +++ b/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java @@ -516,13 +516,22 @@ public class FusionDictionary implements Iterable<Word> { int indexOfGroup = findIndexOfChar(node, s.codePointAt(index)); if (CHARACTER_NOT_FOUND == indexOfGroup) return null; currentGroup = node.mData.get(indexOfGroup); + + if (s.length() - index < currentGroup.mChars.length) return null; + int newIndex = index; + while (newIndex < s.length() && newIndex - index < currentGroup.mChars.length) { + if (currentGroup.mChars[newIndex - index] != s.codePointAt(newIndex)) return null; + newIndex++; + } + index = newIndex; + if (DBG) checker.append(new String(currentGroup.mChars, 0, currentGroup.mChars.length)); - index += currentGroup.mChars.length; if (index < s.length()) { node = currentGroup.mChildren; } } while (null != node && index < s.length()); + if (index < s.length()) return null; if (DBG && !s.equals(checker.toString())) return null; return currentGroup; } diff --git a/native/jni/src/correction.cpp b/native/jni/src/correction.cpp index e55da0113..7513b307d 100644 --- a/native/jni/src/correction.cpp +++ b/native/jni/src/correction.cpp @@ -230,7 +230,7 @@ int Correction::goDownTree( } // TODO: remove -int Correction::getInputIndex() { +int Correction::getInputIndex() const { return mInputIndex; } diff --git a/native/jni/src/correction.h b/native/jni/src/correction.h index 57e7b7189..9899f6e66 100644 --- a/native/jni/src/correction.h +++ b/native/jni/src/correction.h @@ -38,63 +38,8 @@ class Correction { NOT_ON_TERMINAL } CorrectionType; - ///////////////////////// - // static inline utils // - ///////////////////////// - - static const int TWO_31ST_DIV_255 = S_INT_MAX / 255; - static inline int capped255MultForFullMatchAccentsOrCapitalizationDifference(const int num) { - return (num < TWO_31ST_DIV_255 ? 255 * num : S_INT_MAX); - } - - static const int TWO_31ST_DIV_2 = S_INT_MAX / 2; - inline static void multiplyIntCapped(const int multiplier, int *base) { - const int temp = *base; - if (temp != S_INT_MAX) { - // Branch if multiplier == 2 for the optimization - if (multiplier < 0) { - if (DEBUG_DICT) { - assert(false); - } - AKLOGI("--- Invalid multiplier: %d", multiplier); - } else if (multiplier == 0) { - *base = 0; - } else if (multiplier == 2) { - *base = TWO_31ST_DIV_2 >= temp ? temp << 1 : S_INT_MAX; - } else { - // TODO: This overflow check gives a wrong answer when, for example, - // temp = 2^16 + 1 and multiplier = 2^17 + 1. - // Fix this behavior. - const int tempRetval = temp * multiplier; - *base = tempRetval >= temp ? tempRetval : S_INT_MAX; - } - } - } - - inline static int powerIntCapped(const int base, const int n) { - if (n <= 0) return 1; - if (base == 2) { - return n < 31 ? 1 << n : S_INT_MAX; - } else { - int ret = base; - for (int i = 1; i < n; ++i) multiplyIntCapped(base, &ret); - return ret; - } - } - - inline static void multiplyRate(const int rate, int *freq) { - if (*freq != S_INT_MAX) { - if (*freq > 1000000) { - *freq /= 100; - multiplyIntCapped(rate, freq); - } else { - multiplyIntCapped(rate, freq); - *freq /= 100; - } - } - } - Correction() {}; + virtual ~Correction(); void resetCorrection(); void initCorrection( const ProximityInfo *pi, const int inputLength, const int maxWordLength); @@ -108,27 +53,7 @@ class Correction { bool sameAsTyped(); bool initProcessState(const int index); - int getInputIndex(); - - virtual ~Correction(); - int getSpaceProximityPos() const { - return mSpaceProximityPos; - } - int getMissingSpacePos() const { - return mMissingSpacePos; - } - - int getSkipPos() const { - return mSkipPos; - } - - int getExcessivePos() const { - return mExcessivePos; - } - - int getTransposedPos() const { - return mTransposedPos; - } + int getInputIndex() const; bool needsToPrune() const; @@ -195,6 +120,81 @@ class Correction { private: DISALLOW_COPY_AND_ASSIGN(Correction); + + ///////////////////////// + // static inline utils // + ///////////////////////// + static const int TWO_31ST_DIV_255 = S_INT_MAX / 255; + static inline int capped255MultForFullMatchAccentsOrCapitalizationDifference(const int num) { + return (num < TWO_31ST_DIV_255 ? 255 * num : S_INT_MAX); + } + + static const int TWO_31ST_DIV_2 = S_INT_MAX / 2; + inline static void multiplyIntCapped(const int multiplier, int *base) { + const int temp = *base; + if (temp != S_INT_MAX) { + // Branch if multiplier == 2 for the optimization + if (multiplier < 0) { + if (DEBUG_DICT) { + assert(false); + } + AKLOGI("--- Invalid multiplier: %d", multiplier); + } else if (multiplier == 0) { + *base = 0; + } else if (multiplier == 2) { + *base = TWO_31ST_DIV_2 >= temp ? temp << 1 : S_INT_MAX; + } else { + // TODO: This overflow check gives a wrong answer when, for example, + // temp = 2^16 + 1 and multiplier = 2^17 + 1. + // Fix this behavior. + const int tempRetval = temp * multiplier; + *base = tempRetval >= temp ? tempRetval : S_INT_MAX; + } + } + } + + inline static int powerIntCapped(const int base, const int n) { + if (n <= 0) return 1; + if (base == 2) { + return n < 31 ? 1 << n : S_INT_MAX; + } else { + int ret = base; + for (int i = 1; i < n; ++i) multiplyIntCapped(base, &ret); + return ret; + } + } + + inline static void multiplyRate(const int rate, int *freq) { + if (*freq != S_INT_MAX) { + if (*freq > 1000000) { + *freq /= 100; + multiplyIntCapped(rate, freq); + } else { + multiplyIntCapped(rate, freq); + *freq /= 100; + } + } + } + + inline int getSpaceProximityPos() const { + return mSpaceProximityPos; + } + inline int getMissingSpacePos() const { + return mMissingSpacePos; + } + + inline int getSkipPos() const { + return mSkipPos; + } + + inline int getExcessivePos() const { + return mExcessivePos; + } + + inline int getTransposedPos() const { + return mTransposedPos; + } + inline void incrementInputIndex(); inline void incrementOutputIndex(); inline void startToTraverseAllNodes(); diff --git a/native/jni/src/proximity_info.h b/native/jni/src/proximity_info.h index 8a407e71a..7ee633ba8 100644 --- a/native/jni/src/proximity_info.h +++ b/native/jni/src/proximity_info.h @@ -41,21 +41,12 @@ class ProximityInfo { float getNormalizedSquaredDistanceFromCenterFloat( const int keyId, const int x, const int y) const; bool sameAsTyped(const unsigned short *word, int length) const; - int squaredDistanceToEdge(const int keyId, const int x, const int y) const; - bool isOnKey(const int keyId, const int x, const int y) const { - if (keyId < 0) return true; // NOT_A_ID is -1, but return whenever < 0 just in case - const int left = mKeyXCoordinates[keyId]; - const int top = mKeyYCoordinates[keyId]; - const int right = left + mKeyWidths[keyId] + 1; - const int bottom = top + mKeyHeights[keyId]; - return left < right && top < bottom && x >= left && x < right && y >= top && y < bottom; - } int getKeyIndex(const int c) const; int getKeyCode(const int keyIndex) const; bool hasSweetSpotData(const int keyIndex) const { // When there are no calibration data for a key, // the radius of the key is assigned to zero. - return mSweetSpotRadii[keyIndex] > 0.0; + return mSweetSpotRadii[keyIndex] > 0.0f; } float getSweetSpotRadiiAt(int keyIndex) const { return mSweetSpotRadii[keyIndex]; @@ -111,10 +102,6 @@ class ProximityInfo { float getKeyCenterYOfIdG(int keyId) const; int getKeyKeyDistanceG(int key0, int key1) const; - // Returns the keyboard key-center information. - void getCenters(int *centersX, int *centersY, int *codeToKeyIndex, int *keyToCodeIndex, - int *keyCount, int *keyWidth) const; - private: DISALLOW_IMPLICIT_CONSTRUCTORS(ProximityInfo); // The max number of the keys in one keyboard layout @@ -131,6 +118,15 @@ class ProximityInfo { float calculateSquaredDistanceFromSweetSpotCenter( const int keyIndex, const int inputIndex) const; bool hasInputCoordinates() const; + int squaredDistanceToEdge(const int keyId, const int x, const int y) const; + bool isOnKey(const int keyId, const int x, const int y) const { + if (keyId < 0) return true; // NOT_A_ID is -1, but return whenever < 0 just in case + const int left = mKeyXCoordinates[keyId]; + const int top = mKeyYCoordinates[keyId]; + const int right = left + mKeyWidths[keyId] + 1; + const int bottom = top + mKeyHeights[keyId]; + return left < right && top < bottom && x >= left && x < right && y >= top && y < bottom; + } const int MAX_PROXIMITY_CHARS_SIZE; const int KEYBOARD_WIDTH; diff --git a/native/jni/src/proximity_info_state.h b/native/jni/src/proximity_info_state.h index 474c40757..2fb0ae044 100644 --- a/native/jni/src/proximity_info_state.h +++ b/native/jni/src/proximity_info_state.h @@ -48,9 +48,6 @@ class ProximityInfoState { // Defined here // ///////////////////////////////////////// ProximityInfoState() {}; - inline const int *getProximityCharsAt(const int index) const { - return mInputCodes + (index * MAX_PROXIMITY_CHARS_SIZE_INTERNAL); - } inline unsigned short getPrimaryCharAt(const int index) const { return getProximityCharsAt(index)[0]; @@ -194,6 +191,10 @@ class ProximityInfoState { return mInputXCoordinates && mInputYCoordinates; } + inline const int *getProximityCharsAt(const int index) const { + return mInputCodes + (index * MAX_PROXIMITY_CHARS_SIZE_INTERNAL); + } + // const const ProximityInfo *mProximityInfo; bool mHasTouchPositionCorrectionData; diff --git a/tests/src/com/android/inputmethod/latin/FusionDictionaryTests.java b/tests/src/com/android/inputmethod/latin/FusionDictionaryTests.java new file mode 100644 index 000000000..e3f75839e --- /dev/null +++ b/tests/src/com/android/inputmethod/latin/FusionDictionaryTests.java @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.android.inputmethod.latin; + +import android.test.AndroidTestCase; + +import com.android.inputmethod.latin.makedict.FusionDictionary; +import com.android.inputmethod.latin.makedict.FusionDictionary.Node; + +import java.util.HashMap; + +/** + * Unit test for FusionDictionary + */ +public class FusionDictionaryTests extends AndroidTestCase { + public void testFindWordInTree() { + FusionDictionary dict = new FusionDictionary(new Node(), + new FusionDictionary.DictionaryOptions(new HashMap<String,String>(), false, false)); + + dict.add("abc", 10, null); + assertNull(FusionDictionary.findWordInTree(dict.mRoot, "aaa")); + assertNotNull(FusionDictionary.findWordInTree(dict.mRoot, "abc")); + + dict.add("aa", 10, null); + assertNull(FusionDictionary.findWordInTree(dict.mRoot, "aaa")); + assertNotNull(FusionDictionary.findWordInTree(dict.mRoot, "aa")); + } +} |