1 /*
2  * Copyright (C) 2012 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 "suggest/policyimpl/typing/typing_weighting.h"
18 
19 #include "suggest/core/dicnode/dic_node.h"
20 #include "suggest/core/layout/proximity_info.h"
21 #include "suggest/policyimpl/typing/scoring_params.h"
22 
23 namespace latinime {
24 
25 const TypingWeighting TypingWeighting::sInstance;
26 
getErrorType(const CorrectionType correctionType,const DicTraverseSession * const traverseSession,const DicNode * const parentDicNode,const DicNode * const dicNode) const27 ErrorTypeUtils::ErrorType TypingWeighting::getErrorType(const CorrectionType correctionType,
28         const DicTraverseSession *const traverseSession, const DicNode *const parentDicNode,
29         const DicNode *const dicNode) const {
30     switch (correctionType) {
31         case CT_MATCH:
32             if (isProximityDicNode(traverseSession, dicNode)) {
33                 return ErrorTypeUtils::PROXIMITY_CORRECTION;
34             } else if (dicNode->isInDigraph()) {
35                 return ErrorTypeUtils::MATCH_WITH_DIGRAPH;
36             } else {
37                 // Compare the node code point with original primary code point on the keyboard.
38                 const ProximityInfoState *const pInfoState =
39                         traverseSession->getProximityInfoState(0);
40                 const int primaryCodePoint = pInfoState->getPrimaryCodePointAt(
41                         dicNode->getInputIndex(0));
42                 const int nodeCodePoint = dicNode->getNodeCodePoint();
43                 const int keyIndex = traverseSession->getProximityInfo()->getKeyIndexOf(
44                         primaryCodePoint);
45                 // TODO: Check whether the input code point is on the keyboard.
46                 if (primaryCodePoint == nodeCodePoint) {
47                     // Node code point is same as original code point on the keyboard.
48                     return ErrorTypeUtils::NOT_AN_ERROR;
49                 } else if (CharUtils::toLowerCase(primaryCodePoint) ==
50                         CharUtils::toLowerCase(nodeCodePoint)) {
51                     // Only cases of the code points are different.
52                     return ErrorTypeUtils::MATCH_WITH_WRONG_CASE;
53                 } else if (primaryCodePoint == CharUtils::toBaseCodePoint(nodeCodePoint)) {
54                     // Node code point is a variant of original code point.
55                     return ErrorTypeUtils::MATCH_WITH_MISSING_ACCENT;
56                 } else if (CharUtils::toBaseCodePoint(primaryCodePoint)
57                         == CharUtils::toBaseCodePoint(nodeCodePoint)) {
58                     // Base code points are the same but the code point is intentionally input.
59                     if (keyIndex == NOT_AN_INDEX) {
60                         return ErrorTypeUtils::MATCH_WITH_MISSING_EXPLICIT_ACCENT;
61                     }
62                     return ErrorTypeUtils::MATCH_WITH_WRONG_ACCENT;
63                 } else if (CharUtils::toLowerCase(primaryCodePoint)
64                         == CharUtils::toBaseLowerCase(nodeCodePoint)) {
65                     // Node code point is a variant of original code point and the cases are also
66                     // different.
67                     return ErrorTypeUtils::MATCH_WITH_MISSING_ACCENT
68                             | ErrorTypeUtils::MATCH_WITH_WRONG_CASE;
69                 } else {
70                     if (keyIndex == NOT_AN_INDEX) {
71                         return ErrorTypeUtils::MATCH_WITH_MISSING_EXPLICIT_ACCENT
72                                 | ErrorTypeUtils::MATCH_WITH_WRONG_CASE;
73                     }
74                     // Base code points are the same and the cases are different.
75                     return ErrorTypeUtils::MATCH_WITH_WRONG_ACCENT
76                             | ErrorTypeUtils::MATCH_WITH_WRONG_CASE;
77                 }
78             }
79             break;
80         case CT_ADDITIONAL_PROXIMITY:
81             // TODO: Change to EDIT_CORRECTION.
82             return ErrorTypeUtils::PROXIMITY_CORRECTION;
83         case CT_OMISSION:
84             if (parentDicNode->canBeIntentionalOmission()) {
85                 return ErrorTypeUtils::INTENTIONAL_OMISSION;
86             } else {
87                 return ErrorTypeUtils::EDIT_CORRECTION;
88             }
89             break;
90         case CT_SUBSTITUTION:
91             // TODO: Quit settng PROXIMITY_CORRECTION.
92             return ErrorTypeUtils::EDIT_CORRECTION | ErrorTypeUtils::PROXIMITY_CORRECTION;
93         case CT_INSERTION:
94         case CT_TERMINAL_INSERTION:
95         case CT_TRANSPOSITION:
96             return ErrorTypeUtils::EDIT_CORRECTION;
97         case CT_NEW_WORD_SPACE_OMISSION:
98         case CT_NEW_WORD_SPACE_SUBSTITUTION:
99             return ErrorTypeUtils::NEW_WORD;
100         case CT_TERMINAL:
101             return ErrorTypeUtils::NOT_AN_ERROR;
102         case CT_COMPLETION:
103             return ErrorTypeUtils::COMPLETION;
104         default:
105             return ErrorTypeUtils::NOT_AN_ERROR;
106     }
107 }
108 }  // namespace latinime
109