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 | |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 32 | public: |
Jean Chalard | 1059f27 | 2011-06-28 20:45:05 +0900 | [diff] [blame] | 33 | #ifdef NEW_DICTIONARY_FORMAT |
| 34 | |
| 35 | // Mask and flags for children address type selection. |
| 36 | static const int MASK_GROUP_ADDRESS_TYPE = 0xC0; |
| 37 | static const int FLAG_GROUP_ADDRESS_TYPE_NOADDRESS = 0x00; |
| 38 | static const int FLAG_GROUP_ADDRESS_TYPE_ONEBYTE = 0x40; |
| 39 | static const int FLAG_GROUP_ADDRESS_TYPE_TWOBYTES = 0x80; |
| 40 | static const int FLAG_GROUP_ADDRESS_TYPE_THREEBYTES = 0xC0; |
| 41 | |
| 42 | // Flag for single/multiple char group |
| 43 | static const int FLAG_HAS_MULTIPLE_CHARS = 0x20; |
| 44 | |
| 45 | // Flag for terminal groups |
| 46 | static const int FLAG_IS_TERMINAL = 0x10; |
| 47 | |
| 48 | // Flag for bigram presence |
| 49 | static const int FLAG_HAS_BIGRAMS = 0x04; |
| 50 | |
| 51 | // Attribute (bigram/shortcut) related flags: |
| 52 | // Flag for presence of more attributes |
| 53 | static const int FLAG_ATTRIBUTE_HAS_NEXT = 0x80; |
| 54 | // Flag for sign of offset. If this flag is set, the offset value must be negated. |
| 55 | static const int FLAG_ATTRIBUTE_OFFSET_NEGATIVE = 0x40; |
| 56 | |
| 57 | // Mask for attribute frequency, stored on 4 bits inside the flags byte. |
| 58 | static const int MASK_ATTRIBUTE_FREQUENCY = 0x0F; |
| 59 | |
| 60 | // Mask and flags for attribute address type selection. |
| 61 | static const int MASK_ATTRIBUTE_ADDRESS_TYPE = 0x30; |
| 62 | static const int FLAG_ATTRIBUTE_ADDRESS_TYPE_ONEBYTE = 0x10; |
| 63 | static const int FLAG_ATTRIBUTE_ADDRESS_TYPE_TWOBYTES = 0x20; |
| 64 | static const int FLAG_ATTRIBUTE_ADDRESS_TYPE_THREEBYTES = 0x30; |
| 65 | #endif // NEW_DICTIONARY_FORMAT |
| 66 | |
Jean Chalard | 293ece0 | 2011-06-16 20:55:16 +0900 | [diff] [blame] | 67 | UnigramDictionary(const uint8_t* const streamStart, int typedLetterMultipler, |
| 68 | int fullWordMultiplier, int maxWordLength, int maxWords, int maxProximityChars, |
| 69 | const bool isLatestDictVersion); |
Jean Chalard | 1059f27 | 2011-06-28 20:45:05 +0900 | [diff] [blame] | 70 | #ifndef NEW_DICTIONARY_FORMAT |
Jean Chalard | 8124e64 | 2011-06-16 22:33:41 +0900 | [diff] [blame] | 71 | bool isValidWord(unsigned short *word, int length); |
Jean Chalard | 1059f27 | 2011-06-28 20:45:05 +0900 | [diff] [blame] | 72 | #else // NEW_DICTIONARY_FORMAT |
| 73 | bool isValidWord(const uint16_t* const inWord, const int length) const; |
Jean Chalard | 1059f27 | 2011-06-28 20:45:05 +0900 | [diff] [blame] | 74 | #endif // NEW_DICTIONARY_FORMAT |
Jean Chalard | 581335c | 2011-06-17 12:45:17 +0900 | [diff] [blame] | 75 | int getBigramPosition(int pos, unsigned short *word, int offset, int length) const; |
satok | 1d7eaf8 | 2011-07-13 10:32:02 +0900 | [diff] [blame] | 76 | int getSuggestions(ProximityInfo *proximityInfo, const int *xcoordinates, |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 77 | const int *ycoordinates, const int *codes, const int codesSize, const int flags, |
| 78 | unsigned short *outWords, int *frequencies); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 79 | ~UnigramDictionary(); |
| 80 | |
| 81 | private: |
satok | 1d7eaf8 | 2011-07-13 10:32:02 +0900 | [diff] [blame] | 82 | void getWordSuggestions(ProximityInfo *proximityInfo, const int *xcoordinates, |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 83 | const int *ycoordinates, const int *codes, const int codesSize, |
| 84 | unsigned short *outWords, int *frequencies); |
| 85 | bool isDigraph(const int* codes, const int i, const int codesSize) const; |
satok | 1d7eaf8 | 2011-07-13 10:32:02 +0900 | [diff] [blame] | 86 | void getWordWithDigraphSuggestionsRec(ProximityInfo *proximityInfo, |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 87 | const int *xcoordinates, const int* ycoordinates, const int *codesBuffer, |
| 88 | const int codesBufferSize, const int flags, const int* codesSrc, const int codesRemain, |
satok | 3c4bb77 | 2011-03-04 22:50:19 -0800 | [diff] [blame] | 89 | const int currentDepth, int* codesDest, unsigned short* outWords, int* frequencies); |
satok | 1d7eaf8 | 2011-07-13 10:32:02 +0900 | [diff] [blame] | 90 | void initSuggestions(ProximityInfo *proximityInfo, const int *xcoordinates, |
| 91 | const int *ycoordinates, const int *codes, const int codesSize, |
| 92 | unsigned short *outWords, int *frequencies); |
satok | 54fe9e0 | 2010-12-13 14:42:35 +0900 | [diff] [blame] | 93 | void getSuggestionCandidates(const int skipPos, const int excessivePos, |
satok | a3d78f6 | 2010-12-09 22:08:33 +0900 | [diff] [blame] | 94 | const int transposedPos, int *nextLetters, const int nextLettersSize, |
| 95 | const int maxDepth); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 96 | bool addWord(unsigned short *word, int length, int frequency); |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 97 | bool getSplitTwoWordsSuggestion(const int inputLength, |
| 98 | const int firstWordStartPos, const int firstWordLength, |
satok | d8db9f8 | 2011-05-18 15:31:04 +0900 | [diff] [blame] | 99 | const int secondWordStartPos, const int secondWordLength, const bool isSpaceProximity); |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 100 | bool getMissingSpaceWords(const int inputLength, const int missingSpacePos); |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 101 | bool getMistypedSpaceWords(const int inputLength, const int spaceProximityPos); |
satok | 58c49b9 | 2011-01-27 03:23:39 +0900 | [diff] [blame] | 102 | 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] | 103 | const int excessivePos, const int transposedPos, const int freq, |
| 104 | const bool sameLength) const; |
Jean Chalard | ca5ef28 | 2011-06-17 15:36:26 +0900 | [diff] [blame] | 105 | void onTerminal(unsigned short int* word, const int depth, |
Jean Chalard | 980d6b6 | 2011-06-30 17:02:23 +0900 | [diff] [blame] | 106 | const uint8_t* const root, const uint8_t flags, const int pos, |
Jean Chalard | ca5ef28 | 2011-06-17 15:36:26 +0900 | [diff] [blame] | 107 | const int inputIndex, const int matchWeight, const int skipPos, |
| 108 | const int excessivePos, const int transposedPos, const int freq, const bool sameLength, |
| 109 | int *nextLetters, const int nextLettersSize); |
satok | 28bd03b | 2010-12-03 16:39:16 +0900 | [diff] [blame] | 110 | bool needsToSkipCurrentNode(const unsigned short c, |
satok | 6831926 | 2010-12-03 19:38:08 +0900 | [diff] [blame] | 111 | const int inputIndex, const int skipPos, const int depth); |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 112 | // Process a node by considering proximity, missing and excessive character |
Jean Chalard | 0584f02 | 2011-06-30 19:23:16 +0900 | [diff] [blame] | 113 | bool processCurrentNode(const int initialPos, const int initialDepth, |
| 114 | const int maxDepth, const bool initialTraverseAllNodes, const int snr, int inputIndex, |
| 115 | const int initialDiffs, const int skipPos, const int excessivePos, |
| 116 | const int transposedPos, int *nextLetters, const int nextLettersSize, int *newCount, |
| 117 | int *newChildPosition, bool *newTraverseAllNodes, int *newSnr, int*newInputIndex, |
| 118 | int *newDiffs, int *nextSiblingPosition, int *nextOutputIndex); |
Jean Chalard | bb15e77 | 2011-06-30 20:14:38 +0900 | [diff] [blame] | 119 | int getMostFrequentWordLike(const int startInputIndex, const int inputLength, |
| 120 | unsigned short *word); |
Jean Chalard | 1059f27 | 2011-06-28 20:45:05 +0900 | [diff] [blame] | 121 | #ifndef NEW_DICTIONARY_FORMAT |
Jean Chalard | ffefdb6 | 2011-06-30 17:15:32 +0900 | [diff] [blame] | 122 | void getWordsRec(const int childrenCount, const int pos, const int depth, const int maxDepth, |
| 123 | const bool traverseAllNodes, const int snr, const int inputIndex, const int diffs, |
| 124 | const int skipPos, const int excessivePos, const int transposedPos, int *nextLetters, |
| 125 | const int nextLettersSize); |
| 126 | // Keep getWordsOld for comparing performance between getWords and getWordsOld |
| 127 | void getWordsOld(const int initialPos, const int inputLength, const int skipPos, |
| 128 | const int excessivePos, const int transposedPos, int *nextLetters, |
| 129 | const int nextLettersSize); |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 130 | // Process a node by considering missing space |
satok | aee09dc | 2010-12-09 19:21:51 +0900 | [diff] [blame] | 131 | bool processCurrentNodeForExactMatch(const int firstChildPos, |
| 132 | const int startInputIndex, const int depth, unsigned short *word, |
| 133 | int *newChildPosition, int *newCount, bool *newTerminal, int *newFreq, int *siblingPos); |
Jean Chalard | 1059f27 | 2011-06-28 20:45:05 +0900 | [diff] [blame] | 134 | #else // NEW_DICTIONARY_FORMAT |
Jean Chalard | 1059f27 | 2011-06-28 20:45:05 +0900 | [diff] [blame] | 135 | int getMostFrequentWordLikeInner(const uint16_t* const inWord, const int length, |
| 136 | short unsigned int* outWord); |
| 137 | #endif // NEW_DICTIONARY_FORMAT |
Jean Chalard | 293ece0 | 2011-06-16 20:55:16 +0900 | [diff] [blame] | 138 | |
| 139 | const uint8_t* const DICT_ROOT; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 140 | const int MAX_WORD_LENGTH; |
Ken Wakasa | e90b333 | 2011-01-07 15:01:51 +0900 | [diff] [blame] | 141 | const int MAX_WORDS; |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 142 | const int MAX_PROXIMITY_CHARS; |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 143 | const bool IS_LATEST_DICT_VERSION; |
satok | 18c28f4 | 2010-12-02 18:11:54 +0900 | [diff] [blame] | 144 | const int TYPED_LETTER_MULTIPLIER; |
| 145 | const int FULL_WORD_MULTIPLIER; |
Ken Wakasa | e90b333 | 2011-01-07 15:01:51 +0900 | [diff] [blame] | 146 | const int ROOT_POS; |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 147 | const unsigned int BYTES_IN_ONE_CHAR; |
satok | 3c4bb77 | 2011-03-04 22:50:19 -0800 | [diff] [blame] | 148 | const int MAX_UMLAUT_SEARCH_DEPTH; |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 149 | |
| 150 | // Flags for special processing |
| 151 | // Those *must* match the flags in BinaryDictionary.Flags.ALL_FLAGS in BinaryDictionary.java |
| 152 | // or something very bad (like, the apocalypse) will happen. |
| 153 | // Please update both at the same time. |
| 154 | enum { |
| 155 | REQUIRES_GERMAN_UMLAUT_PROCESSING = 0x1 |
| 156 | }; |
| 157 | static const struct digraph_t { int first; int second; } GERMAN_UMLAUT_DIGRAPHS[]; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 158 | |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 159 | int *mFrequencies; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 160 | unsigned short *mOutputChars; |
satok | 1d7eaf8 | 2011-07-13 10:32:02 +0900 | [diff] [blame] | 161 | const ProximityInfo *mProximityInfo; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 162 | int mInputLength; |
satok | 715514d | 2010-12-02 20:19:59 +0900 | [diff] [blame] | 163 | // MAX_WORD_LENGTH_INTERNAL must be bigger than MAX_WORD_LENGTH |
| 164 | unsigned short mWord[MAX_WORD_LENGTH_INTERNAL]; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 165 | int mMaxEditDistance; |
satok | d299792 | 2010-12-07 13:08:39 +0900 | [diff] [blame] | 166 | |
| 167 | int mStackChildCount[MAX_WORD_LENGTH_INTERNAL]; |
| 168 | bool mStackTraverseAll[MAX_WORD_LENGTH_INTERNAL]; |
| 169 | int mStackNodeFreq[MAX_WORD_LENGTH_INTERNAL]; |
| 170 | int mStackInputIndex[MAX_WORD_LENGTH_INTERNAL]; |
| 171 | int mStackDiffs[MAX_WORD_LENGTH_INTERNAL]; |
| 172 | int mStackSiblingPos[MAX_WORD_LENGTH_INTERNAL]; |
Jean Chalard | 17e44a7 | 2011-06-16 22:51:11 +0900 | [diff] [blame] | 173 | int mStackOutputIndex[MAX_WORD_LENGTH_INTERNAL]; |
Tadashi G. Takaoka | 887f11e | 2011-02-10 20:53:58 +0900 | [diff] [blame] | 174 | int mNextLettersFrequency[NEXT_LETTERS_SIZE]; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 175 | }; |
Ken Wakasa | ce9e52a | 2011-06-18 13:09:55 +0900 | [diff] [blame] | 176 | } // namespace latinime |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 177 | |
| 178 | #endif // LATINIME_UNIGRAM_DICTIONARY_H |