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