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