blob: efcf512bb63c0edafcccf1afe6715995294721b9 [file] [log] [blame]
satok30088252010-12-01 21:22:15 +09001/*
Ken Wakasa5460ea32012-07-30 16:27:44 +09002 * Copyright (C) 2010, The Android Open Source Project
Ken Wakasa0bbb9172012-07-25 17:51:43 +09003 *
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 */
satok30088252010-12-01 21:22:15 +090016
Ken Wakasaf1008c52012-07-31 17:56:40 +090017#include <cassert>
18#include <cstring>
satok30088252010-12-01 21:22:15 +090019
satoke808e432010-12-02 14:53:24 +090020#define LOG_TAG "LatinIME: unigram_dictionary.cpp"
satok30088252010-12-01 21:22:15 +090021
satok30088252010-12-01 21:22:15 +090022#include "char_utils.h"
Ken Wakasa3b088a22012-05-16 23:05:32 +090023#include "defines.h"
satoke808e432010-12-02 14:53:24 +090024#include "dictionary.h"
25#include "unigram_dictionary.h"
satok30088252010-12-01 21:22:15 +090026
Jean Chalard1059f272011-06-28 20:45:05 +090027#include "binary_format.h"
Jean Chalardcf9dbbd2011-12-26 15:16:59 +090028#include "terminal_attributes.h"
Jean Chalard1059f272011-06-28 20:45:05 +090029
satok30088252010-12-01 21:22:15 +090030namespace latinime {
31
Jean Chalardc2bbc6a2011-02-25 17:56:53 +090032const UnigramDictionary::digraph_t UnigramDictionary::GERMAN_UMLAUT_DIGRAPHS[] =
Jean Chalardd3043382012-03-21 18:38:25 +090033 { { 'a', 'e', 0x00E4 }, // U+00E4 : LATIN SMALL LETTER A WITH DIAERESIS
34 { 'o', 'e', 0x00F6 }, // U+00F6 : LATIN SMALL LETTER O WITH DIAERESIS
35 { 'u', 'e', 0x00FC } }; // U+00FC : LATIN SMALL LETTER U WITH DIAERESIS
Jean Chalardc2bbc6a2011-02-25 17:56:53 +090036
Jean Chalardcc78d032012-03-23 16:48:49 +090037const UnigramDictionary::digraph_t UnigramDictionary::FRENCH_LIGATURES_DIGRAPHS[] =
38 { { 'a', 'e', 0x00E6 }, // U+00E6 : LATIN SMALL LETTER AE
39 { 'o', 'e', 0x0153 } }; // U+0153 : LATIN SMALL LIGATURE OE
40
Jean Chalard293ece02011-06-16 20:55:16 +090041// TODO: check the header
Ken Wakasa0bbb9172012-07-25 17:51:43 +090042UnigramDictionary::UnigramDictionary(const uint8_t *const streamStart, int typedLetterMultiplier,
Jean Chalardcd274b12012-04-06 18:26:00 +090043 int fullWordMultiplier, int maxWordLength, int maxWords, const unsigned int flags)
Jean Chalard46a1eec2012-02-27 19:48:47 +090044 : DICT_ROOT(streamStart), MAX_WORD_LENGTH(maxWordLength), MAX_WORDS(maxWords),
satok662fe692010-12-08 17:05:39 +090045 TYPED_LETTER_MULTIPLIER(typedLetterMultiplier), FULL_WORD_MULTIPLIER(fullWordMultiplier),
Jean Chalard1059f272011-06-28 20:45:05 +090046 // TODO : remove this variable.
47 ROOT_POS(0),
satok9df4a452012-03-23 16:05:18 +090048 BYTES_IN_ONE_CHAR(sizeof(int)),
Jean Chalardcd274b12012-04-06 18:26:00 +090049 MAX_DIGRAPH_SEARCH_DEPTH(DEFAULT_MAX_DIGRAPH_SEARCH_DEPTH), FLAGS(flags) {
Ken Wakasade3070a2011-03-19 09:16:42 +090050 if (DEBUG_DICT) {
satok9fb6f472012-01-13 18:01:22 +090051 AKLOGI("UnigramDictionary - constructor");
Ken Wakasade3070a2011-03-19 09:16:42 +090052 }
satok30088252010-12-01 21:22:15 +090053}
54
satok2df30602011-07-15 13:49:00 +090055UnigramDictionary::~UnigramDictionary() {
satok2df30602011-07-15 13:49:00 +090056}
satok30088252010-12-01 21:22:15 +090057
satok9df4a452012-03-23 16:05:18 +090058static inline unsigned int getCodesBufferSize(const int *codes, const int codesSize) {
59 return sizeof(*codes) * codesSize;
Jean Chalardc2bbc6a2011-02-25 17:56:53 +090060}
61
Ken Wakasa951ab9d2012-03-09 19:18:59 +090062// TODO: This needs to take a const unsigned short* and not tinker with its contents
satok1147c7b2011-12-14 15:04:58 +090063static inline void addWord(
64 unsigned short *word, int length, int frequency, WordsPriorityQueue *queue) {
65 queue->push(frequency, word, length);
66}
67
Jean Chalardd3043382012-03-21 18:38:25 +090068// Return the replacement code point for a digraph, or 0 if none.
69int UnigramDictionary::getDigraphReplacement(const int *codes, const int i, const int codesSize,
Ken Wakasa0bbb9172012-07-25 17:51:43 +090070 const digraph_t *const digraphs, const unsigned int digraphsSize) const {
Jean Chalardc2bbc6a2011-02-25 17:56:53 +090071
72 // There can't be a digraph if we don't have at least 2 characters to examine
73 if (i + 2 > codesSize) return false;
74
75 // Search for the first char of some digraph
76 int lastDigraphIndex = -1;
satok9df4a452012-03-23 16:05:18 +090077 const int thisChar = codes[i];
Jean Chalard6c300612012-03-06 19:54:03 +090078 for (lastDigraphIndex = digraphsSize - 1; lastDigraphIndex >= 0; --lastDigraphIndex) {
79 if (thisChar == digraphs[lastDigraphIndex].first) break;
Jean Chalardc2bbc6a2011-02-25 17:56:53 +090080 }
81 // No match: return early
Jean Chalardd3043382012-03-21 18:38:25 +090082 if (lastDigraphIndex < 0) return 0;
Jean Chalardc2bbc6a2011-02-25 17:56:53 +090083
84 // It's an interesting digraph if the second char matches too.
satok9df4a452012-03-23 16:05:18 +090085 if (digraphs[lastDigraphIndex].second == codes[i + 1]) {
Jean Chalardd3043382012-03-21 18:38:25 +090086 return digraphs[lastDigraphIndex].replacement;
87 } else {
88 return 0;
89 }
Jean Chalardc2bbc6a2011-02-25 17:56:53 +090090}
91
92// Mostly the same arguments as the non-recursive version, except:
93// codes is the original value. It points to the start of the work buffer, and gets passed as is.
94// codesSize is the size of the user input (thus, it is the size of codesSrc).
95// codesDest is the current point in the work buffer.
96// codesSrc is the current point in the user-input, original, content-unmodified buffer.
97// codesRemain is the remaining size in codesSrc.
satok1d7eaf82011-07-13 10:32:02 +090098void UnigramDictionary::getWordWithDigraphSuggestionsRec(ProximityInfo *proximityInfo,
satok1147c7b2011-12-14 15:04:58 +090099 const int *xcoordinates, const int *ycoordinates, const int *codesBuffer,
satok219a5142012-03-08 11:53:18 +0900100 int *xCoordinatesBuffer, int *yCoordinatesBuffer,
Jean Chalard8950ce62012-05-07 16:28:30 +0900101 const int codesBufferSize, const std::map<int, int> *bigramMap, const uint8_t *bigramFilter,
Jean Chalard4d9b2022012-04-23 19:25:28 +0900102 const bool useFullEditDistance, const int *codesSrc,
satok1147c7b2011-12-14 15:04:58 +0900103 const int codesRemain, const int currentDepth, int *codesDest, Correction *correction,
Jean Chalard6c300612012-03-06 19:54:03 +0900104 WordsPriorityQueuePool *queuePool,
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900105 const digraph_t *const digraphs, const unsigned int digraphsSize) const {
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900106
satok9df4a452012-03-23 16:05:18 +0900107 const int startIndex = codesDest - codesBuffer;
Jean Chalard6c300612012-03-06 19:54:03 +0900108 if (currentDepth < MAX_DIGRAPH_SEARCH_DEPTH) {
Jean Chalarda787dba2011-03-04 12:17:48 +0900109 for (int i = 0; i < codesRemain; ++i) {
satok219a5142012-03-08 11:53:18 +0900110 xCoordinatesBuffer[startIndex + i] = xcoordinates[codesBufferSize - codesRemain + i];
111 yCoordinatesBuffer[startIndex + i] = ycoordinates[codesBufferSize - codesRemain + i];
Jean Chalardd3043382012-03-21 18:38:25 +0900112 const int replacementCodePoint =
113 getDigraphReplacement(codesSrc, i, codesRemain, digraphs, digraphsSize);
114 if (0 != replacementCodePoint) {
Jean Chalarda787dba2011-03-04 12:17:48 +0900115 // Found a digraph. We will try both spellings. eg. the word is "pruefen"
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900116
Jean Chalardd3043382012-03-21 18:38:25 +0900117 // 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 Chalarda787dba2011-03-04 12:17:48 +0900123 // Make i the index of the second char of the digraph for simplicity. Forgetting
124 // to do that results in an infinite recursion so take care!
125 ++i;
126 memcpy(codesDest, codesSrc, i * BYTES_IN_ONE_CHAR);
Jean Chalardd3043382012-03-21 18:38:25 +0900127 codesDest[(i - 1) * (BYTES_IN_ONE_CHAR / sizeof(codesDest[0]))] =
128 replacementCodePoint;
Jean Chalarda787dba2011-03-04 12:17:48 +0900129 getWordWithDigraphSuggestionsRec(proximityInfo, xcoordinates, ycoordinates,
Jean Chalard338d3ec2012-04-06 19:28:34 +0900130 codesBuffer, xCoordinatesBuffer, yCoordinatesBuffer, codesBufferSize,
Jean Chalard8950ce62012-05-07 16:28:30 +0900131 bigramMap, bigramFilter, useFullEditDistance, codesSrc + i + 1,
Jean Chalard4d9b2022012-04-23 19:25:28 +0900132 codesRemain - i - 1, currentDepth + 1, codesDest + i, correction,
Jean Chalard6c300612012-03-06 19:54:03 +0900133 queuePool, digraphs, digraphsSize);
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900134
Jean Chalarda787dba2011-03-04 12:17:48 +0900135 // Copy the second char of the digraph in place, then continue processing on
136 // the remaining part of the word.
137 // In our example, after "pru" in the buffer copy the "e", and continue on "fen"
satok9df4a452012-03-23 16:05:18 +0900138 memcpy(codesDest + i, codesSrc + i, BYTES_IN_ONE_CHAR);
Jean Chalarda787dba2011-03-04 12:17:48 +0900139 getWordWithDigraphSuggestionsRec(proximityInfo, xcoordinates, ycoordinates,
Jean Chalard338d3ec2012-04-06 19:28:34 +0900140 codesBuffer, xCoordinatesBuffer, yCoordinatesBuffer, codesBufferSize,
Jean Chalard8950ce62012-05-07 16:28:30 +0900141 bigramMap, bigramFilter, useFullEditDistance, codesSrc + i, codesRemain - i,
Jean Chalard4d9b2022012-04-23 19:25:28 +0900142 currentDepth + 1, codesDest + i, correction, queuePool, digraphs,
143 digraphsSize);
Jean Chalarda787dba2011-03-04 12:17:48 +0900144 return;
145 }
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900146 }
147 }
148
149 // If we come here, we hit the end of the word: let's check it against the dictionary.
150 // In our example, we'll come here once for "prufen" and then once for "pruefen".
151 // If the word contains several digraphs, we'll come it for the product of them.
152 // eg. if the word is "ueberpruefen" we'll test, in order, against
153 // "uberprufen", "uberpruefen", "ueberprufen", "ueberpruefen".
154 const unsigned int remainingBytes = BYTES_IN_ONE_CHAR * codesRemain;
satok219a5142012-03-08 11:53:18 +0900155 if (0 != remainingBytes) {
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900156 memcpy(codesDest, codesSrc, remainingBytes);
satok219a5142012-03-08 11:53:18 +0900157 memcpy(&xCoordinatesBuffer[startIndex], &xcoordinates[codesBufferSize - codesRemain],
158 sizeof(int) * codesRemain);
159 memcpy(&yCoordinatesBuffer[startIndex], &ycoordinates[codesBufferSize - codesRemain],
160 sizeof(int) * codesRemain);
161 }
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900162
satok219a5142012-03-08 11:53:18 +0900163 getWordSuggestions(proximityInfo, xCoordinatesBuffer, yCoordinatesBuffer, codesBuffer,
Jean Chalard8950ce62012-05-07 16:28:30 +0900164 startIndex + codesRemain, bigramMap, bigramFilter, useFullEditDistance, correction,
satoka7e5a5a2011-12-15 16:49:12 +0900165 queuePool);
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900166}
167
Jean Chalard8950ce62012-05-07 16:28:30 +0900168// bigramMap contains the association <bigram address> -> <bigram frequency>
169// bigramFilter is a bloom filter for fast rejection: see functions setInFilter and isInFilter
170// in bigram_dictionary.cpp
satoka7e5a5a2011-12-15 16:49:12 +0900171int UnigramDictionary::getSuggestions(ProximityInfo *proximityInfo,
satokb1ed1d42012-06-14 16:35:23 -0700172 const int *xcoordinates,
Jean Chalard338d3ec2012-04-06 19:28:34 +0900173 const int *ycoordinates, const int *codes, const int codesSize,
Jean Chalard8950ce62012-05-07 16:28:30 +0900174 const std::map<int, int> *bigramMap, const uint8_t *bigramFilter,
Jean Chalard6931df92012-07-12 12:55:48 +0900175 const bool useFullEditDistance, unsigned short *outWords, int *frequencies,
176 int *outputTypes) const {
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900177
satokb1ed1d42012-06-14 16:35:23 -0700178 WordsPriorityQueuePool queuePool(MAX_WORDS, SUB_QUEUE_MAX_WORDS, MAX_WORD_LENGTH);
179 queuePool.clearAll();
satok1bc038c2012-06-14 11:25:50 -0700180 Correction masterCorrection;
181 masterCorrection.resetCorrection();
Jean Chalardaa8df592012-04-06 18:54:20 +0900182 if (BinaryFormat::REQUIRES_GERMAN_UMLAUT_PROCESSING & FLAGS)
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900183 { // Incrementally tune the word and try all possibilities
satok9df4a452012-03-23 16:05:18 +0900184 int codesBuffer[getCodesBufferSize(codes, codesSize)];
satok219a5142012-03-08 11:53:18 +0900185 int xCoordinatesBuffer[codesSize];
186 int yCoordinatesBuffer[codesSize];
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900187 getWordWithDigraphSuggestionsRec(proximityInfo, xcoordinates, ycoordinates, codesBuffer,
Jean Chalard8950ce62012-05-07 16:28:30 +0900188 xCoordinatesBuffer, yCoordinatesBuffer, codesSize, bigramMap, bigramFilter,
satok1bc038c2012-06-14 11:25:50 -0700189 useFullEditDistance, codes, codesSize, 0, codesBuffer, &masterCorrection,
satokb1ed1d42012-06-14 16:35:23 -0700190 &queuePool, GERMAN_UMLAUT_DIGRAPHS,
Jean Chalard6c300612012-03-06 19:54:03 +0900191 sizeof(GERMAN_UMLAUT_DIGRAPHS) / sizeof(GERMAN_UMLAUT_DIGRAPHS[0]));
Jean Chalardaa8df592012-04-06 18:54:20 +0900192 } else if (BinaryFormat::REQUIRES_FRENCH_LIGATURES_PROCESSING & FLAGS) {
satokacb6c542012-03-23 20:58:18 +0900193 int codesBuffer[getCodesBufferSize(codes, codesSize)];
Jean Chalardcc78d032012-03-23 16:48:49 +0900194 int xCoordinatesBuffer[codesSize];
195 int yCoordinatesBuffer[codesSize];
196 getWordWithDigraphSuggestionsRec(proximityInfo, xcoordinates, ycoordinates, codesBuffer,
Jean Chalard8950ce62012-05-07 16:28:30 +0900197 xCoordinatesBuffer, yCoordinatesBuffer, codesSize, bigramMap, bigramFilter,
satok1bc038c2012-06-14 11:25:50 -0700198 useFullEditDistance, codes, codesSize, 0, codesBuffer, &masterCorrection,
satokb1ed1d42012-06-14 16:35:23 -0700199 &queuePool, FRENCH_LIGATURES_DIGRAPHS,
Jean Chalardcc78d032012-03-23 16:48:49 +0900200 sizeof(FRENCH_LIGATURES_DIGRAPHS) / sizeof(FRENCH_LIGATURES_DIGRAPHS[0]));
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900201 } else { // Normal processing
Jean Chalard338d3ec2012-04-06 19:28:34 +0900202 getWordSuggestions(proximityInfo, xcoordinates, ycoordinates, codes, codesSize,
satokb1ed1d42012-06-14 16:35:23 -0700203 bigramMap, bigramFilter, useFullEditDistance, &masterCorrection, &queuePool);
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900204 }
205
satok817e5172011-03-04 06:06:45 -0800206 PROF_START(20);
satok8330b482012-01-23 16:52:37 +0900207 if (DEBUG_DICT) {
satokb1ed1d42012-06-14 16:35:23 -0700208 float ns = queuePool.getMasterQueue()->getHighestNormalizedScore(
satok1bc038c2012-06-14 11:25:50 -0700209 masterCorrection.getPrimaryInputWord(), codesSize, 0, 0, 0);
satok8330b482012-01-23 16:52:37 +0900210 ns += 0;
211 AKLOGI("Max normalized score = %f", ns);
212 }
satoka7e5a5a2011-12-15 16:49:12 +0900213 const int suggestedWordsCount =
satokb1ed1d42012-06-14 16:35:23 -0700214 queuePool.getMasterQueue()->outputSuggestions(
satok1bc038c2012-06-14 11:25:50 -0700215 masterCorrection.getPrimaryInputWord(), codesSize, frequencies, outWords);
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900216
217 if (DEBUG_DICT) {
satokb1ed1d42012-06-14 16:35:23 -0700218 float ns = queuePool.getMasterQueue()->getHighestNormalizedScore(
satok1bc038c2012-06-14 11:25:50 -0700219 masterCorrection.getPrimaryInputWord(), codesSize, 0, 0, 0);
satok8330b482012-01-23 16:52:37 +0900220 ns += 0;
satok9fb6f472012-01-13 18:01:22 +0900221 AKLOGI("Returning %d words", suggestedWordsCount);
Jean Chalard980d6b62011-06-30 17:02:23 +0900222 /// Print the returned words
223 for (int j = 0; j < suggestedWordsCount; ++j) {
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900224 short unsigned int *w = outWords + j * MAX_WORD_LENGTH;
Jean Chalard980d6b62011-06-30 17:02:23 +0900225 char s[MAX_WORD_LENGTH];
226 for (int i = 0; i <= MAX_WORD_LENGTH; i++) s[i] = w[i];
Jean-Baptiste Querucd7c4132012-05-16 16:56:11 -0700227 (void)s;
satok9fb6f472012-01-13 18:01:22 +0900228 AKLOGI("%s %i", s, frequencies[j]);
Jean Chalard980d6b62011-06-30 17:02:23 +0900229 }
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900230 }
satok817e5172011-03-04 06:06:45 -0800231 PROF_END(20);
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900232 PROF_CLOSE;
233 return suggestedWordsCount;
234}
235
satok1d7eaf82011-07-13 10:32:02 +0900236void UnigramDictionary::getWordSuggestions(ProximityInfo *proximityInfo,
satok1147c7b2011-12-14 15:04:58 +0900237 const int *xcoordinates, const int *ycoordinates, const int *codes,
Jean Chalard8950ce62012-05-07 16:28:30 +0900238 const int inputLength, const std::map<int, int> *bigramMap, const uint8_t *bigramFilter,
Satoshi Kataoka6bc051d2012-06-08 19:52:19 +0900239 const bool useFullEditDistance, Correction *correction,
240 WordsPriorityQueuePool *queuePool) const {
Jean Chalardc2bbc6a2011-02-25 17:56:53 +0900241
satok61e2f852011-01-05 14:13:07 +0900242 PROF_OPEN;
243 PROF_START(0);
satok61e2f852011-01-05 14:13:07 +0900244 PROF_END(0);
satok30088252010-12-01 21:22:15 +0900245
satok61e2f852011-01-05 14:13:07 +0900246 PROF_START(1);
Jean Chalard8950ce62012-05-07 16:28:30 +0900247 getOneWordSuggestions(proximityInfo, xcoordinates, ycoordinates, codes, bigramMap, bigramFilter,
Jean Chalard4d9b2022012-04-23 19:25:28 +0900248 useFullEditDistance, inputLength, correction, queuePool);
satok61e2f852011-01-05 14:13:07 +0900249 PROF_END(1);
250
251 PROF_START(2);
satok10266c02011-08-19 22:05:59 +0900252 // Note: This line is intentionally left blank
satok61e2f852011-01-05 14:13:07 +0900253 PROF_END(2);
satokcdbbea72010-12-08 16:04:16 +0900254
satok61e2f852011-01-05 14:13:07 +0900255 PROF_START(3);
satok10266c02011-08-19 22:05:59 +0900256 // Note: This line is intentionally left blank
satok61e2f852011-01-05 14:13:07 +0900257 PROF_END(3);
satok30088252010-12-01 21:22:15 +0900258
satok61e2f852011-01-05 14:13:07 +0900259 PROF_START(4);
satok8330b482012-01-23 16:52:37 +0900260 bool hasAutoCorrectionCandidate = false;
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900261 WordsPriorityQueue *masterQueue = queuePool->getMasterQueue();
satok8330b482012-01-23 16:52:37 +0900262 if (masterQueue->size() > 0) {
satok0028ed32012-05-16 20:42:12 +0900263 float nsForMaster = masterQueue->getHighestNormalizedScore(
Satoshi Kataoka4a3db702012-06-08 15:29:44 +0900264 correction->getPrimaryInputWord(), inputLength, 0, 0, 0);
satok8330b482012-01-23 16:52:37 +0900265 hasAutoCorrectionCandidate = (nsForMaster > START_TWO_WORDS_CORRECTION_THRESHOLD);
266 }
satok61e2f852011-01-05 14:13:07 +0900267 PROF_END(4);
satoka3d78f62010-12-09 22:08:33 +0900268
satok61e2f852011-01-05 14:13:07 +0900269 PROF_START(5);
satok99557162012-01-26 22:49:13 +0900270 // Multiple word suggestions
271 if (SUGGEST_MULTIPLE_WORDS
272 && inputLength >= MIN_USER_TYPED_LENGTH_FOR_MULTIPLE_WORD_SUGGESTION) {
satoka85f4922012-01-30 18:18:30 +0900273 getSplitMultipleWordsSuggestions(proximityInfo, xcoordinates, ycoordinates, codes,
satok1f6b52e2012-01-30 13:53:58 +0900274 useFullEditDistance, inputLength, correction, queuePool,
275 hasAutoCorrectionCandidate);
satok662fe692010-12-08 17:05:39 +0900276 }
satok61e2f852011-01-05 14:13:07 +0900277 PROF_END(5);
satok817e5172011-03-04 06:06:45 -0800278
279 PROF_START(6);
satok99557162012-01-26 22:49:13 +0900280 // Note: This line is intentionally left blank
satok817e5172011-03-04 06:06:45 -0800281 PROF_END(6);
satok99557162012-01-26 22:49:13 +0900282
satok29dc8062012-01-17 15:59:15 +0900283 if (DEBUG_DICT) {
satok6ad15fc2012-01-16 16:21:21 +0900284 queuePool->dumpSubQueue1TopSuggestions();
satok29dc8062012-01-17 15:59:15 +0900285 for (int i = 0; i < SUB_QUEUE_MAX_COUNT; ++i) {
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900286 WordsPriorityQueue *queue = queuePool->getSubQueue(FIRST_WORD_INDEX, i);
satok29dc8062012-01-17 15:59:15 +0900287 if (queue->size() > 0) {
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900288 WordsPriorityQueue::SuggestedWord *sw = queue->top();
satok29dc8062012-01-17 15:59:15 +0900289 const int score = sw->mScore;
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900290 const unsigned short *word = sw->mWord;
satok29dc8062012-01-17 15:59:15 +0900291 const int wordLength = sw->mWordLength;
satok0028ed32012-05-16 20:42:12 +0900292 float ns = Correction::RankingAlgorithm::calcNormalizedScore(
Satoshi Kataoka4a3db702012-06-08 15:29:44 +0900293 correction->getPrimaryInputWord(), i, word, wordLength, score);
satok29dc8062012-01-17 15:59:15 +0900294 ns += 0;
295 AKLOGI("--- TOP SUB WORDS for %d --- %d %f [%d]", i, score, ns,
satok54af64a2012-01-17 15:58:23 +0900296 (ns > TWO_WORDS_CORRECTION_WITH_OTHER_ERROR_THRESHOLD));
Satoshi Kataoka4a3db702012-06-08 15:29:44 +0900297 DUMP_WORD(correction->getPrimaryInputWord(), i);
satok29dc8062012-01-17 15:59:15 +0900298 DUMP_WORD(word, wordLength);
299 }
300 }
satok6ad15fc2012-01-16 16:21:21 +0900301 }
satok30088252010-12-01 21:22:15 +0900302}
303
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900304void UnigramDictionary::initSuggestions(ProximityInfo *proximityInfo, const int *xCoordinates,
Satoshi Kataoka6bc051d2012-06-08 19:52:19 +0900305 const int *yCoordinates, const int *codes, const int inputLength,
306 Correction *correction) const {
Ken Wakasade3070a2011-03-19 09:16:42 +0900307 if (DEBUG_DICT) {
satok9fb6f472012-01-13 18:01:22 +0900308 AKLOGI("initSuggest");
Satoshi Kataoka6cbe2042012-05-30 17:28:34 +0900309 DUMP_WORD_INT(codes, inputLength);
Ken Wakasade3070a2011-03-19 09:16:42 +0900310 }
Satoshi Kataoka4a3db702012-06-08 15:29:44 +0900311 correction->initInputParams(proximityInfo, codes, inputLength, xCoordinates, yCoordinates);
satok1a6da632011-12-16 23:15:06 +0900312 const int maxDepth = min(inputLength * MAX_DEPTH_MULTIPLIER, MAX_WORD_LENGTH);
313 correction->initCorrection(proximityInfo, inputLength, maxDepth);
satok30088252010-12-01 21:22:15 +0900314}
315
satok715514d2010-12-02 20:19:59 +0900316static const char QUOTE = '\'';
satok662fe692010-12-08 17:05:39 +0900317static const char SPACE = ' ';
satok30088252010-12-01 21:22:15 +0900318
satok744dab62011-12-15 22:29:05 +0900319void UnigramDictionary::getOneWordSuggestions(ProximityInfo *proximityInfo,
320 const int *xcoordinates, const int *ycoordinates, const int *codes,
Jean Chalard8950ce62012-05-07 16:28:30 +0900321 const std::map<int, int> *bigramMap, const uint8_t *bigramFilter,
322 const bool useFullEditDistance, const int inputLength,
Satoshi Kataoka6bc051d2012-06-08 19:52:19 +0900323 Correction *correction, WordsPriorityQueuePool *queuePool) const {
satok6ad15fc2012-01-16 16:21:21 +0900324 initSuggestions(proximityInfo, xcoordinates, ycoordinates, codes, inputLength, correction);
Jean Chalard8950ce62012-05-07 16:28:30 +0900325 getSuggestionCandidates(useFullEditDistance, inputLength, bigramMap, bigramFilter, correction,
Jean Chalard4d9b2022012-04-23 19:25:28 +0900326 queuePool, true /* doAutoCompletion */, DEFAULT_MAX_ERRORS, FIRST_WORD_INDEX);
satok744dab62011-12-15 22:29:05 +0900327}
328
satok1147c7b2011-12-14 15:04:58 +0900329void UnigramDictionary::getSuggestionCandidates(const bool useFullEditDistance,
Jean Chalard8950ce62012-05-07 16:28:30 +0900330 const int inputLength, const std::map<int, int> *bigramMap, const uint8_t *bigramFilter,
Jean Chalard4d9b2022012-04-23 19:25:28 +0900331 Correction *correction, WordsPriorityQueuePool *queuePool,
Satoshi Kataoka6bc051d2012-06-08 19:52:19 +0900332 const bool doAutoCompletion, const int maxErrors, const int currentWordIndex) const {
Satoshi Kataoka6cbe2042012-05-30 17:28:34 +0900333 uint8_t totalTraverseCount = correction->pushAndGetTotalTraverseCount();
334 if (DEBUG_DICT) {
335 AKLOGI("Traverse count %d", totalTraverseCount);
336 }
337 if (totalTraverseCount > MULTIPLE_WORDS_SUGGESTION_MAX_TOTAL_TRAVERSE_COUNT) {
338 if (DEBUG_DICT) {
339 AKLOGI("Abort traversing %d", totalTraverseCount);
340 }
341 return;
342 }
satok10266c02011-08-19 22:05:59 +0900343 // TODO: Remove setCorrectionParams
satok1147c7b2011-12-14 15:04:58 +0900344 correction->setCorrectionParams(0, 0, 0,
satokd03317c2011-12-14 21:38:11 +0900345 -1 /* spaceProximityPos */, -1 /* missingSpacePos */, useFullEditDistance,
satok1a6da632011-12-16 23:15:06 +0900346 doAutoCompletion, maxErrors);
satok662fe692010-12-08 17:05:39 +0900347 int rootPosition = ROOT_POS;
Jean Chalard980d6b62011-06-30 17:02:23 +0900348 // Get the number of children of root, then increment the position
Jean Chalard6d419812012-01-16 15:19:47 +0900349 int childCount = BinaryFormat::getGroupCountAndForwardPointer(DICT_ROOT, &rootPosition);
satok208268d2011-08-10 15:44:08 +0900350 int outputIndex = 0;
satokd2997922010-12-07 13:08:39 +0900351
satok1147c7b2011-12-14 15:04:58 +0900352 correction->initCorrectionState(rootPosition, childCount, (inputLength <= 0));
satokd2997922010-12-07 13:08:39 +0900353
satok662fe692010-12-08 17:05:39 +0900354 // Depth first search
satok208268d2011-08-10 15:44:08 +0900355 while (outputIndex >= 0) {
satok1147c7b2011-12-14 15:04:58 +0900356 if (correction->initProcessState(outputIndex)) {
357 int siblingPos = correction->getTreeSiblingPos(outputIndex);
satokd2997922010-12-07 13:08:39 +0900358 int firstChildPos;
satok0f6c8e82011-08-03 02:19:44 +0900359
satok4e4e74e2011-08-03 23:27:32 +0900360 const bool needsToTraverseChildrenNodes = processCurrentNode(siblingPos,
Jean Chalard8950ce62012-05-07 16:28:30 +0900361 bigramMap, bigramFilter, correction, &childCount, &firstChildPos, &siblingPos,
Jean Chalard4d9b2022012-04-23 19:25:28 +0900362 queuePool, currentWordIndex);
satok662fe692010-12-08 17:05:39 +0900363 // Update next sibling pos
satok1147c7b2011-12-14 15:04:58 +0900364 correction->setTreeSiblingPos(outputIndex, siblingPos);
satok208268d2011-08-10 15:44:08 +0900365
satokd2997922010-12-07 13:08:39 +0900366 if (needsToTraverseChildrenNodes) {
367 // Goes to child node
satok1147c7b2011-12-14 15:04:58 +0900368 outputIndex = correction->goDownTree(outputIndex, childCount, firstChildPos);
satokd2997922010-12-07 13:08:39 +0900369 }
370 } else {
satokcdbbea72010-12-08 16:04:16 +0900371 // Goes to parent sibling node
satok1147c7b2011-12-14 15:04:58 +0900372 outputIndex = correction->getTreeParentIndex(outputIndex);
satokd2997922010-12-07 13:08:39 +0900373 }
374 }
375}
376
Jean Chalard171d1802012-04-23 18:51:00 +0900377inline void UnigramDictionary::onTerminal(const int probability,
Jean Chalardcf9dbbd2011-12-26 15:16:59 +0900378 const TerminalAttributes& terminalAttributes, Correction *correction,
satok8330b482012-01-23 16:52:37 +0900379 WordsPriorityQueuePool *queuePool, const bool addToMasterQueue,
Satoshi Kataoka6bc051d2012-06-08 19:52:19 +0900380 const int currentWordIndex) const {
satok6ad15fc2012-01-16 16:21:21 +0900381 const int inputIndex = correction->getInputIndex();
382 const bool addToSubQueue = inputIndex < SUB_QUEUE_MAX_COUNT;
satok54af64a2012-01-17 15:58:23 +0900383
satok8876b752011-08-04 18:31:57 +0900384 int wordLength;
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900385 unsigned short *wordPointer;
satok54af64a2012-01-17 15:58:23 +0900386
satok1f6b52e2012-01-30 13:53:58 +0900387 if ((currentWordIndex == FIRST_WORD_INDEX) && addToMasterQueue) {
satok54af64a2012-01-17 15:58:23 +0900388 WordsPriorityQueue *masterQueue = queuePool->getMasterQueue();
Jean Chalard171d1802012-04-23 18:51:00 +0900389 const int finalProbability =
390 correction->getFinalProbability(probability, &wordPointer, &wordLength);
391 if (finalProbability != NOT_A_PROBABILITY) {
392 addWord(wordPointer, wordLength, finalProbability, masterQueue);
satok54af64a2012-01-17 15:58:23 +0900393
Jean Chalard171d1802012-04-23 18:51:00 +0900394 const int shortcutProbability = finalProbability > 0 ? finalProbability - 1 : 0;
satok54af64a2012-01-17 15:58:23 +0900395 // Please note that the shortcut candidates will be added to the master queue only.
396 TerminalAttributes::ShortcutIterator iterator =
397 terminalAttributes.getShortcutIterator();
398 while (iterator.hasNextShortcutTarget()) {
399 // TODO: addWord only supports weak ordering, meaning we have no means
400 // to control the order of the shortcuts relative to one another or to the word.
Jean Chalard171d1802012-04-23 18:51:00 +0900401 // We need to either modulate the probability of each shortcut according
402 // to its own shortcut probability or to make the queue
satok54af64a2012-01-17 15:58:23 +0900403 // so that the insert order is protected inside the queue for words
Jean Chalard9a933a72012-03-27 19:56:23 +0900404 // with the same score. For the moment we use -1 to make sure the shortcut will
405 // never be in front of the word.
satok54af64a2012-01-17 15:58:23 +0900406 uint16_t shortcutTarget[MAX_WORD_LENGTH_INTERNAL];
407 const int shortcutTargetStringLength = iterator.getNextShortcutTarget(
408 MAX_WORD_LENGTH_INTERNAL, shortcutTarget);
Jean Chalard171d1802012-04-23 18:51:00 +0900409 addWord(shortcutTarget, shortcutTargetStringLength, shortcutProbability,
410 masterQueue);
satok6ad15fc2012-01-16 16:21:21 +0900411 }
Jean Chalardcf9dbbd2011-12-26 15:16:59 +0900412 }
satok54af64a2012-01-17 15:58:23 +0900413 }
satok6ad15fc2012-01-16 16:21:21 +0900414
satok54af64a2012-01-17 15:58:23 +0900415 // We only allow two words + other error correction for words with SUB_QUEUE_MIN_WORD_LENGTH
416 // or more length.
417 if (inputIndex >= SUB_QUEUE_MIN_WORD_LENGTH && addToSubQueue) {
satok8330b482012-01-23 16:52:37 +0900418 WordsPriorityQueue *subQueue;
satok7409d152012-01-26 16:13:25 +0900419 subQueue = queuePool->getSubQueue(currentWordIndex, inputIndex);
420 if (!subQueue) {
satok8330b482012-01-23 16:52:37 +0900421 return;
422 }
Jean Chalard171d1802012-04-23 18:51:00 +0900423 const int finalProbability = correction->getFinalProbabilityForSubQueue(
424 probability, &wordPointer, &wordLength, inputIndex);
425 addWord(wordPointer, wordLength, finalProbability, subQueue);
Jean Chalardca5ef282011-06-17 15:36:26 +0900426 }
427}
428
Satoshi Kataoka6cbe2042012-05-30 17:28:34 +0900429int UnigramDictionary::getSubStringSuggestion(
satok7409d152012-01-26 16:13:25 +0900430 ProximityInfo *proximityInfo, const int *xcoordinates, const int *ycoordinates,
satok3c09bb12012-01-26 18:36:19 +0900431 const int *codes, const bool useFullEditDistance, Correction *correction,
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900432 WordsPriorityQueuePool *queuePool, const int inputLength,
satok3c09bb12012-01-26 18:36:19 +0900433 const bool hasAutoCorrectionCandidate, const int currentWordIndex,
434 const int inputWordStartPos, const int inputWordLength,
satok99557162012-01-26 22:49:13 +0900435 const int outputWordStartPos, const bool isSpaceProximity, int *freqArray,
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900436 int*wordLengthArray, unsigned short *outputWord, int *outputWordLength) const {
Satoshi Kataoka67e3cc82012-05-31 15:04:58 +0900437 if (inputWordLength > MULTIPLE_WORDS_SUGGESTION_MAX_WORD_LENGTH) {
438 return FLAG_MULTIPLE_SUGGEST_ABORT;
439 }
440
441 /////////////////////////////////////////////
442 // safety net for multiple word suggestion //
443 // TODO: Remove this safety net //
444 /////////////////////////////////////////////
445 int smallWordCount = 0;
446 int singleLetterWordCount = 0;
447 if (inputWordLength == 1) {
448 ++singleLetterWordCount;
449 }
450 if (inputWordLength <= 2) {
451 // small word == single letter or 2-letter word
452 ++smallWordCount;
453 }
454 for (int i = 0; i < currentWordIndex; ++i) {
455 const int length = wordLengthArray[i];
456 if (length == 1) {
457 ++singleLetterWordCount;
458 // Safety net to avoid suggesting sequential single letter words
459 if (i < (currentWordIndex - 1)) {
460 if (wordLengthArray[i + 1] == 1) {
461 return FLAG_MULTIPLE_SUGGEST_ABORT;
462 }
463 } else if (inputWordLength == 1) {
464 return FLAG_MULTIPLE_SUGGEST_ABORT;
465 }
466 }
467 if (length <= 2) {
468 ++smallWordCount;
469 }
470 // Safety net to avoid suggesting multiple words with many (4 or more, for now) small words
471 if (singleLetterWordCount >= 3 || smallWordCount >= 4) {
472 return FLAG_MULTIPLE_SUGGEST_ABORT;
473 }
474 }
475 //////////////////////////////////////////////
476 // TODO: Remove the safety net above //
477 //////////////////////////////////////////////
478
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900479 unsigned short *tempOutputWord = 0;
satok1f6b52e2012-01-30 13:53:58 +0900480 int nextWordLength = 0;
satok99557162012-01-26 22:49:13 +0900481 // TODO: Optimize init suggestion
482 initSuggestions(proximityInfo, xcoordinates, ycoordinates, codes,
483 inputLength, correction);
484
Satoshi Kataoka6bc051d2012-06-08 19:52:19 +0900485 unsigned short word[MAX_WORD_LENGTH_INTERNAL];
satok3c09bb12012-01-26 18:36:19 +0900486 int freq = getMostFrequentWordLike(
Satoshi Kataoka6bc051d2012-06-08 19:52:19 +0900487 inputWordStartPos, inputWordLength, correction, word);
satok3c09bb12012-01-26 18:36:19 +0900488 if (freq > 0) {
satok1f6b52e2012-01-30 13:53:58 +0900489 nextWordLength = inputWordLength;
Satoshi Kataoka6bc051d2012-06-08 19:52:19 +0900490 tempOutputWord = word;
satok3c09bb12012-01-26 18:36:19 +0900491 } else if (!hasAutoCorrectionCandidate) {
492 if (inputWordStartPos > 0) {
493 const int offset = inputWordStartPos;
494 initSuggestions(proximityInfo, &xcoordinates[offset], &ycoordinates[offset],
satok9df4a452012-03-23 16:05:18 +0900495 codes + offset, inputWordLength, correction);
satok3c09bb12012-01-26 18:36:19 +0900496 queuePool->clearSubQueue(currentWordIndex);
Jean Chalard4d9b2022012-04-23 19:25:28 +0900497 // TODO: pass the bigram list for substring suggestion
498 getSuggestionCandidates(useFullEditDistance, inputWordLength,
Jean Chalard8950ce62012-05-07 16:28:30 +0900499 0 /* bigramMap */, 0 /* bigramFilter */, correction, queuePool,
500 false /* doAutoCompletion */, MAX_ERRORS_FOR_TWO_WORDS, currentWordIndex);
satok3c09bb12012-01-26 18:36:19 +0900501 if (DEBUG_DICT) {
satok1f6b52e2012-01-30 13:53:58 +0900502 if (currentWordIndex < MULTIPLE_WORDS_SUGGESTION_MAX_WORDS) {
satok3c09bb12012-01-26 18:36:19 +0900503 AKLOGI("Dump word candidates(%d) %d", currentWordIndex, inputWordLength);
504 for (int i = 0; i < SUB_QUEUE_MAX_COUNT; ++i) {
505 queuePool->getSubQueue(currentWordIndex, i)->dumpTopWord();
506 }
507 }
508 }
509 }
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900510 WordsPriorityQueue *queue = queuePool->getSubQueue(currentWordIndex, inputWordLength);
Satoshi Kataoka6cbe2042012-05-30 17:28:34 +0900511 // TODO: Return the correct value depending on doAutoCompletion
512 if (!queue || queue->size() <= 0) {
513 return FLAG_MULTIPLE_SUGGEST_ABORT;
satok3c09bb12012-01-26 18:36:19 +0900514 }
515 int score = 0;
satok0028ed32012-05-16 20:42:12 +0900516 const float ns = queue->getHighestNormalizedScore(
Satoshi Kataoka4a3db702012-06-08 15:29:44 +0900517 correction->getPrimaryInputWord(), inputWordLength,
satok1f6b52e2012-01-30 13:53:58 +0900518 &tempOutputWord, &score, &nextWordLength);
satok3c09bb12012-01-26 18:36:19 +0900519 if (DEBUG_DICT) {
520 AKLOGI("NS(%d) = %f, Score = %d", currentWordIndex, ns, score);
521 }
522 // Two words correction won't be done if the score of the first word doesn't exceed the
523 // threshold.
524 if (ns < TWO_WORDS_CORRECTION_WITH_OTHER_ERROR_THRESHOLD
satok1f6b52e2012-01-30 13:53:58 +0900525 || nextWordLength < SUB_QUEUE_MIN_WORD_LENGTH) {
Satoshi Kataoka6cbe2042012-05-30 17:28:34 +0900526 return FLAG_MULTIPLE_SUGGEST_SKIP;
satok3c09bb12012-01-26 18:36:19 +0900527 }
satok1f6b52e2012-01-30 13:53:58 +0900528 freq = score >> (nextWordLength + TWO_WORDS_PLUS_OTHER_ERROR_CORRECTION_DEMOTION_DIVIDER);
satok3c09bb12012-01-26 18:36:19 +0900529 }
530 if (DEBUG_DICT) {
satok1f6b52e2012-01-30 13:53:58 +0900531 AKLOGI("Freq(%d): %d, length: %d, input length: %d, input start: %d (%d)"
532 , currentWordIndex, freq, nextWordLength, inputWordLength, inputWordStartPos,
533 wordLengthArray[0]);
satok3c09bb12012-01-26 18:36:19 +0900534 }
satok1f6b52e2012-01-30 13:53:58 +0900535 if (freq <= 0 || nextWordLength <= 0
536 || MAX_WORD_LENGTH <= (outputWordStartPos + nextWordLength)) {
Satoshi Kataoka6cbe2042012-05-30 17:28:34 +0900537 return FLAG_MULTIPLE_SUGGEST_SKIP;
satok3c09bb12012-01-26 18:36:19 +0900538 }
satok1f6b52e2012-01-30 13:53:58 +0900539 for (int i = 0; i < nextWordLength; ++i) {
satok3c09bb12012-01-26 18:36:19 +0900540 outputWord[outputWordStartPos + i] = tempOutputWord[i];
541 }
satok99557162012-01-26 22:49:13 +0900542
543 // Put output values
satok1f6b52e2012-01-30 13:53:58 +0900544 freqArray[currentWordIndex] = freq;
satok99557162012-01-26 22:49:13 +0900545 // TODO: put output length instead of input length
satok1f6b52e2012-01-30 13:53:58 +0900546 wordLengthArray[currentWordIndex] = inputWordLength;
547 const int tempOutputWordLength = outputWordStartPos + nextWordLength;
548 if (outputWordLength) {
549 *outputWordLength = tempOutputWordLength;
550 }
satok99557162012-01-26 22:49:13 +0900551
satok3c09bb12012-01-26 18:36:19 +0900552 if ((inputWordStartPos + inputWordLength) < inputLength) {
satok1f6b52e2012-01-30 13:53:58 +0900553 if (outputWordStartPos + nextWordLength >= MAX_WORD_LENGTH) {
Satoshi Kataoka6cbe2042012-05-30 17:28:34 +0900554 return FLAG_MULTIPLE_SUGGEST_SKIP;
satok3c09bb12012-01-26 18:36:19 +0900555 }
satoka85f4922012-01-30 18:18:30 +0900556 outputWord[tempOutputWordLength] = SPACE;
satok1f6b52e2012-01-30 13:53:58 +0900557 if (outputWordLength) {
558 ++*outputWordLength;
559 }
560 } else if (currentWordIndex >= 1) {
satok99557162012-01-26 22:49:13 +0900561 // TODO: Handle 3 or more words
satoka85f4922012-01-30 18:18:30 +0900562 const int pairFreq = correction->getFreqForSplitMultipleWords(
563 freqArray, wordLengthArray, currentWordIndex + 1, isSpaceProximity, outputWord);
satok99557162012-01-26 22:49:13 +0900564 if (DEBUG_DICT) {
satoka85f4922012-01-30 18:18:30 +0900565 DUMP_WORD(outputWord, tempOutputWordLength);
satoka0ac31f2012-05-23 19:55:27 +0900566 for (int i = 0; i < currentWordIndex + 1; ++i) {
567 AKLOGI("Split %d,%d words: freq = %d, length = %d", i, currentWordIndex + 1,
568 freqArray[i], wordLengthArray[i]);
569 }
570 AKLOGI("Split two words: freq = %d, length = %d, %d, isSpace ? %d", pairFreq,
571 inputLength, tempOutputWordLength, isSpaceProximity);
satok99557162012-01-26 22:49:13 +0900572 }
satok1f6b52e2012-01-30 13:53:58 +0900573 addWord(outputWord, tempOutputWordLength, pairFreq, queuePool->getMasterQueue());
satok3c09bb12012-01-26 18:36:19 +0900574 }
Satoshi Kataoka6cbe2042012-05-30 17:28:34 +0900575 return FLAG_MULTIPLE_SUGGEST_CONTINUE;
satok7409d152012-01-26 16:13:25 +0900576}
577
satok1f6b52e2012-01-30 13:53:58 +0900578void UnigramDictionary::getMultiWordsSuggestionRec(ProximityInfo *proximityInfo,
579 const int *xcoordinates, const int *ycoordinates, const int *codes,
580 const bool useFullEditDistance, const int inputLength,
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900581 Correction *correction, WordsPriorityQueuePool *queuePool,
satok1f6b52e2012-01-30 13:53:58 +0900582 const bool hasAutoCorrectionCandidate, const int startInputPos, const int startWordIndex,
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900583 const int outputWordLength, int *freqArray, int *wordLengthArray,
584 unsigned short *outputWord) const {
satok1f6b52e2012-01-30 13:53:58 +0900585 if (startWordIndex >= (MULTIPLE_WORDS_SUGGESTION_MAX_WORDS - 1)) {
586 // Return if the last word index
587 return;
588 }
satoka85f4922012-01-30 18:18:30 +0900589 if (startWordIndex >= 1
590 && (hasAutoCorrectionCandidate
591 || inputLength < MIN_INPUT_LENGTH_FOR_THREE_OR_MORE_WORDS_CORRECTION)) {
592 // Do not suggest 3+ words if already has auto correction candidate
593 return;
594 }
595 for (int i = startInputPos + 1; i < inputLength; ++i) {
satok1f6b52e2012-01-30 13:53:58 +0900596 if (DEBUG_CORRECTION_FREQ) {
satoka85f4922012-01-30 18:18:30 +0900597 AKLOGI("Multi words(%d), start in %d sep %d start out %d",
598 startWordIndex, startInputPos, i, outputWordLength);
599 DUMP_WORD(outputWord, outputWordLength);
satok1f6b52e2012-01-30 13:53:58 +0900600 }
satoka85f4922012-01-30 18:18:30 +0900601 int tempOutputWordLength = 0;
602 // Current word
603 int inputWordStartPos = startInputPos;
604 int inputWordLength = i - startInputPos;
Satoshi Kataoka6cbe2042012-05-30 17:28:34 +0900605 const int suggestionFlag = getSubStringSuggestion(proximityInfo, xcoordinates, ycoordinates,
606 codes, useFullEditDistance, correction, queuePool, inputLength,
607 hasAutoCorrectionCandidate, startWordIndex, inputWordStartPos, inputWordLength,
608 outputWordLength, true /* not used */, freqArray, wordLengthArray, outputWord,
609 &tempOutputWordLength);
610 if (suggestionFlag == FLAG_MULTIPLE_SUGGEST_ABORT) {
611 // TODO: break here
612 continue;
613 } else if (suggestionFlag == FLAG_MULTIPLE_SUGGEST_SKIP) {
satok1f6b52e2012-01-30 13:53:58 +0900614 continue;
615 }
616
satoka85f4922012-01-30 18:18:30 +0900617 if (DEBUG_CORRECTION_FREQ) {
618 AKLOGI("Do missing space correction");
619 }
620 // Next word
satok1f6b52e2012-01-30 13:53:58 +0900621 // Missing space
622 inputWordStartPos = i;
623 inputWordLength = inputLength - i;
Satoshi Kataoka6cbe2042012-05-30 17:28:34 +0900624 if(getSubStringSuggestion(proximityInfo, xcoordinates, ycoordinates, codes,
satok1f6b52e2012-01-30 13:53:58 +0900625 useFullEditDistance, correction, queuePool, inputLength, hasAutoCorrectionCandidate,
satoka85f4922012-01-30 18:18:30 +0900626 startWordIndex + 1, inputWordStartPos, inputWordLength, tempOutputWordLength,
Satoshi Kataoka6cbe2042012-05-30 17:28:34 +0900627 false /* missing space */, freqArray, wordLengthArray, outputWord, 0)
628 != FLAG_MULTIPLE_SUGGEST_CONTINUE) {
satoka85f4922012-01-30 18:18:30 +0900629 getMultiWordsSuggestionRec(proximityInfo, xcoordinates, ycoordinates, codes,
630 useFullEditDistance, inputLength, correction, queuePool,
631 hasAutoCorrectionCandidate, inputWordStartPos, startWordIndex + 1,
632 tempOutputWordLength, freqArray, wordLengthArray, outputWord);
633 }
satok1f6b52e2012-01-30 13:53:58 +0900634
635 // Mistyped space
636 ++inputWordStartPos;
637 --inputWordLength;
638
639 if (inputWordLength <= 0) {
640 continue;
641 }
642
643 const int x = xcoordinates[inputWordStartPos - 1];
644 const int y = ycoordinates[inputWordStartPos - 1];
645 if (!proximityInfo->hasSpaceProximity(x, y)) {
646 continue;
647 }
648
satoka85f4922012-01-30 18:18:30 +0900649 if (DEBUG_CORRECTION_FREQ) {
650 AKLOGI("Do mistyped space correction");
651 }
satok1f6b52e2012-01-30 13:53:58 +0900652 getSubStringSuggestion(proximityInfo, xcoordinates, ycoordinates, codes,
653 useFullEditDistance, correction, queuePool, inputLength, hasAutoCorrectionCandidate,
satoka85f4922012-01-30 18:18:30 +0900654 startWordIndex + 1, inputWordStartPos, inputWordLength, tempOutputWordLength,
655 true /* mistyped space */, freqArray, wordLengthArray, outputWord, 0);
satok1f6b52e2012-01-30 13:53:58 +0900656 }
657}
658
satoka85f4922012-01-30 18:18:30 +0900659void UnigramDictionary::getSplitMultipleWordsSuggestions(ProximityInfo *proximityInfo,
satok744dab62011-12-15 22:29:05 +0900660 const int *xcoordinates, const int *ycoordinates, const int *codes,
satok1f6b52e2012-01-30 13:53:58 +0900661 const bool useFullEditDistance, const int inputLength,
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900662 Correction *correction, WordsPriorityQueuePool *queuePool,
Satoshi Kataoka6bc051d2012-06-08 19:52:19 +0900663 const bool hasAutoCorrectionCandidate) const {
satokbd6ccdd2012-01-23 12:30:20 +0900664 if (inputLength >= MAX_WORD_LENGTH) return;
satok612c6e42011-08-01 19:35:27 +0900665 if (DEBUG_DICT) {
satok1f6b52e2012-01-30 13:53:58 +0900666 AKLOGI("--- Suggest multiple words");
667 }
satok54af64a2012-01-17 15:58:23 +0900668
satokbd6ccdd2012-01-23 12:30:20 +0900669 // Allocating fixed length array on stack
670 unsigned short outputWord[MAX_WORD_LENGTH];
satok1f6b52e2012-01-30 13:53:58 +0900671 int freqArray[MULTIPLE_WORDS_SUGGESTION_MAX_WORDS];
672 int wordLengthArray[MULTIPLE_WORDS_SUGGESTION_MAX_WORDS];
673 const int outputWordLength = 0;
674 const int startInputPos = 0;
675 const int startWordIndex = 0;
676 getMultiWordsSuggestionRec(proximityInfo, xcoordinates, ycoordinates, codes,
677 useFullEditDistance, inputLength, correction, queuePool, hasAutoCorrectionCandidate,
678 startInputPos, startWordIndex, outputWordLength, freqArray, wordLengthArray,
679 outputWord);
Jean Chalarde6715e32011-06-30 19:47:25 +0900680}
681
Jean Chalard1059f272011-06-28 20:45:05 +0900682// Wrapper for getMostFrequentWordLikeInner, which matches it to the previous
683// interface.
684inline int UnigramDictionary::getMostFrequentWordLike(const int startInputIndex,
Satoshi Kataoka6bc051d2012-06-08 19:52:19 +0900685 const int inputLength, Correction *correction, unsigned short *word) const {
Jean Chalard1059f272011-06-28 20:45:05 +0900686 uint16_t inWord[inputLength];
687
688 for (int i = 0; i < inputLength; ++i) {
Satoshi Kataoka4a3db702012-06-08 15:29:44 +0900689 inWord[i] = (uint16_t)correction->getPrimaryCharAt(startInputIndex + i);
Jean Chalard1059f272011-06-28 20:45:05 +0900690 }
691 return getMostFrequentWordLikeInner(inWord, inputLength, word);
692}
693
694// This function will take the position of a character array within a CharGroup,
695// and check it actually like-matches the word in inWord starting at startInputIndex,
696// that is, it matches it with case and accents squashed.
697// The function returns true if there was a full match, false otherwise.
698// The function will copy on-the-fly the characters in the CharGroup to outNewWord.
699// It will also place the end position of the array in outPos; in outInputIndex,
700// it will place the index of the first char AFTER the match if there was a match,
701// and the initial position if there was not. It makes sense because if there was
702// a match we want to continue searching, but if there was not, we want to go to
703// the next CharGroup.
704// In and out parameters may point to the same location. This function takes care
705// not to use any input parameters after it wrote into its outputs.
706static inline bool testCharGroupForContinuedLikeness(const uint8_t flags,
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900707 const uint8_t *const root, const int startPos,
708 const uint16_t *const inWord, const int startInputIndex,
709 int32_t *outNewWord, int *outInputIndex, int *outPos) {
Jean Chalard19560502012-07-31 23:45:32 +0900710 const bool hasMultipleChars = (0 != (BinaryFormat::FLAG_HAS_MULTIPLE_CHARS & flags));
Jean Chalard1059f272011-06-28 20:45:05 +0900711 int pos = startPos;
712 int32_t character = BinaryFormat::getCharCodeAndForwardPointer(root, &pos);
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +0900713 int32_t baseChar = toBaseLowerCase(character);
714 const uint16_t wChar = toBaseLowerCase(inWord[startInputIndex]);
Jean Chalard1059f272011-06-28 20:45:05 +0900715
716 if (baseChar != wChar) {
717 *outPos = hasMultipleChars ? BinaryFormat::skipOtherCharacters(root, pos) : pos;
718 *outInputIndex = startInputIndex;
719 return false;
720 }
721 int inputIndex = startInputIndex;
722 outNewWord[inputIndex] = character;
723 if (hasMultipleChars) {
724 character = BinaryFormat::getCharCodeAndForwardPointer(root, &pos);
725 while (NOT_A_CHARACTER != character) {
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +0900726 baseChar = toBaseLowerCase(character);
727 if (toBaseLowerCase(inWord[++inputIndex]) != baseChar) {
Jean Chalard1059f272011-06-28 20:45:05 +0900728 *outPos = BinaryFormat::skipOtherCharacters(root, pos);
729 *outInputIndex = startInputIndex;
730 return false;
731 }
732 outNewWord[inputIndex] = character;
733 character = BinaryFormat::getCharCodeAndForwardPointer(root, &pos);
734 }
735 }
736 *outInputIndex = inputIndex + 1;
737 *outPos = pos;
738 return true;
739}
740
741// This function is invoked when a word like the word searched for is found.
742// It will compare the frequency to the max frequency, and if greater, will
743// copy the word into the output buffer. In output value maxFreq, it will
744// write the new maximum frequency if it changed.
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900745static inline void onTerminalWordLike(const int freq, int32_t *newWord, const int length,
746 short unsigned int *outWord, int *maxFreq) {
Jean Chalard1059f272011-06-28 20:45:05 +0900747 if (freq > *maxFreq) {
748 for (int q = 0; q < length; ++q)
749 outWord[q] = newWord[q];
750 outWord[length] = 0;
751 *maxFreq = freq;
752 }
753}
754
755// Will find the highest frequency of the words like the one passed as an argument,
756// that is, everything that only differs by case/accents.
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900757int UnigramDictionary::getMostFrequentWordLikeInner(const uint16_t *const inWord,
758 const int length, short unsigned int *outWord) const {
Jean Chalard1059f272011-06-28 20:45:05 +0900759 int32_t newWord[MAX_WORD_LENGTH_INTERNAL];
760 int depth = 0;
761 int maxFreq = -1;
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900762 const uint8_t *const root = DICT_ROOT;
Satoshi Kataoka6bc051d2012-06-08 19:52:19 +0900763 int stackChildCount[MAX_WORD_LENGTH_INTERNAL];
764 int stackInputIndex[MAX_WORD_LENGTH_INTERNAL];
765 int stackSiblingPos[MAX_WORD_LENGTH_INTERNAL];
Jean Chalard1059f272011-06-28 20:45:05 +0900766
Jean Chalard4c0eca62012-01-16 15:15:53 +0900767 int startPos = 0;
Satoshi Kataoka6bc051d2012-06-08 19:52:19 +0900768 stackChildCount[0] = BinaryFormat::getGroupCountAndForwardPointer(root, &startPos);
769 stackInputIndex[0] = 0;
770 stackSiblingPos[0] = startPos;
Jean Chalard1059f272011-06-28 20:45:05 +0900771 while (depth >= 0) {
Satoshi Kataoka6bc051d2012-06-08 19:52:19 +0900772 const int charGroupCount = stackChildCount[depth];
773 int pos = stackSiblingPos[depth];
Jean Chalard1059f272011-06-28 20:45:05 +0900774 for (int charGroupIndex = charGroupCount - 1; charGroupIndex >= 0; --charGroupIndex) {
Satoshi Kataoka6bc051d2012-06-08 19:52:19 +0900775 int inputIndex = stackInputIndex[depth];
Jean Chalard1059f272011-06-28 20:45:05 +0900776 const uint8_t flags = BinaryFormat::getFlagsAndForwardPointer(root, &pos);
777 // Test whether all chars in this group match with the word we are searching for. If so,
778 // we want to traverse its children (or if the length match, evaluate its frequency).
779 // Note that this function will output the position regardless, but will only write
780 // into inputIndex if there is a match.
781 const bool isAlike = testCharGroupForContinuedLikeness(flags, root, pos, inWord,
782 inputIndex, newWord, &inputIndex, &pos);
Jean Chalard19560502012-07-31 23:45:32 +0900783 if (isAlike && (BinaryFormat::FLAG_IS_TERMINAL & flags) && (inputIndex == length)) {
Jean Chalard1059f272011-06-28 20:45:05 +0900784 const int frequency = BinaryFormat::readFrequencyWithoutMovingPointer(root, pos);
785 onTerminalWordLike(frequency, newWord, inputIndex, outWord, &maxFreq);
786 }
787 pos = BinaryFormat::skipFrequency(flags, pos);
788 const int siblingPos = BinaryFormat::skipChildrenPosAndAttributes(root, flags, pos);
789 const int childrenNodePos = BinaryFormat::readChildrenPosition(root, flags, pos);
790 // If we had a match and the word has children, we want to traverse them. We don't have
791 // to traverse words longer than the one we are searching for, since they will not match
792 // anyway, so don't traverse unless inputIndex < length.
793 if (isAlike && (-1 != childrenNodePos) && (inputIndex < length)) {
794 // Save position for this depth, to get back to this once children are done
Satoshi Kataoka6bc051d2012-06-08 19:52:19 +0900795 stackChildCount[depth] = charGroupIndex;
796 stackSiblingPos[depth] = siblingPos;
Jean Chalard1059f272011-06-28 20:45:05 +0900797 // Prepare stack values for next depth
798 ++depth;
799 int childrenPos = childrenNodePos;
Satoshi Kataoka6bc051d2012-06-08 19:52:19 +0900800 stackChildCount[depth] =
Jean Chalard1059f272011-06-28 20:45:05 +0900801 BinaryFormat::getGroupCountAndForwardPointer(root, &childrenPos);
Satoshi Kataoka6bc051d2012-06-08 19:52:19 +0900802 stackSiblingPos[depth] = childrenPos;
803 stackInputIndex[depth] = inputIndex;
Jean Chalard1059f272011-06-28 20:45:05 +0900804 pos = childrenPos;
805 // Go to the next depth level.
806 ++depth;
807 break;
808 } else {
809 // No match, or no children, or word too long to ever match: go the next sibling.
810 pos = siblingPos;
811 }
812 }
813 --depth;
814 }
815 return maxFreq;
816}
817
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900818int UnigramDictionary::getFrequency(const int32_t *const inWord, const int length) const {
819 const uint8_t *const root = DICT_ROOT;
Jean Chalarde9a86e22012-06-28 21:01:29 +0900820 int pos = BinaryFormat::getTerminalPosition(root, inWord, length,
821 false /* forceLowerCaseSearch */);
Satoshi Kataoka2f854e12012-05-29 15:58:13 +0900822 if (NOT_VALID_WORD == pos) {
823 return NOT_A_PROBABILITY;
824 }
825 const uint8_t flags = BinaryFormat::getFlagsAndForwardPointer(root, &pos);
Jean Chalard19560502012-07-31 23:45:32 +0900826 const bool hasMultipleChars = (0 != (BinaryFormat::FLAG_HAS_MULTIPLE_CHARS & flags));
Satoshi Kataoka2f854e12012-05-29 15:58:13 +0900827 if (hasMultipleChars) {
828 pos = BinaryFormat::skipOtherCharacters(root, pos);
829 } else {
830 BinaryFormat::getCharCodeAndForwardPointer(DICT_ROOT, &pos);
831 }
832 const int unigramFreq = BinaryFormat::readFrequencyWithoutMovingPointer(root, pos);
833 return unigramFreq;
Jean Chalard1059f272011-06-28 20:45:05 +0900834}
835
836// TODO: remove this function.
837int UnigramDictionary::getBigramPosition(int pos, unsigned short *word, int offset,
838 int length) const {
839 return -1;
840}
841
842// ProcessCurrentNode returns a boolean telling whether to traverse children nodes or not.
843// If the return value is false, then the caller should read in the output "nextSiblingPosition"
844// to find out the address of the next sibling node and pass it to a new call of processCurrentNode.
845// It is worthy to note that when false is returned, the output values other than
846// nextSiblingPosition are undefined.
847// If the return value is true, then the caller must proceed to traverse the children of this
848// node. processCurrentNode will output the information about the children: their count in
849// newCount, their position in newChildrenPosition, the traverseAllNodes flag in
850// newTraverseAllNodes, the match weight into newMatchRate, the input index into newInputIndex, the
851// diffs into newDiffs, the sibling position in nextSiblingPosition, and the output index into
852// newOutputIndex. Please also note the following caveat: processCurrentNode does not know when
853// there aren't any more nodes at this level, it merely returns the address of the first byte after
854// the current node in nextSiblingPosition. Thus, the caller must keep count of the nodes at any
855// given level, as output into newCount when traversing this level's parent.
satok8876b752011-08-04 18:31:57 +0900856inline bool UnigramDictionary::processCurrentNode(const int initialPos,
Jean Chalard8950ce62012-05-07 16:28:30 +0900857 const std::map<int, int> *bigramMap, const uint8_t *bigramFilter, Correction *correction,
858 int *newCount, int *newChildrenPosition, int *nextSiblingPosition,
Satoshi Kataoka6bc051d2012-06-08 19:52:19 +0900859 WordsPriorityQueuePool *queuePool, const int currentWordIndex) const {
Jean Chalard85a1d1e2011-06-21 22:23:21 +0900860 if (DEBUG_DICT) {
satokcfca3c62011-08-10 14:30:10 +0900861 correction->checkState();
Jean Chalard85a1d1e2011-06-21 22:23:21 +0900862 }
Jean Chalard0584f022011-06-30 19:23:16 +0900863 int pos = initialPos;
Jean Chalard0584f022011-06-30 19:23:16 +0900864
Jean Chalard1059f272011-06-28 20:45:05 +0900865 // Flags contain the following information:
866 // - Address type (MASK_GROUP_ADDRESS_TYPE) on two bits:
867 // - FLAG_GROUP_ADDRESS_TYPE_{ONE,TWO,THREE}_BYTES means there are children and their address
868 // is on the specified number of bytes.
869 // - FLAG_GROUP_ADDRESS_TYPE_NOADDRESS means there are no children, and therefore no address.
870 // - FLAG_HAS_MULTIPLE_CHARS: whether this node has multiple char or not.
871 // - FLAG_IS_TERMINAL: whether this node is a terminal or not (it may still have children)
872 // - FLAG_HAS_BIGRAMS: whether this node has bigrams or not
873 const uint8_t flags = BinaryFormat::getFlagsAndForwardPointer(DICT_ROOT, &pos);
Jean Chalard19560502012-07-31 23:45:32 +0900874 const bool hasMultipleChars = (0 != (BinaryFormat::FLAG_HAS_MULTIPLE_CHARS & flags));
875 const bool isTerminalNode = (0 != (BinaryFormat::FLAG_IS_TERMINAL & flags));
satok8876b752011-08-04 18:31:57 +0900876
877 bool needsToInvokeOnTerminal = false;
Jean Chalard85a1d1e2011-06-21 22:23:21 +0900878
Jean Chalard1059f272011-06-28 20:45:05 +0900879 // This gets only ONE character from the stream. Next there will be:
880 // if FLAG_HAS_MULTIPLE CHARS: the other characters of the same node
881 // else if FLAG_IS_TERMINAL: the frequency
882 // else if MASK_GROUP_ADDRESS_TYPE is not NONE: the children address
883 // Note that you can't have a node that both is not a terminal and has no children.
884 int32_t c = BinaryFormat::getCharCodeAndForwardPointer(DICT_ROOT, &pos);
885 assert(NOT_A_CHARACTER != c);
Jean Chalard85a1d1e2011-06-21 22:23:21 +0900886
Jean Chalard1059f272011-06-28 20:45:05 +0900887 // We are going to loop through each character and make it look like it's a different
888 // node each time. To do that, we will process characters in this node in order until
889 // we find the character terminator. This is signalled by getCharCode* returning
890 // NOT_A_CHARACTER.
891 // As a special case, if there is only one character in this node, we must not read the
892 // next bytes so we will simulate the NOT_A_CHARACTER return by testing the flags.
893 // This way, each loop run will look like a "virtual node".
894 do {
895 // We prefetch the next char. If 'c' is the last char of this node, we will have
896 // NOT_A_CHARACTER in the next char. From this we can decide whether this virtual node
897 // should behave as a terminal or not and whether we have children.
898 const int32_t nextc = hasMultipleChars
899 ? BinaryFormat::getCharCodeAndForwardPointer(DICT_ROOT, &pos) : NOT_A_CHARACTER;
900 const bool isLastChar = (NOT_A_CHARACTER == nextc);
901 // If there are more chars in this nodes, then this virtual node is not a terminal.
902 // If we are on the last char, this virtual node is a terminal if this node is.
satok8876b752011-08-04 18:31:57 +0900903 const bool isTerminal = isLastChar && isTerminalNode;
Jean Chalard85a1d1e2011-06-21 22:23:21 +0900904
satokcfca3c62011-08-10 14:30:10 +0900905 Correction::CorrectionType stateType = correction->processCharAndCalcState(
satok8876b752011-08-04 18:31:57 +0900906 c, isTerminal);
satokcfca3c62011-08-10 14:30:10 +0900907 if (stateType == Correction::TRAVERSE_ALL_ON_TERMINAL
908 || stateType == Correction::ON_TERMINAL) {
satok8876b752011-08-04 18:31:57 +0900909 needsToInvokeOnTerminal = true;
satokd03317c2011-12-14 21:38:11 +0900910 } else if (stateType == Correction::UNRELATED || correction->needsToPrune()) {
satok8876b752011-08-04 18:31:57 +0900911 // We found that this is an unrelated character, so we should give up traversing
912 // this node and its children entirely.
913 // However we may not be on the last virtual node yet so we skip the remaining
914 // characters in this node, the frequency if it's there, read the next sibling
915 // position to output it, then return false.
916 // We don't have to output other values because we return false, as in
917 // "don't traverse children".
Jean Chalard1059f272011-06-28 20:45:05 +0900918 if (!isLastChar) {
919 pos = BinaryFormat::skipOtherCharacters(DICT_ROOT, pos);
920 }
921 pos = BinaryFormat::skipFrequency(flags, pos);
922 *nextSiblingPosition =
923 BinaryFormat::skipChildrenPosAndAttributes(DICT_ROOT, flags, pos);
924 return false;
Jean Chalard85a1d1e2011-06-21 22:23:21 +0900925 }
926
Jean Chalard1059f272011-06-28 20:45:05 +0900927 // Prepare for the next character. Promote the prefetched char to current char - the loop
928 // will take care of prefetching the next. If we finally found our last char, nextc will
929 // contain NOT_A_CHARACTER.
930 c = nextc;
Jean Chalard1059f272011-06-28 20:45:05 +0900931 } while (NOT_A_CHARACTER != c);
Jean Chalard85a1d1e2011-06-21 22:23:21 +0900932
satok8876b752011-08-04 18:31:57 +0900933 if (isTerminalNode) {
satok6ad15fc2012-01-16 16:21:21 +0900934 // The frequency should be here, because we come here only if this is actually
935 // a terminal node, and we are on its last char.
Jean Chalard171d1802012-04-23 18:51:00 +0900936 const int unigramFreq = BinaryFormat::readFrequencyWithoutMovingPointer(DICT_ROOT, pos);
satok6ad15fc2012-01-16 16:21:21 +0900937 const int childrenAddressPos = BinaryFormat::skipFrequency(flags, pos);
938 const int attributesPos = BinaryFormat::skipChildrenPosition(flags, childrenAddressPos);
939 TerminalAttributes terminalAttributes(DICT_ROOT, flags, attributesPos);
Jean Chalard8950ce62012-05-07 16:28:30 +0900940 // bigramMap contains the bigram frequencies indexed by addresses for fast lookup.
941 // bigramFilter is a bloom filter of said frequencies for even faster rejection.
Jean Chalard49ba1352012-05-07 20:14:00 +0900942 const int probability = BinaryFormat::getProbability(initialPos, bigramMap, bigramFilter,
943 unigramFreq);
Jean Chalard171d1802012-04-23 18:51:00 +0900944 onTerminal(probability, terminalAttributes, correction, queuePool, needsToInvokeOnTerminal,
satok8330b482012-01-23 16:52:37 +0900945 currentWordIndex);
Jean Chalard1059f272011-06-28 20:45:05 +0900946
satok8876b752011-08-04 18:31:57 +0900947 // If there are more chars in this node, then this virtual node has children.
948 // If we are on the last char, this virtual node has children if this node has.
949 const bool hasChildren = BinaryFormat::hasChildrenInFlags(flags);
950
951 // This character matched the typed character (enough to traverse the node at least)
952 // so we just evaluated it. Now we should evaluate this virtual node's children - that
953 // is, if it has any. If it has no children, we're done here - so we skip the end of
954 // the node, output the siblings position, and return false "don't traverse children".
955 // Note that !hasChildren implies isLastChar, so we know we don't have to skip any
956 // remaining char in this group for there can't be any.
957 if (!hasChildren) {
958 pos = BinaryFormat::skipFrequency(flags, pos);
959 *nextSiblingPosition =
960 BinaryFormat::skipChildrenPosAndAttributes(DICT_ROOT, flags, pos);
961 return false;
962 }
963
964 // Optimization: Prune out words that are too long compared to how much was typed.
satokcfca3c62011-08-10 14:30:10 +0900965 if (correction->needsToPrune()) {
satok8876b752011-08-04 18:31:57 +0900966 pos = BinaryFormat::skipFrequency(flags, pos);
967 *nextSiblingPosition =
968 BinaryFormat::skipChildrenPosAndAttributes(DICT_ROOT, flags, pos);
satok10266c02011-08-19 22:05:59 +0900969 if (DEBUG_DICT_FULL) {
satok9fb6f472012-01-13 18:01:22 +0900970 AKLOGI("Traversing was pruned.");
satok10266c02011-08-19 22:05:59 +0900971 }
satok8876b752011-08-04 18:31:57 +0900972 return false;
973 }
974 }
Jean Chalard1059f272011-06-28 20:45:05 +0900975
976 // Now we finished processing this node, and we want to traverse children. If there are no
977 // children, we can't come here.
978 assert(BinaryFormat::hasChildrenInFlags(flags));
979
980 // If this node was a terminal it still has the frequency under the pointer (it may have been
981 // read, but not skipped - see readFrequencyWithoutMovingPointer).
982 // Next come the children position, then possibly attributes (attributes are bigrams only for
983 // now, maybe something related to shortcuts in the future).
984 // Once this is read, we still need to output the number of nodes in the immediate children of
985 // this node, so we read and output it before returning true, as in "please traverse children".
986 pos = BinaryFormat::skipFrequency(flags, pos);
987 int childrenPos = BinaryFormat::readChildrenPosition(DICT_ROOT, flags, pos);
988 *nextSiblingPosition = BinaryFormat::skipChildrenPosAndAttributes(DICT_ROOT, flags, pos);
989 *newCount = BinaryFormat::getGroupCountAndForwardPointer(DICT_ROOT, &childrenPos);
990 *newChildrenPosition = childrenPos;
991 return true;
Jean Chalard85a1d1e2011-06-21 22:23:21 +0900992}
satok30088252010-12-01 21:22:15 +0900993} // namespace latinime