OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2009 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_DICTIONARY_H |
| 18 #define LATINIME_DICTIONARY_H |
| 19 |
| 20 #include <memory> |
| 21 |
| 22 #include "third_party/prediction/defines.h" |
| 23 #include "third_party/prediction/suggest/core/dictionary/ngram_listener.h" |
| 24 #include "third_party/prediction/suggest/core/dictionary/property/word_property.
h" |
| 25 #include "third_party/prediction/suggest/core/policy/dictionary_header_structure
_policy.h" |
| 26 #include "third_party/prediction/suggest/core/policy/dictionary_structure_with_b
uffer_policy.h" |
| 27 #include "third_party/prediction/suggest/core/suggest_interface.h" |
| 28 |
| 29 namespace latinime { |
| 30 |
| 31 class DictionaryStructureWithBufferPolicy; |
| 32 class DicTraverseSession; |
| 33 class PrevWordsInfo; |
| 34 class ProximityInfo; |
| 35 class SuggestionResults; |
| 36 class SuggestOptions; |
| 37 |
| 38 class Dictionary { |
| 39 public: |
| 40 // Taken from SuggestedWords.java |
| 41 static const int KIND_MASK_KIND = 0xFF; // Mask to get only the kind |
| 42 static const int KIND_TYPED = 0; // What user typed |
| 43 static const int KIND_CORRECTION = 1; // Simple correction/suggestion |
| 44 static const int KIND_COMPLETION = |
| 45 2; // Completion (suggestion with appended chars) |
| 46 static const int KIND_WHITELIST = 3; // Whitelisted word |
| 47 static const int KIND_BLACKLIST = 4; // Blacklisted word |
| 48 static const int KIND_HARDCODED = |
| 49 5; // Hardcoded suggestion, e.g. punctuation |
| 50 static const int KIND_APP_DEFINED = 6; // Suggested by the application |
| 51 static const int KIND_SHORTCUT = 7; // A shortcut |
| 52 static const int KIND_PREDICTION = |
| 53 8; // A prediction (== a suggestion with no input) |
| 54 // KIND_RESUMED: A resumed suggestion (comes from a span, currently this type |
| 55 // is used only |
| 56 // in java for re-correction) |
| 57 static const int KIND_RESUMED = 9; |
| 58 static const int KIND_OOV_CORRECTION = 10; // Most probable string correction |
| 59 |
| 60 static const int KIND_MASK_FLAGS = 0xFFFFFF00; // Mask to get the flags |
| 61 static const int KIND_FLAG_POSSIBLY_OFFENSIVE = 0x80000000; |
| 62 static const int KIND_FLAG_EXACT_MATCH = 0x40000000; |
| 63 static const int KIND_FLAG_EXACT_MATCH_WITH_INTENTIONAL_OMISSION = 0x20000000; |
| 64 |
| 65 Dictionary(DictionaryStructureWithBufferPolicy::StructurePolicyPtr |
| 66 dictionaryStructureWithBufferPolicy); |
| 67 |
| 68 void getSuggestions(ProximityInfo* proximityInfo, |
| 69 DicTraverseSession* traverseSession, |
| 70 int* xcoordinates, |
| 71 int* ycoordinates, |
| 72 int* times, |
| 73 int* pointerIds, |
| 74 int* inputCodePoints, |
| 75 int inputSize, |
| 76 const PrevWordsInfo* const prevWordsInfo, |
| 77 const SuggestOptions* const suggestOptions, |
| 78 const float languageWeight, |
| 79 SuggestionResults* const outSuggestionResults) const; |
| 80 |
| 81 void getPredictions(const PrevWordsInfo* const prevWordsInfo, |
| 82 SuggestionResults* const outSuggestionResults) const; |
| 83 |
| 84 int getProbability(const int* word, int length) const; |
| 85 |
| 86 int getMaxProbabilityOfExactMatches(const int* word, int length) const; |
| 87 |
| 88 int getNgramProbability(const PrevWordsInfo* const prevWordsInfo, |
| 89 const int* word, |
| 90 int length) const; |
| 91 |
| 92 bool addUnigramEntry(const int* const codePoints, |
| 93 const int codePointCount, |
| 94 const UnigramProperty* const unigramProperty); |
| 95 |
| 96 bool removeUnigramEntry(const int* const codePoints, |
| 97 const int codePointCount); |
| 98 |
| 99 bool addNgramEntry(const PrevWordsInfo* const prevWordsInfo, |
| 100 const BigramProperty* const bigramProperty); |
| 101 |
| 102 bool removeNgramEntry(const PrevWordsInfo* const prevWordsInfo, |
| 103 const int* const word, |
| 104 const int length); |
| 105 |
| 106 bool flush(const char* const filePath); |
| 107 |
| 108 bool flushWithGC(const char* const filePath); |
| 109 |
| 110 bool needsToRunGC(const bool mindsBlockByGC); |
| 111 |
| 112 void getProperty(const char* const query, |
| 113 const int queryLength, |
| 114 char* const outResult, |
| 115 const int maxResultLength); |
| 116 |
| 117 const WordProperty getWordProperty(const int* const codePoints, |
| 118 const int codePointCount); |
| 119 |
| 120 // Method to iterate all words in the dictionary. |
| 121 // The returned token has to be used to get the next word. If token is 0, this |
| 122 // method newly |
| 123 // starts iterating the dictionary. |
| 124 int getNextWordAndNextToken(const int token, |
| 125 int* const outCodePoints, |
| 126 int* const outCodePointCount); |
| 127 |
| 128 const DictionaryStructureWithBufferPolicy* getDictionaryStructurePolicy() |
| 129 const { |
| 130 return mDictionaryStructureWithBufferPolicy.get(); |
| 131 } |
| 132 |
| 133 private: |
| 134 DISALLOW_IMPLICIT_CONSTRUCTORS(Dictionary); |
| 135 |
| 136 typedef std::unique_ptr<SuggestInterface> SuggestInterfacePtr; |
| 137 |
| 138 class NgramListenerForPrediction : public NgramListener { |
| 139 public: |
| 140 NgramListenerForPrediction( |
| 141 const PrevWordsInfo* const prevWordsInfo, |
| 142 SuggestionResults* const suggestionResults, |
| 143 const DictionaryStructureWithBufferPolicy* const dictStructurePolicy); |
| 144 virtual void onVisitEntry(const int ngramProbability, |
| 145 const int targetPtNodePos); |
| 146 |
| 147 private: |
| 148 DISALLOW_IMPLICIT_CONSTRUCTORS(NgramListenerForPrediction); |
| 149 |
| 150 const PrevWordsInfo* const mPrevWordsInfo; |
| 151 SuggestionResults* const mSuggestionResults; |
| 152 const DictionaryStructureWithBufferPolicy* const mDictStructurePolicy; |
| 153 }; |
| 154 |
| 155 static const int HEADER_ATTRIBUTE_BUFFER_SIZE; |
| 156 |
| 157 const DictionaryStructureWithBufferPolicy::StructurePolicyPtr |
| 158 mDictionaryStructureWithBufferPolicy; |
| 159 const SuggestInterfacePtr mGestureSuggest; |
| 160 const SuggestInterfacePtr mTypingSuggest; |
| 161 }; |
| 162 } // namespace latinime |
| 163 #endif // LATINIME_DICTIONARY_H |
OLD | NEW |