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