Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(263)

Side by Side Diff: third_party/prediction/defines.h

Issue 1247903003: Add spellcheck and word suggestion to the prediction service (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2010 The Android Open Source Project
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 */
16
17 #ifndef LATINIME_DEFINES_H
18 #define LATINIME_DEFINES_H
19
20 #include "base/macros.h"
21
22 #ifdef __GNUC__
23 #define AK_FORCE_INLINE __attribute__((always_inline)) __inline__
24 #else // __GNUC__
25 #define AK_FORCE_INLINE inline
26 #endif // __GNUC__
27
28 #if defined(FLAG_DO_PROFILE) || defined(FLAG_DBG)
29 #undef AK_FORCE_INLINE
30 #define AK_FORCE_INLINE inline
31 #endif // defined(FLAG_DO_PROFILE) || defined(FLAG_DBG)
32
33 // Must be equal to Constants.Dictionary.MAX_WORD_LENGTH in Java
34 #define MAX_WORD_LENGTH 48
35 // Must be equal to BinaryDictionary.MAX_RESULTS in Java
36 #define MAX_RESULTS 18
37 // Must be equal to ProximityInfo.MAX_PROXIMITY_CHARS_SIZE in Java
38 #define MAX_PROXIMITY_CHARS_SIZE 16
39 #define ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE 2
40
41 /*
42 // TODO: Use size_t instead of int.
43 // Disclaimer: You will see a compile error if you use this macro against a
44 variable-length array.
45 // Sorry for the inconvenience. It isn't supported.
46 template <typename T, int N>
47 char (&ArraySizeHelper(T (&array)[N]))[N];
48 */
49 #define NELEMS(x) (sizeof(ArraySizeHelper(x)))
50
51 AK_FORCE_INLINE static int intArrayToCharArray(const int* const source,
52 const int sourceSize,
53 char* dest,
54 const int destSize) {
55 // We want to always terminate with a 0 char, so stop one short of the length
56 // to make
57 // sure there is room.
58 const int destLimit = destSize - 1;
59 int si = 0;
60 int di = 0;
61 while (si < sourceSize && di < destLimit && 0 != source[si]) {
62 const int codePoint = source[si++];
63 if (codePoint < 0x7F) { // One byte
64 dest[di++] = codePoint;
65 } else if (codePoint < 0x7FF) { // Two bytes
66 if (di + 1 >= destLimit)
67 break;
68 dest[di++] = 0xC0 + (codePoint >> 6);
69 dest[di++] = 0x80 + (codePoint & 0x3F);
70 } else if (codePoint < 0xFFFF) { // Three bytes
71 if (di + 2 >= destLimit)
72 break;
73 dest[di++] = 0xE0 + (codePoint >> 12);
74 dest[di++] = 0x80 + ((codePoint >> 6) & 0x3F);
75 dest[di++] = 0x80 + (codePoint & 0x3F);
76 } else if (codePoint <= 0x1FFFFF) { // Four bytes
77 if (di + 3 >= destLimit)
78 break;
79 dest[di++] = 0xF0 + (codePoint >> 18);
80 dest[di++] = 0x80 + ((codePoint >> 12) & 0x3F);
81 dest[di++] = 0x80 + ((codePoint >> 6) & 0x3F);
82 dest[di++] = 0x80 + (codePoint & 0x3F);
83 } else if (codePoint <= 0x3FFFFFF) { // Five bytes
84 if (di + 4 >= destLimit)
85 break;
86 dest[di++] = 0xF8 + (codePoint >> 24);
87 dest[di++] = 0x80 + ((codePoint >> 18) & 0x3F);
88 dest[di++] = 0x80 + ((codePoint >> 12) & 0x3F);
89 dest[di++] = 0x80 + ((codePoint >> 6) & 0x3F);
90 dest[di++] = codePoint & 0x3F;
91 } else if (codePoint <= 0x7FFFFFFF) { // Six bytes
92 if (di + 5 >= destLimit)
93 break;
94 dest[di++] = 0xFC + (codePoint >> 30);
95 dest[di++] = 0x80 + ((codePoint >> 24) & 0x3F);
96 dest[di++] = 0x80 + ((codePoint >> 18) & 0x3F);
97 dest[di++] = 0x80 + ((codePoint >> 12) & 0x3F);
98 dest[di++] = 0x80 + ((codePoint >> 6) & 0x3F);
99 dest[di++] = codePoint & 0x3F;
100 } else {
101 // Not a code point... skip.
102 }
103 }
104 dest[di] = 0;
105 return di;
106 }
107
108 #if defined(FLAG_DO_PROFILE) || defined(FLAG_DBG)
109 #if defined(__ANDROID__)
110 #include <android/log.h>
111 #endif // defined(__ANDROID__)
112 #ifndef LOG_TAG
113 #define LOG_TAG "LatinIME: "
114 #endif // LOG_TAG
115
116 #if defined(HOST_TOOL)
117 #include <stdio.h>
118 #define AKLOGE(fmt, ...) printf(fmt "\n", ##__VA_ARGS__)
119 #define AKLOGI(fmt, ...) printf(fmt "\n", ##__VA_ARGS__)
120 #else // defined(HOST_TOOL)
121 #define AKLOGE(fmt, ...) \
122 __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, fmt, ##__VA_ARGS__)
123 #define AKLOGI(fmt, ...) \
124 __android_log_print(ANDROID_LOG_INFO, LOG_TAG, fmt, ##__VA_ARGS__)
125 #endif // defined(HOST_TOOL)
126
127 #define DUMP_SUGGESTION(words, frequencies, index, score) \
128 do { \
129 dumpWordInfo(words, frequencies, index, score); \
130 } while (0)
131 #define DUMP_WORD(word, length) \
132 do { \
133 dumpWord(word, length); \
134 } while (0)
135 #define INTS_TO_CHARS(input, length, output, outlength) \
136 do { \
137 intArrayToCharArray(input, length, output, outlength); \
138 } while (0)
139
140 static inline void dumpWordInfo(const int* word,
141 const int length,
142 const int rank,
143 const int probability) {
144 static char charBuf[50];
145 const int N = intArrayToCharArray(word, length, charBuf, NELEMS(charBuf));
146 if (N > 1) {
147 AKLOGI("%2d [ %s ] (%d)", rank, charBuf, probability);
148 }
149 }
150
151 static AK_FORCE_INLINE void dumpWord(const int* word, const int length) {
152 static char charBuf[50];
153 const int N = intArrayToCharArray(word, length, charBuf, NELEMS(charBuf));
154 if (N > 1) {
155 AKLOGI("[ %s ]", charBuf);
156 }
157 }
158
159 #ifndef __ANDROID__
160 #include <cassert>
161 #include <execinfo.h>
162 #include <stdlib.h>
163
164 #define DO_ASSERT_TEST
165 #define ASSERT(success) \
166 do { \
167 if (!(success)) { \
168 showStackTrace(); \
169 assert(success); \
170 } \
171 } while (0)
172 #define SHOW_STACK_TRACE \
173 do { \
174 showStackTrace(); \
175 } while (0)
176
177 static inline void showStackTrace() {
178 void* callstack[128];
179 int i, frames = backtrace(callstack, 128);
180 char** strs = backtrace_symbols(callstack, frames);
181 for (i = 0; i < frames; ++i) {
182 if (i == 0) {
183 AKLOGI("=== Trace ===");
184 continue;
185 }
186 AKLOGI("%s", strs[i]);
187 }
188 free(strs);
189 }
190 #else // __ANDROID__
191 #include <cassert>
192 #define DO_ASSERT_TEST
193 #define ASSERT(success) assert(success)
194 #define SHOW_STACK_TRACE
195 #endif // __ANDROID__
196
197 #else // defined(FLAG_DO_PROFILE) || defined(FLAG_DBG)
198 #define AKLOGE(fmt, ...)
199 #define AKLOGI(fmt, ...)
200 #define DUMP_SUGGESTION(words, frequencies, index, score)
201 #define DUMP_WORD(word, length)
202 #undef DO_ASSERT_TEST
203 #define ASSERT(success)
204 #define SHOW_STACK_TRACE
205 #define INTS_TO_CHARS(input, length, output)
206 #endif // defined(FLAG_DO_PROFILE) || defined(FLAG_DBG)
207
208 #ifdef FLAG_DO_PROFILE
209 // Profiler
210 #include <time.h>
211
212 #define PROF_BUF_SIZE 100
213 static float profile_buf[PROF_BUF_SIZE];
214 static float profile_old[PROF_BUF_SIZE];
215 static unsigned int profile_counter[PROF_BUF_SIZE];
216
217 #define PROF_RESET prof_reset()
218 #define PROF_COUNT(prof_buf_id) ++profile_counter[prof_buf_id]
219 #define PROF_OPEN \
220 do { \
221 PROF_RESET; \
222 PROF_START(PROF_BUF_SIZE - 1); \
223 } while (0)
224 #define PROF_START(prof_buf_id) \
225 do { \
226 PROF_COUNT(prof_buf_id); \
227 profile_old[prof_buf_id] = (clock()); \
228 } while (0)
229 #define PROF_CLOSE \
230 do { \
231 PROF_END(PROF_BUF_SIZE - 1); \
232 PROF_OUTALL; \
233 } while (0)
234 #define PROF_END(prof_buf_id) \
235 profile_buf[prof_buf_id] += ((clock()) - profile_old[prof_buf_id])
236 #define PROF_CLOCKOUT(prof_buf_id) \
237 AKLOGI("%s : clock is %f", __FUNCTION__, (clock() - profile_old[prof_buf_id]))
238 #define PROF_OUTALL \
239 do { \
240 AKLOGI("--- %s ---", __FUNCTION__); \
241 prof_out(); \
242 } while (0)
243
244 static inline void prof_reset(void) {
245 for (int i = 0; i < PROF_BUF_SIZE; ++i) {
246 profile_buf[i] = 0;
247 profile_old[i] = 0;
248 profile_counter[i] = 0;
249 }
250 }
251
252 static inline void prof_out(void) {
253 if (profile_counter[PROF_BUF_SIZE - 1] != 1) {
254 AKLOGI("Error: You must call PROF_OPEN before PROF_CLOSE.");
255 }
256 AKLOGI("Total time is %6.3f ms.", profile_buf[PROF_BUF_SIZE - 1] * 1000.0f /
257 static_cast<float>(CLOCKS_PER_SEC));
258 float all = 0.0f;
259 for (int i = 0; i < PROF_BUF_SIZE - 1; ++i) {
260 all += profile_buf[i];
261 }
262 if (all < 1.0f)
263 all = 1.0f;
264 for (int i = 0; i < PROF_BUF_SIZE - 1; ++i) {
265 if (profile_buf[i] > 0.0f) {
266 AKLOGI("(%d): Used %4.2f%%, %8.4f ms. Called %d times.", i,
267 (profile_buf[i] * 100.0f / all),
268 profile_buf[i] * 1000.0f / static_cast<float>(CLOCKS_PER_SEC),
269 profile_counter[i]);
270 }
271 }
272 }
273
274 #else // FLAG_DO_PROFILE
275 #define PROF_BUF_SIZE 0
276 #define PROF_RESET
277 #define PROF_COUNT(prof_buf_id)
278 #define PROF_OPEN
279 #define PROF_START(prof_buf_id)
280 #define PROF_CLOSE
281 #define PROF_END(prof_buf_id)
282 #define PROF_CLOCK_OUT(prof_buf_id)
283 #define PROF_CLOCKOUT(prof_buf_id)
284 #define PROF_OUTALL
285
286 #endif // FLAG_DO_PROFILE
287
288 #ifdef FLAG_DBG
289 #define DEBUG_DICT true
290 #define DEBUG_DICT_FULL false
291 #define DEBUG_EDIT_DISTANCE false
292 #define DEBUG_NODE DEBUG_DICT_FULL
293 #define DEBUG_TRACE DEBUG_DICT_FULL
294 #define DEBUG_PROXIMITY_INFO false
295 #define DEBUG_PROXIMITY_CHARS false
296 #define DEBUG_CORRECTION false
297 #define DEBUG_CORRECTION_FREQ false
298 #define DEBUG_SAMPLING_POINTS false
299 #define DEBUG_POINTS_PROBABILITY false
300 #define DEBUG_DOUBLE_LETTER false
301 #define DEBUG_CACHE false
302 #define DEBUG_DUMP_ERROR false
303 #define DEBUG_EVALUATE_MOST_PROBABLE_STRING false
304
305 #ifdef FLAG_FULL_DBG
306 #define DEBUG_GEO_FULL true
307 #else
308 #define DEBUG_GEO_FULL false
309 #endif
310
311 #else // FLAG_DBG
312
313 #define DEBUG_DICT false
314 #define DEBUG_DICT_FULL false
315 #define DEBUG_EDIT_DISTANCE false
316 #define DEBUG_NODE false
317 #define DEBUG_TRACE false
318 #define DEBUG_PROXIMITY_INFO false
319 #define DEBUG_PROXIMITY_CHARS false
320 #define DEBUG_CORRECTION false
321 #define DEBUG_CORRECTION_FREQ false
322 #define DEBUG_SAMPLING_POINTS false
323 #define DEBUG_POINTS_PROBABILITY false
324 #define DEBUG_DOUBLE_LETTER false
325 #define DEBUG_CACHE false
326 #define DEBUG_DUMP_ERROR false
327 #define DEBUG_EVALUATE_MOST_PROBABLE_STRING false
328
329 #define DEBUG_GEO_FULL false
330
331 #endif // FLAG_DBG
332
333 #ifndef S_INT_MAX
334 #define S_INT_MAX 2147483647 // ((1 << 31) - 1)
335 #endif
336 #ifndef S_INT_MIN
337 // The literal constant -2147483648 does not work in C prior C90, because
338 // the compiler tries to fit the positive number into an int and then negate it.
339 // GCC warns about this.
340 #define S_INT_MIN (-2147483647 - 1) // -(1 << 31)
341 #endif
342
343 #define M_PI_F 3.14159265f
344 #define MAX_PERCENTILE 100
345
346 #define NOT_A_CODE_POINT (-1)
347 #define NOT_A_DISTANCE (-1)
348 #define NOT_A_COORDINATE (-1)
349 #define NOT_AN_INDEX (-1)
350 #define NOT_A_PROBABILITY (-1)
351 #define NOT_A_DICT_POS (S_INT_MIN)
352 #define NOT_A_TIMESTAMP (-1)
353 #define NOT_A_LANGUAGE_WEIGHT (-1.0f)
354
355 // A special value to mean the first word confidence makes no sense in this
356 // case,
357 // e.g. this is not a multi-word suggestion.
358 #define NOT_A_FIRST_WORD_CONFIDENCE (S_INT_MIN)
359 // How high the confidence needs to be for us to auto-commit. Arbitrary.
360 // This needs to be the same as CONFIDENCE_FOR_AUTO_COMMIT in
361 // BinaryDictionary.java
362 #define CONFIDENCE_FOR_AUTO_COMMIT (1000000)
363 // 80% of the full confidence
364 #define DISTANCE_WEIGHT_FOR_AUTO_COMMIT (80 * CONFIDENCE_FOR_AUTO_COMMIT / 100)
365 // 100% of the full confidence
366 #define LENGTH_WEIGHT_FOR_AUTO_COMMIT (CONFIDENCE_FOR_AUTO_COMMIT)
367 // 80% of the full confidence
368 #define SPACE_COUNT_WEIGHT_FOR_AUTO_COMMIT \
369 (80 * CONFIDENCE_FOR_AUTO_COMMIT / 100)
370
371 #define KEYCODE_SPACE ' '
372 #define KEYCODE_SINGLE_QUOTE '\''
373 #define KEYCODE_HYPHEN_MINUS '-'
374 // Code point to indicate beginning-of-sentence. This is not in the code point
375 // space of unicode.
376 #define CODE_POINT_BEGINNING_OF_SENTENCE 0x110000
377
378 #define SUGGEST_INTERFACE_OUTPUT_SCALE 1000000.0f
379 #define MAX_PROBABILITY 255
380 #define MAX_BIGRAM_ENCODED_PROBABILITY 15
381
382 // Max value for length, distance and probability which are used in weighting
383 // TODO: Remove
384 #define MAX_VALUE_FOR_WEIGHTING 10000000
385
386 // The max number of the keys in one keyboard layout
387 #define MAX_KEY_COUNT_IN_A_KEYBOARD 64
388
389 // TODO: Remove
390 #define MAX_POINTER_COUNT 1
391 #define MAX_POINTER_COUNT_G 2
392
393 // (MAX_PREV_WORD_COUNT_FOR_N_GRAM + 1)-gram is supported.
394 #define MAX_PREV_WORD_COUNT_FOR_N_GRAM 1
395
396 #define DISALLOW_DEFAULT_CONSTRUCTOR(TypeName) TypeName() = delete
397
398 #define DISALLOW_COPY_CONSTRUCTOR(TypeName) TypeName(const TypeName&) = delete
399
400 #define DISALLOW_ASSIGNMENT_OPERATOR(TypeName) \
401 void operator=(const TypeName&) = delete
402
403 /*
404 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
405 DISALLOW_COPY_CONSTRUCTOR(TypeName); \
406 DISALLOW_ASSIGNMENT_OPERATOR(TypeName)
407
408 #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
409 DISALLOW_DEFAULT_CONSTRUCTOR(TypeName); \
410 DISALLOW_COPY_AND_ASSIGN(TypeName)
411 */
412
413 // Used as a return value for character comparison
414 typedef enum {
415 // Same char, possibly with different case or accent
416 MATCH_CHAR,
417 // It is a char located nearby on the keyboard
418 PROXIMITY_CHAR,
419 // Additional proximity char which can differ by language.
420 ADDITIONAL_PROXIMITY_CHAR,
421 // It is a substitution char
422 SUBSTITUTION_CHAR,
423 // It is an unrelated char
424 UNRELATED_CHAR,
425 } ProximityType;
426
427 typedef enum {
428 NOT_A_DOUBLE_LETTER,
429 A_DOUBLE_LETTER,
430 A_STRONG_DOUBLE_LETTER
431 } DoubleLetterLevel;
432
433 typedef enum {
434 // Correction for MATCH_CHAR
435 CT_MATCH,
436 // Correction for PROXIMITY_CHAR
437 CT_PROXIMITY,
438 // Correction for ADDITIONAL_PROXIMITY_CHAR
439 CT_ADDITIONAL_PROXIMITY,
440 // Correction for SUBSTITUTION_CHAR
441 CT_SUBSTITUTION,
442 // Skip one omitted letter
443 CT_OMISSION,
444 // Delete an unnecessarily inserted letter
445 CT_INSERTION,
446 // Swap the order of next two touch points
447 CT_TRANSPOSITION,
448 CT_COMPLETION,
449 CT_TERMINAL,
450 CT_TERMINAL_INSERTION,
451 // Create new word with space omission
452 CT_NEW_WORD_SPACE_OMISSION,
453 // Create new word with space substitution
454 CT_NEW_WORD_SPACE_SUBSTITUTION,
455 } CorrectionType;
456 #endif // LATINIME_DEFINES_H
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698