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