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