blob: d13496e01a7d57ff4fa898a654d7564061477a1b [file] [log] [blame]
The Android Open Source Project923bf412009-03-13 15:11:42 -07001/*
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
20namespace latinime {
21
Amith Yamasanicc3e5c72009-03-31 10:51:17 -070022// 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
Jae Yong Sung937d5ad2010-06-30 20:28:04 -070031#define FLAG_BIGRAM_READ 0x80
32#define FLAG_BIGRAM_CHILDEXIST 0x40
33#define FLAG_BIGRAM_CONTINUED 0x80
34#define FLAG_BIGRAM_FREQ 0x7F
35
The Android Open Source Project923bf412009-03-13 15:11:42 -070036class Dictionary {
37public:
38 Dictionary(void *dict, int typedLetterMultipler, int fullWordMultiplier);
Amith Yamasanicc3e5c72009-03-31 10:51:17 -070039 int getSuggestions(int *codes, int codesSize, unsigned short *outWords, int *frequencies,
Amith Yamasani1b62ff12010-02-05 14:07:04 -080040 int maxWordLength, int maxWords, int maxAlternatives, int skipPos,
41 int *nextLetters, int nextLettersSize);
Jae Yong Sung80aa14f2010-07-26 11:43:29 -070042 int getBigrams(unsigned short *word, int length, int *codes, int codesSize,
43 unsigned short *outWords, int *frequencies, int maxWordLength, int maxBigrams,
44 int maxAlternatives);
The Android Open Source Project923bf412009-03-13 15:11:42 -070045 bool isValidWord(unsigned short *word, int length);
46 void setAsset(void *asset) { mAsset = asset; }
47 void *getAsset() { return mAsset; }
48 ~Dictionary();
Amith Yamasanicc3e5c72009-03-31 10:51:17 -070049
The Android Open Source Project923bf412009-03-13 15:11:42 -070050private:
51
Jae Yong Sung937d5ad2010-06-30 20:28:04 -070052 void getVersionNumber();
53 bool checkIfDictVersionIsLatest();
The Android Open Source Project923bf412009-03-13 15:11:42 -070054 int getAddress(int *pos);
Jae Yong Sung937d5ad2010-06-30 20:28:04 -070055 int getBigramAddress(int *pos, bool advance);
56 int getFreq(int *pos);
57 int getBigramFreq(int *pos);
58 void searchForTerminalNode(int address, int frequency);
59
60 bool getFirstBitOfByte(int *pos) { return (mDict[*pos] & 0x80) > 0; }
61 bool getSecondBitOfByte(int *pos) { return (mDict[*pos] & 0x40) > 0; }
Amith Yamasanicc3e5c72009-03-31 10:51:17 -070062 bool getTerminal(int *pos) { return (mDict[*pos] & FLAG_TERMINAL_MASK) > 0; }
The Android Open Source Project923bf412009-03-13 15:11:42 -070063 int getCount(int *pos) { return mDict[(*pos)++] & 0xFF; }
64 unsigned short getChar(int *pos);
65 int wideStrLen(unsigned short *str);
Amith Yamasanicc3e5c72009-03-31 10:51:17 -070066
The Android Open Source Project923bf412009-03-13 15:11:42 -070067 bool sameAsTyped(unsigned short *word, int length);
Jae Yong Sung80aa14f2010-07-26 11:43:29 -070068 bool checkFirstCharacter(unsigned short *word);
The Android Open Source Project923bf412009-03-13 15:11:42 -070069 bool addWord(unsigned short *word, int length, int frequency);
Jae Yong Sung937d5ad2010-06-30 20:28:04 -070070 bool addWordBigram(unsigned short *word, int length, int frequency);
Amith Yamasanif1150882009-08-07 14:04:24 -070071 unsigned short toLowerCase(unsigned short c);
Amith Yamasanicc3e5c72009-03-31 10:51:17 -070072 void getWordsRec(int pos, int depth, int maxDepth, bool completion, int frequency,
Amith Yamasani322dc3d2009-07-15 18:30:47 -070073 int inputIndex, int diffs);
Jae Yong Sung937d5ad2010-06-30 20:28:04 -070074 int isValidWordRec(int pos, unsigned short *word, int offset, int length);
Amith Yamasani1b62ff12010-02-05 14:07:04 -080075 void registerNextLetter(unsigned short c);
The Android Open Source Project923bf412009-03-13 15:11:42 -070076
77 unsigned char *mDict;
78 void *mAsset;
Amith Yamasanicc3e5c72009-03-31 10:51:17 -070079
The Android Open Source Project923bf412009-03-13 15:11:42 -070080 int *mFrequencies;
Jae Yong Sung937d5ad2010-06-30 20:28:04 -070081 int *mBigramFreq;
The Android Open Source Project923bf412009-03-13 15:11:42 -070082 int mMaxWords;
Jae Yong Sung937d5ad2010-06-30 20:28:04 -070083 int mMaxBigrams;
The Android Open Source Project923bf412009-03-13 15:11:42 -070084 int mMaxWordLength;
The Android Open Source Project923bf412009-03-13 15:11:42 -070085 unsigned short *mOutputChars;
Jae Yong Sung937d5ad2010-06-30 20:28:04 -070086 unsigned short *mBigramChars;
The Android Open Source Project923bf412009-03-13 15:11:42 -070087 int *mInputCodes;
88 int mInputLength;
89 int mMaxAlternatives;
90 unsigned short mWord[128];
Amith Yamasanic3df2d62009-06-04 12:20:45 -070091 int mSkipPos;
Amith Yamasani322dc3d2009-07-15 18:30:47 -070092 int mMaxEditDistance;
Amith Yamasanicc3e5c72009-03-31 10:51:17 -070093
The Android Open Source Project923bf412009-03-13 15:11:42 -070094 int mFullWordMultiplier;
95 int mTypedLetterMultiplier;
Amith Yamasani1b62ff12010-02-05 14:07:04 -080096 int *mNextLettersFrequencies;
97 int mNextLettersSize;
Jae Yong Sung937d5ad2010-06-30 20:28:04 -070098 int mVersion;
99 int mBigram;
The Android Open Source Project923bf412009-03-13 15:11:42 -0700100};
101
102// ----------------------------------------------------------------------------
103
104}; // namespace latinime
105
106#endif // LATINIME_DICTIONARY_H