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 | |
Jean Chalard | 293ece0 | 2011-06-16 20:55:16 +0900 | [diff] [blame] | 20 | #include <stdint.h> |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 21 | #include "defines.h" |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 22 | #include "proximity_info.h" |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 23 | |
Jean Chalard | 293ece0 | 2011-06-16 20:55:16 +0900 | [diff] [blame] | 24 | #ifndef NULL |
| 25 | #define NULL 0 |
| 26 | #endif |
| 27 | |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 28 | namespace latinime { |
| 29 | |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 30 | class UnigramDictionary { |
Jean Chalard | 8dc754a | 2011-01-27 14:20:22 +0900 | [diff] [blame] | 31 | |
| 32 | typedef enum { // Used as a return value for character comparison |
| 33 | SAME_OR_ACCENTED_OR_CAPITALIZED_CHAR, // Same char, possibly with different case or accent |
| 34 | NEAR_PROXIMITY_CHAR, // It is a char located nearby on the keyboard |
| 35 | UNRELATED_CHAR // It is an unrelated char |
| 36 | } ProximityType; |
| 37 | |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 38 | public: |
Jean Chalard | 293ece0 | 2011-06-16 20:55:16 +0900 | [diff] [blame] | 39 | UnigramDictionary(const uint8_t* const streamStart, int typedLetterMultipler, |
| 40 | int fullWordMultiplier, int maxWordLength, int maxWords, int maxProximityChars, |
| 41 | const bool isLatestDictVersion); |
Jean Chalard | 8124e64 | 2011-06-16 22:33:41 +0900 | [diff] [blame] | 42 | bool isValidWord(unsigned short *word, int length); |
Jean Chalard | 581335c | 2011-06-17 12:45:17 +0900 | [diff] [blame] | 43 | int getBigramPosition(int pos, unsigned short *word, int offset, int length) const; |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 44 | int getSuggestions(const ProximityInfo *proximityInfo, const int *xcoordinates, |
| 45 | const int *ycoordinates, const int *codes, const int codesSize, const int flags, |
| 46 | unsigned short *outWords, int *frequencies); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 47 | ~UnigramDictionary(); |
| 48 | |
| 49 | private: |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 50 | void getWordSuggestions(const ProximityInfo *proximityInfo, const int *xcoordinates, |
| 51 | const int *ycoordinates, const int *codes, const int codesSize, |
| 52 | unsigned short *outWords, int *frequencies); |
| 53 | bool isDigraph(const int* codes, const int i, const int codesSize) const; |
| 54 | void getWordWithDigraphSuggestionsRec(const ProximityInfo *proximityInfo, |
| 55 | const int *xcoordinates, const int* ycoordinates, const int *codesBuffer, |
| 56 | const int codesBufferSize, const int flags, const int* codesSrc, const int codesRemain, |
satok | 3c4bb77 | 2011-03-04 22:50:19 -0800 | [diff] [blame] | 57 | const int currentDepth, int* codesDest, unsigned short* outWords, int* frequencies); |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 58 | void initSuggestions(const int *codes, const int codesSize, unsigned short *outWords, |
| 59 | int *frequencies); |
satok | 54fe9e0 | 2010-12-13 14:42:35 +0900 | [diff] [blame] | 60 | void getSuggestionCandidates(const int skipPos, const int excessivePos, |
satok | a3d78f6 | 2010-12-09 22:08:33 +0900 | [diff] [blame] | 61 | const int transposedPos, int *nextLetters, const int nextLettersSize, |
| 62 | const int maxDepth); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 63 | void getVersionNumber(); |
| 64 | bool checkIfDictVersionIsLatest(); |
| 65 | int getAddress(int *pos); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 66 | int getFreq(int *pos); |
Jean Chalard | ca5ef28 | 2011-06-17 15:36:26 +0900 | [diff] [blame] | 67 | bool sameAsTyped(const unsigned short *word, int length) const; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 68 | bool addWord(unsigned short *word, int length, int frequency); |
Jean Chalard | ca5ef28 | 2011-06-17 15:36:26 +0900 | [diff] [blame] | 69 | void addWordAlternatesSpellings(const uint8_t* const root, int pos, int depth, int finalFreq); |
satok | 6831926 | 2010-12-03 19:38:08 +0900 | [diff] [blame] | 70 | void getWordsRec(const int childrenCount, const int pos, const int depth, const int maxDepth, |
| 71 | const bool traverseAllNodes, const int snr, const int inputIndex, const int diffs, |
satok | a3d78f6 | 2010-12-09 22:08:33 +0900 | [diff] [blame] | 72 | const int skipPos, const int excessivePos, const int transposedPos, int *nextLetters, |
| 73 | const int nextLettersSize); |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 74 | bool getSplitTwoWordsSuggestion(const int inputLength, |
| 75 | const int firstWordStartPos, const int firstWordLength, |
satok | d8db9f8 | 2011-05-18 15:31:04 +0900 | [diff] [blame] | 76 | const int secondWordStartPos, const int secondWordLength, const bool isSpaceProximity); |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 77 | bool getMissingSpaceWords(const int inputLength, const int missingSpacePos); |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 78 | bool getMistypedSpaceWords(const int inputLength, const int spaceProximityPos); |
satok | d299792 | 2010-12-07 13:08:39 +0900 | [diff] [blame] | 79 | // Keep getWordsOld for comparing performance between getWords and getWordsOld |
| 80 | void getWordsOld(const int initialPos, const int inputLength, const int skipPos, |
satok | a3d78f6 | 2010-12-09 22:08:33 +0900 | [diff] [blame] | 81 | const int excessivePos, const int transposedPos, int *nextLetters, |
| 82 | const int nextLettersSize); |
satok | 58c49b9 | 2011-01-27 03:23:39 +0900 | [diff] [blame] | 83 | int calculateFinalFreq(const int inputIndex, const int depth, const int snr, const int skipPos, |
Jean Chalard | 07a8406 | 2011-03-03 10:22:10 +0900 | [diff] [blame] | 84 | const int excessivePos, const int transposedPos, const int freq, |
| 85 | const bool sameLength) const; |
Jean Chalard | ca5ef28 | 2011-06-17 15:36:26 +0900 | [diff] [blame] | 86 | void onTerminal(unsigned short int* word, const int depth, |
| 87 | const uint8_t* const root, const uint8_t flags, int pos, |
| 88 | const int inputIndex, const int matchWeight, const int skipPos, |
| 89 | const int excessivePos, const int transposedPos, const int freq, const bool sameLength, |
| 90 | int *nextLetters, const int nextLettersSize); |
satok | 28bd03b | 2010-12-03 16:39:16 +0900 | [diff] [blame] | 91 | bool needsToSkipCurrentNode(const unsigned short c, |
satok | 6831926 | 2010-12-03 19:38:08 +0900 | [diff] [blame] | 92 | const int inputIndex, const int skipPos, const int depth); |
Jean Chalard | 8dc754a | 2011-01-27 14:20:22 +0900 | [diff] [blame] | 93 | ProximityType getMatchedProximityId(const int *currentChars, const unsigned short c, |
| 94 | const int skipPos, const int excessivePos, const int transposedPos); |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 95 | // Process a node by considering proximity, missing and excessive character |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 96 | bool processCurrentNode(const int pos, const int depth, |
satok | cdbbea7 | 2010-12-08 16:04:16 +0900 | [diff] [blame] | 97 | const int maxDepth, const bool traverseAllNodes, const int snr, int inputIndex, |
satok | a3d78f6 | 2010-12-09 22:08:33 +0900 | [diff] [blame] | 98 | const int diffs, const int skipPos, const int excessivePos, const int transposedPos, |
| 99 | int *nextLetters, const int nextLettersSize, int *newCount, int *newChildPosition, |
satok | cdbbea7 | 2010-12-08 16:04:16 +0900 | [diff] [blame] | 100 | bool *newTraverseAllNodes, int *newSnr, int*newInputIndex, int *newDiffs, |
Jean Chalard | 17e44a7 | 2011-06-16 22:51:11 +0900 | [diff] [blame] | 101 | int *nextSiblingPosition, int *nextOutputIndex); |
satok | aee09dc | 2010-12-09 19:21:51 +0900 | [diff] [blame] | 102 | int getBestWordFreq(const int startInputIndex, const int inputLength, unsigned short *word); |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 103 | // Process a node by considering missing space |
satok | aee09dc | 2010-12-09 19:21:51 +0900 | [diff] [blame] | 104 | bool processCurrentNodeForExactMatch(const int firstChildPos, |
| 105 | const int startInputIndex, const int depth, unsigned short *word, |
| 106 | int *newChildPosition, int *newCount, bool *newTerminal, int *newFreq, int *siblingPos); |
Jean Chalard | 07a8406 | 2011-03-03 10:22:10 +0900 | [diff] [blame] | 107 | bool existsAdjacentProximityChars(const int inputIndex, const int inputLength) const; |
| 108 | inline const int* getInputCharsAt(const int index) const { |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 109 | return mInputCodes + (index * MAX_PROXIMITY_CHARS); |
| 110 | } |
Jean Chalard | 293ece0 | 2011-06-16 20:55:16 +0900 | [diff] [blame] | 111 | |
| 112 | const uint8_t* const DICT_ROOT; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 113 | const int MAX_WORD_LENGTH; |
Ken Wakasa | e90b333 | 2011-01-07 15:01:51 +0900 | [diff] [blame] | 114 | const int MAX_WORDS; |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 115 | const int MAX_PROXIMITY_CHARS; |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 116 | const bool IS_LATEST_DICT_VERSION; |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 117 | const int TYPED_LETTER_MULTIPLIER; |
| 118 | const int FULL_WORD_MULTIPLIER; |
Ken Wakasa | e90b333 | 2011-01-07 15:01:51 +0900 | [diff] [blame] | 119 | const int ROOT_POS; |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 120 | const unsigned int BYTES_IN_ONE_CHAR; |
satok | 3c4bb77 | 2011-03-04 22:50:19 -0800 | [diff] [blame] | 121 | const int MAX_UMLAUT_SEARCH_DEPTH; |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 122 | |
| 123 | // Flags for special processing |
| 124 | // Those *must* match the flags in BinaryDictionary.Flags.ALL_FLAGS in BinaryDictionary.java |
| 125 | // or something very bad (like, the apocalypse) will happen. |
| 126 | // Please update both at the same time. |
| 127 | enum { |
| 128 | REQUIRES_GERMAN_UMLAUT_PROCESSING = 0x1 |
| 129 | }; |
| 130 | static const struct digraph_t { int first; int second; } GERMAN_UMLAUT_DIGRAPHS[]; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 131 | |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 132 | int *mFrequencies; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 133 | unsigned short *mOutputChars; |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 134 | const int *mInputCodes; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 135 | int mInputLength; |
satok | 715514d | 2010-12-02 20:19:59 +0900 | [diff] [blame] | 136 | // MAX_WORD_LENGTH_INTERNAL must be bigger than MAX_WORD_LENGTH |
| 137 | unsigned short mWord[MAX_WORD_LENGTH_INTERNAL]; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 138 | int mMaxEditDistance; |
satok | d299792 | 2010-12-07 13:08:39 +0900 | [diff] [blame] | 139 | |
| 140 | int mStackChildCount[MAX_WORD_LENGTH_INTERNAL]; |
| 141 | bool mStackTraverseAll[MAX_WORD_LENGTH_INTERNAL]; |
| 142 | int mStackNodeFreq[MAX_WORD_LENGTH_INTERNAL]; |
| 143 | int mStackInputIndex[MAX_WORD_LENGTH_INTERNAL]; |
| 144 | int mStackDiffs[MAX_WORD_LENGTH_INTERNAL]; |
| 145 | int mStackSiblingPos[MAX_WORD_LENGTH_INTERNAL]; |
Jean Chalard | 17e44a7 | 2011-06-16 22:51:11 +0900 | [diff] [blame] | 146 | int mStackOutputIndex[MAX_WORD_LENGTH_INTERNAL]; |
Tadashi G. Takaoka | 887f11e | 2011-02-10 20:53:58 +0900 | [diff] [blame] | 147 | int mNextLettersFrequency[NEXT_LETTERS_SIZE]; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | // ---------------------------------------------------------------------------- |
| 151 | |
| 152 | }; // namespace latinime |
| 153 | |
| 154 | #endif // LATINIME_UNIGRAM_DICTIONARY_H |