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_SUGGEST_UTILS_H 18 #define LATINIME_SUGGEST_UTILS_H 19 20 #include "defines.h" 21 #include "proximity_info_params.h" 22 23 namespace latinime { 24 class SuggestUtils { 25 public: 26 // TODO: (OLD) Remove 27 static float getLengthScalingFactor(const float normalizedSquaredDistance) { 28 // Promote or demote the score according to the distance from the sweet spot 29 static const float A = ZERO_DISTANCE_PROMOTION_RATE / 100.0f; 30 static const float B = 1.0f; 31 static const float C = 0.5f; 32 static const float MIN = 0.3f; 33 static const float R1 = NEUTRAL_SCORE_SQUARED_RADIUS; 34 static const float R2 = HALF_SCORE_SQUARED_RADIUS; 35 const float x = normalizedSquaredDistance / static_cast<float>( 36 ProximityInfoParams::NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR); 37 const float factor = max((x < R1) 38 ? (A * (R1 - x) + B * x) / R1 39 : (B * (R2 - x) + C * (x - R1)) / (R2 - R1), MIN); 40 // factor is a piecewise linear function like: 41 // A -_ . 42 // ^-_ . 43 // B \ . 44 // \_ . 45 // C ------------. 46 // . 47 // 0 R1 R2 . 48 return factor; 49 } 50 51 static float getSweetSpotFactor(const bool isTouchPositionCorrectionEnabled, 52 const float normalizedSquaredDistance) { 53 // Promote or demote the score according to the distance from the sweet spot 54 static const float A = 0.0f; 55 static const float B = 0.24f; 56 static const float C = 1.20f; 57 static const float R0 = 0.0f; 58 static const float R1 = 0.25f; // Sweet spot 59 static const float R2 = 1.0f; 60 const float x = normalizedSquaredDistance; 61 if (!isTouchPositionCorrectionEnabled) { 62 return min(C, x); 63 } 64 65 // factor is a piecewise linear function like: 66 // C -------------. 67 // / . 68 // B / . 69 // -/ . 70 // A _-^ . 71 // . 72 // R0 R1 R2 . 73 74 if (x < R0) { 75 return A; 76 } else if (x < R1) { 77 return (A * (R1 - x) + B * (x - R0)) / (R1 - R0); 78 } else if (x < R2) { 79 return (B * (R2 - x) + C * (x - R1)) / (R2 - R1); 80 } else { 81 return C; 82 } 83 } 84 private: 85 DISALLOW_IMPLICIT_CONSTRUCTORS(SuggestUtils); 86 }; 87 } // namespace latinime 88 #endif // LATINIME_SUGGEST_UTILS_H 89