1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef LATINIME_PROXIMITY_INFO_STATE_UTILS_H
18 #define LATINIME_PROXIMITY_INFO_STATE_UTILS_H
19 
20 #include <bitset>
21 #include <unordered_map>
22 #include <vector>
23 
24 #include "defines.h"
25 
26 namespace latinime {
27 class ProximityInfo;
28 class ProximityInfoParams;
29 
30 class ProximityInfoStateUtils {
31  public:
32     typedef std::unordered_map<int, float> NearKeysDistanceMap;
33     typedef std::bitset<MAX_KEY_COUNT_IN_A_KEYBOARD> NearKeycodesSet;
34 
35     static int trimLastTwoTouchPoints(std::vector<int> *sampledInputXs,
36             std::vector<int> *sampledInputYs, std::vector<int> *sampledInputTimes,
37             std::vector<int> *sampledLengthCache, std::vector<int> *sampledInputIndice);
38     static int updateTouchPoints(const ProximityInfo *const proximityInfo,
39             const int maxPointToKeyLength, const int *const inputProximities,
40             const int *const inputXCoordinates, const int *const inputYCoordinates,
41             const int *const times, const int *const pointerIds, const int inputSize,
42             const bool isGeometric, const int pointerId, const int pushTouchPointStartIndex,
43             std::vector<int> *sampledInputXs, std::vector<int> *sampledInputYs,
44             std::vector<int> *sampledInputTimes, std::vector<int> *sampledLengthCache,
45             std::vector<int> *sampledInputIndice);
46     static const int *getProximityCodePointsAt(const int *const inputProximities, const int index);
47     static int getPrimaryCodePointAt(const int *const inputProximities, const int index);
48     static void popInputData(std::vector<int> *sampledInputXs, std::vector<int> *sampledInputYs,
49             std::vector<int> *sampledInputTimes, std::vector<int> *sampledLengthCache,
50             std::vector<int> *sampledInputIndice);
51     static float refreshSpeedRates(const int inputSize, const int *const xCoordinates,
52             const int *const yCoordinates, const int *const times, const int lastSavedInputSize,
53             const int sampledInputSize, const std::vector<int> *const sampledInputXs,
54             const std::vector<int> *const sampledInputYs,
55             const std::vector<int> *const sampledInputTimes,
56             const std::vector<int> *const sampledLengthCache,
57             const std::vector<int> *const sampledInputIndice,
58             std::vector<float> *sampledSpeedRates, std::vector<float> *sampledDirections);
59     static void refreshBeelineSpeedRates(const int mostCommonKeyWidth, const float averageSpeed,
60             const int inputSize, const int *const xCoordinates, const int *const yCoordinates,
61             const int *times, const int sampledInputSize,
62             const std::vector<int> *const sampledInputXs,
63             const std::vector<int> *const sampledInputYs, const std::vector<int> *const inputIndice,
64             std::vector<int> *beelineSpeedPercentiles);
65     static float getDirection(const std::vector<int> *const sampledInputXs,
66             const std::vector<int> *const sampledInputYs, const int index0, const int index1);
67     static void updateAlignPointProbabilities(const float maxPointToKeyLength,
68             const int mostCommonKeyWidth, const int keyCount, const int start,
69             const int sampledInputSize, const std::vector<int> *const sampledInputXs,
70             const std::vector<int> *const sampledInputYs,
71             const std::vector<float> *const sampledSpeedRates,
72             const std::vector<int> *const sampledLengthCache,
73             const std::vector<float> *const sampledNormalizedSquaredLengthCache,
74             const ProximityInfo *const proximityInfo,
75             std::vector<std::unordered_map<int, float>> *charProbabilities);
76     static void updateSampledSearchKeySets(const ProximityInfo *const proximityInfo,
77             const int sampledInputSize, const int lastSavedInputSize,
78             const std::vector<int> *const sampledLengthCache,
79             const std::vector<std::unordered_map<int, float>> *const charProbabilities,
80             std::vector<NearKeycodesSet> *sampledSearchKeySets,
81             std::vector<std::vector<int>> *sampledSearchKeyVectors);
82     static float getPointToKeyByIdLength(const float maxPointToKeyLength,
83             const std::vector<float> *const sampledNormalizedSquaredLengthCache, const int keyCount,
84             const int inputIndex, const int keyId);
85     static void initGeometricDistanceInfos(const ProximityInfo *const proximityInfo,
86             const int sampledInputSize, const int lastSavedInputSize, const bool isGeometric,
87             const std::vector<int> *const sampledInputXs,
88             const std::vector<int> *const sampledInputYs,
89             std::vector<float> *sampledNormalizedSquaredLengthCache);
90     static void initPrimaryInputWord(const int inputSize, const int *const inputProximities,
91             int *primaryInputWord);
92     static void dump(const bool isGeometric, const int inputSize,
93             const int *const inputXCoordinates, const int *const inputYCoordinates,
94             const int sampledInputSize, const std::vector<int> *const sampledInputXs,
95             const std::vector<int> *const sampledInputYs,
96             const std::vector<int> *const sampledTimes,
97             const std::vector<float> *const sampledSpeedRates,
98             const std::vector<int> *const sampledBeelineSpeedPercentiles);
99     static bool checkAndReturnIsContinuousSuggestionPossible(const int inputSize,
100             const int *const xCoordinates, const int *const yCoordinates, const int *const times,
101             const int sampledInputSize, const std::vector<int> *const sampledInputXs,
102             const std::vector<int> *const sampledInputYs,
103             const std::vector<int> *const sampledTimes,
104             const std::vector<int> *const sampledInputIndices);
105     // TODO: Move to most_probable_string_utils.h
106     static float getMostProbableString(const ProximityInfo *const proximityInfo,
107             const int sampledInputSize,
108             const std::vector<std::unordered_map<int, float>> *const charProbabilities,
109             int *const codePointBuf);
110 
111  private:
112     DISALLOW_IMPLICIT_CONSTRUCTORS(ProximityInfoStateUtils);
113 
114     static float updateNearKeysDistances(const ProximityInfo *const proximityInfo,
115             const float maxPointToKeyLength, const int x, const int y,
116             const bool isGeometric,
117             NearKeysDistanceMap *const currentNearKeysDistances);
118     static bool isPrevLocalMin(const NearKeysDistanceMap *const currentNearKeysDistances,
119             const NearKeysDistanceMap *const prevNearKeysDistances,
120             const NearKeysDistanceMap *const prevPrevNearKeysDistances);
121     static float getPointScore(const int mostCommonKeyWidth, const int x, const int y,
122             const int time, const bool lastPoint, const float nearest, const float sumAngle,
123             const NearKeysDistanceMap *const currentNearKeysDistances,
124             const NearKeysDistanceMap *const prevNearKeysDistances,
125             const NearKeysDistanceMap *const prevPrevNearKeysDistances,
126             std::vector<int> *sampledInputXs, std::vector<int> *sampledInputYs);
127     static bool pushTouchPoint(const ProximityInfo *const proximityInfo,
128             const int maxPointToKeyLength, const int inputIndex, const int nodeCodePoint, int x,
129             int y, const int time, const bool isGeometric,
130             const bool doSampling, const bool isLastPoint,
131             const float sumAngle, NearKeysDistanceMap *const currentNearKeysDistances,
132             const NearKeysDistanceMap *const prevNearKeysDistances,
133             const NearKeysDistanceMap *const prevPrevNearKeysDistances,
134             std::vector<int> *sampledInputXs, std::vector<int> *sampledInputYs,
135             std::vector<int> *sampledInputTimes, std::vector<int> *sampledLengthCache,
136             std::vector<int> *sampledInputIndice);
137     static float calculateBeelineSpeedRate(const int mostCommonKeyWidth, const float averageSpeed,
138             const int id, const int inputSize, const int *const xCoordinates,
139             const int *const yCoordinates, const int *times, const int sampledInputSize,
140             const std::vector<int> *const sampledInputXs,
141             const std::vector<int> *const sampledInputYs,
142             const std::vector<int> *const inputIndice);
143     static float getPointAngle(const std::vector<int> *const sampledInputXs,
144             const std::vector<int> *const sampledInputYs, const int index);
145     static float getPointsAngle(const std::vector<int> *const sampledInputXs,
146             const std::vector<int> *const sampledInputYs, const int index0, const int index1,
147             const int index2);
148     static bool suppressCharProbabilities(const int mostCommonKeyWidth,
149             const int sampledInputSize, const std::vector<int> *const lengthCache, const int index0,
150             const int index1, std::vector<std::unordered_map<int, float>> *charProbabilities);
151     static float calculateSquaredDistanceFromSweetSpotCenter(
152             const ProximityInfo *const proximityInfo, const std::vector<int> *const sampledInputXs,
153             const std::vector<int> *const sampledInputYs, const int keyIndex,
154             const int inputIndex);
155      static float calculateNormalizedSquaredDistance(const ProximityInfo *const proximityInfo,
156             const std::vector<int> *const sampledInputXs,
157             const std::vector<int> *const sampledInputYs, const int keyIndex, const int inputIndex);
158 };
159 } // namespace latinime
160 #endif // LATINIME_PROXIMITY_INFO_STATE_UTILS_H
161