blob: ab326169d10abbb29383d63da5d81ca550464cab [file] [log] [blame]
satoke808e432010-12-02 14:53:24 +09001/*
Ken Wakasaa10b1a82013-01-08 17:23:43 +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 *
Ken Wakasaa10b1a82013-01-08 17:23:43 +09008 * http://www.apache.org/licenses/LICENSE-2.0
Ken Wakasa0bbb9172012-07-25 17:51:43 +09009 *
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 */
satoke808e432010-12-02 14:53:24 +090016
17#ifndef LATINIME_DEFINES_H
18#define LATINIME_DEFINES_H
19
Ken Wakasa1ce96fe2012-11-15 19:09:11 +090020#ifdef __GNUC__
21#define AK_FORCE_INLINE __attribute__((always_inline)) __inline__
22#else // __GNUC__
23#define AK_FORCE_INLINE inline
24#endif // __GNUC__
25
Ken Wakasa6cee61d2013-01-15 16:15:48 +090026#if defined(FLAG_DO_PROFILE) || defined(FLAG_DBG)
27#undef AK_FORCE_INLINE
28#define AK_FORCE_INLINE inline
29#endif // defined(FLAG_DO_PROFILE) || defined(FLAG_DBG)
30
Ken Wakasa6c224392013-01-22 13:14:53 +090031// Must be equal to Constants.Dictionary.MAX_WORD_LENGTH in Java
Ken Wakasa5db594a2013-01-12 01:18:00 +090032#define MAX_WORD_LENGTH 48
Ken Wakasa6c224392013-01-22 13:14:53 +090033// Must be equal to BinaryDictionary.MAX_RESULTS in Java
Ken Wakasa5db594a2013-01-12 01:18:00 +090034#define MAX_RESULTS 18
Ken Wakasa6c224392013-01-22 13:14:53 +090035// Must be equal to ProximityInfo.MAX_PROXIMITY_CHARS_SIZE in Java
36#define MAX_PROXIMITY_CHARS_SIZE 16
37#define ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE 2
Satoshi Kataoka42217382012-12-17 23:28:17 +090038
satok827ced82011-07-14 09:01:09 +090039#if defined(FLAG_DO_PROFILE) || defined(FLAG_DBG)
Ken Wakasae3f26dd2012-07-27 18:06:06 +090040#include <android/log.h>
41#ifndef LOG_TAG
42#define LOG_TAG "LatinIME: "
Ken Wakasa6cee61d2013-01-15 16:15:48 +090043#endif // LOG_TAG
Ken Wakasae3f26dd2012-07-27 18:06:06 +090044#define AKLOGE(fmt, ...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, fmt, ##__VA_ARGS__)
45#define AKLOGI(fmt, ...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, fmt, ##__VA_ARGS__)
satok6ad15fc2012-01-16 16:21:21 +090046
Ken Wakasa5db594a2013-01-12 01:18:00 +090047#define DUMP_RESULT(words, frequencies) do { dumpResult(words, frequencies); } while (0)
Ken Wakasaf2789812012-09-04 12:49:46 +090048#define DUMP_WORD(word, length) do { dumpWord(word, length); } while (0)
Ken Wakasa1e614932012-10-29 18:06:22 +090049#define INTS_TO_CHARS(input, length, output) do { \
50 intArrayToCharArray(input, length, output); } while (0)
satok6ad15fc2012-01-16 16:21:21 +090051
Satoshi Kataoka42217382012-12-17 23:28:17 +090052// TODO: Support full UTF-8 conversion
53AK_FORCE_INLINE static int intArrayToCharArray(const int *source, const int sourceSize,
54 char *dest) {
55 int si = 0;
56 int di = 0;
Ken Wakasa5db594a2013-01-12 01:18:00 +090057 while (si < sourceSize && di < MAX_WORD_LENGTH - 1 && 0 != source[si]) {
Satoshi Kataoka42217382012-12-17 23:28:17 +090058 const int codePoint = source[si++];
59 if (codePoint < 0x7F) {
60 dest[di++] = codePoint;
61 } else if (codePoint < 0x7FF) {
62 dest[di++] = 0xC0 + (codePoint >> 6);
63 dest[di++] = 0x80 + (codePoint & 0x3F);
64 } else if (codePoint < 0xFFFF) {
65 dest[di++] = 0xE0 + (codePoint >> 12);
66 dest[di++] = 0x80 + ((codePoint & 0xFC0) >> 6);
67 dest[di++] = 0x80 + (codePoint & 0x3F);
68 }
69 }
70 dest[di] = 0;
71 return di;
72}
73
Ken Wakasa1e614932012-10-29 18:06:22 +090074static inline void dumpWordInfo(const int *word, const int length, const int rank,
Satoshi Kataokae0e67372013-03-18 13:08:31 +090075 const int probability) {
Satoshi Kataoka586b0ca2012-08-06 11:20:54 +090076 static char charBuf[50];
Satoshi Kataoka42217382012-12-17 23:28:17 +090077 const int N = intArrayToCharArray(word, length, charBuf);
78 if (N > 1) {
Satoshi Kataokae0e67372013-03-18 13:08:31 +090079 AKLOGI("%2d [ %s ] (%d)", rank, charBuf, probability);
Satoshi Kataoka586b0ca2012-08-06 11:20:54 +090080 }
81}
82
Ken Wakasa5db594a2013-01-12 01:18:00 +090083static inline void dumpResult(const int *outWords, const int *frequencies) {
Satoshi Kataoka586b0ca2012-08-06 11:20:54 +090084 AKLOGI("--- DUMP RESULT ---------");
Ken Wakasa5db594a2013-01-12 01:18:00 +090085 for (int i = 0; i < MAX_RESULTS; ++i) {
86 dumpWordInfo(&outWords[i * MAX_WORD_LENGTH], MAX_WORD_LENGTH, i, frequencies[i]);
Satoshi Kataoka586b0ca2012-08-06 11:20:54 +090087 }
88 AKLOGI("-------------------------");
89}
90
Ken Wakasa1ce96fe2012-11-15 19:09:11 +090091static AK_FORCE_INLINE void dumpWord(const int *word, const int length) {
Tadashi G. Takaokad1dbdb62012-03-06 15:35:46 +090092 static char charBuf[50];
Satoshi Kataoka42217382012-12-17 23:28:17 +090093 const int N = intArrayToCharArray(word, length, charBuf);
94 if (N > 1) {
Satoshi Kataoka586b0ca2012-08-06 11:20:54 +090095 AKLOGI("[ %s ]", charBuf);
96 }
satok6ad15fc2012-01-16 16:21:21 +090097}
98
Satoshi Kataoka5540acb2012-09-03 18:35:32 +090099#ifndef __ANDROID__
Satoshi Kataoka1c8fc832012-09-06 21:31:54 +0900100#include <cassert>
Satoshi Kataoka5540acb2012-09-03 18:35:32 +0900101#include <execinfo.h>
102#include <stdlib.h>
Satoshi Kataoka1c8fc832012-09-06 21:31:54 +0900103
Satoshi Kataokadd4d9382013-01-09 12:54:39 +0900104#define DO_ASSERT_TEST
Satoshi Kataoka1c8fc832012-09-06 21:31:54 +0900105#define ASSERT(success) do { if (!(success)) { showStackTrace(); assert(success);} } while (0)
106#define SHOW_STACK_TRACE do { showStackTrace(); } while (0)
107
Satoshi Kataoka5540acb2012-09-03 18:35:32 +0900108static inline void showStackTrace() {
109 void *callstack[128];
110 int i, frames = backtrace(callstack, 128);
111 char **strs = backtrace_symbols(callstack, frames);
112 for (i = 0; i < frames; ++i) {
113 if (i == 0) {
114 AKLOGI("=== Trace ===");
115 continue;
116 }
117 AKLOGI("%s", strs[i]);
118 }
119 free(strs);
120}
Ken Wakasa6cee61d2013-01-15 16:15:48 +0900121#else // __ANDROID__
Satoshi Kataoka1c8fc832012-09-06 21:31:54 +0900122#include <cassert>
Satoshi Kataokadd4d9382013-01-09 12:54:39 +0900123#define DO_ASSERT_TEST
Satoshi Kataoka1c8fc832012-09-06 21:31:54 +0900124#define ASSERT(success) assert(success)
Satoshi Kataoka5540acb2012-09-03 18:35:32 +0900125#define SHOW_STACK_TRACE
Ken Wakasa6cee61d2013-01-15 16:15:48 +0900126#endif // __ANDROID__
Satoshi Kataoka5540acb2012-09-03 18:35:32 +0900127
Ken Wakasa6cee61d2013-01-15 16:15:48 +0900128#else // defined(FLAG_DO_PROFILE) || defined(FLAG_DBG)
satok9fb6f472012-01-13 18:01:22 +0900129#define AKLOGE(fmt, ...)
130#define AKLOGI(fmt, ...)
Ken Wakasa5db594a2013-01-12 01:18:00 +0900131#define DUMP_RESULT(words, frequencies)
satok6ad15fc2012-01-16 16:21:21 +0900132#define DUMP_WORD(word, length)
Satoshi Kataokadd4d9382013-01-09 12:54:39 +0900133#undef DO_ASSERT_TEST
Satoshi Kataoka5540acb2012-09-03 18:35:32 +0900134#define ASSERT(success)
135#define SHOW_STACK_TRACE
Ken Wakasa1e614932012-10-29 18:06:22 +0900136#define INTS_TO_CHARS(input, length, output)
Ken Wakasa6cee61d2013-01-15 16:15:48 +0900137#endif // defined(FLAG_DO_PROFILE) || defined(FLAG_DBG)
satok827ced82011-07-14 09:01:09 +0900138
satok20d9fda2011-07-13 14:40:30 +0900139#ifdef FLAG_DO_PROFILE
satok61e2f852011-01-05 14:13:07 +0900140// Profiler
141#include <time.h>
satok9fb6f472012-01-13 18:01:22 +0900142
satok61e2f852011-01-05 14:13:07 +0900143#define PROF_BUF_SIZE 100
satok0028ed32012-05-16 20:42:12 +0900144static float profile_buf[PROF_BUF_SIZE];
145static float profile_old[PROF_BUF_SIZE];
satok61e2f852011-01-05 14:13:07 +0900146static unsigned int profile_counter[PROF_BUF_SIZE];
147
Ken Wakasae90b3332011-01-07 15:01:51 +0900148#define PROF_RESET prof_reset()
149#define PROF_COUNT(prof_buf_id) ++profile_counter[prof_buf_id]
Ken Wakasaf2789812012-09-04 12:49:46 +0900150#define PROF_OPEN do { PROF_RESET; PROF_START(PROF_BUF_SIZE - 1); } while (0)
Ken Wakasae90b3332011-01-07 15:01:51 +0900151#define PROF_START(prof_buf_id) do { \
Ken Wakasaf2789812012-09-04 12:49:46 +0900152 PROF_COUNT(prof_buf_id); profile_old[prof_buf_id] = (clock()); } while (0)
153#define PROF_CLOSE do { PROF_END(PROF_BUF_SIZE - 1); PROF_OUTALL; } while (0)
Ken Wakasae90b3332011-01-07 15:01:51 +0900154#define PROF_END(prof_buf_id) profile_buf[prof_buf_id] += ((clock()) - profile_old[prof_buf_id])
155#define PROF_CLOCKOUT(prof_buf_id) \
satok9fb6f472012-01-13 18:01:22 +0900156 AKLOGI("%s : clock is %f", __FUNCTION__, (clock() - profile_old[prof_buf_id]))
Ken Wakasaf2789812012-09-04 12:49:46 +0900157#define PROF_OUTALL do { AKLOGI("--- %s ---", __FUNCTION__); prof_out(); } while (0)
satok61e2f852011-01-05 14:13:07 +0900158
Tadashi G. Takaokad1dbdb62012-03-06 15:35:46 +0900159static inline void prof_reset(void) {
Ken Wakasae90b3332011-01-07 15:01:51 +0900160 for (int i = 0; i < PROF_BUF_SIZE; ++i) {
satok61e2f852011-01-05 14:13:07 +0900161 profile_buf[i] = 0;
162 profile_old[i] = 0;
163 profile_counter[i] = 0;
164 }
165}
166
Tadashi G. Takaokad1dbdb62012-03-06 15:35:46 +0900167static inline void prof_out(void) {
satok61e2f852011-01-05 14:13:07 +0900168 if (profile_counter[PROF_BUF_SIZE - 1] != 1) {
satok9fb6f472012-01-13 18:01:22 +0900169 AKLOGI("Error: You must call PROF_OPEN before PROF_CLOSE.");
satok61e2f852011-01-05 14:13:07 +0900170 }
satok9fb6f472012-01-13 18:01:22 +0900171 AKLOGI("Total time is %6.3f ms.",
Ken Wakasa77e8e812012-08-02 19:48:08 +0900172 profile_buf[PROF_BUF_SIZE - 1] * 1000.0f / static_cast<float>(CLOCKS_PER_SEC));
Ken Wakasa2a6f58d2012-11-27 19:40:38 +0900173 float all = 0.0f;
Ken Wakasae90b3332011-01-07 15:01:51 +0900174 for (int i = 0; i < PROF_BUF_SIZE - 1; ++i) {
satok61e2f852011-01-05 14:13:07 +0900175 all += profile_buf[i];
176 }
Ken Wakasa2a6f58d2012-11-27 19:40:38 +0900177 if (all < 1.0f) all = 1.0f;
Ken Wakasae90b3332011-01-07 15:01:51 +0900178 for (int i = 0; i < PROF_BUF_SIZE - 1; ++i) {
Ken Wakasa2a6f58d2012-11-27 19:40:38 +0900179 if (profile_buf[i] > 0.0f) {
satok9fb6f472012-01-13 18:01:22 +0900180 AKLOGI("(%d): Used %4.2f%%, %8.4f ms. Called %d times.",
Ken Wakasa2a6f58d2012-11-27 19:40:38 +0900181 i, (profile_buf[i] * 100.0f / all),
Ken Wakasa77e8e812012-08-02 19:48:08 +0900182 profile_buf[i] * 1000.0f / static_cast<float>(CLOCKS_PER_SEC),
183 profile_counter[i]);
Ken Wakasae90b3332011-01-07 15:01:51 +0900184 }
satok61e2f852011-01-05 14:13:07 +0900185 }
186}
187
satok20d9fda2011-07-13 14:40:30 +0900188#else // FLAG_DO_PROFILE
satok61e2f852011-01-05 14:13:07 +0900189#define PROF_BUF_SIZE 0
190#define PROF_RESET
191#define PROF_COUNT(prof_buf_id)
192#define PROF_OPEN
193#define PROF_START(prof_buf_id)
194#define PROF_CLOSE
195#define PROF_END(prof_buf_id)
196#define PROF_CLOCK_OUT(prof_buf_id)
197#define PROF_CLOCKOUT(prof_buf_id)
198#define PROF_OUTALL
199
satok20d9fda2011-07-13 14:40:30 +0900200#endif // FLAG_DO_PROFILE
201
202#ifdef FLAG_DBG
satok20d9fda2011-07-13 14:40:30 +0900203#define DEBUG_DICT true
204#define DEBUG_DICT_FULL false
satok0cedd2b2011-08-12 01:05:27 +0900205#define DEBUG_EDIT_DISTANCE false
satok20d9fda2011-07-13 14:40:30 +0900206#define DEBUG_NODE DEBUG_DICT_FULL
207#define DEBUG_TRACE DEBUG_DICT_FULL
satok1a6da632011-12-16 23:15:06 +0900208#define DEBUG_PROXIMITY_INFO false
satok0cb20972012-03-13 22:07:56 +0900209#define DEBUG_PROXIMITY_CHARS false
satok10266c02011-08-19 22:05:59 +0900210#define DEBUG_CORRECTION false
satok29dc8062012-01-17 15:59:15 +0900211#define DEBUG_CORRECTION_FREQ false
Satoshi Kataoka0ed8c6e2012-12-04 16:28:06 +0900212#define DEBUG_SAMPLING_POINTS false
213#define DEBUG_POINTS_PROBABILITY false
214#define DEBUG_DOUBLE_LETTER false
Satoshi Kataokabc25b802013-02-21 21:54:21 +0900215#define DEBUG_CACHE false
216#define DEBUG_DUMP_ERROR false
Satoshi Kataoka8b3009e2013-04-10 11:45:12 +0900217#define DEBUG_EVALUATE_MOST_PROBABLE_STRING false
satok20d9fda2011-07-13 14:40:30 +0900218
Satoshi Kataoka952ec492012-09-11 15:51:38 +0900219#ifdef FLAG_FULL_DBG
220#define DEBUG_GEO_FULL true
221#else
Satoshi Kataoka23a57ea2012-09-10 17:59:17 +0900222#define DEBUG_GEO_FULL false
Satoshi Kataoka952ec492012-09-11 15:51:38 +0900223#endif
Satoshi Kataoka23a57ea2012-09-10 17:59:17 +0900224
satok20d9fda2011-07-13 14:40:30 +0900225#else // FLAG_DBG
satok827ced82011-07-14 09:01:09 +0900226
satok20d9fda2011-07-13 14:40:30 +0900227#define DEBUG_DICT false
228#define DEBUG_DICT_FULL false
satok0cedd2b2011-08-12 01:05:27 +0900229#define DEBUG_EDIT_DISTANCE false
satok20d9fda2011-07-13 14:40:30 +0900230#define DEBUG_NODE false
231#define DEBUG_TRACE false
232#define DEBUG_PROXIMITY_INFO false
satok0cb20972012-03-13 22:07:56 +0900233#define DEBUG_PROXIMITY_CHARS false
satok10266c02011-08-19 22:05:59 +0900234#define DEBUG_CORRECTION false
235#define DEBUG_CORRECTION_FREQ false
Keisuke Kuroyanagi806eba42012-10-09 19:57:08 +0900236#define DEBUG_SAMPLING_POINTS false
237#define DEBUG_POINTS_PROBABILITY false
Satoshi Kataoka9af53352012-11-16 23:06:41 +0900238#define DEBUG_DOUBLE_LETTER false
Satoshi Kataokabc25b802013-02-21 21:54:21 +0900239#define DEBUG_CACHE false
240#define DEBUG_DUMP_ERROR false
Satoshi Kataoka8b3009e2013-04-10 11:45:12 +0900241#define DEBUG_EVALUATE_MOST_PROBABLE_STRING false
satok20d9fda2011-07-13 14:40:30 +0900242
Satoshi Kataoka23a57ea2012-09-10 17:59:17 +0900243#define DEBUG_GEO_FULL false
244
satoke808e432010-12-02 14:53:24 +0900245#endif // FLAG_DBG
246
Jean Chalarda5d58492011-02-18 17:50:58 +0900247#ifndef S_INT_MAX
satok3c4bb772011-03-04 22:50:19 -0800248#define S_INT_MAX 2147483647 // ((1 << 31) - 1)
Jean Chalarda5d58492011-02-18 17:50:58 +0900249#endif
Jean Chalard592f2b32012-11-20 21:19:26 +0900250#ifndef S_INT_MIN
251// The literal constant -2147483648 does not work in C prior C90, because
252// the compiler tries to fit the positive number into an int and then negate it.
253// GCC warns about this.
254#define S_INT_MIN (-2147483647 - 1) // -(1 << 31)
255#endif
Satoshi Kataokaee62b782013-01-21 18:29:27 +0900256
Ken Wakasa6c224392013-01-22 13:14:53 +0900257#define M_PI_F 3.14159265f
Satoshi Kataokaee62b782013-01-21 18:29:27 +0900258#define MAX_PERCENTILE 100
259
Jean Chalard22025c62012-11-29 17:33:53 +0900260// Number of base-10 digits in the largest integer + 1 to leave room for a zero terminator.
261// As such, this is the maximum number of characters will be needed to represent an int as a
262// string, including the terminator; this is used as the size of a string buffer large enough to
263// hold any value that is intended to fit in an integer, e.g. in the code that reads the header
264// of the binary dictionary where a {key,value} string pair scheme is used.
265#define LARGEST_INT_DIGIT_COUNT 11
satok662fe692010-12-08 17:05:39 +0900266
Ken Wakasae90b3332011-01-07 15:01:51 +0900267// Define this to use mmap() for dictionary loading. Undefine to use malloc() instead of mmap().
268// We measured and compared performance of both, and found mmap() is fairly good in terms of
269// loading time, and acceptable even for several initial lookups which involve page faults.
270#define USE_MMAP_FOR_DICTIONARY
271
Ken Wakasaf2789812012-09-04 12:49:46 +0900272#define NOT_VALID_WORD (-99)
273#define NOT_A_CODE_POINT (-1)
274#define NOT_A_DISTANCE (-1)
275#define NOT_A_COORDINATE (-1)
Satoshi Kataokaf4425aa2013-03-07 13:06:32 +0900276#define MATCH_CHAR_WITHOUT_DISTANCE_INFO (-2)
Ken Wakasaf2789812012-09-04 12:49:46 +0900277#define PROXIMITY_CHAR_WITHOUT_DISTANCE_INFO (-3)
278#define ADDITIONAL_PROXIMITY_CHAR_DISTANCE_INFO (-4)
279#define NOT_AN_INDEX (-1)
280#define NOT_A_PROBABILITY (-1)
satoke808e432010-12-02 14:53:24 +0900281
satok817e5172011-03-04 06:06:45 -0800282#define KEYCODE_SPACE ' '
Ken Wakasa1e614932012-10-29 18:06:22 +0900283#define KEYCODE_SINGLE_QUOTE '\''
284#define KEYCODE_HYPHEN_MINUS '-'
satok817e5172011-03-04 06:06:45 -0800285
Yusuke Nojimaafb90762011-10-05 18:11:42 +0900286#define CALIBRATE_SCORE_BY_TOUCH_COORDINATES true
satok99557162012-01-26 22:49:13 +0900287#define SUGGEST_MULTIPLE_WORDS true
Tom Ouyang31f7ece2013-02-21 15:42:05 -0800288#define SUGGEST_INTERFACE_OUTPUT_SCALE 1000000.0f
satoka3d78f62010-12-09 22:08:33 +0900289
Ken Wakasaf25e7052013-01-16 01:29:43 +0900290#define ZERO_DISTANCE_PROMOTION_RATE 110.0f
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900291#define NEUTRAL_SCORE_SQUARED_RADIUS 8.0f
292#define HALF_SCORE_SQUARED_RADIUS 32.0f
Satoshi Kataokae0e67372013-03-18 13:08:31 +0900293#define MAX_PROBABILITY 255
294#define MAX_BIGRAM_ENCODED_PROBABILITY 15
Ken Wakasa156d3932013-06-03 20:00:02 +0900295#define MULTIPLE_WORDS_DEMOTION_RATE 80
satoke808e432010-12-02 14:53:24 +0900296
Ken Wakasa9e0c7112012-08-09 22:26:58 +0900297// Assuming locale strings such as en_US, sr-Latn etc.
298#define MAX_LOCALE_STRING_LENGTH 10
299
Satoshi Kataokae0e67372013-03-18 13:08:31 +0900300/* heuristic... This should be changed if we change the unit of the probability. */
301#define SUPPRESS_SHORT_MULTIPLE_WORDS_THRESHOLD_FREQ (MAX_PROBABILITY * 58 / 100)
satok29dc8062012-01-17 15:59:15 +0900302
Satoshi Kataoka830ba672013-03-05 14:12:06 +0900303// Max value for length, distance and probability which are used in weighting
304// TODO: Remove
305#define MAX_VALUE_FOR_WEIGHTING 10000000
Satoshi Kataoka687a2442012-08-23 15:46:43 +0900306
Keisuke Kuroyanagi95a49a52012-09-04 17:00:24 +0900307// The max number of the keys in one keyboard layout
308#define MAX_KEY_COUNT_IN_A_KEYBOARD 64
309
Ken Wakasa5150e152012-09-27 19:21:25 +0900310// TODO: Remove
Satoshi Kataokafe4f1ce2012-12-10 17:17:52 +0900311#define MAX_POINTER_COUNT 1
Satoshi Kataoka99eae8e2013-01-30 20:55:31 +0900312#define MAX_POINTER_COUNT_G 2
Ken Wakasa5150e152012-09-27 19:21:25 +0900313
Ken Wakasa11dc3a32013-06-04 15:40:30 +0900314// Queue IDs and size for DicNodesCache
315#define DIC_NODES_CACHE_INITIAL_QUEUE_ID_ACTIVE 0
316#define DIC_NODES_CACHE_INITIAL_QUEUE_ID_NEXT_ACTIVE 1
317#define DIC_NODES_CACHE_INITIAL_QUEUE_ID_TERMINAL 2
318#define DIC_NODES_CACHE_INITIAL_QUEUE_ID_CACHE_FOR_CONTINUOUS_SUGGESTION 3
319#define DIC_NODES_CACHE_PRIORITY_QUEUES_SIZE 4
320
Jean Chalardf1634c82012-05-02 19:05:27 +0900321// Size, in bytes, of the bloom filter index for bigrams
322// 128 gives us 1024 buckets. The probability of false positive is (1 - e ** (-kn/m))**k,
323// where k is the number of hash functions, n the number of bigrams, and m the number of
324// bits we can test.
325// At the moment 100 is the maximum number of bigrams for a word with the current
326// dictionaries, so n = 100. 1024 buckets give us m = 1024.
327// With 1 hash function, our false positive rate is about 9.3%, which should be enough for
328// our uses since we are only using this to increase average performance. For the record,
329// k = 2 gives 3.1% and k = 3 gives 1.6%. With k = 1, making m = 2048 gives 4.8%,
330// and m = 4096 gives 2.4%.
331#define BIGRAM_FILTER_BYTE_SIZE 128
332// Must be smaller than BIGRAM_FILTER_BYTE_SIZE * 8, and preferably prime. 1021 is the largest
333// prime under 128 * 8.
334#define BIGRAM_FILTER_MODULO 1021
335#if BIGRAM_FILTER_BYTE_SIZE * 8 < BIGRAM_FILTER_MODULO
336#error "BIGRAM_FILTER_MODULO is larger than BIGRAM_FILTER_BYTE_SIZE"
337#endif
338
Tom Ouyang9559dd22013-04-16 16:34:49 -0700339// Max number of bigram maps (previous word contexts) to be cached. Increasing this number could
340// improve bigram lookup speed for multi-word suggestions, but at the cost of more memory usage.
341// Also, there are diminishing returns since the most frequently used bigrams are typically near
342// the beginning of the input and are thus the first ones to be cached. Note that these bigrams
343// are reset for each new composing word.
344#define MAX_CACHED_PREV_WORDS_IN_BIGRAM_MAP 25
345// Most common previous word contexts currently have 100 bigrams
346#define DEFAULT_HASH_MAP_SIZE_FOR_EACH_BIGRAM_MAP 100
347
Ken Wakasa871b8c92013-01-31 18:05:26 +0900348template<typename T> AK_FORCE_INLINE const T &min(const T &a, const T &b) { return a < b ? a : b; }
349template<typename T> AK_FORCE_INLINE const T &max(const T &a, const T &b) { return a > b ? a : b; }
satokf5cded12010-12-06 21:28:24 +0900350
Ken Wakasab02ee3d2012-10-08 11:46:14 +0900351#define NELEMS(x) (sizeof(x) / sizeof((x)[0]))
352
satoke05b3f42012-01-31 17:15:43 +0900353// DEBUG
Ken Wakasaf2789812012-09-04 12:49:46 +0900354#define INPUTLENGTH_FOR_DEBUG (-1)
355#define MIN_OUTPUT_INDEX_FOR_DEBUG (-1)
satoke05b3f42012-01-31 17:15:43 +0900356
satok1bc038c2012-06-14 11:25:50 -0700357#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
358 TypeName(const TypeName&); \
359 void operator=(const TypeName&)
360
361#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
362 TypeName(); \
363 DISALLOW_COPY_AND_ASSIGN(TypeName)
364
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900365// Used as a return value for character comparison
366typedef enum {
367 // Same char, possibly with different case or accent
Satoshi Kataokaf4425aa2013-03-07 13:06:32 +0900368 MATCH_CHAR,
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900369 // It is a char located nearby on the keyboard
Satoshi Kataokaf4425aa2013-03-07 13:06:32 +0900370 PROXIMITY_CHAR,
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900371 // Additional proximity char which can differ by language.
Satoshi Kataokaf4425aa2013-03-07 13:06:32 +0900372 ADDITIONAL_PROXIMITY_CHAR,
373 // It is a substitution char
374 SUBSTITUTION_CHAR,
375 // It is an unrelated char
376 UNRELATED_CHAR,
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900377} ProximityType;
Satoshi Kataoka6ae8dd42012-11-22 20:15:40 +0900378
379typedef enum {
380 NOT_A_DOUBLE_LETTER,
381 A_DOUBLE_LETTER,
382 A_STRONG_DOUBLE_LETTER
383} DoubleLetterLevel;
Satoshi Kataokad8708912013-03-04 17:05:28 +0900384
385typedef enum {
Keisuke Kuroynagi911f3262013-04-24 20:21:18 +0900386 // Correction for MATCH_CHAR
Satoshi Kataokad8708912013-03-04 17:05:28 +0900387 CT_MATCH,
Keisuke Kuroynagi911f3262013-04-24 20:21:18 +0900388 // Correction for PROXIMITY_CHAR
Satoshi Kataokaf4425aa2013-03-07 13:06:32 +0900389 CT_PROXIMITY,
Keisuke Kuroynagi911f3262013-04-24 20:21:18 +0900390 // Correction for ADDITIONAL_PROXIMITY_CHAR
Satoshi Kataokaf4425aa2013-03-07 13:06:32 +0900391 CT_ADDITIONAL_PROXIMITY,
Keisuke Kuroynagi911f3262013-04-24 20:21:18 +0900392 // Correction for SUBSTITUTION_CHAR
Satoshi Kataokaf4425aa2013-03-07 13:06:32 +0900393 CT_SUBSTITUTION,
Keisuke Kuroynagi911f3262013-04-24 20:21:18 +0900394 // Skip one omitted letter
Satoshi Kataokad8708912013-03-04 17:05:28 +0900395 CT_OMISSION,
Keisuke Kuroynagi911f3262013-04-24 20:21:18 +0900396 // Delete an unnecessarily inserted letter
Satoshi Kataokad8708912013-03-04 17:05:28 +0900397 CT_INSERTION,
Keisuke Kuroynagi911f3262013-04-24 20:21:18 +0900398 // Swap the order of next two touch points
Satoshi Kataokad8708912013-03-04 17:05:28 +0900399 CT_TRANSPOSITION,
Satoshi Kataokad8708912013-03-04 17:05:28 +0900400 CT_COMPLETION,
401 CT_TERMINAL,
Keisuke Kuroynagi911f3262013-04-24 20:21:18 +0900402 // Create new word with space omission
Satoshi Kataoka252412d2013-04-16 14:39:30 +0900403 CT_NEW_WORD_SPACE_OMITTION,
Keisuke Kuroynagi911f3262013-04-24 20:21:18 +0900404 // Create new word with space substitution
Satoshi Kataoka252412d2013-04-16 14:39:30 +0900405 CT_NEW_WORD_SPACE_SUBSTITUTION,
Satoshi Kataokad8708912013-03-04 17:05:28 +0900406} CorrectionType;
Keisuke Kuroynagi60a169f2013-04-25 16:47:38 +0900407
408// ErrorType is mainly decided by CorrectionType but it is also depending on if
409// the correction has really been performed or not.
410typedef enum {
411 // Substitution, omission and transposition
412 ET_EDIT_CORRECTION,
413 // Proximity error
414 ET_PROXIMITY_CORRECTION,
415 // Completion
416 ET_COMPLETION,
417 // New word
418 // TODO: Remove.
419 // A new word error should be an edit correction error or a proximity correction error.
420 ET_NEW_WORD,
421 // Treat error as an intentional omission when the CorrectionType is omission and the node can
422 // be intentional omission.
423 ET_INTENTIONAL_OMISSION,
424 // Not treated as an error. Tracked for checking exact match
425 ET_NOT_AN_ERROR
426} ErrorType;
satoke808e432010-12-02 14:53:24 +0900427#endif // LATINIME_DEFINES_H