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 #ifndef LATINIME_SHORTCUT_UTILS 18 #define LATINIME_SHORTCUT_UTILS 19 20 #include "defines.h" 21 #include "suggest/core/dicnode/dic_node_utils.h" 22 #include "terminal_attributes.h" 23 24 namespace latinime { 25 26 class ShortcutUtils { 27 public: 28 static int outputShortcuts(const TerminalAttributes *const terminalAttributes, 29 int outputWordIndex, const int finalScore, int *const outputCodePoints, 30 int *const frequencies, int *const outputTypes, const bool sameAsTyped) { 31 TerminalAttributes::ShortcutIterator iterator = terminalAttributes->getShortcutIterator(); 32 while (iterator.hasNextShortcutTarget() && outputWordIndex < MAX_RESULTS) { 33 int shortcutTarget[MAX_WORD_LENGTH]; 34 int shortcutProbability; 35 const int shortcutTargetStringLength = iterator.getNextShortcutTarget( 36 MAX_WORD_LENGTH, shortcutTarget, &shortcutProbability); 37 int shortcutScore; 38 int kind; 39 if (shortcutProbability == BinaryFormat::WHITELIST_SHORTCUT_PROBABILITY 40 && sameAsTyped) { 41 shortcutScore = S_INT_MAX; 42 kind = Dictionary::KIND_WHITELIST; 43 } else { 44 // shortcut entry's score == its base entry's score - 1 45 shortcutScore = finalScore; 46 // Protection against int underflow 47 shortcutScore = max(S_INT_MIN + 1, shortcutScore) - 1; 48 kind = Dictionary::KIND_CORRECTION; 49 } 50 outputTypes[outputWordIndex] = kind; 51 frequencies[outputWordIndex] = shortcutScore; 52 frequencies[outputWordIndex] = max(S_INT_MIN + 1, shortcutScore) - 1; 53 const int startIndex2 = outputWordIndex * MAX_WORD_LENGTH; 54 DicNodeUtils::appendTwoWords(0, 0, shortcutTarget, shortcutTargetStringLength, 55 &outputCodePoints[startIndex2]); 56 ++outputWordIndex; 57 } 58 return outputWordIndex; 59 } 60 61 private: 62 DISALLOW_IMPLICIT_CONSTRUCTORS(ShortcutUtils); 63 }; 64 } // namespace latinime 65 #endif // LATINIME_SHORTCUT_UTILS 66