satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2010, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
satok | 48e432c | 2010-12-06 17:38:58 +0900 | [diff] [blame] | 18 | #include <assert.h> |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 19 | #include <string.h> |
| 20 | |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 21 | #define LOG_TAG "LatinIME: unigram_dictionary.cpp" |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 22 | |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 23 | #include "char_utils.h" |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 24 | #include "dictionary.h" |
| 25 | #include "unigram_dictionary.h" |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 26 | |
Jean Chalard | 1059f27 | 2011-06-28 20:45:05 +0900 | [diff] [blame] | 27 | #include "binary_format.h" |
Jean Chalard | cf9dbbd | 2011-12-26 15:16:59 +0900 | [diff] [blame] | 28 | #include "terminal_attributes.h" |
Jean Chalard | 1059f27 | 2011-06-28 20:45:05 +0900 | [diff] [blame] | 29 | |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 30 | namespace latinime { |
| 31 | |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 32 | const UnigramDictionary::digraph_t UnigramDictionary::GERMAN_UMLAUT_DIGRAPHS[] = |
Jean Chalard | d304338 | 2012-03-21 18:38:25 +0900 | [diff] [blame] | 33 | { { 'a', 'e', 0x00E4 }, // U+00E4 : LATIN SMALL LETTER A WITH DIAERESIS |
| 34 | { 'o', 'e', 0x00F6 }, // U+00F6 : LATIN SMALL LETTER O WITH DIAERESIS |
| 35 | { 'u', 'e', 0x00FC } }; // U+00FC : LATIN SMALL LETTER U WITH DIAERESIS |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 36 | |
Jean Chalard | cc78d03 | 2012-03-23 16:48:49 +0900 | [diff] [blame] | 37 | const UnigramDictionary::digraph_t UnigramDictionary::FRENCH_LIGATURES_DIGRAPHS[] = |
| 38 | { { 'a', 'e', 0x00E6 }, // U+00E6 : LATIN SMALL LETTER AE |
| 39 | { 'o', 'e', 0x0153 } }; // U+0153 : LATIN SMALL LIGATURE OE |
| 40 | |
Jean Chalard | 293ece0 | 2011-06-16 20:55:16 +0900 | [diff] [blame] | 41 | // TODO: check the header |
| 42 | UnigramDictionary::UnigramDictionary(const uint8_t* const streamStart, int typedLetterMultiplier, |
Jean Chalard | cd274b1 | 2012-04-06 18:26:00 +0900 | [diff] [blame] | 43 | int fullWordMultiplier, int maxWordLength, int maxWords, const unsigned int flags) |
Jean Chalard | 46a1eec | 2012-02-27 19:48:47 +0900 | [diff] [blame] | 44 | : DICT_ROOT(streamStart), MAX_WORD_LENGTH(maxWordLength), MAX_WORDS(maxWords), |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 45 | TYPED_LETTER_MULTIPLIER(typedLetterMultiplier), FULL_WORD_MULTIPLIER(fullWordMultiplier), |
Jean Chalard | 1059f27 | 2011-06-28 20:45:05 +0900 | [diff] [blame] | 46 | // TODO : remove this variable. |
| 47 | ROOT_POS(0), |
satok | 9df4a45 | 2012-03-23 16:05:18 +0900 | [diff] [blame] | 48 | BYTES_IN_ONE_CHAR(sizeof(int)), |
Jean Chalard | cd274b1 | 2012-04-06 18:26:00 +0900 | [diff] [blame] | 49 | MAX_DIGRAPH_SEARCH_DEPTH(DEFAULT_MAX_DIGRAPH_SEARCH_DEPTH), FLAGS(flags) { |
Ken Wakasa | de3070a | 2011-03-19 09:16:42 +0900 | [diff] [blame] | 50 | if (DEBUG_DICT) { |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 51 | AKLOGI("UnigramDictionary - constructor"); |
Ken Wakasa | de3070a | 2011-03-19 09:16:42 +0900 | [diff] [blame] | 52 | } |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 53 | } |
| 54 | |
satok | 2df3060 | 2011-07-15 13:49:00 +0900 | [diff] [blame] | 55 | UnigramDictionary::~UnigramDictionary() { |
satok | 2df3060 | 2011-07-15 13:49:00 +0900 | [diff] [blame] | 56 | } |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 57 | |
satok | 9df4a45 | 2012-03-23 16:05:18 +0900 | [diff] [blame] | 58 | static inline unsigned int getCodesBufferSize(const int *codes, const int codesSize) { |
| 59 | return sizeof(*codes) * codesSize; |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 60 | } |
| 61 | |
Ken Wakasa | 951ab9d | 2012-03-09 19:18:59 +0900 | [diff] [blame] | 62 | // TODO: This needs to take a const unsigned short* and not tinker with its contents |
satok | 1147c7b | 2011-12-14 15:04:58 +0900 | [diff] [blame] | 63 | static inline void addWord( |
| 64 | unsigned short *word, int length, int frequency, WordsPriorityQueue *queue) { |
| 65 | queue->push(frequency, word, length); |
| 66 | } |
| 67 | |
Jean Chalard | d304338 | 2012-03-21 18:38:25 +0900 | [diff] [blame] | 68 | // Return the replacement code point for a digraph, or 0 if none. |
| 69 | int UnigramDictionary::getDigraphReplacement(const int *codes, const int i, const int codesSize, |
Jean Chalard | 6c30061 | 2012-03-06 19:54:03 +0900 | [diff] [blame] | 70 | const digraph_t* const digraphs, const unsigned int digraphsSize) const { |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 71 | |
| 72 | // There can't be a digraph if we don't have at least 2 characters to examine |
| 73 | if (i + 2 > codesSize) return false; |
| 74 | |
| 75 | // Search for the first char of some digraph |
| 76 | int lastDigraphIndex = -1; |
satok | 9df4a45 | 2012-03-23 16:05:18 +0900 | [diff] [blame] | 77 | const int thisChar = codes[i]; |
Jean Chalard | 6c30061 | 2012-03-06 19:54:03 +0900 | [diff] [blame] | 78 | for (lastDigraphIndex = digraphsSize - 1; lastDigraphIndex >= 0; --lastDigraphIndex) { |
| 79 | if (thisChar == digraphs[lastDigraphIndex].first) break; |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 80 | } |
| 81 | // No match: return early |
Jean Chalard | d304338 | 2012-03-21 18:38:25 +0900 | [diff] [blame] | 82 | if (lastDigraphIndex < 0) return 0; |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 83 | |
| 84 | // It's an interesting digraph if the second char matches too. |
satok | 9df4a45 | 2012-03-23 16:05:18 +0900 | [diff] [blame] | 85 | if (digraphs[lastDigraphIndex].second == codes[i + 1]) { |
Jean Chalard | d304338 | 2012-03-21 18:38:25 +0900 | [diff] [blame] | 86 | return digraphs[lastDigraphIndex].replacement; |
| 87 | } else { |
| 88 | return 0; |
| 89 | } |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | // Mostly the same arguments as the non-recursive version, except: |
| 93 | // codes is the original value. It points to the start of the work buffer, and gets passed as is. |
| 94 | // codesSize is the size of the user input (thus, it is the size of codesSrc). |
| 95 | // codesDest is the current point in the work buffer. |
| 96 | // codesSrc is the current point in the user-input, original, content-unmodified buffer. |
| 97 | // codesRemain is the remaining size in codesSrc. |
satok | 1d7eaf8 | 2011-07-13 10:32:02 +0900 | [diff] [blame] | 98 | void UnigramDictionary::getWordWithDigraphSuggestionsRec(ProximityInfo *proximityInfo, |
satok | 1147c7b | 2011-12-14 15:04:58 +0900 | [diff] [blame] | 99 | const int *xcoordinates, const int *ycoordinates, const int *codesBuffer, |
satok | 219a514 | 2012-03-08 11:53:18 +0900 | [diff] [blame] | 100 | int *xCoordinatesBuffer, int *yCoordinatesBuffer, |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame^] | 101 | const int codesBufferSize, const std::map<int, int> *bigramMap, const uint8_t *bigramFilter, |
Jean Chalard | 4d9b202 | 2012-04-23 19:25:28 +0900 | [diff] [blame] | 102 | const bool useFullEditDistance, const int *codesSrc, |
satok | 1147c7b | 2011-12-14 15:04:58 +0900 | [diff] [blame] | 103 | const int codesRemain, const int currentDepth, int *codesDest, Correction *correction, |
Jean Chalard | 6c30061 | 2012-03-06 19:54:03 +0900 | [diff] [blame] | 104 | WordsPriorityQueuePool *queuePool, |
| 105 | const digraph_t* const digraphs, const unsigned int digraphsSize) { |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 106 | |
satok | 9df4a45 | 2012-03-23 16:05:18 +0900 | [diff] [blame] | 107 | const int startIndex = codesDest - codesBuffer; |
Jean Chalard | 6c30061 | 2012-03-06 19:54:03 +0900 | [diff] [blame] | 108 | if (currentDepth < MAX_DIGRAPH_SEARCH_DEPTH) { |
Jean Chalard | a787dba | 2011-03-04 12:17:48 +0900 | [diff] [blame] | 109 | for (int i = 0; i < codesRemain; ++i) { |
satok | 219a514 | 2012-03-08 11:53:18 +0900 | [diff] [blame] | 110 | xCoordinatesBuffer[startIndex + i] = xcoordinates[codesBufferSize - codesRemain + i]; |
| 111 | yCoordinatesBuffer[startIndex + i] = ycoordinates[codesBufferSize - codesRemain + i]; |
Jean Chalard | d304338 | 2012-03-21 18:38:25 +0900 | [diff] [blame] | 112 | const int replacementCodePoint = |
| 113 | getDigraphReplacement(codesSrc, i, codesRemain, digraphs, digraphsSize); |
| 114 | if (0 != replacementCodePoint) { |
Jean Chalard | a787dba | 2011-03-04 12:17:48 +0900 | [diff] [blame] | 115 | // Found a digraph. We will try both spellings. eg. the word is "pruefen" |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 116 | |
Jean Chalard | d304338 | 2012-03-21 18:38:25 +0900 | [diff] [blame] | 117 | // Copy the word up to the first char of the digraph, including proximity chars, |
| 118 | // and overwrite the primary code with the replacement code point. Then, continue |
| 119 | // processing on the remaining part of the word, skipping the second char of the |
| 120 | // digraph. |
| 121 | // In our example, copy "pru", replace "u" with the version with the diaeresis and |
| 122 | // continue running on "fen". |
Jean Chalard | a787dba | 2011-03-04 12:17:48 +0900 | [diff] [blame] | 123 | // Make i the index of the second char of the digraph for simplicity. Forgetting |
| 124 | // to do that results in an infinite recursion so take care! |
| 125 | ++i; |
| 126 | memcpy(codesDest, codesSrc, i * BYTES_IN_ONE_CHAR); |
Jean Chalard | d304338 | 2012-03-21 18:38:25 +0900 | [diff] [blame] | 127 | codesDest[(i - 1) * (BYTES_IN_ONE_CHAR / sizeof(codesDest[0]))] = |
| 128 | replacementCodePoint; |
Jean Chalard | a787dba | 2011-03-04 12:17:48 +0900 | [diff] [blame] | 129 | getWordWithDigraphSuggestionsRec(proximityInfo, xcoordinates, ycoordinates, |
Jean Chalard | 338d3ec | 2012-04-06 19:28:34 +0900 | [diff] [blame] | 130 | codesBuffer, xCoordinatesBuffer, yCoordinatesBuffer, codesBufferSize, |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame^] | 131 | bigramMap, bigramFilter, useFullEditDistance, codesSrc + i + 1, |
Jean Chalard | 4d9b202 | 2012-04-23 19:25:28 +0900 | [diff] [blame] | 132 | codesRemain - i - 1, currentDepth + 1, codesDest + i, correction, |
Jean Chalard | 6c30061 | 2012-03-06 19:54:03 +0900 | [diff] [blame] | 133 | queuePool, digraphs, digraphsSize); |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 134 | |
Jean Chalard | a787dba | 2011-03-04 12:17:48 +0900 | [diff] [blame] | 135 | // Copy the second char of the digraph in place, then continue processing on |
| 136 | // the remaining part of the word. |
| 137 | // In our example, after "pru" in the buffer copy the "e", and continue on "fen" |
satok | 9df4a45 | 2012-03-23 16:05:18 +0900 | [diff] [blame] | 138 | memcpy(codesDest + i, codesSrc + i, BYTES_IN_ONE_CHAR); |
Jean Chalard | a787dba | 2011-03-04 12:17:48 +0900 | [diff] [blame] | 139 | getWordWithDigraphSuggestionsRec(proximityInfo, xcoordinates, ycoordinates, |
Jean Chalard | 338d3ec | 2012-04-06 19:28:34 +0900 | [diff] [blame] | 140 | codesBuffer, xCoordinatesBuffer, yCoordinatesBuffer, codesBufferSize, |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame^] | 141 | bigramMap, bigramFilter, useFullEditDistance, codesSrc + i, codesRemain - i, |
Jean Chalard | 4d9b202 | 2012-04-23 19:25:28 +0900 | [diff] [blame] | 142 | currentDepth + 1, codesDest + i, correction, queuePool, digraphs, |
| 143 | digraphsSize); |
Jean Chalard | a787dba | 2011-03-04 12:17:48 +0900 | [diff] [blame] | 144 | return; |
| 145 | } |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | // If we come here, we hit the end of the word: let's check it against the dictionary. |
| 150 | // In our example, we'll come here once for "prufen" and then once for "pruefen". |
| 151 | // If the word contains several digraphs, we'll come it for the product of them. |
| 152 | // eg. if the word is "ueberpruefen" we'll test, in order, against |
| 153 | // "uberprufen", "uberpruefen", "ueberprufen", "ueberpruefen". |
| 154 | const unsigned int remainingBytes = BYTES_IN_ONE_CHAR * codesRemain; |
satok | 219a514 | 2012-03-08 11:53:18 +0900 | [diff] [blame] | 155 | if (0 != remainingBytes) { |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 156 | memcpy(codesDest, codesSrc, remainingBytes); |
satok | 219a514 | 2012-03-08 11:53:18 +0900 | [diff] [blame] | 157 | memcpy(&xCoordinatesBuffer[startIndex], &xcoordinates[codesBufferSize - codesRemain], |
| 158 | sizeof(int) * codesRemain); |
| 159 | memcpy(&yCoordinatesBuffer[startIndex], &ycoordinates[codesBufferSize - codesRemain], |
| 160 | sizeof(int) * codesRemain); |
| 161 | } |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 162 | |
satok | 219a514 | 2012-03-08 11:53:18 +0900 | [diff] [blame] | 163 | getWordSuggestions(proximityInfo, xCoordinatesBuffer, yCoordinatesBuffer, codesBuffer, |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame^] | 164 | startIndex + codesRemain, bigramMap, bigramFilter, useFullEditDistance, correction, |
satok | a7e5a5a | 2011-12-15 16:49:12 +0900 | [diff] [blame] | 165 | queuePool); |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 166 | } |
| 167 | |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame^] | 168 | // bigramMap contains the association <bigram address> -> <bigram frequency> |
| 169 | // bigramFilter is a bloom filter for fast rejection: see functions setInFilter and isInFilter |
| 170 | // in bigram_dictionary.cpp |
satok | a7e5a5a | 2011-12-15 16:49:12 +0900 | [diff] [blame] | 171 | int UnigramDictionary::getSuggestions(ProximityInfo *proximityInfo, |
| 172 | WordsPriorityQueuePool *queuePool, Correction *correction, const int *xcoordinates, |
Jean Chalard | 338d3ec | 2012-04-06 19:28:34 +0900 | [diff] [blame] | 173 | const int *ycoordinates, const int *codes, const int codesSize, |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame^] | 174 | const std::map<int, int> *bigramMap, const uint8_t *bigramFilter, |
| 175 | const bool useFullEditDistance, unsigned short *outWords, int *frequencies) { |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 176 | |
satok | 219a514 | 2012-03-08 11:53:18 +0900 | [diff] [blame] | 177 | queuePool->clearAll(); |
satok | 1147c7b | 2011-12-14 15:04:58 +0900 | [diff] [blame] | 178 | Correction* masterCorrection = correction; |
Jean Chalard | aa8df59 | 2012-04-06 18:54:20 +0900 | [diff] [blame] | 179 | if (BinaryFormat::REQUIRES_GERMAN_UMLAUT_PROCESSING & FLAGS) |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 180 | { // Incrementally tune the word and try all possibilities |
satok | 9df4a45 | 2012-03-23 16:05:18 +0900 | [diff] [blame] | 181 | int codesBuffer[getCodesBufferSize(codes, codesSize)]; |
satok | 219a514 | 2012-03-08 11:53:18 +0900 | [diff] [blame] | 182 | int xCoordinatesBuffer[codesSize]; |
| 183 | int yCoordinatesBuffer[codesSize]; |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 184 | getWordWithDigraphSuggestionsRec(proximityInfo, xcoordinates, ycoordinates, codesBuffer, |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame^] | 185 | xCoordinatesBuffer, yCoordinatesBuffer, codesSize, bigramMap, bigramFilter, |
Jean Chalard | 4d9b202 | 2012-04-23 19:25:28 +0900 | [diff] [blame] | 186 | useFullEditDistance, codes, codesSize, 0, codesBuffer, masterCorrection, |
Jean Chalard | 338d3ec | 2012-04-06 19:28:34 +0900 | [diff] [blame] | 187 | queuePool, GERMAN_UMLAUT_DIGRAPHS, |
Jean Chalard | 6c30061 | 2012-03-06 19:54:03 +0900 | [diff] [blame] | 188 | sizeof(GERMAN_UMLAUT_DIGRAPHS) / sizeof(GERMAN_UMLAUT_DIGRAPHS[0])); |
Jean Chalard | aa8df59 | 2012-04-06 18:54:20 +0900 | [diff] [blame] | 189 | } else if (BinaryFormat::REQUIRES_FRENCH_LIGATURES_PROCESSING & FLAGS) { |
satok | acb6c54 | 2012-03-23 20:58:18 +0900 | [diff] [blame] | 190 | int codesBuffer[getCodesBufferSize(codes, codesSize)]; |
Jean Chalard | cc78d03 | 2012-03-23 16:48:49 +0900 | [diff] [blame] | 191 | int xCoordinatesBuffer[codesSize]; |
| 192 | int yCoordinatesBuffer[codesSize]; |
| 193 | getWordWithDigraphSuggestionsRec(proximityInfo, xcoordinates, ycoordinates, codesBuffer, |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame^] | 194 | xCoordinatesBuffer, yCoordinatesBuffer, codesSize, bigramMap, bigramFilter, |
Jean Chalard | 4d9b202 | 2012-04-23 19:25:28 +0900 | [diff] [blame] | 195 | useFullEditDistance, codes, codesSize, 0, codesBuffer, masterCorrection, |
Jean Chalard | 338d3ec | 2012-04-06 19:28:34 +0900 | [diff] [blame] | 196 | queuePool, FRENCH_LIGATURES_DIGRAPHS, |
Jean Chalard | cc78d03 | 2012-03-23 16:48:49 +0900 | [diff] [blame] | 197 | sizeof(FRENCH_LIGATURES_DIGRAPHS) / sizeof(FRENCH_LIGATURES_DIGRAPHS[0])); |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 198 | } else { // Normal processing |
Jean Chalard | 338d3ec | 2012-04-06 19:28:34 +0900 | [diff] [blame] | 199 | getWordSuggestions(proximityInfo, xcoordinates, ycoordinates, codes, codesSize, |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame^] | 200 | bigramMap, bigramFilter, useFullEditDistance, masterCorrection, queuePool); |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 201 | } |
| 202 | |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 203 | PROF_START(20); |
satok | 8330b48 | 2012-01-23 16:52:37 +0900 | [diff] [blame] | 204 | if (DEBUG_DICT) { |
| 205 | double ns = queuePool->getMasterQueue()->getHighestNormalizedScore( |
| 206 | proximityInfo->getPrimaryInputWord(), codesSize, 0, 0, 0); |
| 207 | ns += 0; |
| 208 | AKLOGI("Max normalized score = %f", ns); |
| 209 | } |
satok | a7e5a5a | 2011-12-15 16:49:12 +0900 | [diff] [blame] | 210 | const int suggestedWordsCount = |
| 211 | queuePool->getMasterQueue()->outputSuggestions(frequencies, outWords); |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 212 | |
| 213 | if (DEBUG_DICT) { |
satok | 8330b48 | 2012-01-23 16:52:37 +0900 | [diff] [blame] | 214 | double ns = queuePool->getMasterQueue()->getHighestNormalizedScore( |
| 215 | proximityInfo->getPrimaryInputWord(), codesSize, 0, 0, 0); |
| 216 | ns += 0; |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 217 | AKLOGI("Returning %d words", suggestedWordsCount); |
Jean Chalard | 980d6b6 | 2011-06-30 17:02:23 +0900 | [diff] [blame] | 218 | /// Print the returned words |
| 219 | for (int j = 0; j < suggestedWordsCount; ++j) { |
satok | 16379df | 2011-12-12 20:53:22 +0900 | [diff] [blame] | 220 | short unsigned int* w = outWords + j * MAX_WORD_LENGTH; |
Jean Chalard | 980d6b6 | 2011-06-30 17:02:23 +0900 | [diff] [blame] | 221 | char s[MAX_WORD_LENGTH]; |
| 222 | for (int i = 0; i <= MAX_WORD_LENGTH; i++) s[i] = w[i]; |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 223 | AKLOGI("%s %i", s, frequencies[j]); |
Jean Chalard | 980d6b6 | 2011-06-30 17:02:23 +0900 | [diff] [blame] | 224 | } |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 225 | } |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 226 | PROF_END(20); |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 227 | PROF_CLOSE; |
| 228 | return suggestedWordsCount; |
| 229 | } |
| 230 | |
satok | 1d7eaf8 | 2011-07-13 10:32:02 +0900 | [diff] [blame] | 231 | void UnigramDictionary::getWordSuggestions(ProximityInfo *proximityInfo, |
satok | 1147c7b | 2011-12-14 15:04:58 +0900 | [diff] [blame] | 232 | const int *xcoordinates, const int *ycoordinates, const int *codes, |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame^] | 233 | const int inputLength, const std::map<int, int> *bigramMap, const uint8_t *bigramFilter, |
| 234 | const bool useFullEditDistance, Correction *correction, WordsPriorityQueuePool *queuePool) { |
Jean Chalard | c2bbc6a | 2011-02-25 17:56:53 +0900 | [diff] [blame] | 235 | |
satok | 61e2f85 | 2011-01-05 14:13:07 +0900 | [diff] [blame] | 236 | PROF_OPEN; |
| 237 | PROF_START(0); |
satok | 61e2f85 | 2011-01-05 14:13:07 +0900 | [diff] [blame] | 238 | PROF_END(0); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 239 | |
satok | 61e2f85 | 2011-01-05 14:13:07 +0900 | [diff] [blame] | 240 | PROF_START(1); |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame^] | 241 | getOneWordSuggestions(proximityInfo, xcoordinates, ycoordinates, codes, bigramMap, bigramFilter, |
Jean Chalard | 4d9b202 | 2012-04-23 19:25:28 +0900 | [diff] [blame] | 242 | useFullEditDistance, inputLength, correction, queuePool); |
satok | 61e2f85 | 2011-01-05 14:13:07 +0900 | [diff] [blame] | 243 | PROF_END(1); |
| 244 | |
| 245 | PROF_START(2); |
satok | 10266c0 | 2011-08-19 22:05:59 +0900 | [diff] [blame] | 246 | // Note: This line is intentionally left blank |
satok | 61e2f85 | 2011-01-05 14:13:07 +0900 | [diff] [blame] | 247 | PROF_END(2); |
satok | cdbbea7 | 2010-12-08 16:04:16 +0900 | [diff] [blame] | 248 | |
satok | 61e2f85 | 2011-01-05 14:13:07 +0900 | [diff] [blame] | 249 | PROF_START(3); |
satok | 10266c0 | 2011-08-19 22:05:59 +0900 | [diff] [blame] | 250 | // Note: This line is intentionally left blank |
satok | 61e2f85 | 2011-01-05 14:13:07 +0900 | [diff] [blame] | 251 | PROF_END(3); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 252 | |
satok | 61e2f85 | 2011-01-05 14:13:07 +0900 | [diff] [blame] | 253 | PROF_START(4); |
satok | 8330b48 | 2012-01-23 16:52:37 +0900 | [diff] [blame] | 254 | bool hasAutoCorrectionCandidate = false; |
| 255 | WordsPriorityQueue* masterQueue = queuePool->getMasterQueue(); |
| 256 | if (masterQueue->size() > 0) { |
| 257 | double nsForMaster = masterQueue->getHighestNormalizedScore( |
| 258 | proximityInfo->getPrimaryInputWord(), inputLength, 0, 0, 0); |
| 259 | hasAutoCorrectionCandidate = (nsForMaster > START_TWO_WORDS_CORRECTION_THRESHOLD); |
| 260 | } |
satok | 61e2f85 | 2011-01-05 14:13:07 +0900 | [diff] [blame] | 261 | PROF_END(4); |
satok | a3d78f6 | 2010-12-09 22:08:33 +0900 | [diff] [blame] | 262 | |
satok | 61e2f85 | 2011-01-05 14:13:07 +0900 | [diff] [blame] | 263 | PROF_START(5); |
satok | 9955716 | 2012-01-26 22:49:13 +0900 | [diff] [blame] | 264 | // Multiple word suggestions |
| 265 | if (SUGGEST_MULTIPLE_WORDS |
| 266 | && inputLength >= MIN_USER_TYPED_LENGTH_FOR_MULTIPLE_WORD_SUGGESTION) { |
satok | a85f492 | 2012-01-30 18:18:30 +0900 | [diff] [blame] | 267 | getSplitMultipleWordsSuggestions(proximityInfo, xcoordinates, ycoordinates, codes, |
satok | 1f6b52e | 2012-01-30 13:53:58 +0900 | [diff] [blame] | 268 | useFullEditDistance, inputLength, correction, queuePool, |
| 269 | hasAutoCorrectionCandidate); |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 270 | } |
satok | 61e2f85 | 2011-01-05 14:13:07 +0900 | [diff] [blame] | 271 | PROF_END(5); |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 272 | |
| 273 | PROF_START(6); |
satok | 9955716 | 2012-01-26 22:49:13 +0900 | [diff] [blame] | 274 | // Note: This line is intentionally left blank |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 275 | PROF_END(6); |
satok | 9955716 | 2012-01-26 22:49:13 +0900 | [diff] [blame] | 276 | |
satok | 29dc806 | 2012-01-17 15:59:15 +0900 | [diff] [blame] | 277 | if (DEBUG_DICT) { |
satok | 6ad15fc | 2012-01-16 16:21:21 +0900 | [diff] [blame] | 278 | queuePool->dumpSubQueue1TopSuggestions(); |
satok | 29dc806 | 2012-01-17 15:59:15 +0900 | [diff] [blame] | 279 | for (int i = 0; i < SUB_QUEUE_MAX_COUNT; ++i) { |
satok | 7409d15 | 2012-01-26 16:13:25 +0900 | [diff] [blame] | 280 | WordsPriorityQueue* queue = queuePool->getSubQueue(FIRST_WORD_INDEX, i); |
satok | 29dc806 | 2012-01-17 15:59:15 +0900 | [diff] [blame] | 281 | if (queue->size() > 0) { |
| 282 | WordsPriorityQueue::SuggestedWord* sw = queue->top(); |
| 283 | const int score = sw->mScore; |
| 284 | const unsigned short* word = sw->mWord; |
| 285 | const int wordLength = sw->mWordLength; |
| 286 | double ns = Correction::RankingAlgorithm::calcNormalizedScore( |
| 287 | proximityInfo->getPrimaryInputWord(), i, word, wordLength, score); |
| 288 | ns += 0; |
| 289 | AKLOGI("--- TOP SUB WORDS for %d --- %d %f [%d]", i, score, ns, |
satok | 54af64a | 2012-01-17 15:58:23 +0900 | [diff] [blame] | 290 | (ns > TWO_WORDS_CORRECTION_WITH_OTHER_ERROR_THRESHOLD)); |
satok | 29dc806 | 2012-01-17 15:59:15 +0900 | [diff] [blame] | 291 | DUMP_WORD(proximityInfo->getPrimaryInputWord(), i); |
| 292 | DUMP_WORD(word, wordLength); |
| 293 | } |
| 294 | } |
satok | 6ad15fc | 2012-01-16 16:21:21 +0900 | [diff] [blame] | 295 | } |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 296 | } |
| 297 | |
Yusuke Nojima | 258bfe6 | 2011-09-28 12:59:43 +0900 | [diff] [blame] | 298 | void UnigramDictionary::initSuggestions(ProximityInfo *proximityInfo, const int *xCoordinates, |
satok | 6ad15fc | 2012-01-16 16:21:21 +0900 | [diff] [blame] | 299 | const int *yCoordinates, const int *codes, const int inputLength, Correction *correction) { |
Ken Wakasa | de3070a | 2011-03-19 09:16:42 +0900 | [diff] [blame] | 300 | if (DEBUG_DICT) { |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 301 | AKLOGI("initSuggest"); |
Ken Wakasa | de3070a | 2011-03-19 09:16:42 +0900 | [diff] [blame] | 302 | } |
satok | 1a6da63 | 2011-12-16 23:15:06 +0900 | [diff] [blame] | 303 | proximityInfo->setInputParams(codes, inputLength, xCoordinates, yCoordinates); |
satok | 1a6da63 | 2011-12-16 23:15:06 +0900 | [diff] [blame] | 304 | const int maxDepth = min(inputLength * MAX_DEPTH_MULTIPLIER, MAX_WORD_LENGTH); |
| 305 | correction->initCorrection(proximityInfo, inputLength, maxDepth); |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 306 | } |
| 307 | |
satok | 715514d | 2010-12-02 20:19:59 +0900 | [diff] [blame] | 308 | static const char QUOTE = '\''; |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 309 | static const char SPACE = ' '; |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 310 | |
satok | 744dab6 | 2011-12-15 22:29:05 +0900 | [diff] [blame] | 311 | void UnigramDictionary::getOneWordSuggestions(ProximityInfo *proximityInfo, |
| 312 | const int *xcoordinates, const int *ycoordinates, const int *codes, |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame^] | 313 | const std::map<int, int> *bigramMap, const uint8_t *bigramFilter, |
| 314 | const bool useFullEditDistance, const int inputLength, |
Jean Chalard | 4d9b202 | 2012-04-23 19:25:28 +0900 | [diff] [blame] | 315 | Correction *correction, WordsPriorityQueuePool *queuePool) { |
satok | 6ad15fc | 2012-01-16 16:21:21 +0900 | [diff] [blame] | 316 | initSuggestions(proximityInfo, xcoordinates, ycoordinates, codes, inputLength, correction); |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame^] | 317 | getSuggestionCandidates(useFullEditDistance, inputLength, bigramMap, bigramFilter, correction, |
Jean Chalard | 4d9b202 | 2012-04-23 19:25:28 +0900 | [diff] [blame] | 318 | queuePool, true /* doAutoCompletion */, DEFAULT_MAX_ERRORS, FIRST_WORD_INDEX); |
satok | 744dab6 | 2011-12-15 22:29:05 +0900 | [diff] [blame] | 319 | } |
| 320 | |
satok | 1147c7b | 2011-12-14 15:04:58 +0900 | [diff] [blame] | 321 | void UnigramDictionary::getSuggestionCandidates(const bool useFullEditDistance, |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame^] | 322 | const int inputLength, const std::map<int, int> *bigramMap, const uint8_t *bigramFilter, |
Jean Chalard | 4d9b202 | 2012-04-23 19:25:28 +0900 | [diff] [blame] | 323 | Correction *correction, WordsPriorityQueuePool *queuePool, |
satok | 8330b48 | 2012-01-23 16:52:37 +0900 | [diff] [blame] | 324 | const bool doAutoCompletion, const int maxErrors, const int currentWordIndex) { |
satok | 10266c0 | 2011-08-19 22:05:59 +0900 | [diff] [blame] | 325 | // TODO: Remove setCorrectionParams |
satok | 1147c7b | 2011-12-14 15:04:58 +0900 | [diff] [blame] | 326 | correction->setCorrectionParams(0, 0, 0, |
satok | d03317c | 2011-12-14 21:38:11 +0900 | [diff] [blame] | 327 | -1 /* spaceProximityPos */, -1 /* missingSpacePos */, useFullEditDistance, |
satok | 1a6da63 | 2011-12-16 23:15:06 +0900 | [diff] [blame] | 328 | doAutoCompletion, maxErrors); |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 329 | int rootPosition = ROOT_POS; |
Jean Chalard | 980d6b6 | 2011-06-30 17:02:23 +0900 | [diff] [blame] | 330 | // Get the number of children of root, then increment the position |
Jean Chalard | 6d41981 | 2012-01-16 15:19:47 +0900 | [diff] [blame] | 331 | int childCount = BinaryFormat::getGroupCountAndForwardPointer(DICT_ROOT, &rootPosition); |
satok | 208268d | 2011-08-10 15:44:08 +0900 | [diff] [blame] | 332 | int outputIndex = 0; |
satok | d299792 | 2010-12-07 13:08:39 +0900 | [diff] [blame] | 333 | |
satok | 1147c7b | 2011-12-14 15:04:58 +0900 | [diff] [blame] | 334 | correction->initCorrectionState(rootPosition, childCount, (inputLength <= 0)); |
satok | d299792 | 2010-12-07 13:08:39 +0900 | [diff] [blame] | 335 | |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 336 | // Depth first search |
satok | 208268d | 2011-08-10 15:44:08 +0900 | [diff] [blame] | 337 | while (outputIndex >= 0) { |
satok | 1147c7b | 2011-12-14 15:04:58 +0900 | [diff] [blame] | 338 | if (correction->initProcessState(outputIndex)) { |
| 339 | int siblingPos = correction->getTreeSiblingPos(outputIndex); |
satok | d299792 | 2010-12-07 13:08:39 +0900 | [diff] [blame] | 340 | int firstChildPos; |
satok | 0f6c8e8 | 2011-08-03 02:19:44 +0900 | [diff] [blame] | 341 | |
satok | 4e4e74e | 2011-08-03 23:27:32 +0900 | [diff] [blame] | 342 | const bool needsToTraverseChildrenNodes = processCurrentNode(siblingPos, |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame^] | 343 | bigramMap, bigramFilter, correction, &childCount, &firstChildPos, &siblingPos, |
Jean Chalard | 4d9b202 | 2012-04-23 19:25:28 +0900 | [diff] [blame] | 344 | queuePool, currentWordIndex); |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 345 | // Update next sibling pos |
satok | 1147c7b | 2011-12-14 15:04:58 +0900 | [diff] [blame] | 346 | correction->setTreeSiblingPos(outputIndex, siblingPos); |
satok | 208268d | 2011-08-10 15:44:08 +0900 | [diff] [blame] | 347 | |
satok | d299792 | 2010-12-07 13:08:39 +0900 | [diff] [blame] | 348 | if (needsToTraverseChildrenNodes) { |
| 349 | // Goes to child node |
satok | 1147c7b | 2011-12-14 15:04:58 +0900 | [diff] [blame] | 350 | outputIndex = correction->goDownTree(outputIndex, childCount, firstChildPos); |
satok | d299792 | 2010-12-07 13:08:39 +0900 | [diff] [blame] | 351 | } |
| 352 | } else { |
satok | cdbbea7 | 2010-12-08 16:04:16 +0900 | [diff] [blame] | 353 | // Goes to parent sibling node |
satok | 1147c7b | 2011-12-14 15:04:58 +0900 | [diff] [blame] | 354 | outputIndex = correction->getTreeParentIndex(outputIndex); |
satok | d299792 | 2010-12-07 13:08:39 +0900 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
Jean Chalard | 171d180 | 2012-04-23 18:51:00 +0900 | [diff] [blame] | 359 | inline void UnigramDictionary::onTerminal(const int probability, |
Jean Chalard | cf9dbbd | 2011-12-26 15:16:59 +0900 | [diff] [blame] | 360 | const TerminalAttributes& terminalAttributes, Correction *correction, |
satok | 8330b48 | 2012-01-23 16:52:37 +0900 | [diff] [blame] | 361 | WordsPriorityQueuePool *queuePool, const bool addToMasterQueue, |
| 362 | const int currentWordIndex) { |
satok | 6ad15fc | 2012-01-16 16:21:21 +0900 | [diff] [blame] | 363 | const int inputIndex = correction->getInputIndex(); |
| 364 | const bool addToSubQueue = inputIndex < SUB_QUEUE_MAX_COUNT; |
satok | 54af64a | 2012-01-17 15:58:23 +0900 | [diff] [blame] | 365 | |
satok | 8876b75 | 2011-08-04 18:31:57 +0900 | [diff] [blame] | 366 | int wordLength; |
| 367 | unsigned short* wordPointer; |
satok | 54af64a | 2012-01-17 15:58:23 +0900 | [diff] [blame] | 368 | |
satok | 1f6b52e | 2012-01-30 13:53:58 +0900 | [diff] [blame] | 369 | if ((currentWordIndex == FIRST_WORD_INDEX) && addToMasterQueue) { |
satok | 54af64a | 2012-01-17 15:58:23 +0900 | [diff] [blame] | 370 | WordsPriorityQueue *masterQueue = queuePool->getMasterQueue(); |
Jean Chalard | 171d180 | 2012-04-23 18:51:00 +0900 | [diff] [blame] | 371 | const int finalProbability = |
| 372 | correction->getFinalProbability(probability, &wordPointer, &wordLength); |
| 373 | if (finalProbability != NOT_A_PROBABILITY) { |
| 374 | addWord(wordPointer, wordLength, finalProbability, masterQueue); |
satok | 54af64a | 2012-01-17 15:58:23 +0900 | [diff] [blame] | 375 | |
Jean Chalard | 171d180 | 2012-04-23 18:51:00 +0900 | [diff] [blame] | 376 | const int shortcutProbability = finalProbability > 0 ? finalProbability - 1 : 0; |
satok | 54af64a | 2012-01-17 15:58:23 +0900 | [diff] [blame] | 377 | // Please note that the shortcut candidates will be added to the master queue only. |
| 378 | TerminalAttributes::ShortcutIterator iterator = |
| 379 | terminalAttributes.getShortcutIterator(); |
| 380 | while (iterator.hasNextShortcutTarget()) { |
| 381 | // TODO: addWord only supports weak ordering, meaning we have no means |
| 382 | // to control the order of the shortcuts relative to one another or to the word. |
Jean Chalard | 171d180 | 2012-04-23 18:51:00 +0900 | [diff] [blame] | 383 | // We need to either modulate the probability of each shortcut according |
| 384 | // to its own shortcut probability or to make the queue |
satok | 54af64a | 2012-01-17 15:58:23 +0900 | [diff] [blame] | 385 | // so that the insert order is protected inside the queue for words |
Jean Chalard | 9a933a7 | 2012-03-27 19:56:23 +0900 | [diff] [blame] | 386 | // with the same score. For the moment we use -1 to make sure the shortcut will |
| 387 | // never be in front of the word. |
satok | 54af64a | 2012-01-17 15:58:23 +0900 | [diff] [blame] | 388 | uint16_t shortcutTarget[MAX_WORD_LENGTH_INTERNAL]; |
| 389 | const int shortcutTargetStringLength = iterator.getNextShortcutTarget( |
| 390 | MAX_WORD_LENGTH_INTERNAL, shortcutTarget); |
Jean Chalard | 171d180 | 2012-04-23 18:51:00 +0900 | [diff] [blame] | 391 | addWord(shortcutTarget, shortcutTargetStringLength, shortcutProbability, |
| 392 | masterQueue); |
satok | 6ad15fc | 2012-01-16 16:21:21 +0900 | [diff] [blame] | 393 | } |
Jean Chalard | cf9dbbd | 2011-12-26 15:16:59 +0900 | [diff] [blame] | 394 | } |
satok | 54af64a | 2012-01-17 15:58:23 +0900 | [diff] [blame] | 395 | } |
satok | 6ad15fc | 2012-01-16 16:21:21 +0900 | [diff] [blame] | 396 | |
satok | 54af64a | 2012-01-17 15:58:23 +0900 | [diff] [blame] | 397 | // We only allow two words + other error correction for words with SUB_QUEUE_MIN_WORD_LENGTH |
| 398 | // or more length. |
| 399 | if (inputIndex >= SUB_QUEUE_MIN_WORD_LENGTH && addToSubQueue) { |
satok | 8330b48 | 2012-01-23 16:52:37 +0900 | [diff] [blame] | 400 | WordsPriorityQueue *subQueue; |
satok | 7409d15 | 2012-01-26 16:13:25 +0900 | [diff] [blame] | 401 | subQueue = queuePool->getSubQueue(currentWordIndex, inputIndex); |
| 402 | if (!subQueue) { |
satok | 8330b48 | 2012-01-23 16:52:37 +0900 | [diff] [blame] | 403 | return; |
| 404 | } |
Jean Chalard | 171d180 | 2012-04-23 18:51:00 +0900 | [diff] [blame] | 405 | const int finalProbability = correction->getFinalProbabilityForSubQueue( |
| 406 | probability, &wordPointer, &wordLength, inputIndex); |
| 407 | addWord(wordPointer, wordLength, finalProbability, subQueue); |
Jean Chalard | ca5ef28 | 2011-06-17 15:36:26 +0900 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | |
satok | 9955716 | 2012-01-26 22:49:13 +0900 | [diff] [blame] | 411 | bool UnigramDictionary::getSubStringSuggestion( |
satok | 7409d15 | 2012-01-26 16:13:25 +0900 | [diff] [blame] | 412 | ProximityInfo *proximityInfo, const int *xcoordinates, const int *ycoordinates, |
satok | 3c09bb1 | 2012-01-26 18:36:19 +0900 | [diff] [blame] | 413 | const int *codes, const bool useFullEditDistance, Correction *correction, |
| 414 | WordsPriorityQueuePool* queuePool, const int inputLength, |
| 415 | const bool hasAutoCorrectionCandidate, const int currentWordIndex, |
| 416 | const int inputWordStartPos, const int inputWordLength, |
satok | 9955716 | 2012-01-26 22:49:13 +0900 | [diff] [blame] | 417 | const int outputWordStartPos, const bool isSpaceProximity, int *freqArray, |
| 418 | int*wordLengthArray, unsigned short* outputWord, int *outputWordLength) { |
satok | 3c09bb1 | 2012-01-26 18:36:19 +0900 | [diff] [blame] | 419 | unsigned short* tempOutputWord = 0; |
satok | 1f6b52e | 2012-01-30 13:53:58 +0900 | [diff] [blame] | 420 | int nextWordLength = 0; |
satok | 9955716 | 2012-01-26 22:49:13 +0900 | [diff] [blame] | 421 | // TODO: Optimize init suggestion |
| 422 | initSuggestions(proximityInfo, xcoordinates, ycoordinates, codes, |
| 423 | inputLength, correction); |
| 424 | |
satok | 3c09bb1 | 2012-01-26 18:36:19 +0900 | [diff] [blame] | 425 | int freq = getMostFrequentWordLike( |
| 426 | inputWordStartPos, inputWordLength, proximityInfo, mWord); |
| 427 | if (freq > 0) { |
satok | 1f6b52e | 2012-01-30 13:53:58 +0900 | [diff] [blame] | 428 | nextWordLength = inputWordLength; |
satok | 3c09bb1 | 2012-01-26 18:36:19 +0900 | [diff] [blame] | 429 | tempOutputWord = mWord; |
| 430 | } else if (!hasAutoCorrectionCandidate) { |
| 431 | if (inputWordStartPos > 0) { |
| 432 | const int offset = inputWordStartPos; |
| 433 | initSuggestions(proximityInfo, &xcoordinates[offset], &ycoordinates[offset], |
satok | 9df4a45 | 2012-03-23 16:05:18 +0900 | [diff] [blame] | 434 | codes + offset, inputWordLength, correction); |
satok | 3c09bb1 | 2012-01-26 18:36:19 +0900 | [diff] [blame] | 435 | queuePool->clearSubQueue(currentWordIndex); |
Jean Chalard | 4d9b202 | 2012-04-23 19:25:28 +0900 | [diff] [blame] | 436 | // TODO: pass the bigram list for substring suggestion |
| 437 | getSuggestionCandidates(useFullEditDistance, inputWordLength, |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame^] | 438 | 0 /* bigramMap */, 0 /* bigramFilter */, correction, queuePool, |
| 439 | false /* doAutoCompletion */, MAX_ERRORS_FOR_TWO_WORDS, currentWordIndex); |
satok | 3c09bb1 | 2012-01-26 18:36:19 +0900 | [diff] [blame] | 440 | if (DEBUG_DICT) { |
satok | 1f6b52e | 2012-01-30 13:53:58 +0900 | [diff] [blame] | 441 | if (currentWordIndex < MULTIPLE_WORDS_SUGGESTION_MAX_WORDS) { |
satok | 3c09bb1 | 2012-01-26 18:36:19 +0900 | [diff] [blame] | 442 | AKLOGI("Dump word candidates(%d) %d", currentWordIndex, inputWordLength); |
| 443 | for (int i = 0; i < SUB_QUEUE_MAX_COUNT; ++i) { |
| 444 | queuePool->getSubQueue(currentWordIndex, i)->dumpTopWord(); |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | WordsPriorityQueue* queue = queuePool->getSubQueue(currentWordIndex, inputWordLength); |
| 450 | if (!queue || queue->size() < 1) { |
satok | 9955716 | 2012-01-26 22:49:13 +0900 | [diff] [blame] | 451 | return false; |
satok | 3c09bb1 | 2012-01-26 18:36:19 +0900 | [diff] [blame] | 452 | } |
| 453 | int score = 0; |
| 454 | const double ns = queue->getHighestNormalizedScore( |
| 455 | proximityInfo->getPrimaryInputWord(), inputWordLength, |
satok | 1f6b52e | 2012-01-30 13:53:58 +0900 | [diff] [blame] | 456 | &tempOutputWord, &score, &nextWordLength); |
satok | 3c09bb1 | 2012-01-26 18:36:19 +0900 | [diff] [blame] | 457 | if (DEBUG_DICT) { |
| 458 | AKLOGI("NS(%d) = %f, Score = %d", currentWordIndex, ns, score); |
| 459 | } |
| 460 | // Two words correction won't be done if the score of the first word doesn't exceed the |
| 461 | // threshold. |
| 462 | if (ns < TWO_WORDS_CORRECTION_WITH_OTHER_ERROR_THRESHOLD |
satok | 1f6b52e | 2012-01-30 13:53:58 +0900 | [diff] [blame] | 463 | || nextWordLength < SUB_QUEUE_MIN_WORD_LENGTH) { |
satok | 9955716 | 2012-01-26 22:49:13 +0900 | [diff] [blame] | 464 | return false; |
satok | 3c09bb1 | 2012-01-26 18:36:19 +0900 | [diff] [blame] | 465 | } |
satok | 1f6b52e | 2012-01-30 13:53:58 +0900 | [diff] [blame] | 466 | freq = score >> (nextWordLength + TWO_WORDS_PLUS_OTHER_ERROR_CORRECTION_DEMOTION_DIVIDER); |
satok | 3c09bb1 | 2012-01-26 18:36:19 +0900 | [diff] [blame] | 467 | } |
| 468 | if (DEBUG_DICT) { |
satok | 1f6b52e | 2012-01-30 13:53:58 +0900 | [diff] [blame] | 469 | AKLOGI("Freq(%d): %d, length: %d, input length: %d, input start: %d (%d)" |
| 470 | , currentWordIndex, freq, nextWordLength, inputWordLength, inputWordStartPos, |
| 471 | wordLengthArray[0]); |
satok | 3c09bb1 | 2012-01-26 18:36:19 +0900 | [diff] [blame] | 472 | } |
satok | 1f6b52e | 2012-01-30 13:53:58 +0900 | [diff] [blame] | 473 | if (freq <= 0 || nextWordLength <= 0 |
| 474 | || MAX_WORD_LENGTH <= (outputWordStartPos + nextWordLength)) { |
satok | 9955716 | 2012-01-26 22:49:13 +0900 | [diff] [blame] | 475 | return false; |
satok | 3c09bb1 | 2012-01-26 18:36:19 +0900 | [diff] [blame] | 476 | } |
satok | 1f6b52e | 2012-01-30 13:53:58 +0900 | [diff] [blame] | 477 | for (int i = 0; i < nextWordLength; ++i) { |
satok | 3c09bb1 | 2012-01-26 18:36:19 +0900 | [diff] [blame] | 478 | outputWord[outputWordStartPos + i] = tempOutputWord[i]; |
| 479 | } |
satok | 9955716 | 2012-01-26 22:49:13 +0900 | [diff] [blame] | 480 | |
| 481 | // Put output values |
satok | 1f6b52e | 2012-01-30 13:53:58 +0900 | [diff] [blame] | 482 | freqArray[currentWordIndex] = freq; |
satok | 9955716 | 2012-01-26 22:49:13 +0900 | [diff] [blame] | 483 | // TODO: put output length instead of input length |
satok | 1f6b52e | 2012-01-30 13:53:58 +0900 | [diff] [blame] | 484 | wordLengthArray[currentWordIndex] = inputWordLength; |
| 485 | const int tempOutputWordLength = outputWordStartPos + nextWordLength; |
| 486 | if (outputWordLength) { |
| 487 | *outputWordLength = tempOutputWordLength; |
| 488 | } |
satok | 9955716 | 2012-01-26 22:49:13 +0900 | [diff] [blame] | 489 | |
satok | 3c09bb1 | 2012-01-26 18:36:19 +0900 | [diff] [blame] | 490 | if ((inputWordStartPos + inputWordLength) < inputLength) { |
satok | 1f6b52e | 2012-01-30 13:53:58 +0900 | [diff] [blame] | 491 | if (outputWordStartPos + nextWordLength >= MAX_WORD_LENGTH) { |
satok | 9955716 | 2012-01-26 22:49:13 +0900 | [diff] [blame] | 492 | return false; |
satok | 3c09bb1 | 2012-01-26 18:36:19 +0900 | [diff] [blame] | 493 | } |
satok | a85f492 | 2012-01-30 18:18:30 +0900 | [diff] [blame] | 494 | outputWord[tempOutputWordLength] = SPACE; |
satok | 1f6b52e | 2012-01-30 13:53:58 +0900 | [diff] [blame] | 495 | if (outputWordLength) { |
| 496 | ++*outputWordLength; |
| 497 | } |
| 498 | } else if (currentWordIndex >= 1) { |
satok | 9955716 | 2012-01-26 22:49:13 +0900 | [diff] [blame] | 499 | // TODO: Handle 3 or more words |
satok | a85f492 | 2012-01-30 18:18:30 +0900 | [diff] [blame] | 500 | const int pairFreq = correction->getFreqForSplitMultipleWords( |
| 501 | freqArray, wordLengthArray, currentWordIndex + 1, isSpaceProximity, outputWord); |
satok | 9955716 | 2012-01-26 22:49:13 +0900 | [diff] [blame] | 502 | if (DEBUG_DICT) { |
satok | a85f492 | 2012-01-30 18:18:30 +0900 | [diff] [blame] | 503 | DUMP_WORD(outputWord, tempOutputWordLength); |
| 504 | AKLOGI("Split two words: %d, %d, %d, %d, (%d) %d", freqArray[0], freqArray[1], pairFreq, |
| 505 | inputLength, wordLengthArray[0], tempOutputWordLength); |
satok | 9955716 | 2012-01-26 22:49:13 +0900 | [diff] [blame] | 506 | } |
satok | 1f6b52e | 2012-01-30 13:53:58 +0900 | [diff] [blame] | 507 | addWord(outputWord, tempOutputWordLength, pairFreq, queuePool->getMasterQueue()); |
satok | 3c09bb1 | 2012-01-26 18:36:19 +0900 | [diff] [blame] | 508 | } |
satok | 9955716 | 2012-01-26 22:49:13 +0900 | [diff] [blame] | 509 | return true; |
satok | 7409d15 | 2012-01-26 16:13:25 +0900 | [diff] [blame] | 510 | } |
| 511 | |
satok | 1f6b52e | 2012-01-30 13:53:58 +0900 | [diff] [blame] | 512 | void UnigramDictionary::getMultiWordsSuggestionRec(ProximityInfo *proximityInfo, |
| 513 | const int *xcoordinates, const int *ycoordinates, const int *codes, |
| 514 | const bool useFullEditDistance, const int inputLength, |
| 515 | Correction *correction, WordsPriorityQueuePool* queuePool, |
| 516 | const bool hasAutoCorrectionCandidate, const int startInputPos, const int startWordIndex, |
| 517 | const int outputWordLength, int *freqArray, int* wordLengthArray, |
| 518 | unsigned short* outputWord) { |
| 519 | if (startWordIndex >= (MULTIPLE_WORDS_SUGGESTION_MAX_WORDS - 1)) { |
| 520 | // Return if the last word index |
| 521 | return; |
| 522 | } |
satok | a85f492 | 2012-01-30 18:18:30 +0900 | [diff] [blame] | 523 | if (startWordIndex >= 1 |
| 524 | && (hasAutoCorrectionCandidate |
| 525 | || inputLength < MIN_INPUT_LENGTH_FOR_THREE_OR_MORE_WORDS_CORRECTION)) { |
| 526 | // Do not suggest 3+ words if already has auto correction candidate |
| 527 | return; |
| 528 | } |
| 529 | for (int i = startInputPos + 1; i < inputLength; ++i) { |
satok | 1f6b52e | 2012-01-30 13:53:58 +0900 | [diff] [blame] | 530 | if (DEBUG_CORRECTION_FREQ) { |
satok | a85f492 | 2012-01-30 18:18:30 +0900 | [diff] [blame] | 531 | AKLOGI("Multi words(%d), start in %d sep %d start out %d", |
| 532 | startWordIndex, startInputPos, i, outputWordLength); |
| 533 | DUMP_WORD(outputWord, outputWordLength); |
satok | 1f6b52e | 2012-01-30 13:53:58 +0900 | [diff] [blame] | 534 | } |
satok | a85f492 | 2012-01-30 18:18:30 +0900 | [diff] [blame] | 535 | int tempOutputWordLength = 0; |
| 536 | // Current word |
| 537 | int inputWordStartPos = startInputPos; |
| 538 | int inputWordLength = i - startInputPos; |
satok | 1f6b52e | 2012-01-30 13:53:58 +0900 | [diff] [blame] | 539 | if (!getSubStringSuggestion(proximityInfo, xcoordinates, ycoordinates, codes, |
| 540 | useFullEditDistance, correction, queuePool, inputLength, hasAutoCorrectionCandidate, |
satok | a85f492 | 2012-01-30 18:18:30 +0900 | [diff] [blame] | 541 | startWordIndex, inputWordStartPos, inputWordLength, outputWordLength, |
| 542 | true /* not used */, freqArray, wordLengthArray, outputWord, |
| 543 | &tempOutputWordLength)) { |
satok | 1f6b52e | 2012-01-30 13:53:58 +0900 | [diff] [blame] | 544 | continue; |
| 545 | } |
| 546 | |
satok | a85f492 | 2012-01-30 18:18:30 +0900 | [diff] [blame] | 547 | if (DEBUG_CORRECTION_FREQ) { |
| 548 | AKLOGI("Do missing space correction"); |
| 549 | } |
| 550 | // Next word |
satok | 1f6b52e | 2012-01-30 13:53:58 +0900 | [diff] [blame] | 551 | // Missing space |
| 552 | inputWordStartPos = i; |
| 553 | inputWordLength = inputLength - i; |
satok | a85f492 | 2012-01-30 18:18:30 +0900 | [diff] [blame] | 554 | if(!getSubStringSuggestion(proximityInfo, xcoordinates, ycoordinates, codes, |
satok | 1f6b52e | 2012-01-30 13:53:58 +0900 | [diff] [blame] | 555 | useFullEditDistance, correction, queuePool, inputLength, hasAutoCorrectionCandidate, |
satok | a85f492 | 2012-01-30 18:18:30 +0900 | [diff] [blame] | 556 | startWordIndex + 1, inputWordStartPos, inputWordLength, tempOutputWordLength, |
| 557 | false /* missing space */, freqArray, wordLengthArray, outputWord, 0)) { |
| 558 | getMultiWordsSuggestionRec(proximityInfo, xcoordinates, ycoordinates, codes, |
| 559 | useFullEditDistance, inputLength, correction, queuePool, |
| 560 | hasAutoCorrectionCandidate, inputWordStartPos, startWordIndex + 1, |
| 561 | tempOutputWordLength, freqArray, wordLengthArray, outputWord); |
| 562 | } |
satok | 1f6b52e | 2012-01-30 13:53:58 +0900 | [diff] [blame] | 563 | |
| 564 | // Mistyped space |
| 565 | ++inputWordStartPos; |
| 566 | --inputWordLength; |
| 567 | |
| 568 | if (inputWordLength <= 0) { |
| 569 | continue; |
| 570 | } |
| 571 | |
| 572 | const int x = xcoordinates[inputWordStartPos - 1]; |
| 573 | const int y = ycoordinates[inputWordStartPos - 1]; |
| 574 | if (!proximityInfo->hasSpaceProximity(x, y)) { |
| 575 | continue; |
| 576 | } |
| 577 | |
satok | a85f492 | 2012-01-30 18:18:30 +0900 | [diff] [blame] | 578 | if (DEBUG_CORRECTION_FREQ) { |
| 579 | AKLOGI("Do mistyped space correction"); |
| 580 | } |
satok | 1f6b52e | 2012-01-30 13:53:58 +0900 | [diff] [blame] | 581 | getSubStringSuggestion(proximityInfo, xcoordinates, ycoordinates, codes, |
| 582 | useFullEditDistance, correction, queuePool, inputLength, hasAutoCorrectionCandidate, |
satok | a85f492 | 2012-01-30 18:18:30 +0900 | [diff] [blame] | 583 | startWordIndex + 1, inputWordStartPos, inputWordLength, tempOutputWordLength, |
| 584 | true /* mistyped space */, freqArray, wordLengthArray, outputWord, 0); |
satok | 1f6b52e | 2012-01-30 13:53:58 +0900 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | |
satok | a85f492 | 2012-01-30 18:18:30 +0900 | [diff] [blame] | 588 | void UnigramDictionary::getSplitMultipleWordsSuggestions(ProximityInfo *proximityInfo, |
satok | 744dab6 | 2011-12-15 22:29:05 +0900 | [diff] [blame] | 589 | const int *xcoordinates, const int *ycoordinates, const int *codes, |
satok | 1f6b52e | 2012-01-30 13:53:58 +0900 | [diff] [blame] | 590 | const bool useFullEditDistance, const int inputLength, |
satok | 9955716 | 2012-01-26 22:49:13 +0900 | [diff] [blame] | 591 | Correction *correction, WordsPriorityQueuePool* queuePool, |
satok | 8330b48 | 2012-01-23 16:52:37 +0900 | [diff] [blame] | 592 | const bool hasAutoCorrectionCandidate) { |
satok | bd6ccdd | 2012-01-23 12:30:20 +0900 | [diff] [blame] | 593 | if (inputLength >= MAX_WORD_LENGTH) return; |
satok | 612c6e4 | 2011-08-01 19:35:27 +0900 | [diff] [blame] | 594 | if (DEBUG_DICT) { |
satok | 1f6b52e | 2012-01-30 13:53:58 +0900 | [diff] [blame] | 595 | AKLOGI("--- Suggest multiple words"); |
| 596 | } |
satok | 54af64a | 2012-01-17 15:58:23 +0900 | [diff] [blame] | 597 | |
satok | bd6ccdd | 2012-01-23 12:30:20 +0900 | [diff] [blame] | 598 | // Allocating fixed length array on stack |
| 599 | unsigned short outputWord[MAX_WORD_LENGTH]; |
satok | 1f6b52e | 2012-01-30 13:53:58 +0900 | [diff] [blame] | 600 | int freqArray[MULTIPLE_WORDS_SUGGESTION_MAX_WORDS]; |
| 601 | int wordLengthArray[MULTIPLE_WORDS_SUGGESTION_MAX_WORDS]; |
| 602 | const int outputWordLength = 0; |
| 603 | const int startInputPos = 0; |
| 604 | const int startWordIndex = 0; |
| 605 | getMultiWordsSuggestionRec(proximityInfo, xcoordinates, ycoordinates, codes, |
| 606 | useFullEditDistance, inputLength, correction, queuePool, hasAutoCorrectionCandidate, |
| 607 | startInputPos, startWordIndex, outputWordLength, freqArray, wordLengthArray, |
| 608 | outputWord); |
Jean Chalard | e6715e3 | 2011-06-30 19:47:25 +0900 | [diff] [blame] | 609 | } |
| 610 | |
Jean Chalard | 1059f27 | 2011-06-28 20:45:05 +0900 | [diff] [blame] | 611 | // Wrapper for getMostFrequentWordLikeInner, which matches it to the previous |
| 612 | // interface. |
| 613 | inline int UnigramDictionary::getMostFrequentWordLike(const int startInputIndex, |
satok | 1147c7b | 2011-12-14 15:04:58 +0900 | [diff] [blame] | 614 | const int inputLength, ProximityInfo *proximityInfo, unsigned short *word) { |
Jean Chalard | 1059f27 | 2011-06-28 20:45:05 +0900 | [diff] [blame] | 615 | uint16_t inWord[inputLength]; |
| 616 | |
| 617 | for (int i = 0; i < inputLength; ++i) { |
satok | 1147c7b | 2011-12-14 15:04:58 +0900 | [diff] [blame] | 618 | inWord[i] = (uint16_t)proximityInfo->getPrimaryCharAt(startInputIndex + i); |
Jean Chalard | 1059f27 | 2011-06-28 20:45:05 +0900 | [diff] [blame] | 619 | } |
| 620 | return getMostFrequentWordLikeInner(inWord, inputLength, word); |
| 621 | } |
| 622 | |
| 623 | // This function will take the position of a character array within a CharGroup, |
| 624 | // and check it actually like-matches the word in inWord starting at startInputIndex, |
| 625 | // that is, it matches it with case and accents squashed. |
| 626 | // The function returns true if there was a full match, false otherwise. |
| 627 | // The function will copy on-the-fly the characters in the CharGroup to outNewWord. |
| 628 | // It will also place the end position of the array in outPos; in outInputIndex, |
| 629 | // it will place the index of the first char AFTER the match if there was a match, |
| 630 | // and the initial position if there was not. It makes sense because if there was |
| 631 | // a match we want to continue searching, but if there was not, we want to go to |
| 632 | // the next CharGroup. |
| 633 | // In and out parameters may point to the same location. This function takes care |
| 634 | // not to use any input parameters after it wrote into its outputs. |
| 635 | static inline bool testCharGroupForContinuedLikeness(const uint8_t flags, |
| 636 | const uint8_t* const root, const int startPos, |
| 637 | const uint16_t* const inWord, const int startInputIndex, |
| 638 | int32_t* outNewWord, int* outInputIndex, int* outPos) { |
| 639 | const bool hasMultipleChars = (0 != (UnigramDictionary::FLAG_HAS_MULTIPLE_CHARS & flags)); |
| 640 | int pos = startPos; |
| 641 | int32_t character = BinaryFormat::getCharCodeAndForwardPointer(root, &pos); |
Tadashi G. Takaoka | 6e3cb27 | 2011-11-11 14:26:13 +0900 | [diff] [blame] | 642 | int32_t baseChar = toBaseLowerCase(character); |
| 643 | const uint16_t wChar = toBaseLowerCase(inWord[startInputIndex]); |
Jean Chalard | 1059f27 | 2011-06-28 20:45:05 +0900 | [diff] [blame] | 644 | |
| 645 | if (baseChar != wChar) { |
| 646 | *outPos = hasMultipleChars ? BinaryFormat::skipOtherCharacters(root, pos) : pos; |
| 647 | *outInputIndex = startInputIndex; |
| 648 | return false; |
| 649 | } |
| 650 | int inputIndex = startInputIndex; |
| 651 | outNewWord[inputIndex] = character; |
| 652 | if (hasMultipleChars) { |
| 653 | character = BinaryFormat::getCharCodeAndForwardPointer(root, &pos); |
| 654 | while (NOT_A_CHARACTER != character) { |
Tadashi G. Takaoka | 6e3cb27 | 2011-11-11 14:26:13 +0900 | [diff] [blame] | 655 | baseChar = toBaseLowerCase(character); |
| 656 | if (toBaseLowerCase(inWord[++inputIndex]) != baseChar) { |
Jean Chalard | 1059f27 | 2011-06-28 20:45:05 +0900 | [diff] [blame] | 657 | *outPos = BinaryFormat::skipOtherCharacters(root, pos); |
| 658 | *outInputIndex = startInputIndex; |
| 659 | return false; |
| 660 | } |
| 661 | outNewWord[inputIndex] = character; |
| 662 | character = BinaryFormat::getCharCodeAndForwardPointer(root, &pos); |
| 663 | } |
| 664 | } |
| 665 | *outInputIndex = inputIndex + 1; |
| 666 | *outPos = pos; |
| 667 | return true; |
| 668 | } |
| 669 | |
| 670 | // This function is invoked when a word like the word searched for is found. |
| 671 | // It will compare the frequency to the max frequency, and if greater, will |
| 672 | // copy the word into the output buffer. In output value maxFreq, it will |
| 673 | // write the new maximum frequency if it changed. |
| 674 | static inline void onTerminalWordLike(const int freq, int32_t* newWord, const int length, |
| 675 | short unsigned int* outWord, int* maxFreq) { |
| 676 | if (freq > *maxFreq) { |
| 677 | for (int q = 0; q < length; ++q) |
| 678 | outWord[q] = newWord[q]; |
| 679 | outWord[length] = 0; |
| 680 | *maxFreq = freq; |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | // Will find the highest frequency of the words like the one passed as an argument, |
| 685 | // that is, everything that only differs by case/accents. |
| 686 | int UnigramDictionary::getMostFrequentWordLikeInner(const uint16_t * const inWord, |
| 687 | const int length, short unsigned int* outWord) { |
| 688 | int32_t newWord[MAX_WORD_LENGTH_INTERNAL]; |
| 689 | int depth = 0; |
| 690 | int maxFreq = -1; |
| 691 | const uint8_t* const root = DICT_ROOT; |
| 692 | |
Jean Chalard | 4c0eca6 | 2012-01-16 15:15:53 +0900 | [diff] [blame] | 693 | int startPos = 0; |
| 694 | mStackChildCount[0] = BinaryFormat::getGroupCountAndForwardPointer(root, &startPos); |
Jean Chalard | 1059f27 | 2011-06-28 20:45:05 +0900 | [diff] [blame] | 695 | mStackInputIndex[0] = 0; |
Jean Chalard | 4c0eca6 | 2012-01-16 15:15:53 +0900 | [diff] [blame] | 696 | mStackSiblingPos[0] = startPos; |
Jean Chalard | 1059f27 | 2011-06-28 20:45:05 +0900 | [diff] [blame] | 697 | while (depth >= 0) { |
| 698 | const int charGroupCount = mStackChildCount[depth]; |
| 699 | int pos = mStackSiblingPos[depth]; |
| 700 | for (int charGroupIndex = charGroupCount - 1; charGroupIndex >= 0; --charGroupIndex) { |
| 701 | int inputIndex = mStackInputIndex[depth]; |
| 702 | const uint8_t flags = BinaryFormat::getFlagsAndForwardPointer(root, &pos); |
| 703 | // Test whether all chars in this group match with the word we are searching for. If so, |
| 704 | // we want to traverse its children (or if the length match, evaluate its frequency). |
| 705 | // Note that this function will output the position regardless, but will only write |
| 706 | // into inputIndex if there is a match. |
| 707 | const bool isAlike = testCharGroupForContinuedLikeness(flags, root, pos, inWord, |
| 708 | inputIndex, newWord, &inputIndex, &pos); |
| 709 | if (isAlike && (FLAG_IS_TERMINAL & flags) && (inputIndex == length)) { |
| 710 | const int frequency = BinaryFormat::readFrequencyWithoutMovingPointer(root, pos); |
| 711 | onTerminalWordLike(frequency, newWord, inputIndex, outWord, &maxFreq); |
| 712 | } |
| 713 | pos = BinaryFormat::skipFrequency(flags, pos); |
| 714 | const int siblingPos = BinaryFormat::skipChildrenPosAndAttributes(root, flags, pos); |
| 715 | const int childrenNodePos = BinaryFormat::readChildrenPosition(root, flags, pos); |
| 716 | // If we had a match and the word has children, we want to traverse them. We don't have |
| 717 | // to traverse words longer than the one we are searching for, since they will not match |
| 718 | // anyway, so don't traverse unless inputIndex < length. |
| 719 | if (isAlike && (-1 != childrenNodePos) && (inputIndex < length)) { |
| 720 | // Save position for this depth, to get back to this once children are done |
| 721 | mStackChildCount[depth] = charGroupIndex; |
| 722 | mStackSiblingPos[depth] = siblingPos; |
| 723 | // Prepare stack values for next depth |
| 724 | ++depth; |
| 725 | int childrenPos = childrenNodePos; |
| 726 | mStackChildCount[depth] = |
| 727 | BinaryFormat::getGroupCountAndForwardPointer(root, &childrenPos); |
| 728 | mStackSiblingPos[depth] = childrenPos; |
| 729 | mStackInputIndex[depth] = inputIndex; |
| 730 | pos = childrenPos; |
| 731 | // Go to the next depth level. |
| 732 | ++depth; |
| 733 | break; |
| 734 | } else { |
| 735 | // No match, or no children, or word too long to ever match: go the next sibling. |
| 736 | pos = siblingPos; |
| 737 | } |
| 738 | } |
| 739 | --depth; |
| 740 | } |
| 741 | return maxFreq; |
| 742 | } |
| 743 | |
Jean Chalard | 522a04e | 2012-04-23 15:37:07 +0900 | [diff] [blame] | 744 | bool UnigramDictionary::isValidWord(const int32_t* const inWord, const int length) const { |
Jean Chalard | 6a0e964 | 2011-07-25 18:17:11 +0900 | [diff] [blame] | 745 | return NOT_VALID_WORD != BinaryFormat::getTerminalPosition(DICT_ROOT, inWord, length); |
Jean Chalard | 1059f27 | 2011-06-28 20:45:05 +0900 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | // TODO: remove this function. |
| 749 | int UnigramDictionary::getBigramPosition(int pos, unsigned short *word, int offset, |
| 750 | int length) const { |
| 751 | return -1; |
| 752 | } |
| 753 | |
| 754 | // ProcessCurrentNode returns a boolean telling whether to traverse children nodes or not. |
| 755 | // If the return value is false, then the caller should read in the output "nextSiblingPosition" |
| 756 | // to find out the address of the next sibling node and pass it to a new call of processCurrentNode. |
| 757 | // It is worthy to note that when false is returned, the output values other than |
| 758 | // nextSiblingPosition are undefined. |
| 759 | // If the return value is true, then the caller must proceed to traverse the children of this |
| 760 | // node. processCurrentNode will output the information about the children: their count in |
| 761 | // newCount, their position in newChildrenPosition, the traverseAllNodes flag in |
| 762 | // newTraverseAllNodes, the match weight into newMatchRate, the input index into newInputIndex, the |
| 763 | // diffs into newDiffs, the sibling position in nextSiblingPosition, and the output index into |
| 764 | // newOutputIndex. Please also note the following caveat: processCurrentNode does not know when |
| 765 | // there aren't any more nodes at this level, it merely returns the address of the first byte after |
| 766 | // the current node in nextSiblingPosition. Thus, the caller must keep count of the nodes at any |
| 767 | // given level, as output into newCount when traversing this level's parent. |
satok | 8876b75 | 2011-08-04 18:31:57 +0900 | [diff] [blame] | 768 | inline bool UnigramDictionary::processCurrentNode(const int initialPos, |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame^] | 769 | const std::map<int, int> *bigramMap, const uint8_t *bigramFilter, Correction *correction, |
| 770 | int *newCount, int *newChildrenPosition, int *nextSiblingPosition, |
| 771 | WordsPriorityQueuePool *queuePool, const int currentWordIndex) { |
Jean Chalard | 85a1d1e | 2011-06-21 22:23:21 +0900 | [diff] [blame] | 772 | if (DEBUG_DICT) { |
satok | cfca3c6 | 2011-08-10 14:30:10 +0900 | [diff] [blame] | 773 | correction->checkState(); |
Jean Chalard | 85a1d1e | 2011-06-21 22:23:21 +0900 | [diff] [blame] | 774 | } |
Jean Chalard | 0584f02 | 2011-06-30 19:23:16 +0900 | [diff] [blame] | 775 | int pos = initialPos; |
Jean Chalard | 0584f02 | 2011-06-30 19:23:16 +0900 | [diff] [blame] | 776 | |
Jean Chalard | 1059f27 | 2011-06-28 20:45:05 +0900 | [diff] [blame] | 777 | // Flags contain the following information: |
| 778 | // - Address type (MASK_GROUP_ADDRESS_TYPE) on two bits: |
| 779 | // - FLAG_GROUP_ADDRESS_TYPE_{ONE,TWO,THREE}_BYTES means there are children and their address |
| 780 | // is on the specified number of bytes. |
| 781 | // - FLAG_GROUP_ADDRESS_TYPE_NOADDRESS means there are no children, and therefore no address. |
| 782 | // - FLAG_HAS_MULTIPLE_CHARS: whether this node has multiple char or not. |
| 783 | // - FLAG_IS_TERMINAL: whether this node is a terminal or not (it may still have children) |
| 784 | // - FLAG_HAS_BIGRAMS: whether this node has bigrams or not |
| 785 | const uint8_t flags = BinaryFormat::getFlagsAndForwardPointer(DICT_ROOT, &pos); |
| 786 | const bool hasMultipleChars = (0 != (FLAG_HAS_MULTIPLE_CHARS & flags)); |
satok | 8876b75 | 2011-08-04 18:31:57 +0900 | [diff] [blame] | 787 | const bool isTerminalNode = (0 != (FLAG_IS_TERMINAL & flags)); |
| 788 | |
| 789 | bool needsToInvokeOnTerminal = false; |
Jean Chalard | 85a1d1e | 2011-06-21 22:23:21 +0900 | [diff] [blame] | 790 | |
Jean Chalard | 1059f27 | 2011-06-28 20:45:05 +0900 | [diff] [blame] | 791 | // This gets only ONE character from the stream. Next there will be: |
| 792 | // if FLAG_HAS_MULTIPLE CHARS: the other characters of the same node |
| 793 | // else if FLAG_IS_TERMINAL: the frequency |
| 794 | // else if MASK_GROUP_ADDRESS_TYPE is not NONE: the children address |
| 795 | // Note that you can't have a node that both is not a terminal and has no children. |
| 796 | int32_t c = BinaryFormat::getCharCodeAndForwardPointer(DICT_ROOT, &pos); |
| 797 | assert(NOT_A_CHARACTER != c); |
Jean Chalard | 85a1d1e | 2011-06-21 22:23:21 +0900 | [diff] [blame] | 798 | |
Jean Chalard | 1059f27 | 2011-06-28 20:45:05 +0900 | [diff] [blame] | 799 | // We are going to loop through each character and make it look like it's a different |
| 800 | // node each time. To do that, we will process characters in this node in order until |
| 801 | // we find the character terminator. This is signalled by getCharCode* returning |
| 802 | // NOT_A_CHARACTER. |
| 803 | // As a special case, if there is only one character in this node, we must not read the |
| 804 | // next bytes so we will simulate the NOT_A_CHARACTER return by testing the flags. |
| 805 | // This way, each loop run will look like a "virtual node". |
| 806 | do { |
| 807 | // We prefetch the next char. If 'c' is the last char of this node, we will have |
| 808 | // NOT_A_CHARACTER in the next char. From this we can decide whether this virtual node |
| 809 | // should behave as a terminal or not and whether we have children. |
| 810 | const int32_t nextc = hasMultipleChars |
| 811 | ? BinaryFormat::getCharCodeAndForwardPointer(DICT_ROOT, &pos) : NOT_A_CHARACTER; |
| 812 | const bool isLastChar = (NOT_A_CHARACTER == nextc); |
| 813 | // If there are more chars in this nodes, then this virtual node is not a terminal. |
| 814 | // If we are on the last char, this virtual node is a terminal if this node is. |
satok | 8876b75 | 2011-08-04 18:31:57 +0900 | [diff] [blame] | 815 | const bool isTerminal = isLastChar && isTerminalNode; |
Jean Chalard | 85a1d1e | 2011-06-21 22:23:21 +0900 | [diff] [blame] | 816 | |
satok | cfca3c6 | 2011-08-10 14:30:10 +0900 | [diff] [blame] | 817 | Correction::CorrectionType stateType = correction->processCharAndCalcState( |
satok | 8876b75 | 2011-08-04 18:31:57 +0900 | [diff] [blame] | 818 | c, isTerminal); |
satok | cfca3c6 | 2011-08-10 14:30:10 +0900 | [diff] [blame] | 819 | if (stateType == Correction::TRAVERSE_ALL_ON_TERMINAL |
| 820 | || stateType == Correction::ON_TERMINAL) { |
satok | 8876b75 | 2011-08-04 18:31:57 +0900 | [diff] [blame] | 821 | needsToInvokeOnTerminal = true; |
satok | d03317c | 2011-12-14 21:38:11 +0900 | [diff] [blame] | 822 | } else if (stateType == Correction::UNRELATED || correction->needsToPrune()) { |
satok | 8876b75 | 2011-08-04 18:31:57 +0900 | [diff] [blame] | 823 | // We found that this is an unrelated character, so we should give up traversing |
| 824 | // this node and its children entirely. |
| 825 | // However we may not be on the last virtual node yet so we skip the remaining |
| 826 | // characters in this node, the frequency if it's there, read the next sibling |
| 827 | // position to output it, then return false. |
| 828 | // We don't have to output other values because we return false, as in |
| 829 | // "don't traverse children". |
Jean Chalard | 1059f27 | 2011-06-28 20:45:05 +0900 | [diff] [blame] | 830 | if (!isLastChar) { |
| 831 | pos = BinaryFormat::skipOtherCharacters(DICT_ROOT, pos); |
| 832 | } |
| 833 | pos = BinaryFormat::skipFrequency(flags, pos); |
| 834 | *nextSiblingPosition = |
| 835 | BinaryFormat::skipChildrenPosAndAttributes(DICT_ROOT, flags, pos); |
| 836 | return false; |
Jean Chalard | 85a1d1e | 2011-06-21 22:23:21 +0900 | [diff] [blame] | 837 | } |
| 838 | |
Jean Chalard | 1059f27 | 2011-06-28 20:45:05 +0900 | [diff] [blame] | 839 | // Prepare for the next character. Promote the prefetched char to current char - the loop |
| 840 | // will take care of prefetching the next. If we finally found our last char, nextc will |
| 841 | // contain NOT_A_CHARACTER. |
| 842 | c = nextc; |
Jean Chalard | 1059f27 | 2011-06-28 20:45:05 +0900 | [diff] [blame] | 843 | } while (NOT_A_CHARACTER != c); |
Jean Chalard | 85a1d1e | 2011-06-21 22:23:21 +0900 | [diff] [blame] | 844 | |
satok | 8876b75 | 2011-08-04 18:31:57 +0900 | [diff] [blame] | 845 | if (isTerminalNode) { |
satok | 6ad15fc | 2012-01-16 16:21:21 +0900 | [diff] [blame] | 846 | // The frequency should be here, because we come here only if this is actually |
| 847 | // a terminal node, and we are on its last char. |
Jean Chalard | 171d180 | 2012-04-23 18:51:00 +0900 | [diff] [blame] | 848 | const int unigramFreq = BinaryFormat::readFrequencyWithoutMovingPointer(DICT_ROOT, pos); |
satok | 6ad15fc | 2012-01-16 16:21:21 +0900 | [diff] [blame] | 849 | const int childrenAddressPos = BinaryFormat::skipFrequency(flags, pos); |
| 850 | const int attributesPos = BinaryFormat::skipChildrenPosition(flags, childrenAddressPos); |
| 851 | TerminalAttributes terminalAttributes(DICT_ROOT, flags, attributesPos); |
Jean Chalard | 8950ce6 | 2012-05-07 16:28:30 +0900 | [diff] [blame^] | 852 | // bigramMap contains the bigram frequencies indexed by addresses for fast lookup. |
| 853 | // bigramFilter is a bloom filter of said frequencies for even faster rejection. |
| 854 | const int probability = BinaryFormat::getProbability(bigramMap, bigramFilter, unigramFreq); |
Jean Chalard | 171d180 | 2012-04-23 18:51:00 +0900 | [diff] [blame] | 855 | onTerminal(probability, terminalAttributes, correction, queuePool, needsToInvokeOnTerminal, |
satok | 8330b48 | 2012-01-23 16:52:37 +0900 | [diff] [blame] | 856 | currentWordIndex); |
Jean Chalard | 1059f27 | 2011-06-28 20:45:05 +0900 | [diff] [blame] | 857 | |
satok | 8876b75 | 2011-08-04 18:31:57 +0900 | [diff] [blame] | 858 | // If there are more chars in this node, then this virtual node has children. |
| 859 | // If we are on the last char, this virtual node has children if this node has. |
| 860 | const bool hasChildren = BinaryFormat::hasChildrenInFlags(flags); |
| 861 | |
| 862 | // This character matched the typed character (enough to traverse the node at least) |
| 863 | // so we just evaluated it. Now we should evaluate this virtual node's children - that |
| 864 | // is, if it has any. If it has no children, we're done here - so we skip the end of |
| 865 | // the node, output the siblings position, and return false "don't traverse children". |
| 866 | // Note that !hasChildren implies isLastChar, so we know we don't have to skip any |
| 867 | // remaining char in this group for there can't be any. |
| 868 | if (!hasChildren) { |
| 869 | pos = BinaryFormat::skipFrequency(flags, pos); |
| 870 | *nextSiblingPosition = |
| 871 | BinaryFormat::skipChildrenPosAndAttributes(DICT_ROOT, flags, pos); |
| 872 | return false; |
| 873 | } |
| 874 | |
| 875 | // Optimization: Prune out words that are too long compared to how much was typed. |
satok | cfca3c6 | 2011-08-10 14:30:10 +0900 | [diff] [blame] | 876 | if (correction->needsToPrune()) { |
satok | 8876b75 | 2011-08-04 18:31:57 +0900 | [diff] [blame] | 877 | pos = BinaryFormat::skipFrequency(flags, pos); |
| 878 | *nextSiblingPosition = |
| 879 | BinaryFormat::skipChildrenPosAndAttributes(DICT_ROOT, flags, pos); |
satok | 10266c0 | 2011-08-19 22:05:59 +0900 | [diff] [blame] | 880 | if (DEBUG_DICT_FULL) { |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 881 | AKLOGI("Traversing was pruned."); |
satok | 10266c0 | 2011-08-19 22:05:59 +0900 | [diff] [blame] | 882 | } |
satok | 8876b75 | 2011-08-04 18:31:57 +0900 | [diff] [blame] | 883 | return false; |
| 884 | } |
| 885 | } |
Jean Chalard | 1059f27 | 2011-06-28 20:45:05 +0900 | [diff] [blame] | 886 | |
| 887 | // Now we finished processing this node, and we want to traverse children. If there are no |
| 888 | // children, we can't come here. |
| 889 | assert(BinaryFormat::hasChildrenInFlags(flags)); |
| 890 | |
| 891 | // If this node was a terminal it still has the frequency under the pointer (it may have been |
| 892 | // read, but not skipped - see readFrequencyWithoutMovingPointer). |
| 893 | // Next come the children position, then possibly attributes (attributes are bigrams only for |
| 894 | // now, maybe something related to shortcuts in the future). |
| 895 | // Once this is read, we still need to output the number of nodes in the immediate children of |
| 896 | // this node, so we read and output it before returning true, as in "please traverse children". |
| 897 | pos = BinaryFormat::skipFrequency(flags, pos); |
| 898 | int childrenPos = BinaryFormat::readChildrenPosition(DICT_ROOT, flags, pos); |
| 899 | *nextSiblingPosition = BinaryFormat::skipChildrenPosAndAttributes(DICT_ROOT, flags, pos); |
| 900 | *newCount = BinaryFormat::getGroupCountAndForwardPointer(DICT_ROOT, &childrenPos); |
| 901 | *newChildrenPosition = childrenPos; |
| 902 | return true; |
Jean Chalard | 85a1d1e | 2011-06-21 22:23:21 +0900 | [diff] [blame] | 903 | } |
| 904 | |
satok | 3008825 | 2010-12-01 21:22:15 +0900 | [diff] [blame] | 905 | } // namespace latinime |