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#include "utils/autocorrection_threshold_utils.h" 18 19#include <cmath> 20 21#include "defines.h" 22#include "suggest/policyimpl/utils/edit_distance.h" 23#include "suggest/policyimpl/utils/damerau_levenshtein_edit_distance_policy.h" 24 25namespace latinime { 26 27const int AutocorrectionThresholdUtils::MAX_INITIAL_SCORE = 255; 28const int AutocorrectionThresholdUtils::TYPED_LETTER_MULTIPLIER = 2; 29const int AutocorrectionThresholdUtils::FULL_WORD_MULTIPLIER = 2; 30 31/* static */ int AutocorrectionThresholdUtils::editDistance(const int *before, 32 const int beforeLength, const int *after, const int afterLength) { 33 const DamerauLevenshteinEditDistancePolicy daemaruLevenshtein( 34 before, beforeLength, after, afterLength); 35 return static_cast<int>(EditDistance::getEditDistance(&daemaruLevenshtein)); 36} 37 38// In dictionary.cpp, getSuggestion() method, 39// When USE_SUGGEST_INTERFACE_FOR_TYPING is true: 40// 41// // TODO: Revise the following logic thoroughly by referring to the logic 42// // marked as "Otherwise" below. 43// SUGGEST_INTERFACE_OUTPUT_SCALE was multiplied to the original suggestion scores to convert 44// them to integers. 45// score = (int)((original score) * SUGGEST_INTERFACE_OUTPUT_SCALE) 46// Undo the scaling here to recover the original score. 47// normalizedScore = ((float)score) / SUGGEST_INTERFACE_OUTPUT_SCALE 48// 49// Otherwise: suggestion scores are computed using the below formula. 50// original score 51// := powf(mTypedLetterMultiplier (this is defined 2), 52// (the number of matched characters between typed word and suggested word)) 53// * (individual word's score which defined in the unigram dictionary, 54// and this score is defined in range [0, 255].) 55// Then, the following processing is applied. 56// - If the dictionary word is matched up to the point of the user entry 57// (full match up to min(before.length(), after.length()) 58// => Then multiply by FULL_MATCHED_WORDS_PROMOTION_RATE (this is defined 1.2) 59// - If the word is a true full match except for differences in accents or 60// capitalization, then treat it as if the score was 255. 61// - If before.length() == after.length() 62// => multiply by mFullWordMultiplier (this is defined 2)) 63// So, maximum original score is powf(2, min(before.length(), after.length())) * 255 * 2 * 1.2 64// For historical reasons we ignore the 1.2 modifier (because the measure for a good 65// autocorrection threshold was done at a time when it didn't exist). This doesn't change 66// the result. 67// So, we can normalize original score by dividing powf(2, min(b.l(),a.l())) * 255 * 2. 68 69/* static */ float AutocorrectionThresholdUtils::calcNormalizedScore(const int *before, 70 const int beforeLength, const int *after, const int afterLength, const int score) { 71 if (0 == beforeLength || 0 == afterLength) { 72 return 0.0f; 73 } 74 const int distance = editDistance(before, beforeLength, after, afterLength); 75 int spaceCount = 0; 76 for (int i = 0; i < afterLength; ++i) { 77 if (after[i] == KEYCODE_SPACE) { 78 ++spaceCount; 79 } 80 } 81 82 if (spaceCount == afterLength) { 83 return 0.0f; 84 } 85 86 if (score <= 0 || distance >= afterLength) { 87 // normalizedScore must be 0.0f (the minimum value) if the score is less than or equal to 0, 88 // or if the edit distance is larger than or equal to afterLength. 89 return 0.0f; 90 } 91 // add a weight based on edit distance. 92 const float weight = 1.0f - static_cast<float>(distance) / static_cast<float>(afterLength); 93 94 // TODO: Revise the following logic thoroughly by referring to... 95 if (true /* USE_SUGGEST_INTERFACE_FOR_TYPING */) { 96 return (static_cast<float>(score) / SUGGEST_INTERFACE_OUTPUT_SCALE) * weight; 97 } 98 // ...this logic. 99 const float maxScore = score >= S_INT_MAX ? static_cast<float>(S_INT_MAX) 100 : static_cast<float>(MAX_INITIAL_SCORE) 101 * powf(static_cast<float>(TYPED_LETTER_MULTIPLIER), 102 static_cast<float>(min(beforeLength, afterLength - spaceCount))) 103 * static_cast<float>(FULL_WORD_MULTIPLIER); 104 105 return (static_cast<float>(score) / maxScore) * weight; 106} 107 108} // namespace latinime 109