dictionary.h revision de2b312c6d00509a313c076d0be37eea5c2d41f3
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 <stdint.h>
21#include <memory>
22
23#include "defines.h"
24#include "jni.h"
25#include "suggest/core/dictionary/bigram_dictionary.h"
26#include "suggest/core/dictionary/word_property.h"
27#include "suggest/core/policy/dictionary_header_structure_policy.h"
28#include "suggest/core/policy/dictionary_structure_with_buffer_policy.h"
29#include "suggest/core/suggest_interface.h"
30
31namespace latinime {
32
33class DictionaryStructureWithBufferPolicy;
34class DicTraverseSession;
35class ProximityInfo;
36class SuggestionResults;
37class SuggestOptions;
38class WordProperty;
39
40class Dictionary {
41 public:
42    // Taken from SuggestedWords.java
43    static const int KIND_MASK_KIND = 0xFF; // Mask to get only the kind
44    static const int KIND_TYPED = 0; // What user typed
45    static const int KIND_CORRECTION = 1; // Simple correction/suggestion
46    static const int KIND_COMPLETION = 2; // Completion (suggestion with appended chars)
47    static const int KIND_WHITELIST = 3; // Whitelisted word
48    static const int KIND_BLACKLIST = 4; // Blacklisted word
49    static const int KIND_HARDCODED = 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 = 8; // A prediction (== a suggestion with no input)
53    // KIND_RESUMED: A resumed suggestion (comes from a span, currently this type is used only
54    // in java for re-correction)
55    static const int KIND_RESUMED = 9;
56    static const int KIND_OOV_CORRECTION = 10; // Most probable string correction
57
58    static const int KIND_MASK_FLAGS = 0xFFFFFF00; // Mask to get the flags
59    static const int KIND_FLAG_POSSIBLY_OFFENSIVE = 0x80000000;
60    static const int KIND_FLAG_EXACT_MATCH = 0x40000000;
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, int *prevWordCodePoints, int prevWordLength,
68            const SuggestOptions *const suggestOptions, const float languageWeight,
69            SuggestionResults *const outSuggestionResults) const;
70
71    void getPredictions(const int *word, int length,
72            SuggestionResults *const outSuggestionResults) const;
73
74    int getProbability(const int *word, int length) const;
75
76    int getBigramProbability(const int *word0, int length0, const int *word1, int length1) const;
77
78    void addUnigramWord(const int *const word, const int length, const int probability,
79            const int *const shortcutTargetCodePoints, const int shortcutLength,
80            const int shortcutProbability, const bool isNotAWord, const bool isBlacklisted,
81            const int timestamp);
82
83    void addBigramWords(const int *const word0, const int length0, const int *const word1,
84            const int length1, const int probability, const int timestamp);
85
86    void removeBigramWords(const int *const word0, const int length0, const int *const word1,
87            const int length1);
88
89    void flush(const char *const filePath);
90
91    void flushWithGC(const char *const filePath);
92
93    bool needsToRunGC(const bool mindsBlockByGC);
94
95    void getProperty(const char *const query, const int queryLength, char *const outResult,
96            const int maxResultLength);
97
98    const WordProperty getWordProperty(const int *const codePoints, const int codePointCount);
99
100    // Method to iterate all words in the dictionary.
101    // The returned token has to be used to get the next word. If token is 0, this method newly
102    // starts iterating the dictionary.
103    int getNextWordAndNextToken(const int token, int *const outCodePoints);
104
105    const DictionaryStructureWithBufferPolicy *getDictionaryStructurePolicy() const {
106        return mDictionaryStructureWithBufferPolicy.get();
107    }
108
109 private:
110    DISALLOW_IMPLICIT_CONSTRUCTORS(Dictionary);
111
112    typedef std::unique_ptr<SuggestInterface> SuggestInterfacePtr;
113
114    static const int HEADER_ATTRIBUTE_BUFFER_SIZE;
115
116    const DictionaryStructureWithBufferPolicy::StructurePolicyPtr
117            mDictionaryStructureWithBufferPolicy;
118    const BigramDictionary mBigramDictionary;
119    const SuggestInterfacePtr mGestureSuggest;
120    const SuggestInterfacePtr mTypingSuggest;
121
122    void logDictionaryInfo(JNIEnv *const env) const;
123};
124} // namespace latinime
125#endif // LATINIME_DICTIONARY_H
126