satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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_UNIGRAM_DICTIONARY_H |
| 18 | #define LATINIME_UNIGRAM_DICTIONARY_H |
| 19 | |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 20 | #include "defines.h" |
| 21 | |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 22 | namespace latinime { |
| 23 | |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 24 | class UnigramDictionary { |
Jean Chalard | 8dc754a | 2011-01-27 14:20:22 +0900 | [diff] [blame] | 25 | |
| 26 | typedef enum { // Used as a return value for character comparison |
| 27 | SAME_OR_ACCENTED_OR_CAPITALIZED_CHAR, // Same char, possibly with different case or accent |
| 28 | NEAR_PROXIMITY_CHAR, // It is a char located nearby on the keyboard |
| 29 | UNRELATED_CHAR // It is an unrelated char |
| 30 | } ProximityType; |
| 31 | |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 32 | public: |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 33 | UnigramDictionary(const unsigned char *dict, int typedLetterMultipler, int fullWordMultiplier, |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 34 | int maxWordLength, int maxWords, int maxProximityChars, const bool isLatestDictVersion); |
Tadashi G. Takaoka | 887f11e | 2011-02-10 20:53:58 +0900 | [diff] [blame] | 35 | int getSuggestions(int *codes, int codesSize, unsigned short *outWords, int *frequencies); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 36 | ~UnigramDictionary(); |
| 37 | |
| 38 | private: |
| 39 | void initSuggestions(int *codes, int codesSize, unsigned short *outWords, int *frequencies); |
satok | 54fe9e0 | 2010-12-13 14:42:35 +0900 | [diff] [blame] | 40 | void getSuggestionCandidates(const int skipPos, const int excessivePos, |
satok | a3d78f6 | 2010-12-09 22:08:33 +0900 | [diff] [blame] | 41 | const int transposedPos, int *nextLetters, const int nextLettersSize, |
| 42 | const int maxDepth); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 43 | void getVersionNumber(); |
| 44 | bool checkIfDictVersionIsLatest(); |
| 45 | int getAddress(int *pos); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 46 | int getFreq(int *pos); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 47 | int wideStrLen(unsigned short *str); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 48 | bool sameAsTyped(unsigned short *word, int length); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 49 | bool addWord(unsigned short *word, int length, int frequency); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 50 | unsigned short toLowerCase(unsigned short c); |
satok | 6831926 | 2010-12-03 19:38:08 +0900 | [diff] [blame] | 51 | void getWordsRec(const int childrenCount, const int pos, const int depth, const int maxDepth, |
| 52 | const bool traverseAllNodes, const int snr, const int inputIndex, const int diffs, |
satok | a3d78f6 | 2010-12-09 22:08:33 +0900 | [diff] [blame] | 53 | const int skipPos, const int excessivePos, const int transposedPos, int *nextLetters, |
| 54 | const int nextLettersSize); |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 55 | bool getMissingSpaceWords(const int inputLength, const int missingSpacePos); |
satok | d299792 | 2010-12-07 13:08:39 +0900 | [diff] [blame] | 56 | // Keep getWordsOld for comparing performance between getWords and getWordsOld |
| 57 | void getWordsOld(const int initialPos, const int inputLength, const int skipPos, |
satok | a3d78f6 | 2010-12-09 22:08:33 +0900 | [diff] [blame] | 58 | const int excessivePos, const int transposedPos, int *nextLetters, |
| 59 | const int nextLettersSize); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 60 | void registerNextLetter(unsigned short c, int *nextLetters, int nextLettersSize); |
satok | 58c49b9 | 2011-01-27 03:23:39 +0900 | [diff] [blame] | 61 | int calculateFinalFreq(const int inputIndex, const int depth, const int snr, const int skipPos, |
satok | 54fe9e0 | 2010-12-13 14:42:35 +0900 | [diff] [blame] | 62 | const int excessivePos, const int transposedPos, const int freq, const bool sameLength); |
satok | 715514d | 2010-12-02 20:19:59 +0900 | [diff] [blame] | 63 | void onTerminalWhenUserTypedLengthIsGreaterThanInputLength(unsigned short *word, |
satok | 54fe9e0 | 2010-12-13 14:42:35 +0900 | [diff] [blame] | 64 | const int inputIndex, const int depth, const int snr, int *nextLetters, |
satok | a3d78f6 | 2010-12-09 22:08:33 +0900 | [diff] [blame] | 65 | const int nextLettersSize, const int skipPos, const int excessivePos, |
| 66 | const int transposedPos, const int freq); |
satok | 54fe9e0 | 2010-12-13 14:42:35 +0900 | [diff] [blame] | 67 | void onTerminalWhenUserTypedLengthIsSameAsInputLength(unsigned short *word, |
| 68 | const int inputIndex, const int depth, const int snr, const int skipPos, |
Jean Chalard | 8dc754a | 2011-01-27 14:20:22 +0900 | [diff] [blame] | 69 | const int excessivePos, const int transposedPos, const int freq); |
satok | 28bd03b | 2010-12-03 16:39:16 +0900 | [diff] [blame] | 70 | bool needsToSkipCurrentNode(const unsigned short c, |
satok | 6831926 | 2010-12-03 19:38:08 +0900 | [diff] [blame] | 71 | const int inputIndex, const int skipPos, const int depth); |
Jean Chalard | 8dc754a | 2011-01-27 14:20:22 +0900 | [diff] [blame] | 72 | ProximityType getMatchedProximityId(const int *currentChars, const unsigned short c, |
| 73 | const int skipPos, const int excessivePos, const int transposedPos); |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 74 | // Process a node by considering proximity, missing and excessive character |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 75 | bool processCurrentNode(const int pos, const int depth, |
satok | cdbbea7 | 2010-12-08 16:04:16 +0900 | [diff] [blame] | 76 | const int maxDepth, const bool traverseAllNodes, const int snr, int inputIndex, |
satok | a3d78f6 | 2010-12-09 22:08:33 +0900 | [diff] [blame] | 77 | const int diffs, const int skipPos, const int excessivePos, const int transposedPos, |
| 78 | int *nextLetters, const int nextLettersSize, int *newCount, int *newChildPosition, |
satok | cdbbea7 | 2010-12-08 16:04:16 +0900 | [diff] [blame] | 79 | bool *newTraverseAllNodes, int *newSnr, int*newInputIndex, int *newDiffs, |
| 80 | int *nextSiblingPosition); |
satok | aee09dc | 2010-12-09 19:21:51 +0900 | [diff] [blame] | 81 | int getBestWordFreq(const int startInputIndex, const int inputLength, unsigned short *word); |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 82 | // Process a node by considering missing space |
satok | aee09dc | 2010-12-09 19:21:51 +0900 | [diff] [blame] | 83 | bool processCurrentNodeForExactMatch(const int firstChildPos, |
| 84 | const int startInputIndex, const int depth, unsigned short *word, |
| 85 | int *newChildPosition, int *newCount, bool *newTerminal, int *newFreq, int *siblingPos); |
satok | e07baa6 | 2010-12-09 21:55:40 +0900 | [diff] [blame] | 86 | bool existsAdjacentProximityChars(const int inputIndex, const int inputLength); |
| 87 | int* getInputCharsAt(const int index) {return mInputCodes + (index * MAX_PROXIMITY_CHARS);} |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 88 | const unsigned char *DICT; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 89 | const int MAX_WORD_LENGTH; |
Ken Wakasa | e90b333 | 2011-01-07 15:01:51 +0900 | [diff] [blame] | 90 | const int MAX_WORDS; |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 91 | const int MAX_PROXIMITY_CHARS; |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 92 | const bool IS_LATEST_DICT_VERSION; |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 93 | const int TYPED_LETTER_MULTIPLIER; |
| 94 | const int FULL_WORD_MULTIPLIER; |
Ken Wakasa | e90b333 | 2011-01-07 15:01:51 +0900 | [diff] [blame] | 95 | const int ROOT_POS; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 96 | |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 97 | int *mFrequencies; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 98 | unsigned short *mOutputChars; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 99 | int *mInputCodes; |
| 100 | int mInputLength; |
satok | 715514d | 2010-12-02 20:19:59 +0900 | [diff] [blame] | 101 | // MAX_WORD_LENGTH_INTERNAL must be bigger than MAX_WORD_LENGTH |
| 102 | unsigned short mWord[MAX_WORD_LENGTH_INTERNAL]; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 103 | int mMaxEditDistance; |
satok | d299792 | 2010-12-07 13:08:39 +0900 | [diff] [blame] | 104 | |
| 105 | int mStackChildCount[MAX_WORD_LENGTH_INTERNAL]; |
| 106 | bool mStackTraverseAll[MAX_WORD_LENGTH_INTERNAL]; |
| 107 | int mStackNodeFreq[MAX_WORD_LENGTH_INTERNAL]; |
| 108 | int mStackInputIndex[MAX_WORD_LENGTH_INTERNAL]; |
| 109 | int mStackDiffs[MAX_WORD_LENGTH_INTERNAL]; |
| 110 | int mStackSiblingPos[MAX_WORD_LENGTH_INTERNAL]; |
Tadashi G. Takaoka | 887f11e | 2011-02-10 20:53:58 +0900 | [diff] [blame] | 111 | int mNextLettersFrequency[NEXT_LETTERS_SIZE]; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | // ---------------------------------------------------------------------------- |
| 115 | |
| 116 | }; // namespace latinime |
| 117 | |
| 118 | #endif // LATINIME_UNIGRAM_DICTIONARY_H |