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