blob: aab9f7ba837588216e929acf709ada830bf5885d [file] [log] [blame]
Satoshi Kataokaa9763f92013-01-15 19:16:38 +09001/*
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"
Satoshi Kataokad7a8fbf2013-01-22 17:00:43 +090021#include "proximity_info_params.h"
Satoshi Kataokaa9763f92013-01-15 19:16:38 +090022
23namespace latinime {
24class SuggestUtils {
25 public:
Ken Wakasaf25e7052013-01-16 01:29:43 +090026 static float getDistanceScalingFactor(const float normalizedSquaredDistance) {
Satoshi Kataokaa9763f92013-01-15 19:16:38 +090027 if (normalizedSquaredDistance < 0.0f) {
28 return -1.0f;
29 }
30 // Promote or demote the score according to the distance from the sweet spot
31 static const float A = ZERO_DISTANCE_PROMOTION_RATE / 100.0f;
32 static const float B = 1.0f;
33 static const float C = 0.5f;
34 static const float MIN = 0.3f;
35 static const float R1 = NEUTRAL_SCORE_SQUARED_RADIUS;
36 static const float R2 = HALF_SCORE_SQUARED_RADIUS;
Ken Wakasaf25e7052013-01-16 01:29:43 +090037 const float x = normalizedSquaredDistance / static_cast<float>(
Satoshi Kataokad7a8fbf2013-01-22 17:00:43 +090038 ProximityInfoParams::NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR);
Satoshi Kataokaa9763f92013-01-15 19:16:38 +090039 const float factor = max((x < R1)
40 ? (A * (R1 - x) + B * x) / R1
41 : (B * (R2 - x) + C * (x - R1)) / (R2 - R1), MIN);
42 // factor is a piecewise linear function like:
43 // A -_ .
44 // ^-_ .
45 // B \ .
46 // \_ .
47 // C ------------.
48 // .
49 // 0 R1 R2 .
50 return factor;
51 }
Ken Wakasaf25e7052013-01-16 01:29:43 +090052
53 private:
54 DISALLOW_IMPLICIT_CONSTRUCTORS(SuggestUtils);
Satoshi Kataokaa9763f92013-01-15 19:16:38 +090055};
56} // namespace latinime
57#endif // LATINIME_SUGGEST_UTILS_H