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
|
|
|
#ifndef LATINIME_CORRECTION_H
|
|
|
|
#define LATINIME_CORRECTION_H
|
2011-07-15 04:49:00 +00:00
|
|
|
|
2012-08-24 05:51:15 +00:00
|
|
|
#include <cstring> // for memset()
|
2011-07-15 04:49:00 +00:00
|
|
|
|
2012-06-08 06:29:44 +00:00
|
|
|
#include "correction_state.h"
|
2011-07-15 04:49:00 +00:00
|
|
|
#include "defines.h"
|
2012-06-08 06:29:44 +00:00
|
|
|
#include "proximity_info_state.h"
|
2011-07-15 04:49:00 +00:00
|
|
|
|
|
|
|
namespace latinime {
|
|
|
|
|
|
|
|
class ProximityInfo;
|
|
|
|
|
2011-08-10 05:30:10 +00:00
|
|
|
class Correction {
|
2012-01-06 03:24:38 +00:00
|
|
|
public:
|
2011-08-03 14:27:32 +00:00
|
|
|
typedef enum {
|
2011-08-04 09:31:57 +00:00
|
|
|
TRAVERSE_ALL_ON_TERMINAL,
|
|
|
|
TRAVERSE_ALL_NOT_ON_TERMINAL,
|
2011-08-03 14:27:32 +00:00
|
|
|
UNRELATED,
|
2011-08-04 09:31:57 +00:00
|
|
|
ON_TERMINAL,
|
|
|
|
NOT_ON_TERMINAL
|
2011-08-10 05:30:10 +00:00
|
|
|
} CorrectionType;
|
2011-08-03 14:27:32 +00:00
|
|
|
|
2012-08-24 05:51:15 +00:00
|
|
|
Correction()
|
|
|
|
: mProximityInfo(0), mUseFullEditDistance(false), mDoAutoCompletion(false),
|
|
|
|
mMaxEditDistance(0), mMaxDepth(0), mInputSize(0), mSpaceProximityPos(0),
|
|
|
|
mMissingSpacePos(0), mTerminalInputIndex(0), mTerminalOutputIndex(0), mMaxErrors(0),
|
|
|
|
mTotalTraverseCount(0), mNeedsToTraverseAllNodes(false), mOutputIndex(0),
|
|
|
|
mInputIndex(0), mEquivalentCharCount(0), mProximityCount(0), mExcessiveCount(0),
|
|
|
|
mTransposedCount(0), mSkippedCount(0), mTransposedPos(0), mExcessivePos(0),
|
|
|
|
mSkipPos(0), mLastCharExceeded(false), mMatching(false), mProximityMatching(false),
|
|
|
|
mAdditionalProximityMatching(false), mExceeding(false), mTransposing(false),
|
|
|
|
mSkipping(false), mProximityInfoState() {
|
|
|
|
memset(mWord, 0, sizeof(mWord));
|
|
|
|
memset(mDistances, 0, sizeof(mDistances));
|
|
|
|
memset(mEditDistanceTable, 0, sizeof(mEditDistanceTable));
|
|
|
|
// NOTE: mCorrectionStates is an array of instances.
|
|
|
|
// No need to initialize it explicitly here.
|
|
|
|
}
|
|
|
|
|
2012-11-02 17:50:47 +00:00
|
|
|
// Non virtual inline destructor -- never inherit this class
|
|
|
|
~Correction() {}
|
2012-05-30 08:28:34 +00:00
|
|
|
void resetCorrection();
|
2013-01-11 16:18:00 +00:00
|
|
|
void initCorrection(const ProximityInfo *pi, const int inputSize, const int maxDepth);
|
2011-08-10 06:44:08 +00:00
|
|
|
void initCorrectionState(const int rootPos, const int childCount, const bool traverseAll);
|
|
|
|
|
|
|
|
// TODO: remove
|
2011-08-01 10:35:27 +00:00
|
|
|
void setCorrectionParams(const int skipPos, const int excessivePos, const int transposedPos,
|
2011-12-14 12:38:11 +00:00
|
|
|
const int spaceProximityPos, const int missingSpacePos, const bool useFullEditDistance,
|
2011-12-15 05:53:19 +00:00
|
|
|
const bool doAutoCompletion, const int maxErrors);
|
2011-07-15 04:49:00 +00:00
|
|
|
void checkState();
|
2012-08-10 07:52:27 +00:00
|
|
|
bool sameAsTyped();
|
2011-08-10 06:44:08 +00:00
|
|
|
bool initProcessState(const int index);
|
|
|
|
|
2012-08-16 10:34:02 +00:00
|
|
|
int getInputIndex() const;
|
2011-08-04 09:31:57 +00:00
|
|
|
|
|
|
|
bool needsToPrune() const;
|
|
|
|
|
2012-05-30 08:28:34 +00:00
|
|
|
int pushAndGetTotalTraverseCount() {
|
|
|
|
return ++mTotalTraverseCount;
|
|
|
|
}
|
|
|
|
|
2012-10-29 09:06:22 +00:00
|
|
|
int getFreqForSplitMultipleWords(const int *freqArray, const int *wordLengthArray,
|
|
|
|
const int wordCount, const bool isSpaceProximity, const int *word);
|
|
|
|
int getFinalProbability(const int probability, int **word, int *wordLength);
|
|
|
|
int getFinalProbabilityForSubQueue(const int probability, int **word, int *wordLength,
|
|
|
|
const int inputSize);
|
2011-08-04 09:31:57 +00:00
|
|
|
|
2012-10-29 09:06:22 +00:00
|
|
|
CorrectionType processCharAndCalcState(const int c, const bool isTerminal);
|
2011-08-01 10:35:27 +00:00
|
|
|
|
2011-08-10 06:44:08 +00:00
|
|
|
/////////////////////////
|
|
|
|
// Tree helper methods
|
|
|
|
int goDownTree(const int parentIndex, const int childCount, const int firstChildPos);
|
|
|
|
|
|
|
|
inline int getTreeSiblingPos(const int index) const {
|
|
|
|
return mCorrectionStates[index].mSiblingPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void setTreeSiblingPos(const int index, const int pos) {
|
|
|
|
mCorrectionStates[index].mSiblingPos = pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int getTreeParentIndex(const int index) const {
|
|
|
|
return mCorrectionStates[index].mParentIndex;
|
|
|
|
}
|
2012-01-06 03:24:38 +00:00
|
|
|
|
2012-01-12 09:44:40 +00:00
|
|
|
class RankingAlgorithm {
|
|
|
|
public:
|
2012-04-23 09:51:00 +00:00
|
|
|
static int calculateFinalProbability(const int inputIndex, const int depth,
|
2012-07-25 08:51:43 +00:00
|
|
|
const int probability, int *editDistanceTable, const Correction *correction,
|
2012-08-23 06:46:43 +00:00
|
|
|
const int inputSize);
|
2012-01-30 09:18:30 +00:00
|
|
|
static int calcFreqForSplitMultipleWords(const int *freqArray, const int *wordLengthArray,
|
2012-07-25 08:51:43 +00:00
|
|
|
const int wordCount, const Correction *correction, const bool isSpaceProximity,
|
2012-10-29 09:06:22 +00:00
|
|
|
const int *word);
|
|
|
|
static float calcNormalizedScore(const int *before, const int beforeLength,
|
|
|
|
const int *after, const int afterLength, const int score);
|
|
|
|
static int editDistance(const int *before, const int beforeLength, const int *after,
|
|
|
|
const int afterLength);
|
2012-01-12 09:44:40 +00:00
|
|
|
private:
|
|
|
|
static const int MAX_INITIAL_SCORE = 255;
|
|
|
|
};
|
|
|
|
|
2012-06-08 06:29:44 +00:00
|
|
|
// proximity info state
|
2012-10-29 09:06:22 +00:00
|
|
|
void initInputParams(const ProximityInfo *proximityInfo, const int *inputCodes,
|
2012-08-23 06:46:43 +00:00
|
|
|
const int inputSize, const int *xCoordinates, const int *yCoordinates) {
|
|
|
|
mProximityInfoState.initInputParams(0, MAX_POINT_TO_KEY_LENGTH,
|
|
|
|
proximityInfo, inputCodes, inputSize, xCoordinates, yCoordinates, 0, 0, false);
|
2012-06-08 06:29:44 +00:00
|
|
|
}
|
|
|
|
|
2012-10-29 09:06:22 +00:00
|
|
|
const int *getPrimaryInputWord() const {
|
2012-06-08 06:29:44 +00:00
|
|
|
return mProximityInfoState.getPrimaryInputWord();
|
|
|
|
}
|
|
|
|
|
2012-10-29 09:06:22 +00:00
|
|
|
int getPrimaryCodePointAt(const int index) const {
|
|
|
|
return mProximityInfoState.getPrimaryCodePointAt(index);
|
2012-06-08 06:29:44 +00:00
|
|
|
}
|
|
|
|
|
2012-01-06 03:24:38 +00:00
|
|
|
private:
|
2012-06-14 18:25:50 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(Correction);
|
2012-08-16 10:34:02 +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;
|
2012-11-02 14:57:13 +00:00
|
|
|
AK_FORCE_INLINE static void multiplyIntCapped(const int multiplier, int *base) {
|
2012-08-16 10:34:02 +00:00
|
|
|
const int temp = *base;
|
|
|
|
if (temp != S_INT_MAX) {
|
|
|
|
// Branch if multiplier == 2 for the optimization
|
|
|
|
if (multiplier < 0) {
|
|
|
|
if (DEBUG_DICT) {
|
2013-01-09 06:21:44 +00:00
|
|
|
ASSERT(false);
|
2012-08-16 10:34:02 +00:00
|
|
|
}
|
|
|
|
AKLOGI("--- Invalid multiplier: %d", multiplier);
|
|
|
|
} else if (multiplier == 0) {
|
|
|
|
*base = 0;
|
|
|
|
} else if (multiplier == 2) {
|
|
|
|
*base = TWO_31ST_DIV_2 >= temp ? temp << 1 : S_INT_MAX;
|
|
|
|
} else {
|
|
|
|
// TODO: This overflow check gives a wrong answer when, for example,
|
|
|
|
// temp = 2^16 + 1 and multiplier = 2^17 + 1.
|
|
|
|
// Fix this behavior.
|
|
|
|
const int tempRetval = temp * multiplier;
|
|
|
|
*base = tempRetval >= temp ? tempRetval : S_INT_MAX;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-02 14:57:13 +00:00
|
|
|
AK_FORCE_INLINE static int powerIntCapped(const int base, const int n) {
|
|
|
|
if (n <= 0) return 1;
|
|
|
|
if (base == 2) {
|
|
|
|
return n < 31 ? 1 << n : S_INT_MAX;
|
|
|
|
} else {
|
|
|
|
int ret = base;
|
|
|
|
for (int i = 1; i < n; ++i) multiplyIntCapped(base, &ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AK_FORCE_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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-16 10:34:02 +00:00
|
|
|
|
|
|
|
inline int getSpaceProximityPos() const {
|
|
|
|
return mSpaceProximityPos;
|
|
|
|
}
|
|
|
|
inline int getMissingSpacePos() const {
|
|
|
|
return mMissingSpacePos;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int getSkipPos() const {
|
|
|
|
return mSkipPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int getExcessivePos() const {
|
|
|
|
return mExcessivePos;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int getTransposedPos() const {
|
|
|
|
return mTransposedPos;
|
|
|
|
}
|
|
|
|
|
2011-08-10 13:19:33 +00:00
|
|
|
inline void incrementInputIndex();
|
|
|
|
inline void incrementOutputIndex();
|
|
|
|
inline void startToTraverseAllNodes();
|
2012-10-29 09:06:22 +00:00
|
|
|
inline bool isSingleQuote(const int c);
|
2012-11-02 14:57:13 +00:00
|
|
|
inline CorrectionType processSkipChar(const int c, const bool isTerminal,
|
|
|
|
const bool inputIndexIncremented);
|
2012-01-16 07:21:21 +00:00
|
|
|
inline CorrectionType processUnrelatedCorrectionType();
|
2012-10-29 09:06:22 +00:00
|
|
|
inline void addCharToCurrentWord(const int c);
|
|
|
|
inline int getFinalProbabilityInternal(const int probability, int **word, int *wordLength,
|
|
|
|
const int inputSize);
|
2011-08-04 09:31:57 +00:00
|
|
|
|
2012-06-14 18:25:50 +00:00
|
|
|
static const int TYPED_LETTER_MULTIPLIER = 2;
|
|
|
|
static const int FULL_WORD_MULTIPLIER = 2;
|
2011-07-15 04:49:00 +00:00
|
|
|
const ProximityInfo *mProximityInfo;
|
2011-08-04 09:31:57 +00:00
|
|
|
|
2011-09-29 09:36:56 +00:00
|
|
|
bool mUseFullEditDistance;
|
2011-12-14 12:38:11 +00:00
|
|
|
bool mDoAutoCompletion;
|
2011-08-04 09:31:57 +00:00
|
|
|
int mMaxEditDistance;
|
|
|
|
int mMaxDepth;
|
2012-08-23 06:46:43 +00:00
|
|
|
int mInputSize;
|
2011-08-01 10:35:27 +00:00
|
|
|
int mSpaceProximityPos;
|
|
|
|
int mMissingSpacePos;
|
2011-08-05 12:21:01 +00:00
|
|
|
int mTerminalInputIndex;
|
|
|
|
int mTerminalOutputIndex;
|
2011-12-15 05:53:19 +00:00
|
|
|
int mMaxErrors;
|
2011-10-06 10:12:20 +00:00
|
|
|
|
2012-12-11 14:38:32 +00:00
|
|
|
int mTotalTraverseCount;
|
2012-05-30 08:28:34 +00:00
|
|
|
|
2011-10-06 10:12:20 +00:00
|
|
|
// The following arrays are state buffer.
|
2013-01-11 16:18:00 +00:00
|
|
|
int mWord[MAX_WORD_LENGTH];
|
|
|
|
int mDistances[MAX_WORD_LENGTH];
|
2011-10-06 10:12:20 +00:00
|
|
|
|
2011-09-16 02:28:54 +00:00
|
|
|
// Edit distance calculation requires a buffer with (N+1)^2 length for the input length N.
|
2011-08-11 16:05:27 +00:00
|
|
|
// Caveat: Do not create multiple tables per thread as this table eats up RAM a lot.
|
2013-01-11 16:18:00 +00:00
|
|
|
int mEditDistanceTable[(MAX_WORD_LENGTH + 1) * (MAX_WORD_LENGTH + 1)];
|
2011-08-04 09:31:57 +00:00
|
|
|
|
2013-01-11 16:18:00 +00:00
|
|
|
CorrectionState mCorrectionStates[MAX_WORD_LENGTH];
|
2011-08-10 06:44:08 +00:00
|
|
|
|
2011-08-10 13:19:33 +00:00
|
|
|
// The following member variables are being used as cache values of the correction state.
|
2011-08-17 08:55:16 +00:00
|
|
|
bool mNeedsToTraverseAllNodes;
|
2011-08-10 13:19:33 +00:00
|
|
|
int mOutputIndex;
|
|
|
|
int mInputIndex;
|
2011-08-17 08:55:16 +00:00
|
|
|
|
2011-10-05 05:55:07 +00:00
|
|
|
int mEquivalentCharCount;
|
2011-08-11 12:25:39 +00:00
|
|
|
int mProximityCount;
|
2011-08-17 08:55:16 +00:00
|
|
|
int mExcessiveCount;
|
|
|
|
int mTransposedCount;
|
2011-08-10 13:19:33 +00:00
|
|
|
int mSkippedCount;
|
2011-08-17 08:55:16 +00:00
|
|
|
|
|
|
|
int mTransposedPos;
|
|
|
|
int mExcessivePos;
|
2011-08-11 07:27:28 +00:00
|
|
|
int mSkipPos;
|
2011-08-17 08:55:16 +00:00
|
|
|
|
|
|
|
bool mLastCharExceeded;
|
|
|
|
|
2011-08-10 13:19:33 +00:00
|
|
|
bool mMatching;
|
2011-08-11 16:05:27 +00:00
|
|
|
bool mProximityMatching;
|
2012-02-02 09:49:22 +00:00
|
|
|
bool mAdditionalProximityMatching;
|
2011-08-17 08:55:16 +00:00
|
|
|
bool mExceeding;
|
|
|
|
bool mTransposing;
|
|
|
|
bool mSkipping;
|
2012-06-08 06:29:44 +00:00
|
|
|
ProximityInfoState mProximityInfoState;
|
2011-07-15 04:49:00 +00:00
|
|
|
};
|
2012-11-02 14:57:13 +00:00
|
|
|
|
|
|
|
inline void Correction::incrementInputIndex() {
|
|
|
|
++mInputIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
AK_FORCE_INLINE void Correction::incrementOutputIndex() {
|
|
|
|
++mOutputIndex;
|
|
|
|
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;
|
|
|
|
mCorrectionStates[mOutputIndex].mNeedsToTraverseAllNodes = mNeedsToTraverseAllNodes;
|
|
|
|
|
|
|
|
mCorrectionStates[mOutputIndex].mEquivalentCharCount = mEquivalentCharCount;
|
|
|
|
mCorrectionStates[mOutputIndex].mProximityCount = mProximityCount;
|
|
|
|
mCorrectionStates[mOutputIndex].mTransposedCount = mTransposedCount;
|
|
|
|
mCorrectionStates[mOutputIndex].mExcessiveCount = mExcessiveCount;
|
|
|
|
mCorrectionStates[mOutputIndex].mSkippedCount = mSkippedCount;
|
|
|
|
|
|
|
|
mCorrectionStates[mOutputIndex].mSkipPos = mSkipPos;
|
|
|
|
mCorrectionStates[mOutputIndex].mTransposedPos = mTransposedPos;
|
|
|
|
mCorrectionStates[mOutputIndex].mExcessivePos = mExcessivePos;
|
|
|
|
|
|
|
|
mCorrectionStates[mOutputIndex].mLastCharExceeded = mLastCharExceeded;
|
|
|
|
|
|
|
|
mCorrectionStates[mOutputIndex].mMatching = mMatching;
|
|
|
|
mCorrectionStates[mOutputIndex].mProximityMatching = mProximityMatching;
|
|
|
|
mCorrectionStates[mOutputIndex].mAdditionalProximityMatching = mAdditionalProximityMatching;
|
|
|
|
mCorrectionStates[mOutputIndex].mTransposing = mTransposing;
|
|
|
|
mCorrectionStates[mOutputIndex].mExceeding = mExceeding;
|
|
|
|
mCorrectionStates[mOutputIndex].mSkipping = mSkipping;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Correction::startToTraverseAllNodes() {
|
|
|
|
mNeedsToTraverseAllNodes = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool Correction::isSingleQuote(const int c) {
|
|
|
|
const int userTypedChar = mProximityInfoState.getPrimaryCodePointAt(mInputIndex);
|
|
|
|
return (c == KEYCODE_SINGLE_QUOTE && userTypedChar != KEYCODE_SINGLE_QUOTE);
|
|
|
|
}
|
|
|
|
|
|
|
|
AK_FORCE_INLINE Correction::CorrectionType Correction::processSkipChar(const int c,
|
|
|
|
const bool isTerminal, const bool inputIndexIncremented) {
|
|
|
|
addCharToCurrentWord(c);
|
|
|
|
mTerminalInputIndex = mInputIndex - (inputIndexIncremented ? 1 : 0);
|
|
|
|
mTerminalOutputIndex = mOutputIndex;
|
|
|
|
if (mNeedsToTraverseAllNodes && isTerminal) {
|
|
|
|
incrementOutputIndex();
|
|
|
|
return TRAVERSE_ALL_ON_TERMINAL;
|
|
|
|
} else {
|
|
|
|
incrementOutputIndex();
|
|
|
|
return TRAVERSE_ALL_NOT_ON_TERMINAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Correction::CorrectionType Correction::processUnrelatedCorrectionType() {
|
|
|
|
// Needs to set mTerminalInputIndex and mTerminalOutputIndex before returning any CorrectionType
|
|
|
|
mTerminalInputIndex = mInputIndex;
|
|
|
|
mTerminalOutputIndex = mOutputIndex;
|
|
|
|
return UNRELATED;
|
|
|
|
}
|
|
|
|
|
|
|
|
AK_FORCE_INLINE static void calcEditDistanceOneStep(int *editDistanceTable, const int *input,
|
|
|
|
const int inputSize, const int *output, const int outputLength) {
|
2013-01-11 16:18:00 +00:00
|
|
|
// TODO: Make sure that editDistance[0 ~ MAX_WORD_LENGTH] is not touched.
|
2012-11-02 14:57:13 +00:00
|
|
|
// Let dp[i][j] be editDistanceTable[i * (inputSize + 1) + j].
|
|
|
|
// Assuming that dp[0][0] ... dp[outputLength - 1][inputSize] are already calculated,
|
|
|
|
// and calculate dp[ouputLength][0] ... dp[outputLength][inputSize].
|
|
|
|
int *const current = editDistanceTable + outputLength * (inputSize + 1);
|
|
|
|
const int *const prev = editDistanceTable + (outputLength - 1) * (inputSize + 1);
|
|
|
|
const int *const prevprev =
|
|
|
|
outputLength >= 2 ? editDistanceTable + (outputLength - 2) * (inputSize + 1) : 0;
|
|
|
|
current[0] = outputLength;
|
|
|
|
const int co = toBaseLowerCase(output[outputLength - 1]);
|
|
|
|
const int prevCO = outputLength >= 2 ? toBaseLowerCase(output[outputLength - 2]) : 0;
|
|
|
|
for (int i = 1; i <= inputSize; ++i) {
|
|
|
|
const int ci = toBaseLowerCase(input[i - 1]);
|
2012-12-11 14:38:32 +00:00
|
|
|
const int cost = (ci == co) ? 0 : 1;
|
2012-11-02 14:57:13 +00:00
|
|
|
current[i] = min(current[i - 1] + 1, min(prev[i] + 1, prev[i - 1] + cost));
|
|
|
|
if (i >= 2 && prevprev && ci == prevCO && co == toBaseLowerCase(input[i - 2])) {
|
|
|
|
current[i] = min(current[i], prevprev[i - 2] + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AK_FORCE_INLINE void Correction::addCharToCurrentWord(const int c) {
|
|
|
|
mWord[mOutputIndex] = c;
|
|
|
|
const int *primaryInputWord = mProximityInfoState.getPrimaryInputWord();
|
|
|
|
calcEditDistanceOneStep(mEditDistanceTable, primaryInputWord, mInputSize, mWord,
|
|
|
|
mOutputIndex + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int Correction::getFinalProbabilityInternal(const int probability, int **word,
|
|
|
|
int *wordLength, const int inputSize) {
|
|
|
|
const int outputIndex = mTerminalOutputIndex;
|
|
|
|
const int inputIndex = mTerminalInputIndex;
|
|
|
|
*wordLength = outputIndex + 1;
|
|
|
|
*word = mWord;
|
|
|
|
int finalProbability= Correction::RankingAlgorithm::calculateFinalProbability(
|
|
|
|
inputIndex, outputIndex, probability, mEditDistanceTable, this, inputSize);
|
|
|
|
return finalProbability;
|
|
|
|
}
|
|
|
|
|
2011-07-15 04:49:00 +00:00
|
|
|
} // namespace latinime
|
2011-08-10 05:30:10 +00:00
|
|
|
#endif // LATINIME_CORRECTION_H
|