2012-06-05 08:55:52 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-08-02 10:48:08 +00:00
|
|
|
#include <cstring> // for memset()
|
2012-10-09 10:57:08 +00:00
|
|
|
#include <sstream> // for debug prints
|
2012-06-05 08:55:52 +00:00
|
|
|
|
|
|
|
#define LOG_TAG "LatinIME: proximity_info_state.cpp"
|
|
|
|
|
|
|
|
#include "defines.h"
|
2013-01-15 07:15:48 +00:00
|
|
|
#include "geometry_utils.h"
|
2012-06-05 08:55:52 +00:00
|
|
|
#include "proximity_info.h"
|
|
|
|
#include "proximity_info_state.h"
|
2013-01-21 02:37:54 +00:00
|
|
|
#include "proximity_info_utils.h"
|
2012-06-05 08:55:52 +00:00
|
|
|
|
|
|
|
namespace latinime {
|
2012-09-15 16:23:56 +00:00
|
|
|
|
|
|
|
const int ProximityInfoState::NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR_LOG_2 = 10;
|
|
|
|
const int ProximityInfoState::NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR =
|
|
|
|
1 << NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR_LOG_2;
|
|
|
|
const float ProximityInfoState::NOT_A_DISTANCE_FLOAT = -1.0f;
|
|
|
|
const int ProximityInfoState::NOT_A_CODE = -1;
|
2012-11-22 11:15:40 +00:00
|
|
|
const int ProximityInfoState::LOOKUP_RADIUS_PERCENTILE = 50;
|
|
|
|
const int ProximityInfoState::FIRST_POINT_TIME_OFFSET_MILLIS = 150;
|
|
|
|
const int ProximityInfoState::STRONG_DOUBLE_LETTER_TIME_MILLIS = 600;
|
|
|
|
const int ProximityInfoState::MIN_DOUBLE_LETTER_BEELINE_SPEED_PERCENTILE = 5;
|
2012-09-15 16:23:56 +00:00
|
|
|
|
2012-08-24 05:45:54 +00:00
|
|
|
void ProximityInfoState::initInputParams(const int pointerId, const float maxPointToKeyLength,
|
2012-10-29 09:06:22 +00:00
|
|
|
const ProximityInfo *proximityInfo, const int *const inputCodes, const int inputSize,
|
2012-08-23 06:46:43 +00:00
|
|
|
const int *const xCoordinates, const int *const yCoordinates, const int *const times,
|
|
|
|
const int *const pointerIds, const bool isGeometric) {
|
2012-12-10 08:17:52 +00:00
|
|
|
mIsContinuationPossible = checkAndReturnIsContinuationPossible(
|
|
|
|
inputSize, xCoordinates, yCoordinates, times, isGeometric);
|
2012-09-07 12:04:12 +00:00
|
|
|
|
2012-06-08 06:29:44 +00:00
|
|
|
mProximityInfo = proximityInfo;
|
|
|
|
mHasTouchPositionCorrectionData = proximityInfo->hasTouchPositionCorrectionData();
|
|
|
|
mMostCommonKeyWidthSquare = proximityInfo->getMostCommonKeyWidthSquare();
|
|
|
|
mKeyCount = proximityInfo->getKeyCount();
|
|
|
|
mCellHeight = proximityInfo->getCellHeight();
|
|
|
|
mCellWidth = proximityInfo->getCellWidth();
|
|
|
|
mGridHeight = proximityInfo->getGridWidth();
|
|
|
|
mGridWidth = proximityInfo->getGridHeight();
|
2012-06-05 08:55:52 +00:00
|
|
|
|
2013-01-17 10:46:12 +00:00
|
|
|
memset(mInputProximities, 0, sizeof(mInputProximities));
|
2012-06-05 08:55:52 +00:00
|
|
|
|
2012-08-24 06:19:56 +00:00
|
|
|
if (!isGeometric && pointerId == 0) {
|
2013-01-17 10:46:12 +00:00
|
|
|
mProximityInfo->initializeProximities(inputCodes, xCoordinates, yCoordinates,
|
|
|
|
inputSize, mInputProximities);
|
2012-06-05 08:55:52 +00:00
|
|
|
}
|
2012-08-23 06:46:43 +00:00
|
|
|
|
|
|
|
///////////////////////
|
|
|
|
// Setup touch points
|
2012-09-07 12:04:12 +00:00
|
|
|
int pushTouchPointStartIndex = 0;
|
|
|
|
int lastSavedInputSize = 0;
|
2012-08-24 06:19:56 +00:00
|
|
|
mMaxPointToKeyLength = maxPointToKeyLength;
|
2012-09-07 12:04:12 +00:00
|
|
|
if (mIsContinuationPossible && mInputIndice.size() > 1) {
|
|
|
|
// Just update difference.
|
|
|
|
// Two points prior is never skipped. Thus, we pop 2 input point data here.
|
|
|
|
pushTouchPointStartIndex = mInputIndice[mInputIndice.size() - 2];
|
|
|
|
popInputData();
|
|
|
|
popInputData();
|
2012-11-16 10:03:36 +00:00
|
|
|
lastSavedInputSize = mSampledInputXs.size();
|
2012-09-07 12:04:12 +00:00
|
|
|
} else {
|
|
|
|
// Clear all data.
|
2012-11-16 10:03:36 +00:00
|
|
|
mSampledInputXs.clear();
|
|
|
|
mSampledInputYs.clear();
|
2012-09-07 12:04:12 +00:00
|
|
|
mTimes.clear();
|
|
|
|
mInputIndice.clear();
|
|
|
|
mLengthCache.clear();
|
2013-01-15 10:16:38 +00:00
|
|
|
mDistanceCache_G.clear();
|
2012-09-07 12:04:12 +00:00
|
|
|
mNearKeysVector.clear();
|
2012-10-11 04:08:06 +00:00
|
|
|
mSearchKeysVector.clear();
|
2012-11-16 14:06:41 +00:00
|
|
|
mSpeedRates.clear();
|
2012-11-22 11:15:40 +00:00
|
|
|
mBeelineSpeedPercentiles.clear();
|
2012-10-09 10:57:08 +00:00
|
|
|
mCharProbabilities.clear();
|
2012-10-11 11:24:41 +00:00
|
|
|
mDirections.clear();
|
2012-09-07 12:04:12 +00:00
|
|
|
}
|
2012-09-11 08:47:55 +00:00
|
|
|
if (DEBUG_GEO_FULL) {
|
|
|
|
AKLOGI("Init ProximityInfoState: reused points = %d, last input size = %d",
|
|
|
|
pushTouchPointStartIndex, lastSavedInputSize);
|
|
|
|
}
|
2012-11-16 10:03:36 +00:00
|
|
|
mSampledInputSize = 0;
|
2012-08-31 12:22:14 +00:00
|
|
|
|
2012-08-23 06:46:43 +00:00
|
|
|
if (xCoordinates && yCoordinates) {
|
2012-11-16 14:06:41 +00:00
|
|
|
if (DEBUG_SAMPLING_POINTS) {
|
|
|
|
if (isGeometric) {
|
|
|
|
for (int i = 0; i < inputSize; ++i) {
|
|
|
|
AKLOGI("(%d) x %d, y %d, time %d",
|
|
|
|
i, xCoordinates[i], yCoordinates[i], times[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-09 03:54:39 +00:00
|
|
|
#ifdef DO_ASSERT_TEST
|
|
|
|
if (times) {
|
|
|
|
for (int i = 0; i < inputSize; ++i) {
|
|
|
|
if (i > 0) {
|
|
|
|
ASSERT(times[i] >= times[i - 1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2012-08-23 06:46:43 +00:00
|
|
|
const bool proximityOnly = !isGeometric && (xCoordinates[0] < 0 || yCoordinates[0] < 0);
|
2012-09-07 12:04:12 +00:00
|
|
|
int lastInputIndex = pushTouchPointStartIndex;
|
|
|
|
for (int i = lastInputIndex; i < inputSize; ++i) {
|
2012-08-31 12:22:14 +00:00
|
|
|
const int pid = pointerIds ? pointerIds[i] : 0;
|
|
|
|
if (pointerId == pid) {
|
|
|
|
lastInputIndex = i;
|
|
|
|
}
|
|
|
|
}
|
2012-09-11 08:47:55 +00:00
|
|
|
if (DEBUG_GEO_FULL) {
|
|
|
|
AKLOGI("Init ProximityInfoState: last input index = %d", lastInputIndex);
|
|
|
|
}
|
2012-08-31 12:22:14 +00:00
|
|
|
// Working space to save near keys distances for current, prev and prevprev input point.
|
|
|
|
NearKeysDistanceMap nearKeysDistances[3];
|
|
|
|
// These pointers are swapped for each inputs points.
|
|
|
|
NearKeysDistanceMap *currentNearKeysDistances = &nearKeysDistances[0];
|
|
|
|
NearKeysDistanceMap *prevNearKeysDistances = &nearKeysDistances[1];
|
|
|
|
NearKeysDistanceMap *prevPrevNearKeysDistances = &nearKeysDistances[2];
|
2012-10-11 04:08:06 +00:00
|
|
|
// "sumAngle" is accumulated by each angle of input points. And when "sumAngle" exceeds
|
|
|
|
// the threshold we save that point, reset sumAngle. This aims to keep the figure of
|
|
|
|
// the curve.
|
|
|
|
float sumAngle = 0.0f;
|
2012-08-31 12:22:14 +00:00
|
|
|
|
2012-09-07 12:04:12 +00:00
|
|
|
for (int i = pushTouchPointStartIndex; i <= lastInputIndex; ++i) {
|
2012-08-23 06:46:43 +00:00
|
|
|
// Assuming pointerId == 0 if pointerIds is null.
|
|
|
|
const int pid = pointerIds ? pointerIds[i] : 0;
|
2012-09-11 08:47:55 +00:00
|
|
|
if (DEBUG_GEO_FULL) {
|
|
|
|
AKLOGI("Init ProximityInfoState: (%d)PID = %d", i, pid);
|
|
|
|
}
|
2012-08-23 06:46:43 +00:00
|
|
|
if (pointerId == pid) {
|
2012-10-29 09:06:22 +00:00
|
|
|
const int c = isGeometric ? NOT_A_COORDINATE : getPrimaryCodePointAt(i);
|
2012-08-23 06:46:43 +00:00
|
|
|
const int x = proximityOnly ? NOT_A_COORDINATE : xCoordinates[i];
|
|
|
|
const int y = proximityOnly ? NOT_A_COORDINATE : yCoordinates[i];
|
|
|
|
const int time = times ? times[i] : -1;
|
2012-10-11 04:08:06 +00:00
|
|
|
|
|
|
|
if (i > 1) {
|
|
|
|
const float prevAngle = getAngle(xCoordinates[i - 2], yCoordinates[i - 2],
|
|
|
|
xCoordinates[i - 1], yCoordinates[i - 1]);
|
|
|
|
const float currentAngle =
|
|
|
|
getAngle(xCoordinates[i - 1], yCoordinates[i - 1], x, y);
|
|
|
|
sumAngle += getAngleDiff(prevAngle, currentAngle);
|
|
|
|
}
|
|
|
|
|
2012-09-11 06:51:38 +00:00
|
|
|
if (pushTouchPoint(i, c, x, y, time, isGeometric /* do sampling */,
|
2012-10-11 04:08:06 +00:00
|
|
|
i == lastInputIndex, sumAngle, currentNearKeysDistances,
|
|
|
|
prevNearKeysDistances, prevPrevNearKeysDistances)) {
|
2012-08-31 12:22:14 +00:00
|
|
|
// Previous point information was popped.
|
|
|
|
NearKeysDistanceMap *tmp = prevNearKeysDistances;
|
|
|
|
prevNearKeysDistances = currentNearKeysDistances;
|
|
|
|
currentNearKeysDistances = tmp;
|
|
|
|
} else {
|
|
|
|
NearKeysDistanceMap *tmp = prevPrevNearKeysDistances;
|
|
|
|
prevPrevNearKeysDistances = prevNearKeysDistances;
|
|
|
|
prevNearKeysDistances = currentNearKeysDistances;
|
|
|
|
currentNearKeysDistances = tmp;
|
2012-10-11 04:08:06 +00:00
|
|
|
sumAngle = 0.0f;
|
2012-08-27 05:39:17 +00:00
|
|
|
}
|
2012-08-23 06:46:43 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-16 10:03:36 +00:00
|
|
|
mSampledInputSize = mSampledInputXs.size();
|
2012-06-05 08:55:52 +00:00
|
|
|
}
|
2012-08-23 06:46:43 +00:00
|
|
|
|
2012-11-16 10:03:36 +00:00
|
|
|
if (mSampledInputSize > 0 && isGeometric) {
|
2012-11-16 14:06:41 +00:00
|
|
|
refreshSpeedRates(inputSize, xCoordinates, yCoordinates, times, lastSavedInputSize);
|
|
|
|
refreshBeelineSpeedRates(inputSize, xCoordinates, yCoordinates, times);
|
2012-09-24 11:02:57 +00:00
|
|
|
}
|
|
|
|
|
2012-10-11 04:08:06 +00:00
|
|
|
if (DEBUG_GEO_FULL) {
|
2012-11-16 10:03:36 +00:00
|
|
|
for (int i = 0; i < mSampledInputSize; ++i) {
|
|
|
|
AKLOGI("Sampled(%d): x = %d, y = %d, time = %d", i, mSampledInputXs[i],
|
|
|
|
mSampledInputYs[i], mTimes[i]);
|
2012-10-11 04:08:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-16 10:03:36 +00:00
|
|
|
if (mSampledInputSize > 0) {
|
2012-08-23 06:46:43 +00:00
|
|
|
const int keyCount = mProximityInfo->getKeyCount();
|
2012-11-16 10:03:36 +00:00
|
|
|
mNearKeysVector.resize(mSampledInputSize);
|
|
|
|
mSearchKeysVector.resize(mSampledInputSize);
|
2013-01-15 10:16:38 +00:00
|
|
|
mDistanceCache_G.resize(mSampledInputSize * keyCount);
|
2012-11-16 10:03:36 +00:00
|
|
|
for (int i = lastSavedInputSize; i < mSampledInputSize; ++i) {
|
2012-09-04 08:00:24 +00:00
|
|
|
mNearKeysVector[i].reset();
|
2012-10-11 04:08:06 +00:00
|
|
|
mSearchKeysVector[i].reset();
|
2012-09-04 08:00:24 +00:00
|
|
|
static const float NEAR_KEY_NORMALIZED_SQUARED_THRESHOLD = 4.0f;
|
2012-08-23 06:46:43 +00:00
|
|
|
for (int k = 0; k < keyCount; ++k) {
|
|
|
|
const int index = i * keyCount + k;
|
2012-11-16 10:03:36 +00:00
|
|
|
const int x = mSampledInputXs[i];
|
|
|
|
const int y = mSampledInputYs[i];
|
2012-09-04 08:00:24 +00:00
|
|
|
const float normalizedSquaredDistance =
|
2012-09-24 09:29:31 +00:00
|
|
|
mProximityInfo->getNormalizedSquaredDistanceFromCenterFloatG(k, x, y);
|
2013-01-15 10:16:38 +00:00
|
|
|
mDistanceCache_G[index] = normalizedSquaredDistance;
|
2012-09-04 08:00:24 +00:00
|
|
|
if (normalizedSquaredDistance < NEAR_KEY_NORMALIZED_SQUARED_THRESHOLD) {
|
2012-10-11 04:08:06 +00:00
|
|
|
mNearKeysVector[i][k] = true;
|
2012-09-04 08:00:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-10-11 04:08:06 +00:00
|
|
|
if (isGeometric) {
|
|
|
|
// updates probabilities of skipping or mapping each key for all points.
|
|
|
|
updateAlignPointProbabilities(lastSavedInputSize);
|
|
|
|
|
|
|
|
static const float READ_FORWORD_LENGTH_SCALE = 0.95f;
|
|
|
|
const int readForwordLength = static_cast<int>(
|
|
|
|
hypotf(mProximityInfo->getKeyboardWidth(), mProximityInfo->getKeyboardHeight())
|
|
|
|
* READ_FORWORD_LENGTH_SCALE);
|
2012-11-16 10:03:36 +00:00
|
|
|
for (int i = 0; i < mSampledInputSize; ++i) {
|
2012-10-11 04:08:06 +00:00
|
|
|
if (i >= lastSavedInputSize) {
|
|
|
|
mSearchKeysVector[i].reset();
|
|
|
|
}
|
2012-11-16 10:03:36 +00:00
|
|
|
for (int j = max(i, lastSavedInputSize); j < mSampledInputSize; ++j) {
|
2012-10-11 04:08:06 +00:00
|
|
|
if (mLengthCache[j] - mLengthCache[i] >= readForwordLength) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mSearchKeysVector[i] |= mNearKeysVector[j];
|
2012-09-04 08:00:24 +00:00
|
|
|
}
|
2012-08-23 06:46:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-24 06:19:56 +00:00
|
|
|
|
2012-10-09 10:57:08 +00:00
|
|
|
if (DEBUG_SAMPLING_POINTS) {
|
|
|
|
std::stringstream originalX, originalY, sampledX, sampledY;
|
|
|
|
for (int i = 0; i < inputSize; ++i) {
|
|
|
|
originalX << xCoordinates[i];
|
|
|
|
originalY << yCoordinates[i];
|
|
|
|
if (i != inputSize - 1) {
|
|
|
|
originalX << ";";
|
|
|
|
originalY << ";";
|
|
|
|
}
|
|
|
|
}
|
2012-11-16 14:06:41 +00:00
|
|
|
AKLOGI("===== sampled points =====");
|
2012-11-16 10:03:36 +00:00
|
|
|
for (int i = 0; i < mSampledInputSize; ++i) {
|
2012-11-16 14:06:41 +00:00
|
|
|
if (isGeometric) {
|
2012-11-22 11:15:40 +00:00
|
|
|
AKLOGI("%d: x = %d, y = %d, time = %d, relative speed = %.4f, beeline speed = %d",
|
2012-11-16 14:06:41 +00:00
|
|
|
i, mSampledInputXs[i], mSampledInputYs[i], mTimes[i], mSpeedRates[i],
|
2012-11-22 11:15:40 +00:00
|
|
|
getBeelineSpeedPercentile(i));
|
2012-11-16 14:06:41 +00:00
|
|
|
}
|
2012-11-16 10:03:36 +00:00
|
|
|
sampledX << mSampledInputXs[i];
|
|
|
|
sampledY << mSampledInputYs[i];
|
|
|
|
if (i != mSampledInputSize - 1) {
|
2012-10-09 10:57:08 +00:00
|
|
|
sampledX << ";";
|
|
|
|
sampledY << ";";
|
|
|
|
}
|
|
|
|
}
|
2012-11-15 13:48:20 +00:00
|
|
|
AKLOGI("original points:\n%s, %s,\nsampled points:\n%s, %s,\n",
|
|
|
|
originalX.str().c_str(), originalY.str().c_str(), sampledX.str().c_str(),
|
|
|
|
sampledY.str().c_str());
|
2012-10-09 10:57:08 +00:00
|
|
|
}
|
2012-08-23 06:46:43 +00:00
|
|
|
// end
|
|
|
|
///////////////////////
|
|
|
|
|
2012-08-24 06:19:56 +00:00
|
|
|
memset(mNormalizedSquaredDistances, NOT_A_DISTANCE, sizeof(mNormalizedSquaredDistances));
|
|
|
|
memset(mPrimaryInputWord, 0, sizeof(mPrimaryInputWord));
|
2012-11-16 10:03:36 +00:00
|
|
|
mTouchPositionCorrectionEnabled = mSampledInputSize > 0 && mHasTouchPositionCorrectionData
|
2012-09-24 09:29:31 +00:00
|
|
|
&& xCoordinates && yCoordinates;
|
2012-08-24 06:19:56 +00:00
|
|
|
if (!isGeometric && pointerId == 0) {
|
|
|
|
for (int i = 0; i < inputSize; ++i) {
|
2012-10-29 09:06:22 +00:00
|
|
|
mPrimaryInputWord[i] = getPrimaryCodePointAt(i);
|
2012-06-05 08:55:52 +00:00
|
|
|
}
|
2012-08-24 06:19:56 +00:00
|
|
|
|
2012-11-16 10:03:36 +00:00
|
|
|
for (int i = 0; i < mSampledInputSize && mTouchPositionCorrectionEnabled; ++i) {
|
2012-10-29 09:06:22 +00:00
|
|
|
const int *proximityCodePoints = getProximityCodePointsAt(i);
|
|
|
|
const int primaryKey = proximityCodePoints[0];
|
2012-08-24 06:19:56 +00:00
|
|
|
const int x = xCoordinates[i];
|
|
|
|
const int y = yCoordinates[i];
|
2012-06-05 08:55:52 +00:00
|
|
|
if (DEBUG_PROXIMITY_CHARS) {
|
2012-08-24 06:19:56 +00:00
|
|
|
int a = x + y + primaryKey;
|
|
|
|
a += 0;
|
|
|
|
AKLOGI("--- Primary = %c, x = %d, y = %d", primaryKey, x, y);
|
|
|
|
}
|
2012-10-29 09:06:22 +00:00
|
|
|
for (int j = 0; j < MAX_PROXIMITY_CHARS_SIZE_INTERNAL && proximityCodePoints[j] > 0;
|
|
|
|
++j) {
|
|
|
|
const int currentCodePoint = proximityCodePoints[j];
|
2012-08-24 06:19:56 +00:00
|
|
|
const float squaredDistance =
|
|
|
|
hasInputCoordinates() ? calculateNormalizedSquaredDistance(
|
2012-10-29 09:06:22 +00:00
|
|
|
mProximityInfo->getKeyIndexOf(currentCodePoint), i) :
|
2012-08-24 06:19:56 +00:00
|
|
|
NOT_A_DISTANCE_FLOAT;
|
|
|
|
if (squaredDistance >= 0.0f) {
|
|
|
|
mNormalizedSquaredDistances[i * MAX_PROXIMITY_CHARS_SIZE_INTERNAL + j] =
|
|
|
|
(int) (squaredDistance * NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR);
|
|
|
|
} else {
|
|
|
|
mNormalizedSquaredDistances[i * MAX_PROXIMITY_CHARS_SIZE_INTERNAL + j] =
|
|
|
|
(j == 0) ? EQUIVALENT_CHAR_WITHOUT_DISTANCE_INFO :
|
|
|
|
PROXIMITY_CHAR_WITHOUT_DISTANCE_INFO;
|
|
|
|
}
|
|
|
|
if (DEBUG_PROXIMITY_CHARS) {
|
2012-10-29 09:06:22 +00:00
|
|
|
AKLOGI("--- Proximity (%d) = %c", j, currentCodePoint);
|
2012-08-24 06:19:56 +00:00
|
|
|
}
|
2012-06-05 08:55:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-09-11 06:51:38 +00:00
|
|
|
|
|
|
|
if (DEBUG_GEO_FULL) {
|
2012-11-16 10:03:36 +00:00
|
|
|
AKLOGI("ProximityState init finished: %d points out of %d", mSampledInputSize, inputSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-16 14:06:41 +00:00
|
|
|
void ProximityInfoState::refreshSpeedRates(const int inputSize, const int *const xCoordinates,
|
2012-11-16 10:03:36 +00:00
|
|
|
const int *const yCoordinates, const int *const times, const int lastSavedInputSize) {
|
|
|
|
// Relative speed calculation.
|
|
|
|
const int sumDuration = mTimes.back() - mTimes.front();
|
|
|
|
const int sumLength = mLengthCache.back() - mLengthCache.front();
|
2012-11-16 14:06:41 +00:00
|
|
|
mAverageSpeed = static_cast<float>(sumLength) / static_cast<float>(sumDuration);
|
|
|
|
mSpeedRates.resize(mSampledInputSize);
|
2012-11-16 10:03:36 +00:00
|
|
|
for (int i = lastSavedInputSize; i < mSampledInputSize; ++i) {
|
|
|
|
const int index = mInputIndice[i];
|
|
|
|
int length = 0;
|
|
|
|
int duration = 0;
|
|
|
|
|
|
|
|
// Calculate velocity by using distances and durations of
|
|
|
|
// NUM_POINTS_FOR_SPEED_CALCULATION points for both forward and backward.
|
|
|
|
static const int NUM_POINTS_FOR_SPEED_CALCULATION = 2;
|
|
|
|
for (int j = index; j < min(inputSize - 1, index + NUM_POINTS_FOR_SPEED_CALCULATION);
|
|
|
|
++j) {
|
|
|
|
if (i < mSampledInputSize - 1 && j >= mInputIndice[i + 1]) {
|
|
|
|
break;
|
|
|
|
}
|
2013-01-21 02:37:54 +00:00
|
|
|
length += ProximityInfoUtils::getDistanceInt(xCoordinates[j], yCoordinates[j],
|
2012-11-16 10:03:36 +00:00
|
|
|
xCoordinates[j + 1], yCoordinates[j + 1]);
|
|
|
|
duration += times[j + 1] - times[j];
|
|
|
|
}
|
|
|
|
for (int j = index - 1; j >= max(0, index - NUM_POINTS_FOR_SPEED_CALCULATION); --j) {
|
|
|
|
if (i > 0 && j < mInputIndice[i - 1]) {
|
|
|
|
break;
|
|
|
|
}
|
2012-11-16 14:06:41 +00:00
|
|
|
// TODO: use mLengthCache instead?
|
2013-01-21 02:37:54 +00:00
|
|
|
length += ProximityInfoUtils::getDistanceInt(xCoordinates[j], yCoordinates[j],
|
2012-11-16 10:03:36 +00:00
|
|
|
xCoordinates[j + 1], yCoordinates[j + 1]);
|
|
|
|
duration += times[j + 1] - times[j];
|
|
|
|
}
|
|
|
|
if (duration == 0 || sumDuration == 0) {
|
|
|
|
// Cannot calculate speed; thus, it gives an average value (1.0);
|
2012-11-16 14:06:41 +00:00
|
|
|
mSpeedRates[i] = 1.0f;
|
2012-11-16 10:03:36 +00:00
|
|
|
} else {
|
|
|
|
const float speed = static_cast<float>(length) / static_cast<float>(duration);
|
2012-11-16 14:06:41 +00:00
|
|
|
mSpeedRates[i] = speed / mAverageSpeed;
|
2012-11-16 10:03:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Direction calculation.
|
|
|
|
mDirections.resize(mSampledInputSize - 1);
|
|
|
|
for (int i = max(0, lastSavedInputSize - 1); i < mSampledInputSize - 1; ++i) {
|
|
|
|
mDirections[i] = getDirection(i, i + 1);
|
2012-09-11 06:51:38 +00:00
|
|
|
}
|
2012-06-05 08:55:52 +00:00
|
|
|
}
|
2012-06-08 06:29:44 +00:00
|
|
|
|
2012-11-22 11:15:40 +00:00
|
|
|
static const int MAX_PERCENTILE = 100;
|
2012-11-16 14:06:41 +00:00
|
|
|
void ProximityInfoState::refreshBeelineSpeedRates(const int inputSize,
|
|
|
|
const int *const xCoordinates, const int *const yCoordinates, const int * times) {
|
2012-11-22 11:15:40 +00:00
|
|
|
if (DEBUG_SAMPLING_POINTS){
|
|
|
|
AKLOGI("--- refresh beeline speed rates");
|
|
|
|
}
|
|
|
|
mBeelineSpeedPercentiles.resize(mSampledInputSize);
|
2012-11-16 14:06:41 +00:00
|
|
|
for (int i = 0; i < mSampledInputSize; ++i) {
|
2012-11-22 11:15:40 +00:00
|
|
|
mBeelineSpeedPercentiles[i] = static_cast<int>(calculateBeelineSpeedRate(
|
|
|
|
i, inputSize, xCoordinates, yCoordinates, times) * MAX_PERCENTILE);
|
2012-11-16 14:06:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float ProximityInfoState::calculateBeelineSpeedRate(
|
|
|
|
const int id, const int inputSize, const int *const xCoordinates,
|
|
|
|
const int *const yCoordinates, const int * times) const {
|
2012-11-22 11:15:40 +00:00
|
|
|
if (mSampledInputSize <= 0 || mAverageSpeed < 0.001f) {
|
|
|
|
if (DEBUG_SAMPLING_POINTS){
|
|
|
|
AKLOGI("--- invalid state: cancel. size = %d, ave = %f",
|
|
|
|
mSampledInputSize, mAverageSpeed);
|
|
|
|
}
|
2012-11-16 14:06:41 +00:00
|
|
|
return 1.0f;
|
|
|
|
}
|
|
|
|
const int lookupRadius =
|
|
|
|
mProximityInfo->getMostCommonKeyWidth() * LOOKUP_RADIUS_PERCENTILE / MAX_PERCENTILE;
|
|
|
|
const int x0 = mSampledInputXs[id];
|
|
|
|
const int y0 = mSampledInputYs[id];
|
2012-11-22 11:15:40 +00:00
|
|
|
const int actualInputIndex = mInputIndice[id];
|
2012-11-16 14:06:41 +00:00
|
|
|
int tempTime = 0;
|
|
|
|
int tempBeelineDistance = 0;
|
2012-11-22 11:15:40 +00:00
|
|
|
int start = actualInputIndex;
|
2012-11-16 14:06:41 +00:00
|
|
|
// lookup forward
|
2012-11-22 11:15:40 +00:00
|
|
|
while (start > 0 && tempBeelineDistance < lookupRadius) {
|
2012-11-16 14:06:41 +00:00
|
|
|
tempTime += times[start] - times[start - 1];
|
|
|
|
--start;
|
2013-01-21 02:37:54 +00:00
|
|
|
tempBeelineDistance = ProximityInfoUtils::getDistanceInt(x0, y0, xCoordinates[start],
|
|
|
|
yCoordinates[start]);
|
2012-11-16 14:06:41 +00:00
|
|
|
}
|
2012-11-22 11:15:40 +00:00
|
|
|
// Exclusive unless this is an edge point
|
|
|
|
if (start > 0 && start < actualInputIndex) {
|
|
|
|
++start;
|
|
|
|
}
|
2012-11-16 14:06:41 +00:00
|
|
|
tempTime= 0;
|
|
|
|
tempBeelineDistance = 0;
|
2012-11-22 11:15:40 +00:00
|
|
|
int end = actualInputIndex;
|
2012-11-16 14:06:41 +00:00
|
|
|
// lookup backward
|
2012-11-22 11:15:40 +00:00
|
|
|
while (end < (inputSize - 1) && tempBeelineDistance < lookupRadius) {
|
2012-11-16 14:06:41 +00:00
|
|
|
tempTime += times[end + 1] - times[end];
|
|
|
|
++end;
|
2013-01-21 02:37:54 +00:00
|
|
|
tempBeelineDistance = ProximityInfoUtils::getDistanceInt(x0, y0, xCoordinates[end],
|
|
|
|
yCoordinates[end]);
|
2012-11-22 11:15:40 +00:00
|
|
|
}
|
|
|
|
// Exclusive unless this is an edge point
|
|
|
|
if (end > actualInputIndex && end < (inputSize - 1)) {
|
|
|
|
--end;
|
2012-11-16 14:06:41 +00:00
|
|
|
}
|
|
|
|
|
2012-11-22 11:15:40 +00:00
|
|
|
if (start >= end) {
|
|
|
|
if (DEBUG_DOUBLE_LETTER) {
|
|
|
|
AKLOGI("--- double letter: start == end %d", start);
|
|
|
|
}
|
2012-11-16 14:06:41 +00:00
|
|
|
return 1.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int x2 = xCoordinates[start];
|
|
|
|
const int y2 = yCoordinates[start];
|
|
|
|
const int x3 = xCoordinates[end];
|
|
|
|
const int y3 = yCoordinates[end];
|
2013-01-21 02:37:54 +00:00
|
|
|
const int beelineDistance = ProximityInfoUtils::getDistanceInt(x2, y2, x3, y3);
|
2012-11-22 11:15:40 +00:00
|
|
|
int adjustedStartTime = times[start];
|
|
|
|
if (start == 0 && actualInputIndex == 0 && inputSize > 1) {
|
|
|
|
adjustedStartTime += FIRST_POINT_TIME_OFFSET_MILLIS;
|
|
|
|
}
|
|
|
|
int adjustedEndTime = times[end];
|
|
|
|
if (end == (inputSize - 1) && inputSize > 1) {
|
|
|
|
adjustedEndTime -= FIRST_POINT_TIME_OFFSET_MILLIS;
|
|
|
|
}
|
|
|
|
const int time = adjustedEndTime - adjustedStartTime;
|
2012-11-16 14:06:41 +00:00
|
|
|
if (time <= 0) {
|
|
|
|
return 1.0f;
|
|
|
|
}
|
2012-11-22 11:15:40 +00:00
|
|
|
|
|
|
|
if (time >= STRONG_DOUBLE_LETTER_TIME_MILLIS){
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
if (DEBUG_DOUBLE_LETTER) {
|
|
|
|
AKLOGI("--- (%d, %d) double letter: start = %d, end = %d, dist = %d, time = %d, speed = %f,"
|
|
|
|
" ave = %f, val = %f, start time = %d, end time = %d",
|
|
|
|
id, mInputIndice[id], start, end, beelineDistance, time,
|
|
|
|
(static_cast<float>(beelineDistance) / static_cast<float>(time)), mAverageSpeed,
|
|
|
|
((static_cast<float>(beelineDistance) / static_cast<float>(time)) / mAverageSpeed),
|
|
|
|
adjustedStartTime, adjustedEndTime);
|
|
|
|
}
|
|
|
|
// Offset 1%
|
|
|
|
// TODO: Detect double letter more smartly
|
|
|
|
return 0.01f + static_cast<float>(beelineDistance) / static_cast<float>(time) / mAverageSpeed;
|
2012-11-16 14:06:41 +00:00
|
|
|
}
|
|
|
|
|
2012-09-07 12:04:12 +00:00
|
|
|
bool ProximityInfoState::checkAndReturnIsContinuationPossible(const int inputSize,
|
2012-12-10 08:17:52 +00:00
|
|
|
const int *const xCoordinates, const int *const yCoordinates, const int *const times,
|
|
|
|
const bool isGeometric) const {
|
|
|
|
if (isGeometric) {
|
|
|
|
for (int i = 0; i < mSampledInputSize; ++i) {
|
|
|
|
const int index = mInputIndice[i];
|
|
|
|
if (index > inputSize || xCoordinates[index] != mSampledInputXs[i] ||
|
|
|
|
yCoordinates[index] != mSampledInputYs[i] || times[index] != mTimes[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (inputSize < mSampledInputSize) {
|
|
|
|
// Assuming the cache is invalid if the previous input size is larger than the new one.
|
2012-09-07 12:04:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-01-11 16:18:00 +00:00
|
|
|
for (int i = 0; i < mSampledInputSize && i < MAX_WORD_LENGTH; ++i) {
|
2012-12-10 08:17:52 +00:00
|
|
|
if (xCoordinates[i] != mSampledInputXs[i]
|
|
|
|
|| yCoordinates[i] != mSampledInputYs[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-09-07 12:04:12 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-31 12:22:14 +00:00
|
|
|
// Calculating point to key distance for all near keys and returning the distance between
|
|
|
|
// the given point and the nearest key position.
|
|
|
|
float ProximityInfoState::updateNearKeysDistances(const int x, const int y,
|
|
|
|
NearKeysDistanceMap *const currentNearKeysDistances) {
|
2012-10-11 04:08:06 +00:00
|
|
|
static const float NEAR_KEY_THRESHOLD = 2.0f;
|
2012-08-31 12:22:14 +00:00
|
|
|
|
|
|
|
currentNearKeysDistances->clear();
|
|
|
|
const int keyCount = mProximityInfo->getKeyCount();
|
|
|
|
float nearestKeyDistance = mMaxPointToKeyLength;
|
|
|
|
for (int k = 0; k < keyCount; ++k) {
|
2012-09-24 09:29:31 +00:00
|
|
|
const float dist = mProximityInfo->getNormalizedSquaredDistanceFromCenterFloatG(k, x, y);
|
2012-08-31 12:22:14 +00:00
|
|
|
if (dist < NEAR_KEY_THRESHOLD) {
|
|
|
|
currentNearKeysDistances->insert(std::pair<int, float>(k, dist));
|
2012-08-23 06:46:43 +00:00
|
|
|
}
|
2012-08-31 12:22:14 +00:00
|
|
|
if (nearestKeyDistance > dist) {
|
|
|
|
nearestKeyDistance = dist;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nearestKeyDistance;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if previous point is at local minimum position to near keys.
|
|
|
|
bool ProximityInfoState::isPrevLocalMin(const NearKeysDistanceMap *const currentNearKeysDistances,
|
|
|
|
const NearKeysDistanceMap *const prevNearKeysDistances,
|
|
|
|
const NearKeysDistanceMap *const prevPrevNearKeysDistances) const {
|
2012-10-11 04:08:06 +00:00
|
|
|
static const float MARGIN = 0.01f;
|
2012-08-31 12:22:14 +00:00
|
|
|
|
|
|
|
for (NearKeysDistanceMap::const_iterator it = prevNearKeysDistances->begin();
|
|
|
|
it != prevNearKeysDistances->end(); ++it) {
|
|
|
|
NearKeysDistanceMap::const_iterator itPP = prevPrevNearKeysDistances->find(it->first);
|
|
|
|
NearKeysDistanceMap::const_iterator itC = currentNearKeysDistances->find(it->first);
|
|
|
|
if ((itPP == prevPrevNearKeysDistances->end() || itPP->second > it->second + MARGIN)
|
|
|
|
&& (itC == currentNearKeysDistances->end() || itC->second > it->second + MARGIN)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculating a point score that indicates usefulness of the point.
|
|
|
|
float ProximityInfoState::getPointScore(
|
|
|
|
const int x, const int y, const int time, const bool lastPoint, const float nearest,
|
2012-10-11 04:08:06 +00:00
|
|
|
const float sumAngle, const NearKeysDistanceMap *const currentNearKeysDistances,
|
2012-08-31 12:22:14 +00:00
|
|
|
const NearKeysDistanceMap *const prevNearKeysDistances,
|
|
|
|
const NearKeysDistanceMap *const prevPrevNearKeysDistances) const {
|
2012-09-14 11:29:34 +00:00
|
|
|
static const int DISTANCE_BASE_SCALE = 100;
|
2012-10-11 04:08:06 +00:00
|
|
|
static const float NEAR_KEY_THRESHOLD = 0.6f;
|
|
|
|
static const int CORNER_CHECK_DISTANCE_THRESHOLD_SCALE = 25;
|
2012-10-09 10:57:08 +00:00
|
|
|
static const float NOT_LOCALMIN_DISTANCE_SCORE = -1.0f;
|
2012-10-11 04:08:06 +00:00
|
|
|
static const float LOCALMIN_DISTANCE_AND_NEAR_TO_KEY_SCORE = 1.0f;
|
|
|
|
static const float CORNER_ANGLE_THRESHOLD = M_PI_F * 2.0f / 3.0f;
|
|
|
|
static const float CORNER_SUM_ANGLE_THRESHOLD = M_PI_F / 4.0f;
|
2012-09-07 03:56:31 +00:00
|
|
|
static const float CORNER_SCORE = 1.0f;
|
2012-08-31 12:22:14 +00:00
|
|
|
|
2012-11-16 10:03:36 +00:00
|
|
|
const size_t size = mSampledInputXs.size();
|
2012-10-11 04:08:06 +00:00
|
|
|
// If there is only one point, add this point. Besides, if the previous point's distance map
|
|
|
|
// is empty, we re-compute nearby keys distances from the current point.
|
|
|
|
// Note that the current point is the first point in the incremental input that needs to
|
|
|
|
// be re-computed.
|
|
|
|
if (size <= 1 || prevNearKeysDistances->empty()) {
|
2012-09-14 11:29:34 +00:00
|
|
|
return 0.0f;
|
2012-08-23 06:46:43 +00:00
|
|
|
}
|
2012-10-11 04:08:06 +00:00
|
|
|
|
2012-09-14 11:29:34 +00:00
|
|
|
const int baseSampleRate = mProximityInfo->getMostCommonKeyWidth();
|
2013-01-21 02:37:54 +00:00
|
|
|
const int distPrev = ProximityInfoUtils::getDistanceInt(
|
|
|
|
mSampledInputXs.back(), mSampledInputYs.back(),
|
2012-11-16 10:03:36 +00:00
|
|
|
mSampledInputXs[size - 2], mSampledInputYs[size - 2]) * DISTANCE_BASE_SCALE;
|
2012-08-31 12:22:14 +00:00
|
|
|
float score = 0.0f;
|
|
|
|
|
|
|
|
// Location
|
2012-10-11 04:08:06 +00:00
|
|
|
if (!isPrevLocalMin(currentNearKeysDistances, prevNearKeysDistances,
|
|
|
|
prevPrevNearKeysDistances)) {
|
|
|
|
score += NOT_LOCALMIN_DISTANCE_SCORE;
|
|
|
|
} else if (nearest < NEAR_KEY_THRESHOLD) {
|
|
|
|
// Promote points nearby keys
|
|
|
|
score += LOCALMIN_DISTANCE_AND_NEAR_TO_KEY_SCORE;
|
2012-08-31 12:22:14 +00:00
|
|
|
}
|
|
|
|
// Angle
|
2012-11-16 10:03:36 +00:00
|
|
|
const float angle1 = getAngle(x, y, mSampledInputXs.back(), mSampledInputYs.back());
|
|
|
|
const float angle2 = getAngle(mSampledInputXs.back(), mSampledInputYs.back(),
|
|
|
|
mSampledInputXs[size - 2], mSampledInputYs[size - 2]);
|
2012-09-07 03:56:31 +00:00
|
|
|
const float angleDiff = getAngleDiff(angle1, angle2);
|
2012-10-11 04:08:06 +00:00
|
|
|
|
2012-09-07 03:56:31 +00:00
|
|
|
// Save corner
|
|
|
|
if (distPrev > baseSampleRate * CORNER_CHECK_DISTANCE_THRESHOLD_SCALE
|
2012-10-11 04:08:06 +00:00
|
|
|
&& (sumAngle > CORNER_SUM_ANGLE_THRESHOLD || angleDiff > CORNER_ANGLE_THRESHOLD)) {
|
2012-09-07 03:56:31 +00:00
|
|
|
score += CORNER_SCORE;
|
2012-08-31 12:22:14 +00:00
|
|
|
}
|
|
|
|
return score;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sampling touch point and pushing information to vectors.
|
|
|
|
// Returning if previous point is popped or not.
|
2012-10-29 09:06:22 +00:00
|
|
|
bool ProximityInfoState::pushTouchPoint(const int inputIndex, const int nodeCodePoint, int x, int y,
|
2012-10-11 04:08:06 +00:00
|
|
|
const int time, const bool sample, const bool isLastPoint, const float sumAngle,
|
2012-08-31 12:22:14 +00:00
|
|
|
NearKeysDistanceMap *const currentNearKeysDistances,
|
|
|
|
const NearKeysDistanceMap *const prevNearKeysDistances,
|
|
|
|
const NearKeysDistanceMap *const prevPrevNearKeysDistances) {
|
2012-10-09 10:57:08 +00:00
|
|
|
static const int LAST_POINT_SKIP_DISTANCE_SCALE = 4;
|
2012-08-31 12:22:14 +00:00
|
|
|
|
2012-11-16 10:03:36 +00:00
|
|
|
size_t size = mSampledInputXs.size();
|
2012-08-31 12:22:14 +00:00
|
|
|
bool popped = false;
|
2012-10-29 09:06:22 +00:00
|
|
|
if (nodeCodePoint < 0 && sample) {
|
2012-08-31 12:22:14 +00:00
|
|
|
const float nearest = updateNearKeysDistances(x, y, currentNearKeysDistances);
|
2012-10-11 04:08:06 +00:00
|
|
|
const float score = getPointScore(x, y, time, isLastPoint, nearest, sumAngle,
|
2012-08-31 12:22:14 +00:00
|
|
|
currentNearKeysDistances, prevNearKeysDistances, prevPrevNearKeysDistances);
|
|
|
|
if (score < 0) {
|
|
|
|
// Pop previous point because it would be useless.
|
2012-09-07 12:04:12 +00:00
|
|
|
popInputData();
|
2012-11-16 10:03:36 +00:00
|
|
|
size = mSampledInputXs.size();
|
2012-08-31 12:22:14 +00:00
|
|
|
popped = true;
|
|
|
|
} else {
|
|
|
|
popped = false;
|
|
|
|
}
|
|
|
|
// Check if the last point should be skipped.
|
2012-10-09 10:57:08 +00:00
|
|
|
if (isLastPoint && size > 0) {
|
2013-01-21 02:37:54 +00:00
|
|
|
if (ProximityInfoUtils::getDistanceInt(x, y, mSampledInputXs.back(),
|
|
|
|
mSampledInputYs.back()) * LAST_POINT_SKIP_DISTANCE_SCALE
|
|
|
|
< mProximityInfo->getMostCommonKeyWidth()) {
|
2012-10-09 10:57:08 +00:00
|
|
|
// This point is not used because it's too close to the previous point.
|
2012-09-11 08:47:55 +00:00
|
|
|
if (DEBUG_GEO_FULL) {
|
2012-10-09 10:57:08 +00:00
|
|
|
AKLOGI("p0: size = %zd, x = %d, y = %d, lx = %d, ly = %d, dist = %d, "
|
2012-11-16 10:03:36 +00:00
|
|
|
"width = %d", size, x, y, mSampledInputXs.back(), mSampledInputYs.back(),
|
2013-01-21 02:37:54 +00:00
|
|
|
ProximityInfoUtils::getDistanceInt(x, y, mSampledInputXs.back(),
|
|
|
|
mSampledInputYs.back()),
|
2012-09-11 08:47:55 +00:00
|
|
|
mProximityInfo->getMostCommonKeyWidth()
|
2012-10-09 10:57:08 +00:00
|
|
|
/ LAST_POINT_SKIP_DISTANCE_SCALE);
|
2012-09-11 08:47:55 +00:00
|
|
|
}
|
2012-08-31 12:22:14 +00:00
|
|
|
return popped;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-29 09:06:22 +00:00
|
|
|
if (nodeCodePoint >= 0 && (x < 0 || y < 0)) {
|
|
|
|
const int keyId = mProximityInfo->getKeyIndexOf(nodeCodePoint);
|
2012-08-23 06:46:43 +00:00
|
|
|
if (keyId >= 0) {
|
2012-09-04 03:49:46 +00:00
|
|
|
x = mProximityInfo->getKeyCenterXOfKeyIdG(keyId);
|
|
|
|
y = mProximityInfo->getKeyCenterYOfKeyIdG(keyId);
|
2012-08-23 06:46:43 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-31 12:22:14 +00:00
|
|
|
|
|
|
|
// Pushing point information.
|
|
|
|
if (size > 0) {
|
|
|
|
mLengthCache.push_back(
|
2013-01-21 02:37:54 +00:00
|
|
|
mLengthCache.back() + ProximityInfoUtils::getDistanceInt(
|
2012-11-16 10:03:36 +00:00
|
|
|
x, y, mSampledInputXs.back(), mSampledInputYs.back()));
|
2012-08-31 12:22:14 +00:00
|
|
|
} else {
|
|
|
|
mLengthCache.push_back(0);
|
|
|
|
}
|
2012-11-16 10:03:36 +00:00
|
|
|
mSampledInputXs.push_back(x);
|
|
|
|
mSampledInputYs.push_back(y);
|
2012-08-23 06:46:43 +00:00
|
|
|
mTimes.push_back(time);
|
2012-09-07 12:04:12 +00:00
|
|
|
mInputIndice.push_back(inputIndex);
|
2012-09-11 06:51:38 +00:00
|
|
|
if (DEBUG_GEO_FULL) {
|
|
|
|
AKLOGI("pushTouchPoint: x = %03d, y = %03d, time = %d, index = %d, popped ? %01d",
|
|
|
|
x, y, time, inputIndex, popped);
|
|
|
|
}
|
2012-08-31 12:22:14 +00:00
|
|
|
return popped;
|
2012-08-23 06:46:43 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 06:29:44 +00:00
|
|
|
float ProximityInfoState::calculateNormalizedSquaredDistance(
|
|
|
|
const int keyIndex, const int inputIndex) const {
|
|
|
|
if (keyIndex == NOT_AN_INDEX) {
|
|
|
|
return NOT_A_DISTANCE_FLOAT;
|
|
|
|
}
|
|
|
|
if (!mProximityInfo->hasSweetSpotData(keyIndex)) {
|
|
|
|
return NOT_A_DISTANCE_FLOAT;
|
|
|
|
}
|
2012-11-16 10:03:36 +00:00
|
|
|
if (NOT_A_COORDINATE == mSampledInputXs[inputIndex]) {
|
2012-06-08 06:29:44 +00:00
|
|
|
return NOT_A_DISTANCE_FLOAT;
|
|
|
|
}
|
|
|
|
const float squaredDistance = calculateSquaredDistanceFromSweetSpotCenter(
|
|
|
|
keyIndex, inputIndex);
|
|
|
|
const float squaredRadius = square(mProximityInfo->getSweetSpotRadiiAt(keyIndex));
|
|
|
|
return squaredDistance / squaredRadius;
|
|
|
|
}
|
|
|
|
|
2012-08-23 06:46:43 +00:00
|
|
|
int ProximityInfoState::getDuration(const int index) const {
|
2012-11-16 10:03:36 +00:00
|
|
|
if (index >= 0 && index < mSampledInputSize - 1) {
|
2012-09-19 05:56:09 +00:00
|
|
|
return mTimes[index + 1] - mTimes[index];
|
2012-08-23 06:46:43 +00:00
|
|
|
}
|
2012-08-27 05:39:17 +00:00
|
|
|
return 0;
|
2012-08-23 06:46:43 +00:00
|
|
|
}
|
|
|
|
|
2012-12-12 11:19:18 +00:00
|
|
|
// TODO: Remove the "scale" parameter
|
|
|
|
// This function basically converts from a length to an edit distance. Accordingly, it's obviously
|
|
|
|
// wrong to compare with mMaxPointToKeyLength.
|
|
|
|
float ProximityInfoState::getPointToKeyLength(
|
|
|
|
const int inputIndex, const int codePoint, const float scale) const {
|
2012-10-11 04:08:06 +00:00
|
|
|
const int keyId = mProximityInfo->getKeyIndexOf(codePoint);
|
|
|
|
if (keyId != NOT_AN_INDEX) {
|
|
|
|
const int index = inputIndex * mProximityInfo->getKeyCount() + keyId;
|
2013-01-15 10:16:38 +00:00
|
|
|
return min(mDistanceCache_G[index] * scale, mMaxPointToKeyLength);
|
2012-10-11 04:08:06 +00:00
|
|
|
}
|
2012-10-29 09:06:22 +00:00
|
|
|
if (isSkippableCodePoint(codePoint)) {
|
2012-10-09 10:57:08 +00:00
|
|
|
return 0.0f;
|
|
|
|
}
|
2012-10-11 04:08:06 +00:00
|
|
|
// If the char is not a key on the keyboard then return the max length.
|
|
|
|
return MAX_POINT_TO_KEY_LENGTH;
|
2012-10-09 10:57:08 +00:00
|
|
|
}
|
|
|
|
|
2013-01-15 10:16:38 +00:00
|
|
|
float ProximityInfoState::getPointToKeyLength_G(const int inputIndex, const int codePoint) const {
|
2012-12-12 11:19:18 +00:00
|
|
|
return getPointToKeyLength(inputIndex, codePoint, 1.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Remove the "scale" parameter
|
|
|
|
// This function basically converts from a length to an edit distance. Accordingly, it's obviously
|
|
|
|
// wrong to compare with mMaxPointToKeyLength.
|
|
|
|
float ProximityInfoState::getPointToKeyByIdLength(
|
|
|
|
const int inputIndex, const int keyId, const float scale) const {
|
2012-09-04 00:28:27 +00:00
|
|
|
if (keyId != NOT_AN_INDEX) {
|
2012-08-23 06:46:43 +00:00
|
|
|
const int index = inputIndex * mProximityInfo->getKeyCount() + keyId;
|
2013-01-15 10:16:38 +00:00
|
|
|
return min(mDistanceCache_G[index] * scale, mMaxPointToKeyLength);
|
2012-09-05 11:26:01 +00:00
|
|
|
}
|
2012-09-04 00:28:27 +00:00
|
|
|
// If the char is not a key on the keyboard then return the max length.
|
2012-10-09 10:57:08 +00:00
|
|
|
return static_cast<float>(MAX_POINT_TO_KEY_LENGTH);
|
2012-08-23 06:46:43 +00:00
|
|
|
}
|
|
|
|
|
2012-12-12 11:19:18 +00:00
|
|
|
float ProximityInfoState::getPointToKeyByIdLength(const int inputIndex, const int keyId) const {
|
|
|
|
return getPointToKeyByIdLength(inputIndex, keyId, 1.0f);
|
|
|
|
}
|
|
|
|
|
2012-11-02 09:29:03 +00:00
|
|
|
// In the following function, c is the current character of the dictionary word currently examined.
|
|
|
|
// currentChars is an array containing the keys close to the character the user actually typed at
|
|
|
|
// the same position. We want to see if c is in it: if so, then the word contains at that position
|
|
|
|
// a character close to what the user typed.
|
|
|
|
// What the user typed is actually the first character of the array.
|
|
|
|
// proximityIndex is a pointer to the variable where getMatchedProximityId returns the index of c
|
|
|
|
// in the proximity chars of the input index.
|
|
|
|
// Notice : accented characters do not have a proximity list, so they are alone in their list. The
|
|
|
|
// non-accented version of the character should be considered "close", but not the other keys close
|
|
|
|
// to the non-accented version.
|
|
|
|
ProximityType ProximityInfoState::getMatchedProximityId(const int index, const int c,
|
|
|
|
const bool checkProximityChars, int *proximityIndex) const {
|
|
|
|
const int *currentCodePoints = getProximityCodePointsAt(index);
|
|
|
|
const int firstCodePoint = currentCodePoints[0];
|
|
|
|
const int baseLowerC = toBaseLowerCase(c);
|
|
|
|
|
|
|
|
// The first char in the array is what user typed. If it matches right away, that means the
|
|
|
|
// user typed that same char for this pos.
|
|
|
|
if (firstCodePoint == baseLowerC || firstCodePoint == c) {
|
|
|
|
return EQUIVALENT_CHAR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!checkProximityChars) return UNRELATED_CHAR;
|
|
|
|
|
|
|
|
// If the non-accented, lowercased version of that first character matches c, then we have a
|
|
|
|
// non-accented version of the accented character the user typed. Treat it as a close char.
|
|
|
|
if (toBaseLowerCase(firstCodePoint) == baseLowerC) {
|
|
|
|
return NEAR_PROXIMITY_CHAR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not an exact nor an accent-alike match: search the list of close keys
|
|
|
|
int j = 1;
|
|
|
|
while (j < MAX_PROXIMITY_CHARS_SIZE_INTERNAL
|
|
|
|
&& currentCodePoints[j] > ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE) {
|
|
|
|
const bool matched = (currentCodePoints[j] == baseLowerC || currentCodePoints[j] == c);
|
|
|
|
if (matched) {
|
|
|
|
if (proximityIndex) {
|
|
|
|
*proximityIndex = j;
|
|
|
|
}
|
|
|
|
return NEAR_PROXIMITY_CHAR;
|
|
|
|
}
|
|
|
|
++j;
|
|
|
|
}
|
|
|
|
if (j < MAX_PROXIMITY_CHARS_SIZE_INTERNAL
|
|
|
|
&& currentCodePoints[j] == ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE) {
|
|
|
|
++j;
|
|
|
|
while (j < MAX_PROXIMITY_CHARS_SIZE_INTERNAL
|
|
|
|
&& currentCodePoints[j] > ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE) {
|
|
|
|
const bool matched = (currentCodePoints[j] == baseLowerC || currentCodePoints[j] == c);
|
|
|
|
if (matched) {
|
|
|
|
if (proximityIndex) {
|
|
|
|
*proximityIndex = j;
|
|
|
|
}
|
|
|
|
return ADDITIONAL_PROXIMITY_CHAR;
|
|
|
|
}
|
|
|
|
++j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Was not included, signal this as an unrelated character.
|
|
|
|
return UNRELATED_CHAR;
|
|
|
|
}
|
|
|
|
|
2012-09-06 11:55:45 +00:00
|
|
|
int ProximityInfoState::getSpaceY() const {
|
2012-10-08 02:46:14 +00:00
|
|
|
const int keyId = mProximityInfo->getKeyIndexOf(KEYCODE_SPACE);
|
2012-09-04 03:49:46 +00:00
|
|
|
return mProximityInfo->getKeyCenterYOfKeyIdG(keyId);
|
2012-08-23 06:46:43 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 06:29:44 +00:00
|
|
|
float ProximityInfoState::calculateSquaredDistanceFromSweetSpotCenter(
|
|
|
|
const int keyIndex, const int inputIndex) const {
|
|
|
|
const float sweetSpotCenterX = mProximityInfo->getSweetSpotCenterXAt(keyIndex);
|
|
|
|
const float sweetSpotCenterY = mProximityInfo->getSweetSpotCenterYAt(keyIndex);
|
2012-11-16 10:03:36 +00:00
|
|
|
const float inputX = static_cast<float>(mSampledInputXs[inputIndex]);
|
|
|
|
const float inputY = static_cast<float>(mSampledInputYs[inputIndex]);
|
2012-06-08 06:29:44 +00:00
|
|
|
return square(inputX - sweetSpotCenterX) + square(inputY - sweetSpotCenterY);
|
|
|
|
}
|
2012-09-04 08:00:24 +00:00
|
|
|
|
|
|
|
// Puts possible characters into filter and returns new filter size.
|
2012-12-10 14:36:22 +00:00
|
|
|
int ProximityInfoState::getAllPossibleChars(
|
|
|
|
const size_t index, int *const filter, const int filterSize) const {
|
2012-11-16 10:03:36 +00:00
|
|
|
if (index >= mSampledInputXs.size()) {
|
2012-09-04 08:00:24 +00:00
|
|
|
return filterSize;
|
|
|
|
}
|
2012-09-19 03:03:47 +00:00
|
|
|
int newFilterSize = filterSize;
|
2012-10-11 04:08:06 +00:00
|
|
|
const int keyCount = mProximityInfo->getKeyCount();
|
|
|
|
for (int j = 0; j < keyCount; ++j) {
|
|
|
|
if (mSearchKeysVector[index].test(j)) {
|
2012-12-10 14:36:22 +00:00
|
|
|
const int keyCodePoint = mProximityInfo->getCodePointOf(j);
|
2012-09-04 08:00:24 +00:00
|
|
|
bool insert = true;
|
|
|
|
// TODO: Avoid linear search
|
|
|
|
for (int k = 0; k < filterSize; ++k) {
|
|
|
|
if (filter[k] == keyCodePoint) {
|
|
|
|
insert = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (insert) {
|
2012-09-19 03:03:47 +00:00
|
|
|
filter[newFilterSize++] = keyCodePoint;
|
2012-09-04 08:00:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-09-19 03:03:47 +00:00
|
|
|
return newFilterSize;
|
2012-09-04 08:00:24 +00:00
|
|
|
}
|
2012-09-06 11:55:45 +00:00
|
|
|
|
2012-10-11 04:08:06 +00:00
|
|
|
bool ProximityInfoState::isKeyInSerchKeysAfterIndex(const int index, const int keyId) const {
|
|
|
|
ASSERT(keyId >= 0);
|
2012-11-16 10:03:36 +00:00
|
|
|
ASSERT(index >= 0 && index < mSampledInputSize);
|
2012-10-11 04:08:06 +00:00
|
|
|
return mSearchKeysVector[index].test(keyId);
|
|
|
|
}
|
|
|
|
|
2012-09-07 12:04:12 +00:00
|
|
|
void ProximityInfoState::popInputData() {
|
2012-11-16 10:03:36 +00:00
|
|
|
mSampledInputXs.pop_back();
|
|
|
|
mSampledInputYs.pop_back();
|
2012-09-07 12:04:12 +00:00
|
|
|
mTimes.pop_back();
|
|
|
|
mLengthCache.pop_back();
|
|
|
|
mInputIndice.pop_back();
|
|
|
|
}
|
|
|
|
|
2012-10-11 04:08:06 +00:00
|
|
|
float ProximityInfoState::getDirection(const int index0, const int index1) const {
|
2012-11-16 10:03:36 +00:00
|
|
|
if (index0 < 0 || index0 > mSampledInputSize - 1) {
|
2012-10-11 04:08:06 +00:00
|
|
|
return 0.0f;
|
|
|
|
}
|
2012-11-16 10:03:36 +00:00
|
|
|
if (index1 < 0 || index1 > mSampledInputSize - 1) {
|
2012-10-11 04:08:06 +00:00
|
|
|
return 0.0f;
|
|
|
|
}
|
2012-11-16 10:03:36 +00:00
|
|
|
const int x1 = mSampledInputXs[index0];
|
|
|
|
const int y1 = mSampledInputYs[index0];
|
|
|
|
const int x2 = mSampledInputXs[index1];
|
|
|
|
const int y2 = mSampledInputYs[index1];
|
2012-10-11 04:08:06 +00:00
|
|
|
return getAngle(x1, y1, x2, y2);
|
|
|
|
}
|
|
|
|
|
2012-10-09 10:57:08 +00:00
|
|
|
float ProximityInfoState::getPointAngle(const int index) const {
|
2012-11-16 10:03:36 +00:00
|
|
|
if (index <= 0 || index >= mSampledInputSize - 1) {
|
2012-10-09 10:57:08 +00:00
|
|
|
return 0.0f;
|
|
|
|
}
|
2012-10-11 04:08:06 +00:00
|
|
|
const float previousDirection = getDirection(index - 1, index);
|
|
|
|
const float nextDirection = getDirection(index, index + 1);
|
2012-10-09 10:57:08 +00:00
|
|
|
const float directionDiff = getAngleDiff(previousDirection, nextDirection);
|
|
|
|
return directionDiff;
|
|
|
|
}
|
|
|
|
|
|
|
|
float ProximityInfoState::getPointsAngle(
|
|
|
|
const int index0, const int index1, const int index2) const {
|
2012-11-16 10:03:36 +00:00
|
|
|
if (index0 < 0 || index0 > mSampledInputSize - 1) {
|
2012-10-09 10:57:08 +00:00
|
|
|
return 0.0f;
|
|
|
|
}
|
2012-11-16 10:03:36 +00:00
|
|
|
if (index1 < 0 || index1 > mSampledInputSize - 1) {
|
2012-10-09 10:57:08 +00:00
|
|
|
return 0.0f;
|
|
|
|
}
|
2012-11-16 10:03:36 +00:00
|
|
|
if (index2 < 0 || index2 > mSampledInputSize - 1) {
|
2012-10-09 10:57:08 +00:00
|
|
|
return 0.0f;
|
|
|
|
}
|
2012-10-11 04:08:06 +00:00
|
|
|
const float previousDirection = getDirection(index0, index1);
|
|
|
|
const float nextDirection = getDirection(index1, index2);
|
|
|
|
return getAngleDiff(previousDirection, nextDirection);
|
|
|
|
}
|
|
|
|
|
|
|
|
float ProximityInfoState::getLineToKeyDistance(
|
|
|
|
const int from, const int to, const int keyId, const bool extend) const {
|
2012-11-16 10:03:36 +00:00
|
|
|
if (from < 0 || from > mSampledInputSize - 1) {
|
2012-10-11 04:08:06 +00:00
|
|
|
return 0.0f;
|
|
|
|
}
|
2012-11-16 10:03:36 +00:00
|
|
|
if (to < 0 || to > mSampledInputSize - 1) {
|
2012-10-11 04:08:06 +00:00
|
|
|
return 0.0f;
|
|
|
|
}
|
2012-11-16 10:03:36 +00:00
|
|
|
const int x0 = mSampledInputXs[from];
|
|
|
|
const int y0 = mSampledInputYs[from];
|
|
|
|
const int x1 = mSampledInputXs[to];
|
|
|
|
const int y1 = mSampledInputYs[to];
|
2012-10-11 04:08:06 +00:00
|
|
|
|
|
|
|
const int keyX = mProximityInfo->getKeyCenterXOfKeyIdG(keyId);
|
|
|
|
const int keyY = mProximityInfo->getKeyCenterYOfKeyIdG(keyId);
|
|
|
|
|
2013-01-21 02:37:54 +00:00
|
|
|
return ProximityInfoUtils::pointToLineSegSquaredDistanceFloat(
|
|
|
|
keyX, keyY, x0, y0, x1, y1, extend);
|
2012-10-09 10:57:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Updates probabilities of aligning to some keys and skipping.
|
|
|
|
// Word suggestion should be based on this probabilities.
|
2012-10-11 04:08:06 +00:00
|
|
|
void ProximityInfoState::updateAlignPointProbabilities(const int start) {
|
|
|
|
static const float MIN_PROBABILITY = 0.000001f;
|
|
|
|
static const float MAX_SKIP_PROBABILITY = 0.95f;
|
2012-10-09 10:57:08 +00:00
|
|
|
static const float SKIP_FIRST_POINT_PROBABILITY = 0.01f;
|
|
|
|
static const float SKIP_LAST_POINT_PROBABILITY = 0.1f;
|
2012-10-11 04:08:06 +00:00
|
|
|
static const float MIN_SPEED_RATE_FOR_SKIP_PROBABILITY = 0.15f;
|
|
|
|
static const float SPEED_WEIGHT_FOR_SKIP_PROBABILITY = 0.9f;
|
|
|
|
static const float SLOW_STRAIGHT_WEIGHT_FOR_SKIP_PROBABILITY = 0.6f;
|
|
|
|
static const float NEAREST_DISTANCE_WEIGHT = 0.5f;
|
|
|
|
static const float NEAREST_DISTANCE_BIAS = 0.5f;
|
|
|
|
static const float NEAREST_DISTANCE_WEIGHT_FOR_LAST = 0.6f;
|
|
|
|
static const float NEAREST_DISTANCE_BIAS_FOR_LAST = 0.4f;
|
|
|
|
|
|
|
|
static const float ANGLE_WEIGHT = 0.90f;
|
|
|
|
static const float DEEP_CORNER_ANGLE_THRESHOLD = M_PI_F * 60.0f / 180.0f;
|
|
|
|
static const float SKIP_DEEP_CORNER_PROBABILITY = 0.1f;
|
|
|
|
static const float CORNER_ANGLE_THRESHOLD = M_PI_F * 30.0f / 180.0f;
|
2012-10-09 10:57:08 +00:00
|
|
|
static const float STRAIGHT_ANGLE_THRESHOLD = M_PI_F * 15.0f / 180.0f;
|
2012-10-11 04:08:06 +00:00
|
|
|
static const float SKIP_CORNER_PROBABILITY = 0.4f;
|
|
|
|
static const float SPEED_MARGIN = 0.1f;
|
2012-10-09 10:57:08 +00:00
|
|
|
static const float CENTER_VALUE_OF_NORMALIZED_DISTRIBUTION = 0.0f;
|
|
|
|
|
2012-10-11 04:08:06 +00:00
|
|
|
const int keyCount = mProximityInfo->getKeyCount();
|
2012-11-16 10:03:36 +00:00
|
|
|
mCharProbabilities.resize(mSampledInputSize);
|
2012-10-09 10:57:08 +00:00
|
|
|
// Calculates probabilities of using a point as a correlated point with the character
|
|
|
|
// for each point.
|
2012-11-16 10:03:36 +00:00
|
|
|
for (int i = start; i < mSampledInputSize; ++i) {
|
2012-10-11 04:08:06 +00:00
|
|
|
mCharProbabilities[i].clear();
|
|
|
|
// First, calculates skip probability. Starts form MIN_SKIP_PROBABILITY.
|
2012-10-09 10:57:08 +00:00
|
|
|
// Note that all values that are multiplied to this probability should be in [0.0, 1.0];
|
2012-10-11 04:08:06 +00:00
|
|
|
float skipProbability = MAX_SKIP_PROBABILITY;
|
|
|
|
|
|
|
|
const float currentAngle = getPointAngle(i);
|
2012-11-16 14:06:41 +00:00
|
|
|
const float speedRate = getSpeedRate(i);
|
2012-10-11 04:08:06 +00:00
|
|
|
|
|
|
|
float nearestKeyDistance = static_cast<float>(MAX_POINT_TO_KEY_LENGTH);
|
|
|
|
for (int j = 0; j < keyCount; ++j) {
|
|
|
|
if (mNearKeysVector[i].test(j)) {
|
|
|
|
const float distance = getPointToKeyByIdLength(i, j);
|
|
|
|
if (distance < nearestKeyDistance) {
|
|
|
|
nearestKeyDistance = distance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-10-09 10:57:08 +00:00
|
|
|
|
|
|
|
if (i == 0) {
|
2012-10-11 04:08:06 +00:00
|
|
|
skipProbability *= min(1.0f, nearestKeyDistance * NEAREST_DISTANCE_WEIGHT
|
|
|
|
+ NEAREST_DISTANCE_BIAS);
|
|
|
|
// Promote the first point
|
2012-10-09 10:57:08 +00:00
|
|
|
skipProbability *= SKIP_FIRST_POINT_PROBABILITY;
|
2012-11-16 10:03:36 +00:00
|
|
|
} else if (i == mSampledInputSize - 1) {
|
2012-10-11 04:08:06 +00:00
|
|
|
skipProbability *= min(1.0f, nearestKeyDistance * NEAREST_DISTANCE_WEIGHT_FOR_LAST
|
|
|
|
+ NEAREST_DISTANCE_BIAS_FOR_LAST);
|
|
|
|
// Promote the last point
|
2012-10-09 10:57:08 +00:00
|
|
|
skipProbability *= SKIP_LAST_POINT_PROBABILITY;
|
|
|
|
} else {
|
2012-10-11 04:08:06 +00:00
|
|
|
// If the current speed is relatively slower than adjacent keys, we promote this point.
|
2012-11-16 14:06:41 +00:00
|
|
|
if (getSpeedRate(i - 1) - SPEED_MARGIN > speedRate
|
|
|
|
&& speedRate < getSpeedRate(i + 1) - SPEED_MARGIN) {
|
2012-10-11 04:08:06 +00:00
|
|
|
if (currentAngle < CORNER_ANGLE_THRESHOLD) {
|
2012-11-16 14:06:41 +00:00
|
|
|
skipProbability *= min(1.0f, speedRate
|
2012-10-11 04:08:06 +00:00
|
|
|
* SLOW_STRAIGHT_WEIGHT_FOR_SKIP_PROBABILITY);
|
|
|
|
} else {
|
|
|
|
// If the angle is small enough, we promote this point more. (e.g. pit vs put)
|
2012-11-16 14:06:41 +00:00
|
|
|
skipProbability *= min(1.0f, speedRate * SPEED_WEIGHT_FOR_SKIP_PROBABILITY
|
2012-10-11 04:08:06 +00:00
|
|
|
+ MIN_SPEED_RATE_FOR_SKIP_PROBABILITY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-16 14:06:41 +00:00
|
|
|
skipProbability *= min(1.0f, speedRate * nearestKeyDistance *
|
2012-10-11 04:08:06 +00:00
|
|
|
NEAREST_DISTANCE_WEIGHT + NEAREST_DISTANCE_BIAS);
|
2012-10-09 10:57:08 +00:00
|
|
|
|
|
|
|
// Adjusts skip probability by a rate depending on angle.
|
|
|
|
// ANGLE_RATE of skipProbability is adjusted by current angle.
|
2012-10-11 04:08:06 +00:00
|
|
|
skipProbability *= (M_PI_F - currentAngle) / M_PI_F * ANGLE_WEIGHT
|
|
|
|
+ (1.0f - ANGLE_WEIGHT);
|
2012-10-09 10:57:08 +00:00
|
|
|
if (currentAngle > DEEP_CORNER_ANGLE_THRESHOLD) {
|
|
|
|
skipProbability *= SKIP_DEEP_CORNER_PROBABILITY;
|
|
|
|
}
|
2012-10-11 04:08:06 +00:00
|
|
|
// We assume the angle of this point is the angle for point[i], point[i - 2]
|
|
|
|
// and point[i - 3]. The reason why we don't use the angle for point[i], point[i - 1]
|
|
|
|
// and point[i - 2] is this angle can be more affected by the noise.
|
|
|
|
const float prevAngle = getPointsAngle(i, i - 2, i - 3);
|
|
|
|
if (i >= 3 && prevAngle < STRAIGHT_ANGLE_THRESHOLD
|
|
|
|
&& currentAngle > CORNER_ANGLE_THRESHOLD) {
|
2012-10-09 10:57:08 +00:00
|
|
|
skipProbability *= SKIP_CORNER_PROBABILITY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-11 04:08:06 +00:00
|
|
|
// probabilities must be in [0.0, MAX_SKIP_PROBABILITY];
|
2012-10-09 10:57:08 +00:00
|
|
|
ASSERT(skipProbability >= 0.0f);
|
2012-10-11 04:08:06 +00:00
|
|
|
ASSERT(skipProbability <= MAX_SKIP_PROBABILITY);
|
2012-10-09 10:57:08 +00:00
|
|
|
mCharProbabilities[i][NOT_AN_INDEX] = skipProbability;
|
2012-10-11 04:08:06 +00:00
|
|
|
|
2012-10-09 10:57:08 +00:00
|
|
|
// Second, calculates key probabilities by dividing the rest probability
|
|
|
|
// (1.0f - skipProbability).
|
|
|
|
const float inputCharProbability = 1.0f - skipProbability;
|
2012-10-11 04:08:06 +00:00
|
|
|
|
|
|
|
// TODO: The variance is critical for accuracy; thus, adjusting these parameter by machine
|
|
|
|
// learning or something would be efficient.
|
|
|
|
static const float SPEEDxANGLE_WEIGHT_FOR_STANDARD_DIVIATION = 0.3f;
|
|
|
|
static const float MAX_SPEEDxANGLE_RATE_FOR_STANDERD_DIVIATION = 0.25f;
|
|
|
|
static const float SPEEDxNEAREST_WEIGHT_FOR_STANDARD_DIVIATION = 0.5f;
|
|
|
|
static const float MAX_SPEEDxNEAREST_RATE_FOR_STANDERD_DIVIATION = 0.15f;
|
|
|
|
static const float MIN_STANDERD_DIVIATION = 0.37f;
|
|
|
|
|
2012-11-16 14:06:41 +00:00
|
|
|
const float speedxAngleRate = min(speedRate * currentAngle / M_PI_F
|
2012-10-11 04:08:06 +00:00
|
|
|
* SPEEDxANGLE_WEIGHT_FOR_STANDARD_DIVIATION,
|
|
|
|
MAX_SPEEDxANGLE_RATE_FOR_STANDERD_DIVIATION);
|
2012-11-16 14:06:41 +00:00
|
|
|
const float speedxNearestKeyDistanceRate = min(speedRate * nearestKeyDistance
|
2012-10-11 04:08:06 +00:00
|
|
|
* SPEEDxNEAREST_WEIGHT_FOR_STANDARD_DIVIATION,
|
|
|
|
MAX_SPEEDxNEAREST_RATE_FOR_STANDERD_DIVIATION);
|
|
|
|
const float sigma = speedxAngleRate + speedxNearestKeyDistanceRate + MIN_STANDERD_DIVIATION;
|
|
|
|
|
2013-01-21 02:37:54 +00:00
|
|
|
ProximityInfoUtils::NormalDistribution
|
|
|
|
distribution(CENTER_VALUE_OF_NORMALIZED_DISTRIBUTION, sigma);
|
2012-10-11 04:08:06 +00:00
|
|
|
static const float PREV_DISTANCE_WEIGHT = 0.5f;
|
|
|
|
static const float NEXT_DISTANCE_WEIGHT = 0.6f;
|
|
|
|
// Summing up probability densities of all near keys.
|
|
|
|
float sumOfProbabilityDensities = 0.0f;
|
|
|
|
for (int j = 0; j < keyCount; ++j) {
|
2012-10-09 10:57:08 +00:00
|
|
|
if (mNearKeysVector[i].test(j)) {
|
2012-10-11 04:08:06 +00:00
|
|
|
float distance = sqrtf(getPointToKeyByIdLength(i, j));
|
2012-11-16 10:03:36 +00:00
|
|
|
if (i == 0 && i != mSampledInputSize - 1) {
|
2012-10-11 04:08:06 +00:00
|
|
|
// For the first point, weighted average of distances from first point and the
|
|
|
|
// next point to the key is used as a point to key distance.
|
|
|
|
const float nextDistance = sqrtf(getPointToKeyByIdLength(i + 1, j));
|
|
|
|
if (nextDistance < distance) {
|
|
|
|
// The distance of the first point tends to bigger than continuing
|
|
|
|
// points because the first touch by the user can be sloppy.
|
|
|
|
// So we promote the first point if the distance of that point is larger
|
|
|
|
// than the distance of the next point.
|
|
|
|
distance = (distance + nextDistance * NEXT_DISTANCE_WEIGHT)
|
|
|
|
/ (1.0f + NEXT_DISTANCE_WEIGHT);
|
|
|
|
}
|
2012-11-16 10:03:36 +00:00
|
|
|
} else if (i != 0 && i == mSampledInputSize - 1) {
|
2012-10-11 04:08:06 +00:00
|
|
|
// For the first point, weighted average of distances from last point and
|
|
|
|
// the previous point to the key is used as a point to key distance.
|
|
|
|
const float previousDistance = sqrtf(getPointToKeyByIdLength(i - 1, j));
|
|
|
|
if (previousDistance < distance) {
|
|
|
|
// The distance of the last point tends to bigger than continuing points
|
|
|
|
// because the last touch by the user can be sloppy. So we promote the
|
|
|
|
// last point if the distance of that point is larger than the distance of
|
|
|
|
// the previous point.
|
|
|
|
distance = (distance + previousDistance * PREV_DISTANCE_WEIGHT)
|
|
|
|
/ (1.0f + PREV_DISTANCE_WEIGHT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: Promote the first point when the extended line from the next input is near
|
|
|
|
// from a key. Also, promote the last point as well.
|
|
|
|
sumOfProbabilityDensities += distribution.getProbabilityDensity(distance);
|
2012-10-09 10:57:08 +00:00
|
|
|
}
|
|
|
|
}
|
2012-10-11 04:08:06 +00:00
|
|
|
|
|
|
|
// Split the probability of an input point to keys that are close to the input point.
|
|
|
|
for (int j = 0; j < keyCount; ++j) {
|
2012-10-09 10:57:08 +00:00
|
|
|
if (mNearKeysVector[i].test(j)) {
|
2012-10-11 04:08:06 +00:00
|
|
|
float distance = sqrtf(getPointToKeyByIdLength(i, j));
|
2012-11-16 10:03:36 +00:00
|
|
|
if (i == 0 && i != mSampledInputSize - 1) {
|
2012-10-11 04:08:06 +00:00
|
|
|
// For the first point, weighted average of distances from the first point and
|
|
|
|
// the next point to the key is used as a point to key distance.
|
|
|
|
const float prevDistance = sqrtf(getPointToKeyByIdLength(i + 1, j));
|
|
|
|
if (prevDistance < distance) {
|
|
|
|
distance = (distance + prevDistance * NEXT_DISTANCE_WEIGHT)
|
|
|
|
/ (1.0f + NEXT_DISTANCE_WEIGHT);
|
|
|
|
}
|
2012-11-16 10:03:36 +00:00
|
|
|
} else if (i != 0 && i == mSampledInputSize - 1) {
|
2012-10-11 04:08:06 +00:00
|
|
|
// For the first point, weighted average of distances from last point and
|
|
|
|
// the previous point to the key is used as a point to key distance.
|
|
|
|
const float prevDistance = sqrtf(getPointToKeyByIdLength(i - 1, j));
|
|
|
|
if (prevDistance < distance) {
|
|
|
|
distance = (distance + prevDistance * PREV_DISTANCE_WEIGHT)
|
|
|
|
/ (1.0f + PREV_DISTANCE_WEIGHT);
|
|
|
|
}
|
2012-10-09 10:57:08 +00:00
|
|
|
}
|
2012-10-11 04:08:06 +00:00
|
|
|
const float probabilityDensity = distribution.getProbabilityDensity(distance);
|
|
|
|
const float probability = inputCharProbability * probabilityDensity
|
|
|
|
/ sumOfProbabilityDensities;
|
|
|
|
mCharProbabilities[i][j] = probability;
|
2012-10-09 10:57:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-11 04:08:06 +00:00
|
|
|
|
|
|
|
if (DEBUG_POINTS_PROBABILITY) {
|
2012-11-16 10:03:36 +00:00
|
|
|
for (int i = 0; i < mSampledInputSize; ++i) {
|
2012-10-11 04:08:06 +00:00
|
|
|
std::stringstream sstream;
|
|
|
|
sstream << i << ", ";
|
2012-11-16 10:03:36 +00:00
|
|
|
sstream << "(" << mSampledInputXs[i] << ", " << mSampledInputYs[i] << "), ";
|
2012-11-16 14:06:41 +00:00
|
|
|
sstream << "Speed: "<< getSpeedRate(i) << ", ";
|
2012-10-11 04:08:06 +00:00
|
|
|
sstream << "Angle: "<< getPointAngle(i) << ", \n";
|
|
|
|
|
|
|
|
for (hash_map_compat<int, float>::iterator it = mCharProbabilities[i].begin();
|
|
|
|
it != mCharProbabilities[i].end(); ++it) {
|
|
|
|
if (it->first == NOT_AN_INDEX) {
|
|
|
|
sstream << it->first
|
|
|
|
<< "(skip):"
|
|
|
|
<< it->second
|
|
|
|
<< "\n";
|
|
|
|
} else {
|
|
|
|
sstream << it->first
|
|
|
|
<< "("
|
|
|
|
<< static_cast<char>(mProximityInfo->getCodePointOf(it->first))
|
|
|
|
<< "):"
|
|
|
|
<< it->second
|
|
|
|
<< "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
AKLOGI("%s", sstream.str().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-09 10:57:08 +00:00
|
|
|
// Decrease key probabilities of points which don't have the highest probability of that key
|
|
|
|
// among nearby points. Probabilities of the first point and the last point are not suppressed.
|
2012-11-16 10:03:36 +00:00
|
|
|
for (int i = max(start, 1); i < mSampledInputSize; ++i) {
|
|
|
|
for (int j = i + 1; j < mSampledInputSize; ++j) {
|
2012-10-11 04:08:06 +00:00
|
|
|
if (!suppressCharProbabilities(i, j)) {
|
2012-10-09 10:57:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-10-11 04:08:06 +00:00
|
|
|
for (int j = i - 1; j >= max(start, 0); --j) {
|
|
|
|
if (!suppressCharProbabilities(i, j)) {
|
2012-10-09 10:57:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-11 04:08:06 +00:00
|
|
|
// Converting from raw probabilities to log probabilities to calculate spatial distance.
|
2012-11-16 10:03:36 +00:00
|
|
|
for (int i = start; i < mSampledInputSize; ++i) {
|
2012-10-11 04:08:06 +00:00
|
|
|
for (int j = 0; j < keyCount; ++j) {
|
|
|
|
hash_map_compat<int, float>::iterator it = mCharProbabilities[i].find(j);
|
|
|
|
if (it == mCharProbabilities[i].end()){
|
|
|
|
mNearKeysVector[i].reset(j);
|
|
|
|
} else if(it->second < MIN_PROBABILITY) {
|
|
|
|
// Erases from near keys vector because it has very low probability.
|
|
|
|
mNearKeysVector[i].reset(j);
|
|
|
|
mCharProbabilities[i].erase(j);
|
|
|
|
} else {
|
|
|
|
it->second = -logf(it->second);
|
2012-10-09 10:57:08 +00:00
|
|
|
}
|
|
|
|
}
|
2012-10-11 04:08:06 +00:00
|
|
|
mCharProbabilities[i][NOT_AN_INDEX] = -logf(mCharProbabilities[i][NOT_AN_INDEX]);
|
2012-10-09 10:57:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-11 04:08:06 +00:00
|
|
|
// Decreases char probabilities of index0 by checking probabilities of a near point (index1) and
|
|
|
|
// increases char probabilities of index1 by checking probabilities of index0.
|
2012-10-09 10:57:08 +00:00
|
|
|
bool ProximityInfoState::suppressCharProbabilities(const int index0, const int index1) {
|
2012-11-16 10:03:36 +00:00
|
|
|
ASSERT(0 <= index0 && index0 < mSampledInputSize);
|
|
|
|
ASSERT(0 <= index1 && index1 < mSampledInputSize);
|
2012-10-11 04:08:06 +00:00
|
|
|
|
2012-10-09 10:57:08 +00:00
|
|
|
static const float SUPPRESSION_LENGTH_WEIGHT = 1.5f;
|
2012-10-11 04:08:06 +00:00
|
|
|
static const float MIN_SUPPRESSION_RATE = 0.1f;
|
|
|
|
static const float SUPPRESSION_WEIGHT = 0.5f;
|
|
|
|
static const float SUPPRESSION_WEIGHT_FOR_PROBABILITY_GAIN = 0.1f;
|
|
|
|
static const float SKIP_PROBABALITY_WEIGHT_FOR_PROBABILITY_GAIN = 0.3f;
|
|
|
|
|
2012-10-09 10:57:08 +00:00
|
|
|
const float keyWidthFloat = static_cast<float>(mProximityInfo->getMostCommonKeyWidth());
|
|
|
|
const float diff = fabsf(static_cast<float>(mLengthCache[index0] - mLengthCache[index1]));
|
|
|
|
if (diff > keyWidthFloat * SUPPRESSION_LENGTH_WEIGHT) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-10-11 04:08:06 +00:00
|
|
|
const float suppressionRate = MIN_SUPPRESSION_RATE
|
|
|
|
+ diff / keyWidthFloat / SUPPRESSION_LENGTH_WEIGHT * SUPPRESSION_WEIGHT;
|
2012-10-09 10:57:08 +00:00
|
|
|
for (hash_map_compat<int, float>::iterator it = mCharProbabilities[index0].begin();
|
|
|
|
it != mCharProbabilities[index0].end(); ++it) {
|
2012-10-11 04:08:06 +00:00
|
|
|
hash_map_compat<int, float>::iterator it2 = mCharProbabilities[index1].find(it->first);
|
2012-10-09 10:57:08 +00:00
|
|
|
if (it2 != mCharProbabilities[index1].end() && it->second < it2->second) {
|
|
|
|
const float newProbability = it->second * suppressionRate;
|
2012-10-11 04:08:06 +00:00
|
|
|
const float suppression = it->second - newProbability;
|
2012-10-09 10:57:08 +00:00
|
|
|
it->second = newProbability;
|
2012-10-11 04:08:06 +00:00
|
|
|
// mCharProbabilities[index0][NOT_AN_INDEX] is the probability of skipping this point.
|
|
|
|
mCharProbabilities[index0][NOT_AN_INDEX] += suppression;
|
|
|
|
|
|
|
|
// Add the probability of the same key nearby index1
|
|
|
|
const float probabilityGain = min(suppression * SUPPRESSION_WEIGHT_FOR_PROBABILITY_GAIN,
|
|
|
|
mCharProbabilities[index1][NOT_AN_INDEX]
|
|
|
|
* SKIP_PROBABALITY_WEIGHT_FOR_PROBABILITY_GAIN);
|
|
|
|
it2->second += probabilityGain;
|
|
|
|
mCharProbabilities[index1][NOT_AN_INDEX] -= probabilityGain;
|
2012-10-09 10:57:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-12-17 06:20:58 +00:00
|
|
|
// Get a word that is detected by tracing the most probable string into codePointBuf and
|
2012-10-29 09:06:22 +00:00
|
|
|
// returns probability of generating the word.
|
2012-12-17 06:20:58 +00:00
|
|
|
float ProximityInfoState::getMostProbableString(int *const codePointBuf) const {
|
2012-10-12 10:46:23 +00:00
|
|
|
static const float DEMOTION_LOG_PROBABILITY = 0.3f;
|
2012-10-11 04:08:06 +00:00
|
|
|
int index = 0;
|
|
|
|
float sumLogProbability = 0.0f;
|
2012-10-09 10:57:08 +00:00
|
|
|
// TODO: Current implementation is greedy algorithm. DP would be efficient for many cases.
|
2013-01-11 16:18:00 +00:00
|
|
|
for (int i = 0; i < mSampledInputSize && index < MAX_WORD_LENGTH - 1; ++i) {
|
2012-10-11 04:08:06 +00:00
|
|
|
float minLogProbability = static_cast<float>(MAX_POINT_TO_KEY_LENGTH);
|
|
|
|
int character = NOT_AN_INDEX;
|
2012-10-09 10:57:08 +00:00
|
|
|
for (hash_map_compat<int, float>::const_iterator it = mCharProbabilities[i].begin();
|
|
|
|
it != mCharProbabilities[i].end(); ++it) {
|
2012-10-11 04:08:06 +00:00
|
|
|
const float logProbability = (it->first != NOT_AN_INDEX)
|
2012-10-12 10:46:23 +00:00
|
|
|
? it->second + DEMOTION_LOG_PROBABILITY : it->second;
|
2012-10-11 04:08:06 +00:00
|
|
|
if (logProbability < minLogProbability) {
|
|
|
|
minLogProbability = logProbability;
|
|
|
|
character = it->first;
|
2012-10-09 10:57:08 +00:00
|
|
|
}
|
|
|
|
}
|
2012-10-11 04:08:06 +00:00
|
|
|
if (character != NOT_AN_INDEX) {
|
2012-10-29 09:06:22 +00:00
|
|
|
codePointBuf[index] = mProximityInfo->getCodePointOf(character);
|
2012-10-09 10:57:08 +00:00
|
|
|
index++;
|
|
|
|
}
|
2012-10-11 04:08:06 +00:00
|
|
|
sumLogProbability += minLogProbability;
|
2012-10-09 10:57:08 +00:00
|
|
|
}
|
2012-10-29 09:06:22 +00:00
|
|
|
codePointBuf[index] = '\0';
|
2012-10-11 04:08:06 +00:00
|
|
|
return sumLogProbability;
|
|
|
|
}
|
|
|
|
|
2013-01-18 09:01:59 +00:00
|
|
|
bool ProximityInfoState::hasSpaceProximity(const int index) const {
|
|
|
|
ASSERT(0 <= index && index < mSampledInputSize);
|
|
|
|
return mProximityInfo->hasSpaceProximity(getInputX(index), getInputY(index));
|
|
|
|
}
|
|
|
|
|
2012-10-11 04:08:06 +00:00
|
|
|
// Returns a probability of mapping index to keyIndex.
|
|
|
|
float ProximityInfoState::getProbability(const int index, const int keyIndex) const {
|
2012-11-16 10:03:36 +00:00
|
|
|
ASSERT(0 <= index && index < mSampledInputSize);
|
2012-10-11 04:08:06 +00:00
|
|
|
hash_map_compat<int, float>::const_iterator it = mCharProbabilities[index].find(keyIndex);
|
|
|
|
if (it != mCharProbabilities[index].end()) {
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
return static_cast<float>(MAX_POINT_TO_KEY_LENGTH);
|
2012-10-09 10:57:08 +00:00
|
|
|
}
|
2012-06-05 08:55:52 +00:00
|
|
|
} // namespace latinime
|