blob: 2dac3b2c4934d7e50c7824c6a05f59bbbe92c56c [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:
satokd4952c82010-12-01 19:09:29 +090051 void initSuggestions(int *codes, int codesSize, unsigned short *outWords, int *frequencies,
52 int maxWordLength, int maxWords, int maxAlternatives);
53 int getSuggestionCandidates(int inputLength, int maxWords, int skipPos, int *nextLetters,
54 int nextLettersSize);
Jae Yong Sung937d5ad2010-06-30 20:28:04 -070055 void getVersionNumber();
56 bool checkIfDictVersionIsLatest();
The Android Open Source Project923bf412009-03-13 15:11:42 -070057 int getAddress(int *pos);
Jae Yong Sung937d5ad2010-06-30 20:28:04 -070058 int getBigramAddress(int *pos, bool advance);
59 int getFreq(int *pos);
60 int getBigramFreq(int *pos);
61 void searchForTerminalNode(int address, int frequency);
62
63 bool getFirstBitOfByte(int *pos) { return (mDict[*pos] & 0x80) > 0; }
64 bool getSecondBitOfByte(int *pos) { return (mDict[*pos] & 0x40) > 0; }
Amith Yamasanicc3e5c72009-03-31 10:51:17 -070065 bool getTerminal(int *pos) { return (mDict[*pos] & FLAG_TERMINAL_MASK) > 0; }
The Android Open Source Project923bf412009-03-13 15:11:42 -070066 int getCount(int *pos) { return mDict[(*pos)++] & 0xFF; }
67 unsigned short getChar(int *pos);
68 int wideStrLen(unsigned short *str);
Amith Yamasanicc3e5c72009-03-31 10:51:17 -070069
The Android Open Source Project923bf412009-03-13 15:11:42 -070070 bool sameAsTyped(unsigned short *word, int length);
Jae Yong Sung80aa14f2010-07-26 11:43:29 -070071 bool checkFirstCharacter(unsigned short *word);
The Android Open Source Project923bf412009-03-13 15:11:42 -070072 bool addWord(unsigned short *word, int length, int frequency);
Jae Yong Sung937d5ad2010-06-30 20:28:04 -070073 bool addWordBigram(unsigned short *word, int length, int frequency);
Amith Yamasanif1150882009-08-07 14:04:24 -070074 unsigned short toLowerCase(unsigned short c);
Amith Yamasanicc3e5c72009-03-31 10:51:17 -070075 void getWordsRec(int pos, int depth, int maxDepth, bool completion, int frequency,
satokd4952c82010-12-01 19:09:29 +090076 int inputIndex, int diffs, int skipPos, int *nextLetters, int nextLettersSize);
Jae Yong Sung937d5ad2010-06-30 20:28:04 -070077 int isValidWordRec(int pos, unsigned short *word, int offset, int length);
satokd4952c82010-12-01 19:09:29 +090078 void registerNextLetter(unsigned short c, int *nextLetters, int nextLettersSize);
The Android Open Source Project923bf412009-03-13 15:11:42 -070079
80 unsigned char *mDict;
81 void *mAsset;
Amith Yamasanicc3e5c72009-03-31 10:51:17 -070082
The Android Open Source Project923bf412009-03-13 15:11:42 -070083 int *mFrequencies;
Jae Yong Sung937d5ad2010-06-30 20:28:04 -070084 int *mBigramFreq;
The Android Open Source Project923bf412009-03-13 15:11:42 -070085 int mMaxWords;
Jae Yong Sung937d5ad2010-06-30 20:28:04 -070086 int mMaxBigrams;
The Android Open Source Project923bf412009-03-13 15:11:42 -070087 int mMaxWordLength;
The Android Open Source Project923bf412009-03-13 15:11:42 -070088 unsigned short *mOutputChars;
Jae Yong Sung937d5ad2010-06-30 20:28:04 -070089 unsigned short *mBigramChars;
The Android Open Source Project923bf412009-03-13 15:11:42 -070090 int *mInputCodes;
91 int mInputLength;
92 int mMaxAlternatives;
93 unsigned short mWord[128];
Amith Yamasani322dc3d2009-07-15 18:30:47 -070094 int mMaxEditDistance;
Amith Yamasanicc3e5c72009-03-31 10:51:17 -070095
The Android Open Source Project923bf412009-03-13 15:11:42 -070096 int mFullWordMultiplier;
97 int mTypedLetterMultiplier;
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