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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2011-08-10 05:30:10 +00:00
|
|
|
#define LOG_TAG "LatinIME: correction.cpp"
|
2011-07-15 04:49:00 +00:00
|
|
|
|
2011-08-10 05:30:10 +00:00
|
|
|
#include "correction.h"
|
2011-08-11 16:05:27 +00:00
|
|
|
#include "dictionary.h"
|
2011-08-01 10:35:27 +00:00
|
|
|
#include "proximity_info.h"
|
2011-07-15 04:49:00 +00:00
|
|
|
|
|
|
|
namespace latinime {
|
|
|
|
|
2011-08-04 09:31:57 +00:00
|
|
|
//////////////////////
|
|
|
|
// inline functions //
|
|
|
|
//////////////////////
|
|
|
|
static const char QUOTE = '\'';
|
|
|
|
|
2011-08-10 05:30:10 +00:00
|
|
|
inline bool Correction::isQuote(const unsigned short c) {
|
2011-08-04 09:31:57 +00:00
|
|
|
const unsigned short userTypedChar = mProximityInfo->getPrimaryCharAt(mInputIndex);
|
2011-08-05 12:21:01 +00:00
|
|
|
return (c == QUOTE && userTypedChar != QUOTE);
|
2011-08-04 09:31:57 +00:00
|
|
|
}
|
|
|
|
|
2011-08-10 05:30:10 +00:00
|
|
|
////////////////
|
|
|
|
// Correction //
|
|
|
|
////////////////
|
2011-08-04 09:31:57 +00:00
|
|
|
|
2011-08-10 05:30:10 +00:00
|
|
|
Correction::Correction(const int typedLetterMultiplier, const int fullWordMultiplier)
|
2011-08-01 10:35:27 +00:00
|
|
|
: TYPED_LETTER_MULTIPLIER(typedLetterMultiplier), FULL_WORD_MULTIPLIER(fullWordMultiplier) {
|
2011-07-15 04:49:00 +00:00
|
|
|
}
|
|
|
|
|
2011-08-10 05:30:10 +00:00
|
|
|
void Correction::initCorrection(const ProximityInfo *pi, const int inputLength,
|
2011-08-04 09:31:57 +00:00
|
|
|
const int maxDepth) {
|
2011-07-15 04:49:00 +00:00
|
|
|
mProximityInfo = pi;
|
2011-08-01 10:35:27 +00:00
|
|
|
mInputLength = inputLength;
|
2011-08-04 09:31:57 +00:00
|
|
|
mMaxDepth = maxDepth;
|
|
|
|
mMaxEditDistance = mInputLength < 5 ? 2 : mInputLength / 2;
|
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,
|
|
|
|
const bool useFullEditDistance) {
|
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-07-15 04:49:00 +00:00
|
|
|
}
|
|
|
|
|
2011-08-10 05:30:10 +00:00
|
|
|
void Correction::checkState() {
|
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;
|
|
|
|
// TODO: remove this assert
|
|
|
|
assert(inputCount <= 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-10 05:30:10 +00:00
|
|
|
int Correction::getFreqForSplitTwoWords(const int firstFreq, const int secondFreq) {
|
|
|
|
return Correction::RankingAlgorithm::calcFreqForSplitTwoWords(firstFreq, secondFreq, this);
|
2011-08-01 10:35:27 +00:00
|
|
|
}
|
|
|
|
|
2011-08-10 05:30:10 +00:00
|
|
|
int Correction::getFinalFreq(const int freq, unsigned short **word, int *wordLength) {
|
2011-08-05 12:21:01 +00:00
|
|
|
const int outputIndex = mTerminalOutputIndex;
|
|
|
|
const int inputIndex = mTerminalInputIndex;
|
2011-08-04 09:31:57 +00:00
|
|
|
*wordLength = outputIndex + 1;
|
|
|
|
if (mProximityInfo->sameAsTyped(mWord, outputIndex + 1) || outputIndex < MIN_SUGGEST_DEPTH) {
|
2011-08-03 14:27:32 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2011-08-10 13:19:33 +00:00
|
|
|
|
2011-08-04 09:31:57 +00:00
|
|
|
*word = mWord;
|
2011-08-10 05:30:10 +00:00
|
|
|
return Correction::RankingAlgorithm::calculateFinalFreq(
|
2011-08-15 13:30:33 +00:00
|
|
|
inputIndex, outputIndex, freq, mEditDistanceTable, this);
|
2011-08-02 17:19:44 +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-09-28 03:59:43 +00:00
|
|
|
mEquivalentCharStrongCount = mCorrectionStates[outputIndex].mEquivalentCharStrongCount;
|
|
|
|
mEquivalentCharNormalCount = mCorrectionStates[outputIndex].mEquivalentCharNormalCount;
|
|
|
|
mEquivalentCharWeakCount = mCorrectionStates[outputIndex].mEquivalentCharWeakCount;
|
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;
|
|
|
|
mTransposing = false;
|
|
|
|
mExceeding = false;
|
|
|
|
mSkipping = false;
|
|
|
|
|
2011-08-10 06:44:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Correction::goDownTree(
|
|
|
|
const int parentIndex, const int childCount, const int firstChildPos) {
|
|
|
|
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
|
2011-08-10 05:30:10 +00:00
|
|
|
int Correction::getOutputIndex() {
|
2011-08-03 14:27:32 +00:00
|
|
|
return mOutputIndex;
|
2011-08-02 17:19:44 +00:00
|
|
|
}
|
|
|
|
|
2011-08-03 14:27:32 +00:00
|
|
|
// TODO: remove
|
2011-08-10 05:30:10 +00:00
|
|
|
int Correction::getInputIndex() {
|
2011-08-03 14:27:32 +00:00
|
|
|
return mInputIndex;
|
2011-08-02 17:19:44 +00:00
|
|
|
}
|
|
|
|
|
2011-08-04 09:31:57 +00:00
|
|
|
// TODO: remove
|
2011-08-10 13:19:33 +00:00
|
|
|
bool Correction::needsToTraverseAllNodes() {
|
|
|
|
return mNeedsToTraverseAllNodes;
|
2011-08-04 09:31:57 +00:00
|
|
|
}
|
|
|
|
|
2011-08-10 05:30:10 +00:00
|
|
|
void Correction::incrementInputIndex() {
|
2011-08-03 14:27:32 +00:00
|
|
|
++mInputIndex;
|
|
|
|
}
|
|
|
|
|
2011-08-10 05:30:10 +00:00
|
|
|
void Correction::incrementOutputIndex() {
|
2011-08-03 14:27:32 +00:00
|
|
|
++mOutputIndex;
|
2011-08-10 06:44:08 +00:00
|
|
|
mCorrectionStates[mOutputIndex].mParentIndex = mCorrectionStates[mOutputIndex - 1].mParentIndex;
|
|
|
|
mCorrectionStates[mOutputIndex].mChildCount = mCorrectionStates[mOutputIndex - 1].mChildCount;
|
|
|
|
mCorrectionStates[mOutputIndex].mSiblingPos = mCorrectionStates[mOutputIndex - 1].mSiblingPos;
|
|
|
|
mCorrectionStates[mOutputIndex].mInputIndex = mInputIndex;
|
2011-08-10 13:19:33 +00:00
|
|
|
mCorrectionStates[mOutputIndex].mNeedsToTraverseAllNodes = mNeedsToTraverseAllNodes;
|
2011-08-17 08:55:16 +00:00
|
|
|
|
2011-09-28 03:59:43 +00:00
|
|
|
mCorrectionStates[mOutputIndex].mEquivalentCharStrongCount = mEquivalentCharStrongCount;
|
|
|
|
mCorrectionStates[mOutputIndex].mEquivalentCharNormalCount = mEquivalentCharNormalCount;
|
|
|
|
mCorrectionStates[mOutputIndex].mEquivalentCharWeakCount = mEquivalentCharWeakCount;
|
2011-08-11 12:25:39 +00:00
|
|
|
mCorrectionStates[mOutputIndex].mProximityCount = mProximityCount;
|
2011-08-17 08:55:16 +00:00
|
|
|
mCorrectionStates[mOutputIndex].mTransposedCount = mTransposedCount;
|
|
|
|
mCorrectionStates[mOutputIndex].mExcessiveCount = mExcessiveCount;
|
2011-08-10 13:19:33 +00:00
|
|
|
mCorrectionStates[mOutputIndex].mSkippedCount = mSkippedCount;
|
2011-08-17 08:55:16 +00:00
|
|
|
|
2011-08-11 07:27:28 +00:00
|
|
|
mCorrectionStates[mOutputIndex].mSkipPos = mSkipPos;
|
2011-08-17 08:55:16 +00:00
|
|
|
mCorrectionStates[mOutputIndex].mTransposedPos = mTransposedPos;
|
|
|
|
mCorrectionStates[mOutputIndex].mExcessivePos = mExcessivePos;
|
|
|
|
|
|
|
|
mCorrectionStates[mOutputIndex].mLastCharExceeded = mLastCharExceeded;
|
|
|
|
|
2011-08-10 13:19:33 +00:00
|
|
|
mCorrectionStates[mOutputIndex].mMatching = mMatching;
|
2011-08-11 16:05:27 +00:00
|
|
|
mCorrectionStates[mOutputIndex].mProximityMatching = mProximityMatching;
|
2011-08-17 08:55:16 +00:00
|
|
|
mCorrectionStates[mOutputIndex].mTransposing = mTransposing;
|
|
|
|
mCorrectionStates[mOutputIndex].mExceeding = mExceeding;
|
|
|
|
mCorrectionStates[mOutputIndex].mSkipping = mSkipping;
|
2011-08-01 10:35:27 +00:00
|
|
|
}
|
|
|
|
|
2011-08-10 13:19:33 +00:00
|
|
|
void Correction::startToTraverseAllNodes() {
|
|
|
|
mNeedsToTraverseAllNodes = true;
|
2011-08-04 09:31:57 +00:00
|
|
|
}
|
|
|
|
|
2011-08-10 05:30:10 +00:00
|
|
|
bool Correction::needsToPrune() const {
|
2011-08-19 13:05:59 +00:00
|
|
|
return mOutputIndex - 1 >= mMaxDepth || mProximityCount > mMaxEditDistance;
|
2011-08-04 09:31:57 +00:00
|
|
|
}
|
|
|
|
|
2011-08-19 13:05:59 +00:00
|
|
|
// TODO: inline?
|
2011-08-10 05:30:10 +00:00
|
|
|
Correction::CorrectionType Correction::processSkipChar(
|
2011-08-19 13:05:59 +00:00
|
|
|
const int32_t c, const bool isTerminal, const bool inputIndexIncremented) {
|
2011-08-05 12:21:01 +00:00
|
|
|
mWord[mOutputIndex] = c;
|
2011-08-10 13:19:33 +00:00
|
|
|
if (needsToTraverseAllNodes() && isTerminal) {
|
2011-08-19 13:05:59 +00:00
|
|
|
mTerminalInputIndex = mInputIndex - (inputIndexIncremented ? 1 : 0);
|
2011-08-05 12:21:01 +00:00
|
|
|
mTerminalOutputIndex = mOutputIndex;
|
|
|
|
incrementOutputIndex();
|
|
|
|
return TRAVERSE_ALL_ON_TERMINAL;
|
|
|
|
} else {
|
|
|
|
incrementOutputIndex();
|
|
|
|
return TRAVERSE_ALL_NOT_ON_TERMINAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 03:59:43 +00:00
|
|
|
inline bool isEquivalentChar(ProximityInfo::ProximityType type) {
|
|
|
|
// 'type ProximityInfo::EQUIVALENT_CHAR_WEAK' means that
|
|
|
|
// type == ..._WEAK or type == ..._NORMAL or type == ..._STRONG.
|
|
|
|
return type <= ProximityInfo::EQUIVALENT_CHAR_WEAK;
|
|
|
|
}
|
|
|
|
|
2011-08-10 05:30:10 +00:00
|
|
|
Correction::CorrectionType Correction::processCharAndCalcState(
|
2011-08-04 09:31:57 +00:00
|
|
|
const int32_t c, const bool isTerminal) {
|
2011-08-24 07:30:36 +00:00
|
|
|
const int correctionCount = (mSkippedCount + mExcessiveCount + mTransposedCount);
|
|
|
|
// TODO: Change the limit if we'll allow two or more corrections
|
|
|
|
const bool noCorrectionsHappenedSoFar = correctionCount == 0;
|
|
|
|
const bool canTryCorrection = noCorrectionsHappenedSoFar;
|
2011-08-19 08:27:46 +00:00
|
|
|
|
|
|
|
if (mNeedsToTraverseAllNodes || isQuote(c)) {
|
2011-08-19 13:05:59 +00:00
|
|
|
bool incremented = false;
|
|
|
|
if (mLastCharExceeded && mInputIndex == mInputLength - 1) {
|
|
|
|
// TODO: Do not check the proximity if EditDistance exceeds the threshold
|
2011-09-28 03:59:43 +00:00
|
|
|
const ProximityInfo::ProximityType matchId =
|
|
|
|
mProximityInfo->getMatchedProximityId(mInputIndex, c, true);
|
|
|
|
if (isEquivalentChar(matchId)) {
|
2011-08-19 13:05:59 +00:00
|
|
|
mLastCharExceeded = false;
|
|
|
|
--mExcessiveCount;
|
|
|
|
} else if (matchId == ProximityInfo::NEAR_PROXIMITY_CHAR) {
|
|
|
|
mLastCharExceeded = false;
|
|
|
|
--mExcessiveCount;
|
|
|
|
++mProximityCount;
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
if (mExcessivePos < mInputLength - 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) {
|
|
|
|
assert(mSkipPos == mOutputIndex - 1);
|
|
|
|
}
|
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;
|
|
|
|
}
|
2011-08-17 08:55:16 +00:00
|
|
|
if (mTransposedPos < mInputLength - 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) {
|
2011-09-28 03:59:43 +00:00
|
|
|
if (isEquivalentChar(mProximityInfo->getMatchedProximityId(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;
|
2011-08-19 13:05:59 +00:00
|
|
|
if (DEBUG_CORRECTION) {
|
|
|
|
DUMP_WORD(mWord, mOutputIndex);
|
|
|
|
LOGI("UNRELATED(0): %d, %d, %d, %d, %c", mProximityCount, mSkippedCount,
|
|
|
|
mTransposedCount, mExcessiveCount, c);
|
|
|
|
}
|
2011-08-19 08:27:46 +00:00
|
|
|
return UNRELATED;
|
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
|
|
|
|
const bool checkProximityChars = noCorrectionsHappenedSoFar || mProximityCount == 0;
|
2011-09-28 03:59:43 +00:00
|
|
|
const ProximityInfo::ProximityType matchedProximityCharId = secondTransposing
|
|
|
|
? ProximityInfo::EQUIVALENT_CHAR_NORMAL
|
2011-08-19 08:27:46 +00:00
|
|
|
: mProximityInfo->getMatchedProximityId(mInputIndex, c, checkProximityChars);
|
|
|
|
|
|
|
|
if (ProximityInfo::UNRELATED_CHAR == matchedProximityCharId) {
|
2011-08-19 13:05: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.
|
2011-08-24 07:30:36 +00:00
|
|
|
if (canTryCorrection && mCorrectionStates[mOutputIndex].mProximityMatching
|
2011-08-19 13:05:59 +00:00
|
|
|
&& mCorrectionStates[mOutputIndex].mExceeding
|
2011-09-28 03:59:43 +00:00
|
|
|
&& isEquivalentChar(mProximityInfo->getMatchedProximityId(
|
|
|
|
mInputIndex, mWord[mOutputIndex], false))) {
|
2011-08-24 07:30:36 +00:00
|
|
|
// Conversion p->e
|
2011-08-19 13:05:59 +00:00
|
|
|
++mExcessiveCount;
|
|
|
|
--mProximityCount;
|
2011-08-24 07:30:36 +00:00
|
|
|
} else if (mInputIndex < mInputLength - 1 && mOutputIndex > 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(mProximityInfo->getMatchedProximityId(
|
|
|
|
mInputIndex, mWord[mOutputIndex - 1], false))
|
|
|
|
&& isEquivalentChar(
|
|
|
|
mProximityInfo->getMatchedProximityId(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(
|
|
|
|
mProximityInfo->getMatchedProximityId(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(
|
|
|
|
mProximityInfo->getMatchedProximityId(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);
|
|
|
|
} else if ((mExceeding || mTransposing) && mInputIndex - 1 < mInputLength
|
2011-09-28 03:59:43 +00:00
|
|
|
&& isEquivalentChar(
|
|
|
|
mProximityInfo->getMatchedProximityId(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
|
|
|
}
|
2011-08-24 07:30:36 +00:00
|
|
|
} else if (mSkipping) {
|
|
|
|
// 3. Skip correction
|
2011-08-19 08:27:46 +00:00
|
|
|
++mSkippedCount;
|
2011-08-19 13:05:59 +00:00
|
|
|
return processSkipChar(c, isTerminal, false);
|
2011-08-19 08:27:46 +00:00
|
|
|
} else {
|
2011-08-19 13:05:59 +00:00
|
|
|
if (DEBUG_CORRECTION) {
|
|
|
|
DUMP_WORD(mWord, mOutputIndex);
|
|
|
|
LOGI("UNRELATED(1): %d, %d, %d, %d, %c", mProximityCount, mSkippedCount,
|
|
|
|
mTransposedCount, mExcessiveCount, c);
|
|
|
|
}
|
2011-08-19 08:27:46 +00:00
|
|
|
return UNRELATED;
|
2011-08-04 09:31:57 +00:00
|
|
|
}
|
2011-09-28 03:59:43 +00:00
|
|
|
} else if (secondTransposing) {
|
2011-08-19 08:27:46 +00:00
|
|
|
// If inputIndex is greater than mInputLength, that means there is no
|
|
|
|
// 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;
|
|
|
|
switch (matchedProximityCharId) {
|
|
|
|
case ProximityInfo::EQUIVALENT_CHAR_STRONG:
|
|
|
|
++mEquivalentCharStrongCount;
|
|
|
|
break;
|
|
|
|
case ProximityInfo::EQUIVALENT_CHAR_NORMAL:
|
|
|
|
++mEquivalentCharNormalCount;
|
|
|
|
break;
|
|
|
|
case ProximityInfo::EQUIVALENT_CHAR_WEAK:
|
|
|
|
++mEquivalentCharWeakCount;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
}
|
2011-08-19 08:27:46 +00:00
|
|
|
} else if (ProximityInfo::NEAR_PROXIMITY_CHAR == matchedProximityCharId) {
|
|
|
|
mProximityMatching = true;
|
2011-09-28 03:59:43 +00:00
|
|
|
++mProximityCount;
|
2011-08-19 08:27:46 +00:00
|
|
|
}
|
2011-08-04 09:31:57 +00:00
|
|
|
|
2011-08-19 08:27:46 +00:00
|
|
|
mWord[mOutputIndex] = 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
|
|
|
|
&& mProximityCount == 0 && (mInputIndex == mInputLength - 2);
|
2011-08-19 08:27:46 +00:00
|
|
|
const bool isSameAsUserTypedLength = (mInputLength == mInputIndex + 1) || mLastCharExceeded;
|
|
|
|
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 =
|
|
|
|
mExceeding && mInputIndex == mInputLength - 2;
|
|
|
|
|
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;
|
|
|
|
return ON_TERMINAL;
|
|
|
|
} else {
|
|
|
|
return NOT_ON_TERMINAL;
|
|
|
|
}
|
2011-08-04 09:31:57 +00:00
|
|
|
}
|
|
|
|
|
2011-08-10 05:30:10 +00:00
|
|
|
Correction::~Correction() {
|
2011-07-15 04:49:00 +00:00
|
|
|
}
|
|
|
|
|
2011-08-01 10:35:27 +00:00
|
|
|
/////////////////////////
|
|
|
|
// 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 == 2) {
|
|
|
|
*base = TWO_31ST_DIV_2 >= temp ? temp << 1 : S_INT_MAX;
|
|
|
|
} else {
|
2011-09-14 07:09:24 +00:00
|
|
|
// TODO: This overflow check gives a wrong answer when, for example,
|
|
|
|
// temp = 2^16 + 1 and multiplier = 2^17 + 1.
|
|
|
|
// Fix this behavior.
|
2011-08-01 10:35:27 +00:00
|
|
|
const int tempRetval = temp * multiplier;
|
|
|
|
*base = tempRetval >= temp ? tempRetval : S_INT_MAX;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline static int powerIntCapped(const int base, const int n) {
|
2011-09-29 09:36:56 +00:00
|
|
|
if (n <= 0) return 1;
|
2011-08-01 10:35:27 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-15 13:30:33 +00:00
|
|
|
inline static int getQuoteCount(const unsigned short* word, const int length) {
|
|
|
|
int quoteCount = 0;
|
|
|
|
for (int i = 0; i < length; ++i) {
|
|
|
|
if(word[i] == '\'') {
|
|
|
|
++quoteCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return quoteCount;
|
|
|
|
}
|
|
|
|
|
2011-08-11 16:05:27 +00:00
|
|
|
/* static */
|
|
|
|
inline static int editDistance(
|
|
|
|
int* editDistanceTable, const unsigned short* input,
|
|
|
|
const int inputLength, const unsigned short* output, const int outputLength) {
|
|
|
|
// dp[li][lo] dp[a][b] = dp[ a * lo + b]
|
|
|
|
int* dp = editDistanceTable;
|
|
|
|
const int li = inputLength + 1;
|
|
|
|
const int lo = outputLength + 1;
|
|
|
|
for (int i = 0; i < li; ++i) {
|
|
|
|
dp[lo * i] = i;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < lo; ++i) {
|
|
|
|
dp[i] = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < li - 1; ++i) {
|
|
|
|
for (int j = 0; j < lo - 1; ++j) {
|
|
|
|
const uint32_t ci = Dictionary::toBaseLowerCase(input[i]);
|
|
|
|
const uint32_t co = Dictionary::toBaseLowerCase(output[j]);
|
|
|
|
const uint16_t cost = (ci == co) ? 0 : 1;
|
|
|
|
dp[(i + 1) * lo + (j + 1)] = min(dp[i * lo + (j + 1)] + 1,
|
|
|
|
min(dp[(i + 1) * lo + j] + 1, dp[i * lo + j] + cost));
|
2011-08-26 10:30:56 +00:00
|
|
|
if (i > 0 && j > 0 && ci == Dictionary::toBaseLowerCase(output[j - 1])
|
2011-08-11 16:05:27 +00:00
|
|
|
&& co == Dictionary::toBaseLowerCase(input[i - 1])) {
|
|
|
|
dp[(i + 1) * lo + (j + 1)] = min(
|
|
|
|
dp[(i + 1) * lo + (j + 1)], dp[(i - 1) * lo + (j - 1)] + cost);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DEBUG_EDIT_DISTANCE) {
|
|
|
|
LOGI("IN = %d, OUT = %d", inputLength, outputLength);
|
|
|
|
for (int i = 0; i < li; ++i) {
|
|
|
|
for (int j = 0; j < lo; ++j) {
|
|
|
|
LOGI("EDIT[%d][%d], %d", i, j, dp[i * lo + j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dp[li * lo - 1];
|
|
|
|
}
|
|
|
|
|
2011-08-01 10:35:27 +00:00
|
|
|
//////////////////////
|
|
|
|
// RankingAlgorithm //
|
|
|
|
//////////////////////
|
|
|
|
|
2011-08-10 13:19:33 +00:00
|
|
|
/* static */
|
2011-08-11 12:25:39 +00:00
|
|
|
int Correction::RankingAlgorithm::calculateFinalFreq(const int inputIndex, const int outputIndex,
|
2011-08-15 13:30:33 +00:00
|
|
|
const int freq, int* editDistanceTable, const Correction* correction) {
|
2011-08-10 05:30:10 +00:00
|
|
|
const int excessivePos = correction->getExcessivePos();
|
|
|
|
const int inputLength = correction->mInputLength;
|
|
|
|
const int typedLetterMultiplier = correction->TYPED_LETTER_MULTIPLIER;
|
|
|
|
const int fullWordMultiplier = correction->FULL_WORD_MULTIPLIER;
|
|
|
|
const ProximityInfo *proximityInfo = correction->mProximityInfo;
|
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-09-30 09:08:28 +00:00
|
|
|
const int equivalentCharStrongCount = correction->mEquivalentCharStrongCount;
|
|
|
|
const int equivalentCharWeakCount = correction->mEquivalentCharWeakCount;
|
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;
|
2011-08-17 08:55:16 +00:00
|
|
|
if (skippedCount >= inputLength || inputLength == 0) {
|
2011-08-15 13:30:33 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-08-19 13:05:59 +00:00
|
|
|
// TODO: find more robust way
|
|
|
|
bool sameLength = lastCharExceeded ? (inputLength == inputIndex + 2)
|
2011-08-17 08:55:16 +00:00
|
|
|
: (inputLength == inputIndex + 1);
|
2011-08-11 12:25:39 +00:00
|
|
|
|
|
|
|
// TODO: use mExcessiveCount
|
2011-08-19 13:05:59 +00:00
|
|
|
const int matchCount = inputLength - correction->mProximityCount - excessiveCount;
|
2011-08-11 12:25:39 +00:00
|
|
|
|
2011-08-10 13:19:33 +00:00
|
|
|
const unsigned short* word = correction->mWord;
|
2011-08-17 08:55:16 +00:00
|
|
|
const bool skipped = skippedCount > 0;
|
2011-08-11 16:05:27 +00:00
|
|
|
|
2011-08-15 13:30:33 +00:00
|
|
|
const int quoteDiffCount = max(0, getQuoteCount(word, outputIndex + 1)
|
|
|
|
- getQuoteCount(proximityInfo->getPrimaryInputWord(), inputLength));
|
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-08-19 13:05:59 +00:00
|
|
|
int adjustedProximityMatchedCount = proximityMatchedCount;
|
|
|
|
|
|
|
|
int finalFreq = freq;
|
2011-08-17 08:55:16 +00:00
|
|
|
|
|
|
|
// TODO: Optimize this.
|
2011-08-19 13:05:59 +00:00
|
|
|
// TODO: Ignoring edit distance for transposed char, for now
|
|
|
|
if (transposedCount == 0 && (proximityMatchedCount > 0 || skipped || excessiveCount > 0)) {
|
2011-08-15 13:30:33 +00:00
|
|
|
const unsigned short* primaryInputWord = proximityInfo->getPrimaryInputWord();
|
|
|
|
ed = editDistance(editDistanceTable, primaryInputWord,
|
2011-08-11 16:05:27 +00:00
|
|
|
inputLength, word, outputIndex + 1);
|
2011-08-19 13:05:59 +00:00
|
|
|
const int matchWeight = powerIntCapped(typedLetterMultiplier,
|
|
|
|
max(inputLength, outputIndex + 1) - ed);
|
|
|
|
multiplyIntCapped(matchWeight, &finalFreq);
|
|
|
|
|
|
|
|
// TODO: Demote further if there are two or more excessive chars with longer user input?
|
|
|
|
if (inputLength > outputIndex + 1) {
|
|
|
|
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);
|
2011-08-19 13:05:59 +00:00
|
|
|
|
|
|
|
if (ed == 1 && (inputLength == outputIndex || inputLength == outputIndex + 2)) {
|
|
|
|
// Promote a word with just one skipped or excessive char
|
|
|
|
if (sameLength) {
|
|
|
|
multiplyRate(WORDS_WITH_JUST_ONE_CORRECTION_PROMOTION_RATE, &finalFreq);
|
|
|
|
} else {
|
|
|
|
multiplyIntCapped(typedLetterMultiplier, &finalFreq);
|
|
|
|
}
|
|
|
|
} else if (ed == 0) {
|
|
|
|
multiplyIntCapped(typedLetterMultiplier, &finalFreq);
|
|
|
|
sameLength = true;
|
|
|
|
}
|
|
|
|
adjustedProximityMatchedCount = min(max(0, ed - (outputIndex + 1 - inputLength)),
|
2011-08-15 13:30:33 +00:00
|
|
|
proximityMatchedCount);
|
|
|
|
} else {
|
2011-08-19 13:05:59 +00:00
|
|
|
// TODO: Calculate the edit distance for transposed char
|
|
|
|
const int matchWeight = powerIntCapped(typedLetterMultiplier, matchCount);
|
|
|
|
multiplyIntCapped(matchWeight, &finalFreq);
|
2011-08-11 16:05:27 +00:00
|
|
|
}
|
|
|
|
|
2011-08-19 13:05:59 +00:00
|
|
|
if (proximityInfo->getMatchedProximityId(0, word[0], true)
|
|
|
|
== ProximityInfo::UNRELATED_CHAR) {
|
|
|
|
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
|
|
|
|
* (10 * inputLength - WORDS_WITH_MISSING_CHARACTER_DEMOTION_START_POS_10X)
|
|
|
|
/ (10 * inputLength
|
|
|
|
- WORDS_WITH_MISSING_CHARACTER_DEMOTION_START_POS_10X + 10);
|
|
|
|
if (DEBUG_DICT_FULL) {
|
|
|
|
LOGI("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);
|
2011-08-19 13:05:59 +00:00
|
|
|
if (!lastCharExceeded && !proximityInfo->existsAdjacentProximityChars(excessivePos)) {
|
|
|
|
if (DEBUG_CORRECTION_FREQ) {
|
|
|
|
LOGI("Double excessive demotion");
|
|
|
|
}
|
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
|
|
|
|
|
|
|
// Promotion for a word with proximity characters
|
2011-08-19 13:05:59 +00:00
|
|
|
for (int i = 0; i < adjustedProximityMatchedCount; ++i) {
|
2011-08-01 10:35:27 +00:00
|
|
|
// A word with proximity corrections
|
2011-08-15 13:30:33 +00:00
|
|
|
if (DEBUG_DICT_FULL) {
|
|
|
|
LOGI("Found a proximity correction.");
|
2011-08-01 10:35:27 +00:00
|
|
|
}
|
|
|
|
multiplyIntCapped(typedLetterMultiplier, &finalFreq);
|
|
|
|
multiplyRate(WORDS_WITH_PROXIMITY_CHARACTER_DEMOTION_RATE, &finalFreq);
|
|
|
|
}
|
2011-08-15 13:30:33 +00:00
|
|
|
|
2011-09-30 09:08:28 +00:00
|
|
|
for (int i = 0; i < equivalentCharStrongCount; ++i) {
|
|
|
|
if (DEBUG_DICT_FULL) {
|
|
|
|
LOGI("equivalent char strong");
|
|
|
|
}
|
|
|
|
multiplyRate(WORDS_WITH_EQUIVALENT_CHAR_STRONG_PROMOTION_RATE, &finalFreq);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < equivalentCharWeakCount; ++i) {
|
|
|
|
if (DEBUG_DICT_FULL) {
|
|
|
|
LOGI("equivalent char weak");
|
|
|
|
}
|
|
|
|
multiplyRate(WORDS_WITH_EQUIVALENT_CHAR_WEAK_DEMOTION_RATE, &finalFreq);
|
|
|
|
}
|
|
|
|
|
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(
|
|
|
|
100 - CORRECTION_COUNT_RATE_DEMOTION_RATE_BASE * errorCount / inputLength, &finalFreq);
|
|
|
|
|
|
|
|
// 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
|
2011-08-19 13:05:59 +00:00
|
|
|
if (sameLength && transposedCount == 0 && !skipped && excessiveCount == 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
|
2011-08-19 13:05:59 +00:00
|
|
|
if (proximityMatchedCount == 0 && transposedCount == 0 && !skipped && excessiveCount == 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
|
|
|
*/
|
2011-08-11 12:25:39 +00:00
|
|
|
if (matchCount == inputLength && 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);
|
|
|
|
}
|
|
|
|
|
2011-09-29 09:36:56 +00:00
|
|
|
if (useFullEditDistance && outputLength > inputLength + 1) {
|
|
|
|
const int diff = outputLength - inputLength - 1;
|
|
|
|
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) {
|
|
|
|
LOGI("calc: %d, %d", outputIndex, sameLength);
|
|
|
|
}
|
|
|
|
|
2011-08-19 13:05:59 +00:00
|
|
|
if (DEBUG_CORRECTION_FREQ) {
|
|
|
|
DUMP_WORD(correction->mWord, outputIndex + 1);
|
|
|
|
LOGI("FinalFreq: [P%d, S%d, T%d, E%d] %d, %d, %d, %d, %d", proximityMatchedCount,
|
|
|
|
skippedCount, transposedCount, excessiveCount, lastCharExceeded, sameLength,
|
|
|
|
quoteDiffCount, ed, finalFreq);
|
|
|
|
}
|
|
|
|
|
2011-08-01 10:35:27 +00:00
|
|
|
return finalFreq;
|
|
|
|
}
|
|
|
|
|
2011-08-10 13:19:33 +00:00
|
|
|
/* static */
|
2011-08-10 05:30:10 +00:00
|
|
|
int Correction::RankingAlgorithm::calcFreqForSplitTwoWords(
|
|
|
|
const int firstFreq, const int secondFreq, const Correction* correction) {
|
|
|
|
const int spaceProximityPos = correction->mSpaceProximityPos;
|
|
|
|
const int missingSpacePos = correction->mMissingSpacePos;
|
2011-08-01 10:35:27 +00:00
|
|
|
if (DEBUG_DICT) {
|
|
|
|
int inputCount = 0;
|
|
|
|
if (spaceProximityPos >= 0) ++inputCount;
|
|
|
|
if (missingSpacePos >= 0) ++inputCount;
|
|
|
|
assert(inputCount <= 1);
|
|
|
|
}
|
|
|
|
const bool isSpaceProximity = spaceProximityPos >= 0;
|
2011-08-10 05:30:10 +00:00
|
|
|
const int inputLength = correction->mInputLength;
|
2011-08-01 10:35:27 +00:00
|
|
|
const int firstWordLength = isSpaceProximity ? spaceProximityPos : missingSpacePos;
|
|
|
|
const int secondWordLength = isSpaceProximity
|
|
|
|
? (inputLength - spaceProximityPos - 1)
|
|
|
|
: (inputLength - missingSpacePos);
|
2011-08-10 05:30:10 +00:00
|
|
|
const int typedLetterMultiplier = correction->TYPED_LETTER_MULTIPLIER;
|
2011-08-01 10:35:27 +00:00
|
|
|
|
|
|
|
if (firstWordLength == 0 || secondWordLength == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
const int firstDemotionRate = 100 - 100 / (firstWordLength + 1);
|
|
|
|
int tempFirstFreq = firstFreq;
|
|
|
|
multiplyRate(firstDemotionRate, &tempFirstFreq);
|
|
|
|
|
|
|
|
const int secondDemotionRate = 100 - 100 / (secondWordLength + 1);
|
|
|
|
int tempSecondFreq = secondFreq;
|
|
|
|
multiplyRate(secondDemotionRate, &tempSecondFreq);
|
|
|
|
|
|
|
|
const int totalLength = firstWordLength + secondWordLength;
|
|
|
|
|
|
|
|
// Promote pairFreq with multiplying by 2, because the word length is the same as the typed
|
|
|
|
// length.
|
|
|
|
int totalFreq = tempFirstFreq + tempSecondFreq;
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
LOGI("Found a word pair with space proximity correction.");
|
|
|
|
}
|
|
|
|
multiplyIntCapped(typedLetterMultiplier, &totalFreq);
|
|
|
|
multiplyRate(WORDS_WITH_PROXIMITY_CHARACTER_DEMOTION_RATE, &totalFreq);
|
|
|
|
}
|
|
|
|
|
|
|
|
multiplyRate(WORDS_WITH_MISSING_SPACE_CHARACTER_DEMOTION_RATE, &totalFreq);
|
|
|
|
return totalFreq;
|
|
|
|
}
|
|
|
|
|
2011-07-15 04:49:00 +00:00
|
|
|
} // namespace latinime
|