Home | History | Annotate | Download | only in dictionary
      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 
     22 #include "defines.h"
     23 #include "jni.h"
     24 
     25 namespace latinime {
     26 
     27 class BigramDictionary;
     28 class DictionaryStructureWithBufferPolicy;
     29 class DicTraverseSession;
     30 class ProximityInfo;
     31 class SuggestInterface;
     32 class SuggestOptions;
     33 
     34 class Dictionary {
     35  public:
     36     // Taken from SuggestedWords.java
     37     static const int KIND_MASK_KIND = 0xFF; // Mask to get only the kind
     38     static const int KIND_TYPED = 0; // What user typed
     39     static const int KIND_CORRECTION = 1; // Simple correction/suggestion
     40     static const int KIND_COMPLETION = 2; // Completion (suggestion with appended chars)
     41     static const int KIND_WHITELIST = 3; // Whitelisted word
     42     static const int KIND_BLACKLIST = 4; // Blacklisted word
     43     static const int KIND_HARDCODED = 5; // Hardcoded suggestion, e.g. punctuation
     44     static const int KIND_APP_DEFINED = 6; // Suggested by the application
     45     static const int KIND_SHORTCUT = 7; // A shortcut
     46     static const int KIND_PREDICTION = 8; // A prediction (== a suggestion with no input)
     47     // KIND_RESUMED: A resumed suggestion (comes from a span, currently this type is used only
     48     // in java for re-correction)
     49     static const int KIND_RESUMED = 9;
     50     static const int KIND_OOV_CORRECTION = 10; // Most probable string correction
     51 
     52     static const int KIND_MASK_FLAGS = 0xFFFFFF00; // Mask to get the flags
     53     static const int KIND_FLAG_POSSIBLY_OFFENSIVE = 0x80000000;
     54     static const int KIND_FLAG_EXACT_MATCH = 0x40000000;
     55 
     56     Dictionary(JNIEnv *env,
     57             DictionaryStructureWithBufferPolicy *const dictionaryStructureWithBufferPoilcy);
     58 
     59     int getSuggestions(ProximityInfo *proximityInfo, DicTraverseSession *traverseSession,
     60             int *xcoordinates, int *ycoordinates, int *times, int *pointerIds, int *inputCodePoints,
     61             int inputSize, int *prevWordCodePoints, int prevWordLength, int commitPoint,
     62             const SuggestOptions *const suggestOptions, int *outWords, int *frequencies,
     63             int *spaceIndices, int *outputTypes, int *outputAutoCommitFirstWordConfidence) const;
     64 
     65     int getBigrams(const int *word, int length, int *outWords, int *frequencies,
     66             int *outputTypes) const;
     67 
     68     int getProbability(const int *word, int length) const;
     69 
     70     int getBigramProbability(const int *word0, int length0, const int *word1, int length1) const;
     71 
     72     void addUnigramWord(const int *const word, const int length, const int probability);
     73 
     74     void addBigramWords(const int *const word0, const int length0, const int *const word1,
     75             const int length1, const int probability);
     76 
     77     void removeBigramWords(const int *const word0, const int length0, const int *const word1,
     78             const int length1);
     79 
     80     void flush(const char *const filePath);
     81 
     82     void flushWithGC(const char *const filePath);
     83 
     84     bool needsToRunGC(const bool mindsBlockByGC);
     85 
     86     void getProperty(const char *const query, char *const outResult,
     87             const int maxResultLength);
     88 
     89     const DictionaryStructureWithBufferPolicy *getDictionaryStructurePolicy() const {
     90         return mDictionaryStructureWithBufferPolicy;
     91     }
     92 
     93     virtual ~Dictionary();
     94 
     95  private:
     96     DISALLOW_IMPLICIT_CONSTRUCTORS(Dictionary);
     97 
     98     static const int HEADER_ATTRIBUTE_BUFFER_SIZE;
     99 
    100     DictionaryStructureWithBufferPolicy *const mDictionaryStructureWithBufferPolicy;
    101     const BigramDictionary *const mBigramDictionary;
    102     const SuggestInterface *const mGestureSuggest;
    103     const SuggestInterface *const mTypingSuggest;
    104 
    105     void logDictionaryInfo(JNIEnv *const env) const;
    106 };
    107 } // namespace latinime
    108 #endif // LATINIME_DICTIONARY_H
    109