dictionary.h revision 793124855de9dabb9e85b1e06619716649f087c5
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/bigram_dictionary.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 ProximityInfo;
35class SuggestionResults;
36class SuggestOptions;
37
38class 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 appended 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. punctuation
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 type 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 correction
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
60    Dictionary(JNIEnv *env, DictionaryStructureWithBufferPolicy::StructurePolicyPtr
61            dictionaryStructureWithBufferPolicy);
62
63    void getSuggestions(ProximityInfo *proximityInfo, DicTraverseSession *traverseSession,
64            int *xcoordinates, int *ycoordinates, int *times, int *pointerIds, int *inputCodePoints,
65            int inputSize, int *prevWordCodePoints, int prevWordLength,
66            const SuggestOptions *const suggestOptions, const float languageWeight,
67            SuggestionResults *const outSuggestionResults) const;
68
69    void getPredictions(const int *word, int length,
70            SuggestionResults *const outSuggestionResults) const;
71
72    int getProbability(const int *word, int length) const;
73
74    int getBigramProbability(const int *word0, int length0, const int *word1, int length1) const;
75
76    void addUnigramWord(const int *const codePoints, const int codePointCount,
77            const UnigramProperty *const unigramProperty);
78
79    void addBigramWords(const int *const word0, const int length0, const int *const word1,
80            const int length1, const int probability, const int timestamp);
81
82    void removeBigramWords(const int *const word0, const int length0, const int *const word1,
83            const int length1);
84
85    void flush(const char *const filePath);
86
87    void flushWithGC(const char *const filePath);
88
89    bool needsToRunGC(const bool mindsBlockByGC);
90
91    void getProperty(const char *const query, const int queryLength, char *const outResult,
92            const int maxResultLength);
93
94    const WordProperty getWordProperty(const int *const codePoints, const int codePointCount);
95
96    // Method to iterate all words in the dictionary.
97    // The returned token has to be used to get the next word. If token is 0, this method newly
98    // starts iterating the dictionary.
99    int getNextWordAndNextToken(const int token, int *const outCodePoints);
100
101    const DictionaryStructureWithBufferPolicy *getDictionaryStructurePolicy() const {
102        return mDictionaryStructureWithBufferPolicy.get();
103    }
104
105 private:
106    DISALLOW_IMPLICIT_CONSTRUCTORS(Dictionary);
107
108    typedef std::unique_ptr<SuggestInterface> SuggestInterfacePtr;
109
110    static const int HEADER_ATTRIBUTE_BUFFER_SIZE;
111
112    const DictionaryStructureWithBufferPolicy::StructurePolicyPtr
113            mDictionaryStructureWithBufferPolicy;
114    const BigramDictionary mBigramDictionary;
115    const SuggestInterfacePtr mGestureSuggest;
116    const SuggestInterfacePtr mTypingSuggest;
117
118    void logDictionaryInfo(JNIEnv *const env) const;
119};
120} // namespace latinime
121#endif // LATINIME_DICTIONARY_H
122