1 /*
2  * Copyright (C) 2011 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 package com.android.inputmethod.latin.utils;
18 
19 import android.util.Log;
20 
21 import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
22 import com.android.inputmethod.latin.define.DebugFlags;
23 
24 public final class AutoCorrectionUtils {
25     private static final boolean DBG = DebugFlags.DEBUG_ENABLED;
26     private static final String TAG = AutoCorrectionUtils.class.getSimpleName();
27 
AutoCorrectionUtils()28     private AutoCorrectionUtils() {
29         // Purely static class: can't instantiate.
30     }
31 
suggestionExceedsThreshold(final SuggestedWordInfo suggestion, final String consideredWord, final float threshold)32     public static boolean suggestionExceedsThreshold(final SuggestedWordInfo suggestion,
33             final String consideredWord, final float threshold) {
34         if (null != suggestion) {
35             // Shortlist a whitelisted word
36             if (suggestion.isKindOf(SuggestedWordInfo.KIND_WHITELIST)) {
37                 return true;
38             }
39             // TODO: return suggestion.isAprapreateForAutoCorrection();
40             if (!suggestion.isAprapreateForAutoCorrection()) {
41                 return false;
42             }
43             final int autoCorrectionSuggestionScore = suggestion.mScore;
44             // TODO: when the normalized score of the first suggestion is nearly equals to
45             //       the normalized score of the second suggestion, behave less aggressive.
46             final float normalizedScore = BinaryDictionaryUtils.calcNormalizedScore(
47                     consideredWord, suggestion.mWord, autoCorrectionSuggestionScore);
48             if (DBG) {
49                 Log.d(TAG, "Normalized " + consideredWord + "," + suggestion + ","
50                         + autoCorrectionSuggestionScore + ", " + normalizedScore
51                         + "(" + threshold + ")");
52             }
53             if (normalizedScore >= threshold) {
54                 if (DBG) {
55                     Log.d(TAG, "Exceeds threshold.");
56                 }
57                 return true;
58             }
59         }
60         return false;
61     }
62 }
63