Home | History | Annotate | Download | only in typing
      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_TYPING_SCORING_H
     18 #define LATINIME_TYPING_SCORING_H
     19 
     20 #include "defines.h"
     21 #include "suggest/core/dictionary/error_type_utils.h"
     22 #include "suggest/core/policy/scoring.h"
     23 #include "suggest/core/session/dic_traverse_session.h"
     24 #include "suggest/policyimpl/typing/scoring_params.h"
     25 
     26 namespace latinime {
     27 
     28 class DicNode;
     29 class DicTraverseSession;
     30 
     31 class TypingScoring : public Scoring {
     32  public:
     33     static const TypingScoring *getInstance() { return &sInstance; }
     34 
     35     AK_FORCE_INLINE void getMostProbableString(const DicTraverseSession *const traverseSession,
     36             const float weightOfLangModelVsSpatialModel,
     37             SuggestionResults *const outSuggestionResults) const {}
     38 
     39     AK_FORCE_INLINE float getAdjustedWeightOfLangModelVsSpatialModel(
     40             DicTraverseSession *const traverseSession, DicNode *const terminals,
     41             const int size) const {
     42         return 1.0f;
     43     }
     44 
     45     AK_FORCE_INLINE int calculateFinalScore(const float compoundDistance, const int inputSize,
     46             const ErrorTypeUtils::ErrorType containedErrorTypes, const bool forceCommit,
     47             const bool boostExactMatches, const bool hasProbabilityZero) const {
     48         const float maxDistance = ScoringParams::DISTANCE_WEIGHT_LANGUAGE
     49                 + static_cast<float>(inputSize) * ScoringParams::TYPING_MAX_OUTPUT_SCORE_PER_INPUT;
     50         float score = ScoringParams::TYPING_BASE_OUTPUT_SCORE - compoundDistance / maxDistance;
     51         if (forceCommit) {
     52             score += ScoringParams::AUTOCORRECT_OUTPUT_THRESHOLD;
     53         }
     54         if (hasProbabilityZero) {
     55             // Previously, when both legitimate 0-frequency words (such as distracters) and
     56             // offensive words were encoded in the same way, distracters would never show up
     57             // when the user blocked offensive words (the default setting, as well as the
     58             // setting for regression tests).
     59             //
     60             // When b/11031090 was fixed and a separate encoding was used for offensive words,
     61             // 0-frequency words would no longer be blocked when they were an "exact match"
     62             // (where case mismatches and accent mismatches would be considered an "exact
     63             // match"). The exact match boosting functionality meant that, for example, when
     64             // the user typed "mt" they would be suggested the word "Mt", although they most
     65             // probably meant to type "my".
     66             //
     67             // For this reason, we introduced this change, which does the following:
     68             // * Defines the "perfect match" as a really exact match, with no room for case or
     69             // accent mismatches
     70             // * When the target word has probability zero (as "Mt" does, because it is a
     71             // distracter), ONLY boost its score if it is a perfect match.
     72             //
     73             // By doing this, when the user types "mt", the word "Mt" will NOT be boosted, and
     74             // they will get "my". However, if the user makes an explicit effort to type "Mt",
     75             // we do boost the word "Mt" so that the user's input is not autocorrected to "My".
     76             if (boostExactMatches && ErrorTypeUtils::isPerfectMatch(containedErrorTypes)) {
     77                 score += ScoringParams::PERFECT_MATCH_PROMOTION;
     78             }
     79         } else {
     80             if (boostExactMatches && ErrorTypeUtils::isExactMatch(containedErrorTypes)) {
     81                 score += ScoringParams::EXACT_MATCH_PROMOTION;
     82                 if ((ErrorTypeUtils::MATCH_WITH_WRONG_CASE & containedErrorTypes) != 0) {
     83                     score -= ScoringParams::CASE_ERROR_PENALTY_FOR_EXACT_MATCH;
     84                 }
     85                 if ((ErrorTypeUtils::MATCH_WITH_MISSING_ACCENT & containedErrorTypes) != 0) {
     86                     score -= ScoringParams::ACCENT_ERROR_PENALTY_FOR_EXACT_MATCH;
     87                 }
     88                 if ((ErrorTypeUtils::MATCH_WITH_DIGRAPH & containedErrorTypes) != 0) {
     89                     score -= ScoringParams::DIGRAPH_PENALTY_FOR_EXACT_MATCH;
     90                 }
     91             }
     92         }
     93         return static_cast<int>(score * SUGGEST_INTERFACE_OUTPUT_SCALE);
     94     }
     95 
     96     AK_FORCE_INLINE float getDoubleLetterDemotionDistanceCost(
     97             const DicNode *const terminalDicNode) const {
     98         return 0.0f;
     99     }
    100 
    101     AK_FORCE_INLINE bool autoCorrectsToMultiWordSuggestionIfTop() const {
    102         return true;
    103     }
    104 
    105     AK_FORCE_INLINE bool sameAsTyped(const DicTraverseSession *const traverseSession,
    106             const DicNode *const dicNode) const {
    107         return traverseSession->getProximityInfoState(0)->sameAsTyped(
    108                 dicNode->getOutputWordBuf(), dicNode->getNodeCodePointCount());
    109     }
    110 
    111  private:
    112     DISALLOW_COPY_AND_ASSIGN(TypingScoring);
    113     static const TypingScoring sInstance;
    114 
    115     TypingScoring() {}
    116     ~TypingScoring() {}
    117 };
    118 } // namespace latinime
    119 #endif // LATINIME_TYPING_SCORING_H
    120