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/android_prediction/defines.h" |
| 23 #include "third_party/android_prediction/suggest/core/dictionary/ngram_listener.
h" |
| 24 #include "third_party/android_prediction/suggest/core/dictionary/property/word_p
roperty.h" |
| 25 #include "third_party/android_prediction/suggest/core/policy/dictionary_header_s
tructure_policy.h" |
| 26 #include "third_party/android_prediction/suggest/core/policy/dictionary_structur
e_with_buffer_policy.h" |
| 27 #include "third_party/android_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 = 2; // Completion (suggestion with appende
d chars) |
| 45 static const int KIND_WHITELIST = 3; // Whitelisted word |
| 46 static const int KIND_BLACKLIST = 4; // Blacklisted word |
| 47 static const int KIND_HARDCODED = 5; // Hardcoded suggestion, e.g. punctuati
on |
| 48 static const int KIND_APP_DEFINED = 6; // Suggested by the application |
| 49 static const int KIND_SHORTCUT = 7; // A shortcut |
| 50 static const int KIND_PREDICTION = 8; // A prediction (== a suggestion with
no input) |
| 51 // KIND_RESUMED: A resumed suggestion (comes from a span, currently this typ
e is used only |
| 52 // in java for re-correction) |
| 53 static const int KIND_RESUMED = 9; |
| 54 static const int KIND_OOV_CORRECTION = 10; // Most probable string correctio
n |
| 55 |
| 56 static const int KIND_MASK_FLAGS = 0xFFFFFF00; // Mask to get the flags |
| 57 static const int KIND_FLAG_POSSIBLY_OFFENSIVE = 0x80000000; |
| 58 static const int KIND_FLAG_EXACT_MATCH = 0x40000000; |
| 59 static const int KIND_FLAG_EXACT_MATCH_WITH_INTENTIONAL_OMISSION = 0x2000000
0; |
| 60 |
| 61 Dictionary(DictionaryStructureWithBufferPolicy::StructurePolicyPtr |
| 62 dictionaryStructureWithBufferPolicy); |
| 63 |
| 64 void getSuggestions(ProximityInfo *proximityInfo, DicTraverseSession *traver
seSession, |
| 65 int *xcoordinates, int *ycoordinates, int *times, int *pointerIds, i
nt *inputCodePoints, |
| 66 int inputSize, const PrevWordsInfo *const prevWordsInfo, |
| 67 const SuggestOptions *const suggestOptions, const float languageWeig
ht, |
| 68 SuggestionResults *const outSuggestionResults) const; |
| 69 |
| 70 void getPredictions(const PrevWordsInfo *const prevWordsInfo, |
| 71 SuggestionResults *const outSuggestionResults) const; |
| 72 |
| 73 int getProbability(const int *word, int length) const; |
| 74 |
| 75 int getMaxProbabilityOfExactMatches(const int *word, int length) const; |
| 76 |
| 77 int getNgramProbability(const PrevWordsInfo *const prevWordsInfo, |
| 78 const int *word, int length) const; |
| 79 |
| 80 bool addUnigramEntry(const int *const codePoints, const int codePointCount, |
| 81 const UnigramProperty *const unigramProperty); |
| 82 |
| 83 bool removeUnigramEntry(const int *const codePoints, const int codePointCoun
t); |
| 84 |
| 85 bool addNgramEntry(const PrevWordsInfo *const prevWordsInfo, |
| 86 const BigramProperty *const bigramProperty); |
| 87 |
| 88 bool removeNgramEntry(const PrevWordsInfo *const prevWordsInfo, const int *c
onst word, |
| 89 const int length); |
| 90 |
| 91 bool flush(const char *const filePath); |
| 92 |
| 93 bool flushWithGC(const char *const filePath); |
| 94 |
| 95 bool needsToRunGC(const bool mindsBlockByGC); |
| 96 |
| 97 void getProperty(const char *const query, const int queryLength, char *const
outResult, |
| 98 const int maxResultLength); |
| 99 |
| 100 const WordProperty getWordProperty(const int *const codePoints, const int co
dePointCount); |
| 101 |
| 102 // Method to iterate all words in the dictionary. |
| 103 // The returned token has to be used to get the next word. If token is 0, th
is method newly |
| 104 // starts iterating the dictionary. |
| 105 int getNextWordAndNextToken(const int token, int *const outCodePoints, |
| 106 int *const outCodePointCount); |
| 107 |
| 108 const DictionaryStructureWithBufferPolicy *getDictionaryStructurePolicy() co
nst { |
| 109 return mDictionaryStructureWithBufferPolicy.get(); |
| 110 } |
| 111 |
| 112 private: |
| 113 DISALLOW_IMPLICIT_CONSTRUCTORS(Dictionary); |
| 114 |
| 115 typedef std::unique_ptr<SuggestInterface> SuggestInterfacePtr; |
| 116 |
| 117 class NgramListenerForPrediction : public NgramListener { |
| 118 public: |
| 119 NgramListenerForPrediction(const PrevWordsInfo *const prevWordsInfo, |
| 120 SuggestionResults *const suggestionResults, |
| 121 const DictionaryStructureWithBufferPolicy *const dictStructurePo
licy); |
| 122 virtual void onVisitEntry(const int ngramProbability, const int targetPt
NodePos); |
| 123 |
| 124 private: |
| 125 DISALLOW_IMPLICIT_CONSTRUCTORS(NgramListenerForPrediction); |
| 126 |
| 127 const PrevWordsInfo *const mPrevWordsInfo; |
| 128 SuggestionResults *const mSuggestionResults; |
| 129 const DictionaryStructureWithBufferPolicy *const mDictStructurePolicy; |
| 130 }; |
| 131 |
| 132 static const int HEADER_ATTRIBUTE_BUFFER_SIZE; |
| 133 |
| 134 const DictionaryStructureWithBufferPolicy::StructurePolicyPtr |
| 135 mDictionaryStructureWithBufferPolicy; |
| 136 const SuggestInterfacePtr mGestureSuggest; |
| 137 const SuggestInterfacePtr mTypingSuggest; |
| 138 }; |
| 139 } // namespace latinime |
| 140 #endif // LATINIME_DICTIONARY_H |
OLD | NEW |