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: getInstance()33 static const TypingScoring *getInstance() { return &sInstance; } 34 getMostProbableString(const DicTraverseSession * const traverseSession,const float languageWeight,SuggestionResults * const outSuggestionResults)35 AK_FORCE_INLINE void getMostProbableString(const DicTraverseSession *const traverseSession, 36 const float languageWeight, SuggestionResults *const outSuggestionResults) const {} 37 getAdjustedLanguageWeight(DicTraverseSession * const traverseSession,DicNode * const terminals,const int size)38 AK_FORCE_INLINE float getAdjustedLanguageWeight(DicTraverseSession *const traverseSession, 39 DicNode *const terminals, const int size) const { 40 return 1.0f; 41 } 42 calculateFinalScore(const float compoundDistance,const int inputSize,const ErrorTypeUtils::ErrorType containedErrorTypes,const bool forceCommit,const bool boostExactMatches)43 AK_FORCE_INLINE int calculateFinalScore(const float compoundDistance, const int inputSize, 44 const ErrorTypeUtils::ErrorType containedErrorTypes, const bool forceCommit, 45 const bool boostExactMatches) const { 46 const float maxDistance = ScoringParams::DISTANCE_WEIGHT_LANGUAGE 47 + static_cast<float>(inputSize) * ScoringParams::TYPING_MAX_OUTPUT_SCORE_PER_INPUT; 48 float score = ScoringParams::TYPING_BASE_OUTPUT_SCORE - compoundDistance / maxDistance; 49 if (forceCommit) { 50 score += ScoringParams::AUTOCORRECT_OUTPUT_THRESHOLD; 51 } 52 if (boostExactMatches && ErrorTypeUtils::isExactMatch(containedErrorTypes)) { 53 score += ScoringParams::EXACT_MATCH_PROMOTION; 54 if ((ErrorTypeUtils::MATCH_WITH_CASE_ERROR & containedErrorTypes) != 0) { 55 score -= ScoringParams::CASE_ERROR_PENALTY_FOR_EXACT_MATCH; 56 } 57 if ((ErrorTypeUtils::MATCH_WITH_ACCENT_ERROR & containedErrorTypes) != 0) { 58 score -= ScoringParams::ACCENT_ERROR_PENALTY_FOR_EXACT_MATCH; 59 } 60 if ((ErrorTypeUtils::MATCH_WITH_DIGRAPH & containedErrorTypes) != 0) { 61 score -= ScoringParams::DIGRAPH_PENALTY_FOR_EXACT_MATCH; 62 } 63 } 64 return static_cast<int>(score * SUGGEST_INTERFACE_OUTPUT_SCALE); 65 } 66 getDoubleLetterDemotionDistanceCost(const DicNode * const terminalDicNode)67 AK_FORCE_INLINE float getDoubleLetterDemotionDistanceCost( 68 const DicNode *const terminalDicNode) const { 69 return 0.0f; 70 } 71 autoCorrectsToMultiWordSuggestionIfTop()72 AK_FORCE_INLINE bool autoCorrectsToMultiWordSuggestionIfTop() const { 73 return true; 74 } 75 sameAsTyped(const DicTraverseSession * const traverseSession,const DicNode * const dicNode)76 AK_FORCE_INLINE bool sameAsTyped(const DicTraverseSession *const traverseSession, 77 const DicNode *const dicNode) const { 78 return traverseSession->getProximityInfoState(0)->sameAsTyped( 79 dicNode->getOutputWordBuf(), dicNode->getNodeCodePointCount()); 80 } 81 82 private: 83 DISALLOW_COPY_AND_ASSIGN(TypingScoring); 84 static const TypingScoring sInstance; 85 TypingScoring()86 TypingScoring() {} ~TypingScoring()87 ~TypingScoring() {} 88 }; 89 } // namespace latinime 90 #endif // LATINIME_TYPING_SCORING_H 91