The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 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 | |
Amith Yamasani | cc3e5c7 | 2009-03-31 10:51:17 -0700 | [diff] [blame] | 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 | |
Jae Yong Sung | 937d5ad | 2010-06-30 20:28:04 -0700 | [diff] [blame] | 31 | #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 Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 36 | class Dictionary { |
| 37 | public: |
| 38 | Dictionary(void *dict, int typedLetterMultipler, int fullWordMultiplier); |
Amith Yamasani | cc3e5c7 | 2009-03-31 10:51:17 -0700 | [diff] [blame] | 39 | int getSuggestions(int *codes, int codesSize, unsigned short *outWords, int *frequencies, |
Amith Yamasani | 1b62ff1 | 2010-02-05 14:07:04 -0800 | [diff] [blame] | 40 | int maxWordLength, int maxWords, int maxAlternatives, int skipPos, |
| 41 | int *nextLetters, int nextLettersSize); |
Jae Yong Sung | 80aa14f | 2010-07-26 11:43:29 -0700 | [diff] [blame] | 42 | 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 Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 45 | bool isValidWord(unsigned short *word, int length); |
| 46 | void setAsset(void *asset) { mAsset = asset; } |
| 47 | void *getAsset() { return mAsset; } |
| 48 | ~Dictionary(); |
Amith Yamasani | cc3e5c7 | 2009-03-31 10:51:17 -0700 | [diff] [blame] | 49 | |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 50 | private: |
satok | d4952c8 | 2010-12-01 19:09:29 +0900 | [diff] [blame^] | 51 | 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 Sung | 937d5ad | 2010-06-30 20:28:04 -0700 | [diff] [blame] | 55 | void getVersionNumber(); |
| 56 | bool checkIfDictVersionIsLatest(); |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 57 | int getAddress(int *pos); |
Jae Yong Sung | 937d5ad | 2010-06-30 20:28:04 -0700 | [diff] [blame] | 58 | 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 Yamasani | cc3e5c7 | 2009-03-31 10:51:17 -0700 | [diff] [blame] | 65 | bool getTerminal(int *pos) { return (mDict[*pos] & FLAG_TERMINAL_MASK) > 0; } |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 66 | int getCount(int *pos) { return mDict[(*pos)++] & 0xFF; } |
| 67 | unsigned short getChar(int *pos); |
| 68 | int wideStrLen(unsigned short *str); |
Amith Yamasani | cc3e5c7 | 2009-03-31 10:51:17 -0700 | [diff] [blame] | 69 | |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 70 | bool sameAsTyped(unsigned short *word, int length); |
Jae Yong Sung | 80aa14f | 2010-07-26 11:43:29 -0700 | [diff] [blame] | 71 | bool checkFirstCharacter(unsigned short *word); |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 72 | bool addWord(unsigned short *word, int length, int frequency); |
Jae Yong Sung | 937d5ad | 2010-06-30 20:28:04 -0700 | [diff] [blame] | 73 | bool addWordBigram(unsigned short *word, int length, int frequency); |
Amith Yamasani | f115088 | 2009-08-07 14:04:24 -0700 | [diff] [blame] | 74 | unsigned short toLowerCase(unsigned short c); |
Amith Yamasani | cc3e5c7 | 2009-03-31 10:51:17 -0700 | [diff] [blame] | 75 | void getWordsRec(int pos, int depth, int maxDepth, bool completion, int frequency, |
satok | d4952c8 | 2010-12-01 19:09:29 +0900 | [diff] [blame^] | 76 | int inputIndex, int diffs, int skipPos, int *nextLetters, int nextLettersSize); |
Jae Yong Sung | 937d5ad | 2010-06-30 20:28:04 -0700 | [diff] [blame] | 77 | int isValidWordRec(int pos, unsigned short *word, int offset, int length); |
satok | d4952c8 | 2010-12-01 19:09:29 +0900 | [diff] [blame^] | 78 | void registerNextLetter(unsigned short c, int *nextLetters, int nextLettersSize); |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 79 | |
| 80 | unsigned char *mDict; |
| 81 | void *mAsset; |
Amith Yamasani | cc3e5c7 | 2009-03-31 10:51:17 -0700 | [diff] [blame] | 82 | |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 83 | int *mFrequencies; |
Jae Yong Sung | 937d5ad | 2010-06-30 20:28:04 -0700 | [diff] [blame] | 84 | int *mBigramFreq; |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 85 | int mMaxWords; |
Jae Yong Sung | 937d5ad | 2010-06-30 20:28:04 -0700 | [diff] [blame] | 86 | int mMaxBigrams; |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 87 | int mMaxWordLength; |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 88 | unsigned short *mOutputChars; |
Jae Yong Sung | 937d5ad | 2010-06-30 20:28:04 -0700 | [diff] [blame] | 89 | unsigned short *mBigramChars; |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 90 | int *mInputCodes; |
| 91 | int mInputLength; |
| 92 | int mMaxAlternatives; |
| 93 | unsigned short mWord[128]; |
Amith Yamasani | 322dc3d | 2009-07-15 18:30:47 -0700 | [diff] [blame] | 94 | int mMaxEditDistance; |
Amith Yamasani | cc3e5c7 | 2009-03-31 10:51:17 -0700 | [diff] [blame] | 95 | |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 96 | int mFullWordMultiplier; |
| 97 | int mTypedLetterMultiplier; |
Jae Yong Sung | 937d5ad | 2010-06-30 20:28:04 -0700 | [diff] [blame] | 98 | int mVersion; |
| 99 | int mBigram; |
The Android Open Source Project | 923bf41 | 2009-03-13 15:11:42 -0700 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | // ---------------------------------------------------------------------------- |
| 103 | |
| 104 | }; // namespace latinime |
| 105 | |
| 106 | #endif // LATINIME_DICTIONARY_H |