2011-07-15 04:49:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 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.
|
|
|
|
*/
|
|
|
|
|
2011-08-10 05:30:10 +00:00
|
|
|
#define LOG_TAG "LatinIME: correction.cpp"
|
2011-07-15 04:49:00 +00:00
|
|
|
|
2013-01-15 07:15:48 +00:00
|
|
|
#include <cmath>
|
|
|
|
|
2011-11-11 05:26:13 +00:00
|
|
|
#include "char_utils.h"
|
2011-08-10 05:30:10 +00:00
|
|
|
#include "correction.h"
|
2012-02-03 12:24:53 +00:00
|
|
|
#include "defines.h"
|
2012-06-05 08:55:52 +00:00
|
|
|
#include "proximity_info_state.h"
|
2013-01-15 10:16:38 +00:00
|
|
|
#include "suggest_utils.h"
|
2013-05-07 10:47:20 +00:00
|
|
|
#include "suggest/policyimpl/utils/edit_distance.h"
|
|
|
|
#include "suggest/policyimpl/utils/damerau_levenshtein_edit_distance_policy.h"
|
2011-07-15 04:49:00 +00:00
|
|
|
|
|
|
|
namespace latinime {
|
|
|
|
|
2012-08-01 09:03:55 +00:00
|
|
|
class ProximityInfo;
|
|
|
|
|
2011-10-13 06:26:45 +00:00
|
|
|
/////////////////////////////
|
|
|
|
// edit distance funcitons //
|
|
|
|
/////////////////////////////
|
|
|
|
|
|
|
|
inline static void initEditDistance(int *editDistanceTable) {
|
2013-01-11 16:18:00 +00:00
|
|
|
for (int i = 0; i <= MAX_WORD_LENGTH; ++i) {
|
2011-10-13 06:26:45 +00:00
|
|
|
editDistanceTable[i] = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-17 06:58:23 +00:00
|
|
|
inline static void dumpEditDistance10ForDebug(int *editDistanceTable,
|
|
|
|
const int editDistanceTableWidth, const int outputLength) {
|
2011-12-16 14:15:06 +00:00
|
|
|
if (DEBUG_DICT) {
|
2012-01-13 09:01:22 +00:00
|
|
|
AKLOGI("EditDistanceTable");
|
2011-12-16 14:15:06 +00:00
|
|
|
for (int i = 0; i <= 10; ++i) {
|
|
|
|
int c[11];
|
|
|
|
for (int j = 0; j <= 10; ++j) {
|
2012-01-17 06:58:23 +00:00
|
|
|
if (j < editDistanceTableWidth + 1 && i < outputLength + 1) {
|
|
|
|
c[j] = (editDistanceTable + i * (editDistanceTableWidth + 1))[j];
|
2011-12-16 14:15:06 +00:00
|
|
|
} else {
|
|
|
|
c[j] = -1;
|
|
|
|
}
|
|
|
|
}
|
2012-01-13 09:01:22 +00:00
|
|
|
AKLOGI("[ %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d ]",
|
2011-12-16 14:15:06 +00:00
|
|
|
c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7], c[8], c[9], c[10]);
|
2012-09-06 23:55:16 +00:00
|
|
|
(void)c; // To suppress compiler warning
|
2011-12-16 14:15:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-17 06:58:23 +00:00
|
|
|
inline static int getCurrentEditDistance(int *editDistanceTable, const int editDistanceTableWidth,
|
2012-08-23 06:46:43 +00:00
|
|
|
const int outputLength, const int inputSize) {
|
2012-01-17 06:59:15 +00:00
|
|
|
if (DEBUG_EDIT_DISTANCE) {
|
2012-08-23 06:46:43 +00:00
|
|
|
AKLOGI("getCurrentEditDistance %d, %d", inputSize, outputLength);
|
2011-12-16 14:15:06 +00:00
|
|
|
}
|
2012-08-23 06:46:43 +00:00
|
|
|
return editDistanceTable[(editDistanceTableWidth + 1) * (outputLength) + inputSize];
|
2011-10-13 06:26:45 +00:00
|
|
|
}
|
|
|
|
|
2011-08-10 05:30:10 +00:00
|
|
|
////////////////
|
|
|
|
// Correction //
|
|
|
|
////////////////
|
2011-08-04 09:31:57 +00:00
|
|
|
|
2012-05-30 08:28:34 +00:00
|
|
|
void Correction::resetCorrection() {
|
|
|
|
mTotalTraverseCount = 0;
|
|
|
|
}
|
|
|
|
|
2013-01-08 08:23:43 +00:00
|
|
|
void Correction::initCorrection(const ProximityInfo *pi, const int inputSize, const int maxDepth) {
|
2011-07-15 04:49:00 +00:00
|
|
|
mProximityInfo = pi;
|
2012-08-23 06:46:43 +00:00
|
|
|
mInputSize = inputSize;
|
2011-08-04 09:31:57 +00:00
|
|
|
mMaxDepth = maxDepth;
|
2012-08-23 06:46:43 +00:00
|
|
|
mMaxEditDistance = mInputSize < 5 ? 2 : mInputSize / 2;
|
2011-12-16 14:15:06 +00:00
|
|
|
// TODO: This is not supposed to be required. Check what's going wrong with
|
2013-01-11 16:18:00 +00:00
|
|
|
// editDistance[0 ~ MAX_WORD_LENGTH]
|
2011-12-16 14:15:06 +00:00
|
|
|
initEditDistance(mEditDistanceTable);
|
2011-08-01 10:35:27 +00:00
|
|
|
}
|
|
|
|
|
2011-08-10 06:44:08 +00:00
|
|
|
void Correction::initCorrectionState(
|
|
|
|
const int rootPos, const int childCount, const bool traverseAll) {
|
2011-08-10 13:19:33 +00:00
|
|
|
latinime::initCorrectionState(mCorrectionStates, rootPos, childCount, traverseAll);
|
2011-08-11 07:27:28 +00:00
|
|
|
// TODO: remove
|
2011-08-17 08:55:16 +00:00
|
|
|
mCorrectionStates[0].mTransposedPos = mTransposedPos;
|
|
|
|
mCorrectionStates[0].mExcessivePos = mExcessivePos;
|
2011-08-11 07:27:28 +00:00
|
|
|
mCorrectionStates[0].mSkipPos = mSkipPos;
|
2011-08-10 06:44:08 +00:00
|
|
|
}
|
|
|
|
|
2011-08-10 05:30:10 +00:00
|
|
|
void Correction::setCorrectionParams(const int skipPos, const int excessivePos,
|
2011-09-29 09:36:56 +00:00
|
|
|
const int transposedPos, const int spaceProximityPos, const int missingSpacePos,
|
2011-12-15 05:53:19 +00:00
|
|
|
const bool useFullEditDistance, const bool doAutoCompletion, const int maxErrors) {
|
2011-08-11 07:27:28 +00:00
|
|
|
// TODO: remove
|
2011-08-17 08:55:16 +00:00
|
|
|
mTransposedPos = transposedPos;
|
|
|
|
mExcessivePos = excessivePos;
|
2011-07-15 04:49:00 +00:00
|
|
|
mSkipPos = skipPos;
|
2011-08-11 07:27:28 +00:00
|
|
|
// TODO: remove
|
2011-08-17 08:55:16 +00:00
|
|
|
mCorrectionStates[0].mTransposedPos = transposedPos;
|
|
|
|
mCorrectionStates[0].mExcessivePos = excessivePos;
|
2011-08-11 07:27:28 +00:00
|
|
|
mCorrectionStates[0].mSkipPos = skipPos;
|
2011-08-17 08:55:16 +00:00
|
|
|
|
2011-08-01 10:35:27 +00:00
|
|
|
mSpaceProximityPos = spaceProximityPos;
|
|
|
|
mMissingSpacePos = missingSpacePos;
|
2011-09-29 09:36:56 +00:00
|
|
|
mUseFullEditDistance = useFullEditDistance;
|
2011-12-14 12:38:11 +00:00
|
|
|
mDoAutoCompletion = doAutoCompletion;
|
2011-12-15 05:53:19 +00:00
|
|
|
mMaxErrors = maxErrors;
|
2011-07-15 04:49:00 +00:00
|
|
|
}
|
|
|
|
|
2013-01-29 10:52:04 +00:00
|
|
|
void Correction::checkState() const {
|
2011-07-15 04:49:00 +00:00
|
|
|
if (DEBUG_DICT) {
|
|
|
|
int inputCount = 0;
|
|
|
|
if (mSkipPos >= 0) ++inputCount;
|
|
|
|
if (mExcessivePos >= 0) ++inputCount;
|
|
|
|
if (mTransposedPos >= 0) ++inputCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-29 10:52:04 +00:00
|
|
|
bool Correction::sameAsTyped() const {
|
2012-08-10 07:52:27 +00:00
|
|
|
return mProximityInfoState.sameAsTyped(mWord, mOutputIndex);
|
|
|
|
}
|
|
|
|
|
2012-01-30 09:18:30 +00:00
|
|
|
int Correction::getFreqForSplitMultipleWords(const int *freqArray, const int *wordLengthArray,
|
2013-01-29 10:52:04 +00:00
|
|
|
const int wordCount, const bool isSpaceProximity, const int *word) const {
|
2012-01-30 09:18:30 +00:00
|
|
|
return Correction::RankingAlgorithm::calcFreqForSplitMultipleWords(freqArray, wordLengthArray,
|
|
|
|
wordCount, this, isSpaceProximity, word);
|
2011-08-01 10:35:27 +00:00
|
|
|
}
|
|
|
|
|
2012-10-29 09:06:22 +00:00
|
|
|
int Correction::getFinalProbability(const int probability, int **word, int *wordLength) {
|
2012-08-23 06:46:43 +00:00
|
|
|
return getFinalProbabilityInternal(probability, word, wordLength, mInputSize);
|
2012-01-17 06:58:23 +00:00
|
|
|
}
|
|
|
|
|
2012-10-29 09:06:22 +00:00
|
|
|
int Correction::getFinalProbabilityForSubQueue(const int probability, int **word, int *wordLength,
|
|
|
|
const int inputSize) {
|
2012-08-23 06:46:43 +00:00
|
|
|
return getFinalProbabilityInternal(probability, word, wordLength, inputSize);
|
2012-01-17 06:58:23 +00:00
|
|
|
}
|
|
|
|
|
2011-08-10 06:44:08 +00:00
|
|
|
bool Correction::initProcessState(const int outputIndex) {
|
|
|
|
if (mCorrectionStates[outputIndex].mChildCount <= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-08-03 14:27:32 +00:00
|
|
|
mOutputIndex = outputIndex;
|
2011-08-10 06:44:08 +00:00
|
|
|
--(mCorrectionStates[outputIndex].mChildCount);
|
|
|
|
mInputIndex = mCorrectionStates[outputIndex].mInputIndex;
|
2011-08-10 13:19:33 +00:00
|
|
|
mNeedsToTraverseAllNodes = mCorrectionStates[outputIndex].mNeedsToTraverseAllNodes;
|
2011-08-17 08:55:16 +00:00
|
|
|
|
2011-10-05 05:55:07 +00:00
|
|
|
mEquivalentCharCount = mCorrectionStates[outputIndex].mEquivalentCharCount;
|
2011-08-11 12:25:39 +00:00
|
|
|
mProximityCount = mCorrectionStates[outputIndex].mProximityCount;
|
2011-08-17 08:55:16 +00:00
|
|
|
mTransposedCount = mCorrectionStates[outputIndex].mTransposedCount;
|
|
|
|
mExcessiveCount = mCorrectionStates[outputIndex].mExcessiveCount;
|
2011-08-10 13:19:33 +00:00
|
|
|
mSkippedCount = mCorrectionStates[outputIndex].mSkippedCount;
|
2011-08-17 08:55:16 +00:00
|
|
|
mLastCharExceeded = mCorrectionStates[outputIndex].mLastCharExceeded;
|
|
|
|
|
|
|
|
mTransposedPos = mCorrectionStates[outputIndex].mTransposedPos;
|
|
|
|
mExcessivePos = mCorrectionStates[outputIndex].mExcessivePos;
|
2011-08-11 07:27:28 +00:00
|
|
|
mSkipPos = mCorrectionStates[outputIndex].mSkipPos;
|
2011-08-17 08:55:16 +00:00
|
|
|
|
2011-08-10 13:19:33 +00:00
|
|
|
mMatching = false;
|
2011-08-17 08:55:16 +00:00
|
|
|
mProximityMatching = false;
|
2012-02-02 09:49:22 +00:00
|
|
|
mAdditionalProximityMatching = false;
|
2011-08-17 08:55:16 +00:00
|
|
|
mTransposing = false;
|
|
|
|
mExceeding = false;
|
|
|
|
mSkipping = false;
|
|
|
|
|
2011-08-10 06:44:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-08 08:23:43 +00:00
|
|
|
int Correction::goDownTree(const int parentIndex, const int childCount, const int firstChildPos) {
|
2011-08-10 06:44:08 +00:00
|
|
|
mCorrectionStates[mOutputIndex].mParentIndex = parentIndex;
|
|
|
|
mCorrectionStates[mOutputIndex].mChildCount = childCount;
|
|
|
|
mCorrectionStates[mOutputIndex].mSiblingPos = firstChildPos;
|
|
|
|
return mOutputIndex;
|
2011-08-02 17:19:44 +00:00
|
|
|
}
|
|
|
|
|
2011-08-03 14:27:32 +00:00
|
|
|
// TODO: remove
|
2012-08-16 10:34:02 +00:00
|
|
|
int Correction::getInputIndex() const {
|
2011-08-03 14:27:32 +00:00
|
|
|
return mInputIndex;
|
2011-08-02 17:19:44 +00:00
|
|
|
}
|
|
|
|
|
2011-08-10 05:30:10 +00:00
|
|
|
bool Correction::needsToPrune() const {
|
2011-10-13 06:26:45 +00:00
|
|
|
// TODO: use edit distance here
|
2011-12-14 12:38:11 +00:00
|
|
|
return mOutputIndex - 1 >= mMaxDepth || mProximityCount > mMaxEditDistance
|
|
|
|
// Allow one char longer word for missing character
|
2012-08-23 06:46:43 +00:00
|
|
|
|| (!mDoAutoCompletion && (mOutputIndex > mInputSize));
|
2011-08-04 09:31:57 +00:00
|
|
|
}
|
|
|
|
|
2012-11-02 14:57:13 +00:00
|
|
|
inline static bool isEquivalentChar(ProximityType type) {
|
2013-03-07 04:06:32 +00:00
|
|
|
return type == MATCH_CHAR;
|
2011-09-28 03:59:43 +00:00
|
|
|
}
|
|
|
|
|
2012-11-02 14:57:13 +00:00
|
|
|
inline static bool isProximityCharOrEquivalentChar(ProximityType type) {
|
2013-03-07 04:06:32 +00:00
|
|
|
return type == MATCH_CHAR || type == PROXIMITY_CHAR;
|
2012-02-02 09:49:22 +00:00
|
|
|
}
|
|
|
|
|
2012-10-29 09:06:22 +00:00
|
|
|
Correction::CorrectionType Correction::processCharAndCalcState(const int c, const bool isTerminal) {
|
2011-08-24 07:30:36 +00:00
|
|
|
const int correctionCount = (mSkippedCount + mExcessiveCount + mTransposedCount);
|
2011-12-15 05:53:19 +00:00
|
|
|
if (correctionCount > mMaxErrors) {
|
2012-01-16 07:21:21 +00:00
|
|
|
return processUnrelatedCorrectionType();
|
2011-12-15 05:53:19 +00:00
|
|
|
}
|
|
|
|
|
2011-08-24 07:30:36 +00:00
|
|
|
// TODO: Change the limit if we'll allow two or more corrections
|
|
|
|
const bool noCorrectionsHappenedSoFar = correctionCount == 0;
|
|
|
|
const bool canTryCorrection = noCorrectionsHappenedSoFar;
|
2011-10-06 10:12:20 +00:00
|
|
|
int proximityIndex = 0;
|
|
|
|
mDistances[mOutputIndex] = NOT_A_DISTANCE;
|
2011-08-19 08:27:46 +00:00
|
|
|
|
2011-12-15 05:53:19 +00:00
|
|
|
// Skip checking this node
|
2012-08-02 10:48:08 +00:00
|
|
|
if (mNeedsToTraverseAllNodes || isSingleQuote(c)) {
|
2011-08-19 13:05:59 +00:00
|
|
|
bool incremented = false;
|
2012-08-23 06:46:43 +00:00
|
|
|
if (mLastCharExceeded && mInputIndex == mInputSize - 1) {
|
2011-08-19 13:05:59 +00:00
|
|
|
// TODO: Do not check the proximity if EditDistance exceeds the threshold
|
2013-02-28 05:17:25 +00:00
|
|
|
const ProximityType matchId = mProximityInfoState.getProximityType(
|
2012-06-08 06:29:44 +00:00
|
|
|
mInputIndex, c, true, &proximityIndex);
|
2011-09-28 03:59:43 +00:00
|
|
|
if (isEquivalentChar(matchId)) {
|
2011-08-19 13:05:59 +00:00
|
|
|
mLastCharExceeded = false;
|
|
|
|
--mExcessiveCount;
|
2011-10-06 10:12:20 +00:00
|
|
|
mDistances[mOutputIndex] =
|
2012-06-08 06:29:44 +00:00
|
|
|
mProximityInfoState.getNormalizedSquaredDistance(mInputIndex, 0);
|
2013-03-07 04:06:32 +00:00
|
|
|
} else if (matchId == PROXIMITY_CHAR) {
|
2011-08-19 13:05:59 +00:00
|
|
|
mLastCharExceeded = false;
|
|
|
|
--mExcessiveCount;
|
|
|
|
++mProximityCount;
|
2012-06-08 06:29:44 +00:00
|
|
|
mDistances[mOutputIndex] = mProximityInfoState.getNormalizedSquaredDistance(
|
|
|
|
mInputIndex, proximityIndex);
|
2011-08-19 13:05:59 +00:00
|
|
|
}
|
2012-08-02 10:48:08 +00:00
|
|
|
if (!isSingleQuote(c)) {
|
2012-03-02 09:28:58 +00:00
|
|
|
incrementInputIndex();
|
|
|
|
incremented = true;
|
|
|
|
}
|
2011-08-19 09:22:28 +00:00
|
|
|
}
|
2011-08-19 13:05:59 +00:00
|
|
|
return processSkipChar(c, isTerminal, incremented);
|
2011-08-19 08:27:46 +00:00
|
|
|
}
|
2011-08-17 08:55:16 +00:00
|
|
|
|
2011-12-15 05:53:19 +00:00
|
|
|
// Check possible corrections.
|
2011-08-17 08:55:16 +00:00
|
|
|
if (mExcessivePos >= 0) {
|
|
|
|
if (mExcessiveCount == 0 && mExcessivePos < mOutputIndex) {
|
2011-08-19 08:27:46 +00:00
|
|
|
mExcessivePos = mOutputIndex;
|
2011-08-17 08:55:16 +00:00
|
|
|
}
|
2012-08-23 06:46:43 +00:00
|
|
|
if (mExcessivePos < mInputSize - 1) {
|
2011-08-24 07:30:36 +00:00
|
|
|
mExceeding = mExcessivePos == mInputIndex && canTryCorrection;
|
2011-08-17 08:55:16 +00:00
|
|
|
}
|
2011-08-04 09:31:57 +00:00
|
|
|
}
|
|
|
|
|
2011-08-05 12:21:01 +00:00
|
|
|
if (mSkipPos >= 0) {
|
2011-08-11 07:27:28 +00:00
|
|
|
if (mSkippedCount == 0 && mSkipPos < mOutputIndex) {
|
|
|
|
if (DEBUG_DICT) {
|
2012-09-04 03:49:46 +00:00
|
|
|
// TODO: Enable this assertion.
|
2013-01-09 06:21:44 +00:00
|
|
|
//ASSERT(mSkipPos == mOutputIndex - 1);
|
2011-08-11 07:27:28 +00:00
|
|
|
}
|
2011-08-19 08:27:46 +00:00
|
|
|
mSkipPos = mOutputIndex;
|
2011-08-11 07:27:28 +00:00
|
|
|
}
|
2011-08-24 07:30:36 +00:00
|
|
|
mSkipping = mSkipPos == mOutputIndex && canTryCorrection;
|
2011-08-17 08:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mTransposedPos >= 0) {
|
|
|
|
if (mTransposedCount == 0 && mTransposedPos < mOutputIndex) {
|
2011-08-19 08:27:46 +00:00
|
|
|
mTransposedPos = mOutputIndex;
|
|
|
|
}
|
2012-08-23 06:46:43 +00:00
|
|
|
if (mTransposedPos < mInputSize - 1) {
|
2011-08-24 07:30:36 +00:00
|
|
|
mTransposing = mInputIndex == mTransposedPos && canTryCorrection;
|
2011-08-17 08:55:16 +00:00
|
|
|
}
|
2011-08-05 12:21:01 +00:00
|
|
|
}
|
|
|
|
|
2011-08-19 08:27:46 +00:00
|
|
|
bool secondTransposing = false;
|
|
|
|
if (mTransposedCount % 2 == 1) {
|
2013-02-28 05:17:25 +00:00
|
|
|
if (isEquivalentChar(mProximityInfoState.getProximityType(
|
2012-06-08 06:29:44 +00:00
|
|
|
mInputIndex - 1, c, false))) {
|
2011-08-19 08:27:46 +00:00
|
|
|
++mTransposedCount;
|
|
|
|
secondTransposing = true;
|
|
|
|
} else if (mCorrectionStates[mOutputIndex].mExceeding) {
|
|
|
|
--mTransposedCount;
|
|
|
|
++mExcessiveCount;
|
2011-08-19 13:05:59 +00:00
|
|
|
--mExcessivePos;
|
2011-08-19 08:27:46 +00:00
|
|
|
incrementInputIndex();
|
|
|
|
} else {
|
|
|
|
--mTransposedCount;
|
2012-01-31 08:15:43 +00:00
|
|
|
if (DEBUG_CORRECTION
|
2012-08-23 06:46:43 +00:00
|
|
|
&& (INPUTLENGTH_FOR_DEBUG <= 0 || INPUTLENGTH_FOR_DEBUG == mInputSize)
|
2012-01-31 08:15:43 +00:00
|
|
|
&& (MIN_OUTPUT_INDEX_FOR_DEBUG <= 0
|
|
|
|
|| MIN_OUTPUT_INDEX_FOR_DEBUG < mOutputIndex)) {
|
2011-08-19 13:05:59 +00:00
|
|
|
DUMP_WORD(mWord, mOutputIndex);
|
2012-01-13 09:01:22 +00:00
|
|
|
AKLOGI("UNRELATED(0): %d, %d, %d, %d, %c", mProximityCount, mSkippedCount,
|
2011-08-19 13:05:59 +00:00
|
|
|
mTransposedCount, mExcessiveCount, c);
|
|
|
|
}
|
2012-01-16 07:21:21 +00:00
|
|
|
return processUnrelatedCorrectionType();
|
2011-08-04 09:31:57 +00:00
|
|
|
}
|
2011-08-19 08:27:46 +00:00
|
|
|
}
|
2011-08-04 09:31:57 +00:00
|
|
|
|
2011-08-24 07:30:36 +00:00
|
|
|
// TODO: Change the limit if we'll allow two or more proximity chars with corrections
|
2011-12-15 05:53:19 +00:00
|
|
|
// Work around: When the mMaxErrors is 1, we only allow just one error
|
|
|
|
// including proximity correction.
|
|
|
|
const bool checkProximityChars = (mMaxErrors > 1)
|
|
|
|
? (noCorrectionsHappenedSoFar || mProximityCount == 0)
|
|
|
|
: (noCorrectionsHappenedSoFar && mProximityCount == 0);
|
|
|
|
|
2012-06-05 08:55:52 +00:00
|
|
|
ProximityType matchedProximityCharId = secondTransposing
|
2013-03-07 04:06:32 +00:00
|
|
|
? MATCH_CHAR
|
2013-02-28 05:17:25 +00:00
|
|
|
: mProximityInfoState.getProximityType(
|
2011-10-06 10:12:20 +00:00
|
|
|
mInputIndex, c, checkProximityChars, &proximityIndex);
|
2011-08-19 08:27:46 +00:00
|
|
|
|
2013-03-07 04:06:32 +00:00
|
|
|
if (SUBSTITUTION_CHAR == matchedProximityCharId
|
2012-06-05 08:55:52 +00:00
|
|
|
|| ADDITIONAL_PROXIMITY_CHAR == matchedProximityCharId) {
|
2011-10-06 03:24:59 +00:00
|
|
|
if (canTryCorrection && mOutputIndex > 0
|
|
|
|
&& mCorrectionStates[mOutputIndex].mProximityMatching
|
2011-08-19 13:05:59 +00:00
|
|
|
&& mCorrectionStates[mOutputIndex].mExceeding
|
2013-02-28 05:17:25 +00:00
|
|
|
&& isEquivalentChar(mProximityInfoState.getProximityType(
|
2011-10-06 03:24:59 +00:00
|
|
|
mInputIndex, mWord[mOutputIndex - 1], false))) {
|
2012-01-31 08:15:43 +00:00
|
|
|
if (DEBUG_CORRECTION
|
2012-08-23 06:46:43 +00:00
|
|
|
&& (INPUTLENGTH_FOR_DEBUG <= 0 || INPUTLENGTH_FOR_DEBUG == mInputSize)
|
2012-01-31 08:15:43 +00:00
|
|
|
&& (MIN_OUTPUT_INDEX_FOR_DEBUG <= 0
|
|
|
|
|| MIN_OUTPUT_INDEX_FOR_DEBUG < mOutputIndex)) {
|
2012-01-13 09:01:22 +00:00
|
|
|
AKLOGI("CONVERSION p->e %c", mWord[mOutputIndex - 1]);
|
2011-10-06 03:24:59 +00:00
|
|
|
}
|
2011-08-24 07:30:36 +00:00
|
|
|
// Conversion p->e
|
2011-10-06 03:24:59 +00:00
|
|
|
// Example:
|
|
|
|
// wearth -> earth
|
|
|
|
// px -> (E)mmmmm
|
2011-08-19 13:05:59 +00:00
|
|
|
++mExcessiveCount;
|
|
|
|
--mProximityCount;
|
2011-10-06 03:24:59 +00:00
|
|
|
mExcessivePos = mOutputIndex - 1;
|
|
|
|
++mInputIndex;
|
|
|
|
// Here, we are doing something equivalent to matchedProximityCharId,
|
|
|
|
// but we already know that "excessive char correction" just happened
|
|
|
|
// so that we just need to check "mProximityCount == 0".
|
2013-02-28 05:17:25 +00:00
|
|
|
matchedProximityCharId = mProximityInfoState.getProximityType(
|
2011-10-06 10:12:20 +00:00
|
|
|
mInputIndex, c, mProximityCount == 0, &proximityIndex);
|
2011-10-06 03:24:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-07 04:06:32 +00:00
|
|
|
if (SUBSTITUTION_CHAR == matchedProximityCharId
|
2012-06-05 08:55:52 +00:00
|
|
|
|| ADDITIONAL_PROXIMITY_CHAR == matchedProximityCharId) {
|
|
|
|
if (ADDITIONAL_PROXIMITY_CHAR == matchedProximityCharId) {
|
2012-02-02 09:49:22 +00:00
|
|
|
mAdditionalProximityMatching = true;
|
|
|
|
}
|
2011-10-06 03:24:59 +00:00
|
|
|
// TODO: Optimize
|
|
|
|
// As the current char turned out to be an unrelated char,
|
|
|
|
// we will try other correction-types. Please note that mCorrectionStates[mOutputIndex]
|
|
|
|
// here refers to the previous state.
|
2012-08-23 06:46:43 +00:00
|
|
|
if (mInputIndex < mInputSize - 1 && mOutputIndex > 0 && mTransposedCount > 0
|
2011-08-19 13:05:59 +00:00
|
|
|
&& !mCorrectionStates[mOutputIndex].mTransposing
|
|
|
|
&& mCorrectionStates[mOutputIndex - 1].mTransposing
|
2013-02-28 05:17:25 +00:00
|
|
|
&& isEquivalentChar(mProximityInfoState.getProximityType(
|
2011-09-28 03:59:43 +00:00
|
|
|
mInputIndex, mWord[mOutputIndex - 1], false))
|
|
|
|
&& isEquivalentChar(
|
2013-02-28 05:17:25 +00:00
|
|
|
mProximityInfoState.getProximityType(mInputIndex + 1, c, false))) {
|
2011-08-24 07:30:36 +00:00
|
|
|
// Conversion t->e
|
2011-08-19 13:05:59 +00:00
|
|
|
// Example:
|
|
|
|
// occaisional -> occa sional
|
|
|
|
// mmmmttx -> mmmm(E)mmmmmm
|
|
|
|
mTransposedCount -= 2;
|
|
|
|
++mExcessiveCount;
|
|
|
|
++mInputIndex;
|
2011-08-24 07:30:36 +00:00
|
|
|
} else if (mOutputIndex > 0 && mInputIndex > 0 && mTransposedCount > 0
|
2011-08-19 13:05:59 +00:00
|
|
|
&& !mCorrectionStates[mOutputIndex].mTransposing
|
|
|
|
&& mCorrectionStates[mOutputIndex - 1].mTransposing
|
2011-09-28 03:59:43 +00:00
|
|
|
&& isEquivalentChar(
|
2013-02-28 05:17:25 +00:00
|
|
|
mProximityInfoState.getProximityType(mInputIndex - 1, c, false))) {
|
2011-08-24 07:30:36 +00:00
|
|
|
// Conversion t->s
|
2011-08-19 13:05:59 +00:00
|
|
|
// Example:
|
|
|
|
// chcolate -> chocolate
|
|
|
|
// mmttx -> mmsmmmmmm
|
|
|
|
mTransposedCount -= 2;
|
|
|
|
++mSkippedCount;
|
|
|
|
--mInputIndex;
|
2011-08-24 07:30:36 +00:00
|
|
|
} else if (canTryCorrection && mInputIndex > 0
|
|
|
|
&& mCorrectionStates[mOutputIndex].mProximityMatching
|
|
|
|
&& mCorrectionStates[mOutputIndex].mSkipping
|
2011-09-28 03:59:43 +00:00
|
|
|
&& isEquivalentChar(
|
2013-02-28 05:17:25 +00:00
|
|
|
mProximityInfoState.getProximityType(mInputIndex - 1, c, false))) {
|
2011-08-24 07:30:36 +00:00
|
|
|
// Conversion p->s
|
|
|
|
// Note: This logic tries saving cases like contrst --> contrast -- "a" is one of
|
|
|
|
// proximity chars of "s", but it should rather be handled as a skipped char.
|
|
|
|
++mSkippedCount;
|
|
|
|
--mProximityCount;
|
|
|
|
return processSkipChar(c, isTerminal, false);
|
2012-08-23 06:46:43 +00:00
|
|
|
} else if (mInputIndex - 1 < mInputSize
|
2012-02-02 09:49:22 +00:00
|
|
|
&& mSkippedCount > 0
|
|
|
|
&& mCorrectionStates[mOutputIndex].mSkipping
|
|
|
|
&& mCorrectionStates[mOutputIndex].mAdditionalProximityMatching
|
|
|
|
&& isProximityCharOrEquivalentChar(
|
2013-02-28 05:17:25 +00:00
|
|
|
mProximityInfoState.getProximityType(mInputIndex + 1, c, false))) {
|
2012-02-02 09:49:22 +00:00
|
|
|
// Conversion s->a
|
|
|
|
incrementInputIndex();
|
|
|
|
--mSkippedCount;
|
|
|
|
mProximityMatching = true;
|
|
|
|
++mProximityCount;
|
|
|
|
mDistances[mOutputIndex] = ADDITIONAL_PROXIMITY_CHAR_DISTANCE_INFO;
|
2012-08-23 06:46:43 +00:00
|
|
|
} else if ((mExceeding || mTransposing) && mInputIndex - 1 < mInputSize
|
2011-09-28 03:59:43 +00:00
|
|
|
&& isEquivalentChar(
|
2013-02-28 05:17:25 +00:00
|
|
|
mProximityInfoState.getProximityType(mInputIndex + 1, c, false))) {
|
2011-08-24 07:30:36 +00:00
|
|
|
// 1.2. Excessive or transpose correction
|
2011-08-19 08:27:46 +00:00
|
|
|
if (mTransposing) {
|
|
|
|
++mTransposedCount;
|
2011-08-05 12:21:01 +00:00
|
|
|
} else {
|
2011-08-19 08:27:46 +00:00
|
|
|
++mExcessiveCount;
|
|
|
|
incrementInputIndex();
|
2011-08-05 12:21:01 +00:00
|
|
|
}
|
2012-01-31 08:15:43 +00:00
|
|
|
if (DEBUG_CORRECTION
|
2012-08-23 06:46:43 +00:00
|
|
|
&& (INPUTLENGTH_FOR_DEBUG <= 0 || INPUTLENGTH_FOR_DEBUG == mInputSize)
|
2012-01-31 08:15:43 +00:00
|
|
|
&& (MIN_OUTPUT_INDEX_FOR_DEBUG <= 0
|
|
|
|
|| MIN_OUTPUT_INDEX_FOR_DEBUG < mOutputIndex)) {
|
|
|
|
DUMP_WORD(mWord, mOutputIndex);
|
|
|
|
if (mTransposing) {
|
|
|
|
AKLOGI("TRANSPOSE: %d, %d, %d, %d, %c", mProximityCount, mSkippedCount,
|
|
|
|
mTransposedCount, mExcessiveCount, c);
|
|
|
|
} else {
|
|
|
|
AKLOGI("EXCEED: %d, %d, %d, %d, %c", mProximityCount, mSkippedCount,
|
|
|
|
mTransposedCount, mExcessiveCount, c);
|
|
|
|
}
|
|
|
|
}
|
2011-08-24 07:30:36 +00:00
|
|
|
} else if (mSkipping) {
|
|
|
|
// 3. Skip correction
|
2011-08-19 08:27:46 +00:00
|
|
|
++mSkippedCount;
|
2012-01-31 08:15:43 +00:00
|
|
|
if (DEBUG_CORRECTION
|
2012-08-23 06:46:43 +00:00
|
|
|
&& (INPUTLENGTH_FOR_DEBUG <= 0 || INPUTLENGTH_FOR_DEBUG == mInputSize)
|
2012-01-31 08:15:43 +00:00
|
|
|
&& (MIN_OUTPUT_INDEX_FOR_DEBUG <= 0
|
|
|
|
|| MIN_OUTPUT_INDEX_FOR_DEBUG < mOutputIndex)) {
|
|
|
|
AKLOGI("SKIP: %d, %d, %d, %d, %c", mProximityCount, mSkippedCount,
|
|
|
|
mTransposedCount, mExcessiveCount, c);
|
|
|
|
}
|
2011-08-19 13:05:59 +00:00
|
|
|
return processSkipChar(c, isTerminal, false);
|
2012-06-05 08:55:52 +00:00
|
|
|
} else if (ADDITIONAL_PROXIMITY_CHAR == matchedProximityCharId) {
|
2012-01-31 08:15:43 +00:00
|
|
|
// As a last resort, use additional proximity characters
|
|
|
|
mProximityMatching = true;
|
|
|
|
++mProximityCount;
|
|
|
|
mDistances[mOutputIndex] = ADDITIONAL_PROXIMITY_CHAR_DISTANCE_INFO;
|
|
|
|
if (DEBUG_CORRECTION
|
2012-08-23 06:46:43 +00:00
|
|
|
&& (INPUTLENGTH_FOR_DEBUG <= 0 || INPUTLENGTH_FOR_DEBUG == mInputSize)
|
2012-01-31 08:15:43 +00:00
|
|
|
&& (MIN_OUTPUT_INDEX_FOR_DEBUG <= 0
|
|
|
|
|| MIN_OUTPUT_INDEX_FOR_DEBUG < mOutputIndex)) {
|
|
|
|
AKLOGI("ADDITIONALPROX: %d, %d, %d, %d, %c", mProximityCount, mSkippedCount,
|
|
|
|
mTransposedCount, mExcessiveCount, c);
|
|
|
|
}
|
2011-08-19 08:27:46 +00:00
|
|
|
} else {
|
2012-01-31 08:15:43 +00:00
|
|
|
if (DEBUG_CORRECTION
|
2012-08-23 06:46:43 +00:00
|
|
|
&& (INPUTLENGTH_FOR_DEBUG <= 0 || INPUTLENGTH_FOR_DEBUG == mInputSize)
|
2012-01-31 08:15:43 +00:00
|
|
|
&& (MIN_OUTPUT_INDEX_FOR_DEBUG <= 0
|
|
|
|
|| MIN_OUTPUT_INDEX_FOR_DEBUG < mOutputIndex)) {
|
2011-08-19 13:05:59 +00:00
|
|
|
DUMP_WORD(mWord, mOutputIndex);
|
2012-01-13 09:01:22 +00:00
|
|
|
AKLOGI("UNRELATED(1): %d, %d, %d, %d, %c", mProximityCount, mSkippedCount,
|
2011-08-19 13:05:59 +00:00
|
|
|
mTransposedCount, mExcessiveCount, c);
|
|
|
|
}
|
2012-01-16 07:21:21 +00:00
|
|
|
return processUnrelatedCorrectionType();
|
2011-08-04 09:31:57 +00:00
|
|
|
}
|
2011-09-28 03:59:43 +00:00
|
|
|
} else if (secondTransposing) {
|
2012-08-23 06:46:43 +00:00
|
|
|
// If inputIndex is greater than mInputSize, that means there is no
|
2011-08-19 08:27:46 +00:00
|
|
|
// proximity chars. So, we don't need to check proximity.
|
|
|
|
mMatching = true;
|
2011-09-28 03:59:43 +00:00
|
|
|
} else if (isEquivalentChar(matchedProximityCharId)) {
|
|
|
|
mMatching = true;
|
2011-10-05 05:55:07 +00:00
|
|
|
++mEquivalentCharCount;
|
2012-06-08 06:29:44 +00:00
|
|
|
mDistances[mOutputIndex] = mProximityInfoState.getNormalizedSquaredDistance(mInputIndex, 0);
|
2013-03-07 04:06:32 +00:00
|
|
|
} else if (PROXIMITY_CHAR == matchedProximityCharId) {
|
2011-08-19 08:27:46 +00:00
|
|
|
mProximityMatching = true;
|
2011-09-28 03:59:43 +00:00
|
|
|
++mProximityCount;
|
2011-10-06 10:12:20 +00:00
|
|
|
mDistances[mOutputIndex] =
|
2012-06-08 06:29:44 +00:00
|
|
|
mProximityInfoState.getNormalizedSquaredDistance(mInputIndex, proximityIndex);
|
2012-01-31 08:15:43 +00:00
|
|
|
if (DEBUG_CORRECTION
|
2012-08-23 06:46:43 +00:00
|
|
|
&& (INPUTLENGTH_FOR_DEBUG <= 0 || INPUTLENGTH_FOR_DEBUG == mInputSize)
|
2012-01-31 08:15:43 +00:00
|
|
|
&& (MIN_OUTPUT_INDEX_FOR_DEBUG <= 0
|
|
|
|
|| MIN_OUTPUT_INDEX_FOR_DEBUG < mOutputIndex)) {
|
|
|
|
AKLOGI("PROX: %d, %d, %d, %d, %c", mProximityCount, mSkippedCount,
|
|
|
|
mTransposedCount, mExcessiveCount, c);
|
|
|
|
}
|
2011-08-19 08:27:46 +00:00
|
|
|
}
|
2011-08-04 09:31:57 +00:00
|
|
|
|
2011-10-13 06:26:45 +00:00
|
|
|
addCharToCurrentWord(c);
|
2011-08-04 09:31:57 +00:00
|
|
|
|
2011-08-24 07:30:36 +00:00
|
|
|
// 4. Last char excessive correction
|
|
|
|
mLastCharExceeded = mExcessiveCount == 0 && mSkippedCount == 0 && mTransposedCount == 0
|
2012-08-23 06:46:43 +00:00
|
|
|
&& mProximityCount == 0 && (mInputIndex == mInputSize - 2);
|
|
|
|
const bool isSameAsUserTypedLength = (mInputSize == mInputIndex + 1) || mLastCharExceeded;
|
2011-08-19 08:27:46 +00:00
|
|
|
if (mLastCharExceeded) {
|
|
|
|
++mExcessiveCount;
|
|
|
|
}
|
2011-08-04 09:31:57 +00:00
|
|
|
|
2011-08-19 08:27:46 +00:00
|
|
|
// Start traversing all nodes after the index exceeds the user typed length
|
|
|
|
if (isSameAsUserTypedLength) {
|
|
|
|
startToTraverseAllNodes();
|
2011-08-04 09:31:57 +00:00
|
|
|
}
|
|
|
|
|
2011-08-19 13:05:59 +00:00
|
|
|
const bool needsToTryOnTerminalForTheLastPossibleExcessiveChar =
|
2012-08-23 06:46:43 +00:00
|
|
|
mExceeding && mInputIndex == mInputSize - 2;
|
2011-08-19 13:05:59 +00:00
|
|
|
|
2011-08-19 08:27:46 +00:00
|
|
|
// Finally, we are ready to go to the next character, the next "virtual node".
|
|
|
|
// We should advance the input index.
|
|
|
|
// We do this in this branch of the 'if traverseAllNodes' because we are still matching
|
|
|
|
// characters to input; the other branch is not matching them but searching for
|
|
|
|
// completions, this is why it does not have to do it.
|
|
|
|
incrementInputIndex();
|
2011-08-04 09:31:57 +00:00
|
|
|
// Also, the next char is one "virtual node" depth more than this char.
|
|
|
|
incrementOutputIndex();
|
|
|
|
|
2011-08-19 13:05:59 +00:00
|
|
|
if ((needsToTryOnTerminalForTheLastPossibleExcessiveChar
|
|
|
|
|| isSameAsUserTypedLength) && isTerminal) {
|
2011-08-19 08:27:46 +00:00
|
|
|
mTerminalInputIndex = mInputIndex - 1;
|
|
|
|
mTerminalOutputIndex = mOutputIndex - 1;
|
2012-01-31 08:15:43 +00:00
|
|
|
if (DEBUG_CORRECTION
|
2012-08-23 06:46:43 +00:00
|
|
|
&& (INPUTLENGTH_FOR_DEBUG <= 0 || INPUTLENGTH_FOR_DEBUG == mInputSize)
|
2012-01-31 08:15:43 +00:00
|
|
|
&& (MIN_OUTPUT_INDEX_FOR_DEBUG <= 0 || MIN_OUTPUT_INDEX_FOR_DEBUG < mOutputIndex)) {
|
2011-10-06 03:24:59 +00:00
|
|
|
DUMP_WORD(mWord, mOutputIndex);
|
2012-01-13 09:01:22 +00:00
|
|
|
AKLOGI("ONTERMINAL(1): %d, %d, %d, %d, %c", mProximityCount, mSkippedCount,
|
2011-10-06 03:24:59 +00:00
|
|
|
mTransposedCount, mExcessiveCount, c);
|
|
|
|
}
|
2011-08-19 08:27:46 +00:00
|
|
|
return ON_TERMINAL;
|
|
|
|
} else {
|
2012-01-16 07:21:21 +00:00
|
|
|
mTerminalInputIndex = mInputIndex - 1;
|
|
|
|
mTerminalOutputIndex = mOutputIndex - 1;
|
2011-08-19 08:27:46 +00:00
|
|
|
return NOT_ON_TERMINAL;
|
|
|
|
}
|
2011-08-04 09:31:57 +00:00
|
|
|
}
|
|
|
|
|
2012-10-29 09:06:22 +00:00
|
|
|
inline static int getQuoteCount(const int *word, const int length) {
|
2011-08-15 13:30:33 +00:00
|
|
|
int quoteCount = 0;
|
|
|
|
for (int i = 0; i < length; ++i) {
|
2012-10-29 09:06:22 +00:00
|
|
|
if (word[i] == KEYCODE_SINGLE_QUOTE) {
|
2011-08-15 13:30:33 +00:00
|
|
|
++quoteCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return quoteCount;
|
|
|
|
}
|
|
|
|
|
2011-10-03 10:21:13 +00:00
|
|
|
inline static bool isUpperCase(unsigned short c) {
|
2012-10-29 09:06:22 +00:00
|
|
|
return isAsciiUpper(toBaseCodePoint(c));
|
2011-10-03 10:21:13 +00:00
|
|
|
}
|
|
|
|
|
2011-08-01 10:35:27 +00:00
|
|
|
//////////////////////
|
|
|
|
// RankingAlgorithm //
|
|
|
|
//////////////////////
|
|
|
|
|
2013-01-08 08:23:43 +00:00
|
|
|
/* static */ int Correction::RankingAlgorithm::calculateFinalProbability(const int inputIndex,
|
2012-07-25 08:51:43 +00:00
|
|
|
const int outputIndex, const int freq, int *editDistanceTable, const Correction *correction,
|
2012-08-23 06:46:43 +00:00
|
|
|
const int inputSize) {
|
2011-08-10 05:30:10 +00:00
|
|
|
const int excessivePos = correction->getExcessivePos();
|
|
|
|
const int typedLetterMultiplier = correction->TYPED_LETTER_MULTIPLIER;
|
|
|
|
const int fullWordMultiplier = correction->FULL_WORD_MULTIPLIER;
|
2012-06-08 06:29:44 +00:00
|
|
|
const ProximityInfoState *proximityInfoState = &correction->mProximityInfoState;
|
2011-08-17 08:55:16 +00:00
|
|
|
const int skippedCount = correction->mSkippedCount;
|
2011-08-19 13:05:59 +00:00
|
|
|
const int transposedCount = correction->mTransposedCount / 2;
|
|
|
|
const int excessiveCount = correction->mExcessiveCount + correction->mTransposedCount % 2;
|
2011-08-11 16:05:27 +00:00
|
|
|
const int proximityMatchedCount = correction->mProximityCount;
|
2011-08-17 08:55:16 +00:00
|
|
|
const bool lastCharExceeded = correction->mLastCharExceeded;
|
2011-09-29 09:36:56 +00:00
|
|
|
const bool useFullEditDistance = correction->mUseFullEditDistance;
|
|
|
|
const int outputLength = outputIndex + 1;
|
2012-08-23 06:46:43 +00:00
|
|
|
if (skippedCount >= inputSize || inputSize == 0) {
|
2011-08-15 13:30:33 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-08-19 13:05:59 +00:00
|
|
|
// TODO: find more robust way
|
2012-08-23 06:46:43 +00:00
|
|
|
bool sameLength = lastCharExceeded ? (inputSize == inputIndex + 2)
|
|
|
|
: (inputSize == inputIndex + 1);
|
2011-08-11 12:25:39 +00:00
|
|
|
|
|
|
|
// TODO: use mExcessiveCount
|
2012-08-23 06:46:43 +00:00
|
|
|
const int matchCount = inputSize - correction->mProximityCount - excessiveCount;
|
2011-08-11 12:25:39 +00:00
|
|
|
|
2012-10-29 09:06:22 +00:00
|
|
|
const int *word = correction->mWord;
|
2011-08-17 08:55:16 +00:00
|
|
|
const bool skipped = skippedCount > 0;
|
2011-08-11 16:05:27 +00:00
|
|
|
|
2012-01-17 06:58:23 +00:00
|
|
|
const int quoteDiffCount = max(0, getQuoteCount(word, outputLength)
|
2012-08-23 06:46:43 +00:00
|
|
|
- getQuoteCount(proximityInfoState->getPrimaryInputWord(), inputSize));
|
2011-08-01 10:35:27 +00:00
|
|
|
|
2011-08-15 13:30:33 +00:00
|
|
|
// TODO: Calculate edit distance for transposed and excessive
|
|
|
|
int ed = 0;
|
2011-12-16 14:15:06 +00:00
|
|
|
if (DEBUG_DICT_FULL) {
|
2012-08-23 06:46:43 +00:00
|
|
|
dumpEditDistance10ForDebug(editDistanceTable, correction->mInputSize, outputLength);
|
2011-12-16 14:15:06 +00:00
|
|
|
}
|
2011-08-19 13:05:59 +00:00
|
|
|
int adjustedProximityMatchedCount = proximityMatchedCount;
|
|
|
|
|
|
|
|
int finalFreq = freq;
|
2011-08-17 08:55:16 +00:00
|
|
|
|
2012-02-02 09:49:22 +00:00
|
|
|
if (DEBUG_CORRECTION_FREQ
|
2012-08-23 06:46:43 +00:00
|
|
|
&& (INPUTLENGTH_FOR_DEBUG <= 0 || INPUTLENGTH_FOR_DEBUG == inputSize)) {
|
2012-02-02 09:49:22 +00:00
|
|
|
AKLOGI("FinalFreq0: %d", finalFreq);
|
|
|
|
}
|
2011-08-17 08:55:16 +00:00
|
|
|
// TODO: Optimize this.
|
2012-01-16 09:38:32 +00:00
|
|
|
if (transposedCount > 0 || proximityMatchedCount > 0 || skipped || excessiveCount > 0) {
|
2012-08-23 06:46:43 +00:00
|
|
|
ed = getCurrentEditDistance(editDistanceTable, correction->mInputSize, outputLength,
|
|
|
|
inputSize) - transposedCount;
|
2012-01-16 09:38:32 +00:00
|
|
|
|
2011-08-19 13:05:59 +00:00
|
|
|
const int matchWeight = powerIntCapped(typedLetterMultiplier,
|
2012-08-23 06:46:43 +00:00
|
|
|
max(inputSize, outputLength) - ed);
|
2011-08-19 13:05:59 +00:00
|
|
|
multiplyIntCapped(matchWeight, &finalFreq);
|
|
|
|
|
|
|
|
// TODO: Demote further if there are two or more excessive chars with longer user input?
|
2012-08-23 06:46:43 +00:00
|
|
|
if (inputSize > outputLength) {
|
2011-08-19 13:05:59 +00:00
|
|
|
multiplyRate(INPUT_EXCEEDS_OUTPUT_DEMOTION_RATE, &finalFreq);
|
2011-08-11 16:05:27 +00:00
|
|
|
}
|
2011-08-19 13:05:59 +00:00
|
|
|
|
2011-08-15 13:30:33 +00:00
|
|
|
ed = max(0, ed - quoteDiffCount);
|
2012-08-23 06:46:43 +00:00
|
|
|
adjustedProximityMatchedCount = min(max(0, ed - (outputLength - inputSize)),
|
2012-02-02 09:49:22 +00:00
|
|
|
proximityMatchedCount);
|
2012-03-02 09:28:58 +00:00
|
|
|
if (transposedCount <= 0) {
|
2012-08-23 06:46:43 +00:00
|
|
|
if (ed == 1 && (inputSize == outputLength - 1 || inputSize == outputLength + 1)) {
|
2012-01-16 09:38:32 +00:00
|
|
|
// Promote a word with just one skipped or excessive char
|
|
|
|
if (sameLength) {
|
2012-02-02 09:49:22 +00:00
|
|
|
multiplyRate(WORDS_WITH_JUST_ONE_CORRECTION_PROMOTION_RATE
|
|
|
|
+ WORDS_WITH_JUST_ONE_CORRECTION_PROMOTION_MULTIPLIER * outputLength,
|
|
|
|
&finalFreq);
|
2012-01-16 09:38:32 +00:00
|
|
|
} else {
|
|
|
|
multiplyIntCapped(typedLetterMultiplier, &finalFreq);
|
|
|
|
}
|
|
|
|
} else if (ed == 0) {
|
2011-08-19 13:05:59 +00:00
|
|
|
multiplyIntCapped(typedLetterMultiplier, &finalFreq);
|
2012-01-16 09:38:32 +00:00
|
|
|
sameLength = true;
|
2011-08-19 13:05:59 +00:00
|
|
|
}
|
|
|
|
}
|
2011-08-15 13:30:33 +00:00
|
|
|
} else {
|
2011-08-19 13:05:59 +00:00
|
|
|
const int matchWeight = powerIntCapped(typedLetterMultiplier, matchCount);
|
|
|
|
multiplyIntCapped(matchWeight, &finalFreq);
|
2011-08-11 16:05:27 +00:00
|
|
|
}
|
|
|
|
|
2013-03-07 04:06:32 +00:00
|
|
|
if (proximityInfoState->getProximityType(0, word[0], true) == SUBSTITUTION_CHAR) {
|
2011-08-19 13:05:59 +00:00
|
|
|
multiplyRate(FIRST_CHAR_DIFFERENT_DEMOTION_RATE, &finalFreq);
|
|
|
|
}
|
2011-08-15 13:30:33 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////
|
|
|
|
// Promotion and Demotion for each correction
|
2011-08-11 16:05:27 +00:00
|
|
|
|
2011-08-15 13:30:33 +00:00
|
|
|
// Demotion for a word with missing character
|
2011-08-11 12:25:39 +00:00
|
|
|
if (skipped) {
|
2011-08-15 13:30:33 +00:00
|
|
|
const int demotionRate = WORDS_WITH_MISSING_CHARACTER_DEMOTION_RATE
|
2012-08-23 06:46:43 +00:00
|
|
|
* (10 * inputSize - WORDS_WITH_MISSING_CHARACTER_DEMOTION_START_POS_10X)
|
|
|
|
/ (10 * inputSize
|
2011-08-15 13:30:33 +00:00
|
|
|
- WORDS_WITH_MISSING_CHARACTER_DEMOTION_START_POS_10X + 10);
|
|
|
|
if (DEBUG_DICT_FULL) {
|
2012-01-13 09:01:22 +00:00
|
|
|
AKLOGI("Demotion rate for missing character is %d.", demotionRate);
|
2011-08-01 10:35:27 +00:00
|
|
|
}
|
2011-08-15 13:30:33 +00:00
|
|
|
multiplyRate(demotionRate, &finalFreq);
|
2011-08-01 10:35:27 +00:00
|
|
|
}
|
2011-08-15 13:30:33 +00:00
|
|
|
|
|
|
|
// Demotion for a word with transposed character
|
2011-08-19 13:05:59 +00:00
|
|
|
if (transposedCount > 0) multiplyRate(
|
2011-08-01 10:35:27 +00:00
|
|
|
WORDS_WITH_TRANSPOSED_CHARACTERS_DEMOTION_RATE, &finalFreq);
|
2011-08-15 13:30:33 +00:00
|
|
|
|
|
|
|
// Demotion for a word with excessive character
|
2011-08-19 13:05:59 +00:00
|
|
|
if (excessiveCount > 0) {
|
2011-08-01 10:35:27 +00:00
|
|
|
multiplyRate(WORDS_WITH_EXCESSIVE_CHARACTER_DEMOTION_RATE, &finalFreq);
|
2012-06-08 06:29:44 +00:00
|
|
|
if (!lastCharExceeded && !proximityInfoState->existsAdjacentProximityChars(excessivePos)) {
|
2012-01-31 08:15:43 +00:00
|
|
|
if (DEBUG_DICT_FULL) {
|
2012-01-13 09:01:22 +00:00
|
|
|
AKLOGI("Double excessive demotion");
|
2011-08-19 13:05:59 +00:00
|
|
|
}
|
2011-08-01 10:35:27 +00:00
|
|
|
// If an excessive character is not adjacent to the left char or the right char,
|
|
|
|
// we will demote this word.
|
|
|
|
multiplyRate(WORDS_WITH_EXCESSIVE_CHARACTER_OUT_OF_PROXIMITY_DEMOTION_RATE, &finalFreq);
|
|
|
|
}
|
|
|
|
}
|
2011-08-15 13:30:33 +00:00
|
|
|
|
2012-10-05 11:54:57 +00:00
|
|
|
int additionalProximityCount = 0;
|
|
|
|
// Demote additional proximity characters
|
|
|
|
for (int i = 0; i < outputLength; ++i) {
|
|
|
|
const int squaredDistance = correction->mDistances[i];
|
|
|
|
if (squaredDistance == ADDITIONAL_PROXIMITY_CHAR_DISTANCE_INFO) {
|
|
|
|
++additionalProximityCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-31 08:15:43 +00:00
|
|
|
const bool performTouchPositionCorrection =
|
2012-06-08 06:29:44 +00:00
|
|
|
CALIBRATE_SCORE_BY_TOUCH_COORDINATES
|
|
|
|
&& proximityInfoState->touchPositionCorrectionEnabled()
|
2012-10-05 11:54:57 +00:00
|
|
|
&& skippedCount == 0 && excessiveCount == 0 && transposedCount == 0
|
|
|
|
&& additionalProximityCount == 0;
|
|
|
|
|
2011-10-06 10:12:20 +00:00
|
|
|
// Score calibration by touch coordinates is being done only for pure-fat finger typing error
|
|
|
|
// cases.
|
|
|
|
// TODO: Remove this constraint.
|
2012-02-02 09:58:12 +00:00
|
|
|
if (performTouchPositionCorrection) {
|
|
|
|
for (int i = 0; i < outputLength; ++i) {
|
|
|
|
const int squaredDistance = correction->mDistances[i];
|
|
|
|
if (i < adjustedProximityMatchedCount) {
|
|
|
|
multiplyIntCapped(typedLetterMultiplier, &finalFreq);
|
|
|
|
}
|
2013-01-15 10:16:38 +00:00
|
|
|
const float factor =
|
2013-04-11 03:44:15 +00:00
|
|
|
SuggestUtils::getLengthScalingFactor(static_cast<float>(squaredDistance));
|
2013-01-15 10:16:38 +00:00
|
|
|
if (factor > 0.0f) {
|
2013-01-29 16:04:42 +00:00
|
|
|
multiplyRate(static_cast<int>(factor * 100.0f), &finalFreq);
|
2012-02-02 09:58:12 +00:00
|
|
|
} else if (squaredDistance == PROXIMITY_CHAR_WITHOUT_DISTANCE_INFO) {
|
|
|
|
multiplyRate(WORDS_WITH_PROXIMITY_CHARACTER_DEMOTION_RATE, &finalFreq);
|
|
|
|
}
|
2012-01-31 08:15:43 +00:00
|
|
|
}
|
2012-02-02 09:58:12 +00:00
|
|
|
} else {
|
|
|
|
// Promotion for a word with proximity characters
|
|
|
|
for (int i = 0; i < adjustedProximityMatchedCount; ++i) {
|
|
|
|
// A word with proximity corrections
|
|
|
|
if (DEBUG_DICT_FULL) {
|
|
|
|
AKLOGI("Found a proximity correction.");
|
|
|
|
}
|
|
|
|
multiplyIntCapped(typedLetterMultiplier, &finalFreq);
|
|
|
|
if (i < additionalProximityCount) {
|
|
|
|
multiplyRate(WORDS_WITH_ADDITIONAL_PROXIMITY_CHARACTER_DEMOTION_RATE, &finalFreq);
|
|
|
|
} else {
|
|
|
|
multiplyRate(WORDS_WITH_PROXIMITY_CHARACTER_DEMOTION_RATE, &finalFreq);
|
|
|
|
}
|
2011-08-01 10:35:27 +00:00
|
|
|
}
|
2011-09-30 09:08:28 +00:00
|
|
|
}
|
|
|
|
|
2012-02-02 09:49:22 +00:00
|
|
|
// If the user types too many(three or more) proximity characters with additional proximity
|
|
|
|
// character,do not treat as the same length word.
|
|
|
|
if (sameLength && additionalProximityCount > 0 && (adjustedProximityMatchedCount >= 3
|
|
|
|
|| transposedCount > 0 || skipped || excessiveCount > 0)) {
|
|
|
|
sameLength = false;
|
|
|
|
}
|
|
|
|
|
2011-08-19 13:05:59 +00:00
|
|
|
const int errorCount = adjustedProximityMatchedCount > 0
|
|
|
|
? adjustedProximityMatchedCount
|
|
|
|
: (proximityMatchedCount + transposedCount);
|
2011-08-15 13:30:33 +00:00
|
|
|
multiplyRate(
|
2012-08-23 06:46:43 +00:00
|
|
|
100 - CORRECTION_COUNT_RATE_DEMOTION_RATE_BASE * errorCount / inputSize, &finalFreq);
|
2011-08-15 13:30:33 +00:00
|
|
|
|
|
|
|
// Promotion for an exactly matched word
|
2011-08-19 13:05:59 +00:00
|
|
|
if (ed == 0) {
|
2011-08-15 13:30:33 +00:00
|
|
|
// Full exact match
|
2012-01-17 06:58:23 +00:00
|
|
|
if (sameLength && transposedCount == 0 && !skipped && excessiveCount == 0
|
2012-02-02 09:49:22 +00:00
|
|
|
&& quoteDiffCount == 0 && additionalProximityCount == 0) {
|
2011-08-15 13:30:33 +00:00
|
|
|
finalFreq = capped255MultForFullMatchAccentsOrCapitalizationDifference(finalFreq);
|
|
|
|
}
|
2011-08-01 10:35:27 +00:00
|
|
|
}
|
2011-08-10 13:19:33 +00:00
|
|
|
|
2011-08-15 13:30:33 +00:00
|
|
|
// Promote a word with no correction
|
2012-02-02 09:49:22 +00:00
|
|
|
if (proximityMatchedCount == 0 && transposedCount == 0 && !skipped && excessiveCount == 0
|
|
|
|
&& additionalProximityCount == 0) {
|
2011-08-15 13:30:33 +00:00
|
|
|
multiplyRate(FULL_MATCHED_WORDS_PROMOTION_RATE, &finalFreq);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Check excessive count and transposed count
|
|
|
|
// TODO: Remove this if possible
|
2011-08-10 13:19:33 +00:00
|
|
|
/*
|
2011-08-15 13:30:33 +00:00
|
|
|
If the last character of the user input word is the same as the next character
|
|
|
|
of the output word, and also all of characters of the user input are matched
|
|
|
|
to the output word, we'll promote that word a bit because
|
|
|
|
that word can be considered the combination of skipped and matched characters.
|
|
|
|
This means that the 'sm' pattern wins over the 'ma' pattern.
|
|
|
|
e.g.)
|
|
|
|
shel -> shell [mmmma] or [mmmsm]
|
|
|
|
hel -> hello [mmmaa] or [mmsma]
|
|
|
|
m ... matching
|
|
|
|
s ... skipping
|
|
|
|
a ... traversing all
|
2011-08-24 07:30:36 +00:00
|
|
|
t ... transposing
|
|
|
|
e ... exceeding
|
|
|
|
p ... proximity matching
|
2011-08-10 13:19:33 +00:00
|
|
|
*/
|
2012-08-23 06:46:43 +00:00
|
|
|
if (matchCount == inputSize && matchCount >= 2 && !skipped
|
2011-08-10 13:19:33 +00:00
|
|
|
&& word[matchCount] == word[matchCount - 1]) {
|
|
|
|
multiplyRate(WORDS_WITH_MATCH_SKIP_PROMOTION_RATE, &finalFreq);
|
|
|
|
}
|
|
|
|
|
2011-08-19 13:05:59 +00:00
|
|
|
// TODO: Do not use sameLength?
|
2011-08-15 13:30:33 +00:00
|
|
|
if (sameLength) {
|
|
|
|
multiplyIntCapped(fullWordMultiplier, &finalFreq);
|
|
|
|
}
|
|
|
|
|
2012-08-23 06:46:43 +00:00
|
|
|
if (useFullEditDistance && outputLength > inputSize + 1) {
|
|
|
|
const int diff = outputLength - inputSize - 1;
|
2011-09-29 09:36:56 +00:00
|
|
|
const int divider = diff < 31 ? 1 << diff : S_INT_MAX;
|
|
|
|
finalFreq = divider > finalFreq ? 1 : finalFreq / divider;
|
|
|
|
}
|
|
|
|
|
2011-08-15 13:30:33 +00:00
|
|
|
if (DEBUG_DICT_FULL) {
|
2012-01-17 06:58:23 +00:00
|
|
|
AKLOGI("calc: %d, %d", outputLength, sameLength);
|
2011-08-15 13:30:33 +00:00
|
|
|
}
|
|
|
|
|
2012-01-31 08:15:43 +00:00
|
|
|
if (DEBUG_CORRECTION_FREQ
|
2012-08-23 06:46:43 +00:00
|
|
|
&& (INPUTLENGTH_FOR_DEBUG <= 0 || INPUTLENGTH_FOR_DEBUG == inputSize)) {
|
|
|
|
DUMP_WORD(correction->getPrimaryInputWord(), inputSize);
|
2012-01-17 06:58:23 +00:00
|
|
|
DUMP_WORD(correction->mWord, outputLength);
|
2012-02-02 09:49:22 +00:00
|
|
|
AKLOGI("FinalFreq: [P%d, S%d, T%d, E%d, A%d] %d, %d, %d, %d, %d, %d", proximityMatchedCount,
|
|
|
|
skippedCount, transposedCount, excessiveCount, additionalProximityCount,
|
|
|
|
outputLength, lastCharExceeded, sameLength, quoteDiffCount, ed, finalFreq);
|
2011-08-19 13:05:59 +00:00
|
|
|
}
|
|
|
|
|
2011-08-01 10:35:27 +00:00
|
|
|
return finalFreq;
|
|
|
|
}
|
|
|
|
|
2013-01-08 08:23:43 +00:00
|
|
|
/* static */ int Correction::RankingAlgorithm::calcFreqForSplitMultipleWords(const int *freqArray,
|
|
|
|
const int *wordLengthArray, const int wordCount, const Correction *correction,
|
|
|
|
const bool isSpaceProximity, const int *word) {
|
2012-01-17 06:59:15 +00:00
|
|
|
const int typedLetterMultiplier = correction->TYPED_LETTER_MULTIPLIER;
|
|
|
|
|
|
|
|
bool firstCapitalizedWordDemotion = false;
|
|
|
|
bool secondCapitalizedWordDemotion = false;
|
2012-01-30 09:18:30 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
// TODO: Handle multiple capitalized word demotion properly
|
|
|
|
const int firstWordLength = wordLengthArray[0];
|
|
|
|
const int secondWordLength = wordLengthArray[1];
|
|
|
|
if (firstWordLength >= 2) {
|
|
|
|
firstCapitalizedWordDemotion = isUpperCase(word[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (secondWordLength >= 2) {
|
|
|
|
// FIXME: word[firstWordLength + 1] is incorrect.
|
|
|
|
secondCapitalizedWordDemotion = isUpperCase(word[firstWordLength + 1]);
|
|
|
|
}
|
2012-01-17 06:59:15 +00:00
|
|
|
}
|
|
|
|
|
2012-01-30 09:18:30 +00:00
|
|
|
|
2012-01-17 06:59:15 +00:00
|
|
|
const bool capitalizedWordDemotion =
|
|
|
|
firstCapitalizedWordDemotion ^ secondCapitalizedWordDemotion;
|
|
|
|
|
2012-01-30 09:18:30 +00:00
|
|
|
int totalLength = 0;
|
|
|
|
int totalFreq = 0;
|
2012-09-04 03:49:46 +00:00
|
|
|
for (int i = 0; i < wordCount; ++i) {
|
2012-01-30 09:18:30 +00:00
|
|
|
const int wordLength = wordLengthArray[i];
|
|
|
|
if (wordLength <= 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
totalLength += wordLength;
|
|
|
|
const int demotionRate = 100 - TWO_WORDS_CORRECTION_DEMOTION_BASE / (wordLength + 1);
|
|
|
|
int tempFirstFreq = freqArray[i];
|
|
|
|
multiplyRate(demotionRate, &tempFirstFreq);
|
|
|
|
totalFreq += tempFirstFreq;
|
2012-01-17 06:59:15 +00:00
|
|
|
}
|
|
|
|
|
2012-01-30 09:18:30 +00:00
|
|
|
if (totalLength <= 0 || totalFreq <= 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2012-01-17 06:59:15 +00:00
|
|
|
|
2012-01-30 09:18:30 +00:00
|
|
|
// TODO: Currently totalFreq is adjusted to two word metrix.
|
2012-01-17 06:59:15 +00:00
|
|
|
// Promote pairFreq with multiplying by 2, because the word length is the same as the typed
|
|
|
|
// length.
|
2012-01-30 09:18:30 +00:00
|
|
|
totalFreq = totalFreq * 2 / wordCount;
|
|
|
|
if (wordCount > 2) {
|
|
|
|
// Safety net for 3+ words -- Caveats: many heuristics and workarounds here.
|
|
|
|
int oneLengthCounter = 0;
|
|
|
|
int twoLengthCounter = 0;
|
|
|
|
for (int i = 0; i < wordCount; ++i) {
|
|
|
|
const int wordLength = wordLengthArray[i];
|
|
|
|
// TODO: Use bigram instead of this safety net
|
|
|
|
if (i < wordCount - 1) {
|
|
|
|
const int nextWordLength = wordLengthArray[i + 1];
|
|
|
|
if (wordLength == 1 && nextWordLength == 2) {
|
|
|
|
// Safety net to filter 1 length and 2 length sequential words
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const int freq = freqArray[i];
|
|
|
|
// Demote too short weak words
|
2012-05-23 10:55:27 +00:00
|
|
|
if (wordLength <= 4 && freq <= SUPPRESS_SHORT_MULTIPLE_WORDS_THRESHOLD_FREQ) {
|
2013-03-18 04:08:31 +00:00
|
|
|
multiplyRate(100 * freq / MAX_PROBABILITY, &totalFreq);
|
2012-01-30 09:18:30 +00:00
|
|
|
}
|
|
|
|
if (wordLength == 1) {
|
|
|
|
++oneLengthCounter;
|
|
|
|
} else if (wordLength == 2) {
|
|
|
|
++twoLengthCounter;
|
|
|
|
}
|
|
|
|
if (oneLengthCounter >= 2 || (oneLengthCounter + twoLengthCounter) >= 4) {
|
|
|
|
// Safety net to filter too many short words
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
multiplyRate(MULTIPLE_WORDS_DEMOTION_RATE, &totalFreq);
|
|
|
|
}
|
2012-01-17 06:59:15 +00:00
|
|
|
|
|
|
|
// This is a workaround to try offsetting the not-enough-demotion which will be done in
|
|
|
|
// calcNormalizedScore in Utils.java.
|
|
|
|
// In calcNormalizedScore the score will be demoted by (1 - 1 / length)
|
|
|
|
// but we demoted only (1 - 1 / (length + 1)) so we will additionally adjust freq by
|
|
|
|
// (1 - 1 / length) / (1 - 1 / (length + 1)) = (1 - 1 / (length * length))
|
|
|
|
const int normalizedScoreNotEnoughDemotionAdjustment = 100 - 100 / (totalLength * totalLength);
|
|
|
|
multiplyRate(normalizedScoreNotEnoughDemotionAdjustment, &totalFreq);
|
|
|
|
|
|
|
|
// At this moment, totalFreq is calculated by the following formula:
|
|
|
|
// (firstFreq * (1 - 1 / (firstWordLength + 1)) + secondFreq * (1 - 1 / (secondWordLength + 1)))
|
|
|
|
// * (1 - 1 / totalLength) / (1 - 1 / (totalLength + 1))
|
|
|
|
|
|
|
|
multiplyIntCapped(powerIntCapped(typedLetterMultiplier, totalLength), &totalFreq);
|
|
|
|
|
|
|
|
// This is another workaround to offset the demotion which will be done in
|
|
|
|
// calcNormalizedScore in Utils.java.
|
|
|
|
// In calcNormalizedScore the score will be demoted by (1 - 1 / length) so we have to promote
|
|
|
|
// the same amount because we already have adjusted the synthetic freq of this "missing or
|
|
|
|
// mistyped space" suggestion candidate above in this method.
|
|
|
|
const int normalizedScoreDemotionRateOffset = (100 + 100 / totalLength);
|
|
|
|
multiplyRate(normalizedScoreDemotionRateOffset, &totalFreq);
|
|
|
|
|
|
|
|
if (isSpaceProximity) {
|
|
|
|
// A word pair with one space proximity correction
|
|
|
|
if (DEBUG_DICT) {
|
|
|
|
AKLOGI("Found a word pair with space proximity correction.");
|
|
|
|
}
|
|
|
|
multiplyIntCapped(typedLetterMultiplier, &totalFreq);
|
|
|
|
multiplyRate(WORDS_WITH_PROXIMITY_CHARACTER_DEMOTION_RATE, &totalFreq);
|
|
|
|
}
|
|
|
|
|
2012-01-23 07:52:37 +00:00
|
|
|
if (isSpaceProximity) {
|
|
|
|
multiplyRate(WORDS_WITH_MISTYPED_SPACE_DEMOTION_RATE, &totalFreq);
|
|
|
|
} else {
|
|
|
|
multiplyRate(WORDS_WITH_MISSING_SPACE_CHARACTER_DEMOTION_RATE, &totalFreq);
|
|
|
|
}
|
2012-01-17 06:59:15 +00:00
|
|
|
|
|
|
|
if (capitalizedWordDemotion) {
|
|
|
|
multiplyRate(TWO_WORDS_CAPITALIZED_DEMOTION_RATE, &totalFreq);
|
|
|
|
}
|
|
|
|
|
2012-01-30 04:53:58 +00:00
|
|
|
if (DEBUG_CORRECTION_FREQ) {
|
2012-01-30 09:18:30 +00:00
|
|
|
AKLOGI("Multiple words (%d, %d) (%d, %d) %d, %d", freqArray[0], freqArray[1],
|
|
|
|
wordLengthArray[0], wordLengthArray[1], capitalizedWordDemotion, totalFreq);
|
|
|
|
DUMP_WORD(word, wordLengthArray[0]);
|
2012-01-30 04:53:58 +00:00
|
|
|
}
|
|
|
|
|
2012-01-17 06:59:15 +00:00
|
|
|
return totalFreq;
|
|
|
|
}
|
|
|
|
|
2013-01-08 08:23:43 +00:00
|
|
|
/* static */ int Correction::RankingAlgorithm::editDistance(const int *before,
|
|
|
|
const int beforeLength, const int *after, const int afterLength) {
|
2013-05-07 10:47:20 +00:00
|
|
|
const DamerauLevenshteinEditDistancePolicy daemaruLevenshtein(
|
|
|
|
before, beforeLength, after, afterLength);
|
|
|
|
return static_cast<int>(EditDistance::getEditDistance(&daemaruLevenshtein));
|
2012-01-12 09:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// In dictionary.cpp, getSuggestion() method,
|
2013-02-23 00:34:01 +00:00
|
|
|
// When USE_SUGGEST_INTERFACE_FOR_TYPING is true:
|
|
|
|
// SUGGEST_INTERFACE_OUTPUT_SCALE was multiplied to the original suggestion scores to convert
|
|
|
|
// them to integers.
|
|
|
|
// score = (int)((original score) * SUGGEST_INTERFACE_OUTPUT_SCALE)
|
|
|
|
// Undo the scaling here to recover the original score.
|
|
|
|
// normalizedScore = ((float)score) / SUGGEST_INTERFACE_OUTPUT_SCALE
|
|
|
|
// Otherwise: suggestion scores are computed using the below formula.
|
2012-01-12 09:44:40 +00:00
|
|
|
// original score
|
2012-08-12 02:10:48 +00:00
|
|
|
// := powf(mTypedLetterMultiplier (this is defined 2),
|
2012-01-12 09:44:40 +00:00
|
|
|
// (the number of matched characters between typed word and suggested word))
|
|
|
|
// * (individual word's score which defined in the unigram dictionary,
|
|
|
|
// and this score is defined in range [0, 255].)
|
|
|
|
// Then, the following processing is applied.
|
|
|
|
// - If the dictionary word is matched up to the point of the user entry
|
|
|
|
// (full match up to min(before.length(), after.length())
|
|
|
|
// => Then multiply by FULL_MATCHED_WORDS_PROMOTION_RATE (this is defined 1.2)
|
|
|
|
// - If the word is a true full match except for differences in accents or
|
|
|
|
// capitalization, then treat it as if the score was 255.
|
|
|
|
// - If before.length() == after.length()
|
|
|
|
// => multiply by mFullWordMultiplier (this is defined 2))
|
2012-08-12 02:10:48 +00:00
|
|
|
// So, maximum original score is powf(2, min(before.length(), after.length())) * 255 * 2 * 1.2
|
2012-01-12 09:44:40 +00:00
|
|
|
// For historical reasons we ignore the 1.2 modifier (because the measure for a good
|
|
|
|
// autocorrection threshold was done at a time when it didn't exist). This doesn't change
|
|
|
|
// the result.
|
2012-08-12 02:10:48 +00:00
|
|
|
// So, we can normalize original score by dividing powf(2, min(b.l(),a.l())) * 255 * 2.
|
2012-01-12 09:44:40 +00:00
|
|
|
|
2013-01-08 08:23:43 +00:00
|
|
|
/* static */ float Correction::RankingAlgorithm::calcNormalizedScore(const int *before,
|
|
|
|
const int beforeLength, const int *after, const int afterLength, const int score) {
|
2012-01-12 09:44:40 +00:00
|
|
|
if (0 == beforeLength || 0 == afterLength) {
|
2012-12-11 14:38:32 +00:00
|
|
|
return 0.0f;
|
2012-01-12 09:44:40 +00:00
|
|
|
}
|
|
|
|
const int distance = editDistance(before, beforeLength, after, afterLength);
|
|
|
|
int spaceCount = 0;
|
|
|
|
for (int i = 0; i < afterLength; ++i) {
|
2012-10-08 02:46:14 +00:00
|
|
|
if (after[i] == KEYCODE_SPACE) {
|
2012-01-12 09:44:40 +00:00
|
|
|
++spaceCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (spaceCount == afterLength) {
|
2012-12-11 14:38:32 +00:00
|
|
|
return 0.0f;
|
2012-01-12 09:44:40 +00:00
|
|
|
}
|
|
|
|
|
2013-02-23 00:34:01 +00:00
|
|
|
// add a weight based on edit distance.
|
|
|
|
// distance <= max(afterLength, beforeLength) == afterLength,
|
|
|
|
// so, 0 <= distance / afterLength <= 1
|
|
|
|
const float weight = 1.0f - static_cast<float>(distance) / static_cast<float>(afterLength);
|
|
|
|
|
|
|
|
if (USE_SUGGEST_INTERFACE_FOR_TYPING) {
|
|
|
|
return (static_cast<float>(score) / SUGGEST_INTERFACE_OUTPUT_SCALE) * weight;
|
|
|
|
}
|
2012-09-04 03:49:46 +00:00
|
|
|
const float maxScore = score >= S_INT_MAX ? static_cast<float>(S_INT_MAX)
|
|
|
|
: static_cast<float>(MAX_INITIAL_SCORE)
|
|
|
|
* powf(static_cast<float>(TYPED_LETTER_MULTIPLIER),
|
|
|
|
static_cast<float>(min(beforeLength, afterLength - spaceCount)))
|
|
|
|
* static_cast<float>(FULL_WORD_MULTIPLIER);
|
2012-01-12 09:44:40 +00:00
|
|
|
|
2012-09-04 03:49:46 +00:00
|
|
|
return (static_cast<float>(score) / maxScore) * weight;
|
2012-01-12 09:44:40 +00:00
|
|
|
}
|
2011-07-15 04:49:00 +00:00
|
|
|
} // namespace latinime
|