• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 namespace latinime {
21 
22 // 22-bit address = ~4MB dictionary size limit, which on average would be about 200k-300k words
23 #define ADDRESS_MASK 0x3FFFFF
24 
25 // The bit that decides if an address follows in the next 22 bits
26 #define FLAG_ADDRESS_MASK 0x40
27 // The bit that decides if this is a terminal node for a word. The node could still have children,
28 // if the word has other endings.
29 #define FLAG_TERMINAL_MASK 0x80
30 
31 class Dictionary {
32 public:
33     Dictionary(void *dict, int typedLetterMultipler, int fullWordMultiplier);
34     int getSuggestions(int *codes, int codesSize, unsigned short *outWords, int *frequencies,
35         int maxWordLength, int maxWords, int maxAlternatives, int skipPos);
36     bool isValidWord(unsigned short *word, int length);
setAsset(void * asset)37     void setAsset(void *asset) { mAsset = asset; }
getAsset()38     void *getAsset() { return mAsset; }
39     ~Dictionary();
40 
41 private:
42 
43     int getAddress(int *pos);
getTerminal(int * pos)44     bool getTerminal(int *pos) { return (mDict[*pos] & FLAG_TERMINAL_MASK) > 0; }
getFreq(int * pos)45     int getFreq(int *pos) { return mDict[(*pos)++] & 0xFF; }
getCount(int * pos)46     int getCount(int *pos) { return mDict[(*pos)++] & 0xFF; }
47     unsigned short getChar(int *pos);
48     int wideStrLen(unsigned short *str);
49 
50     bool sameAsTyped(unsigned short *word, int length);
51     bool addWord(unsigned short *word, int length, int frequency);
52     unsigned short toLowerCase(unsigned short c);
53     void getWordsRec(int pos, int depth, int maxDepth, bool completion, int frequency,
54             int inputIndex, int diffs);
55     bool isValidWordRec(int pos, unsigned short *word, int offset, int length);
56 
57     unsigned char *mDict;
58     void *mAsset;
59 
60     int *mFrequencies;
61     int mMaxWords;
62     int mMaxWordLength;
63     int mWords;
64     unsigned short *mOutputChars;
65     int *mInputCodes;
66     int mInputLength;
67     int mMaxAlternatives;
68     unsigned short mWord[128];
69     int mSkipPos;
70     int mMaxEditDistance;
71 
72     int mFullWordMultiplier;
73     int mTypedLetterMultiplier;
74 };
75 
76 // ----------------------------------------------------------------------------
77 
78 }; // namespace latinime
79 
80 #endif // LATINIME_DICTIONARY_H
81