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