blob: 1f432b431ef8a7d472d4555205ddf86293cf422a [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 Wakasa5db594a2013-01-12 01:18:00 +090031// Must be identical to Constants.Dictionary.MAX_WORD_LENGTH in Java
32#define MAX_WORD_LENGTH 48
33// Must be identical to BinaryDictionary.MAX_RESULTS in Java
34#define MAX_RESULTS 18
Satoshi Kataoka42217382012-12-17 23:28:17 +090035
satok827ced82011-07-14 09:01:09 +090036#if defined(FLAG_DO_PROFILE) || defined(FLAG_DBG)
Ken Wakasae3f26dd2012-07-27 18:06:06 +090037#include <android/log.h>
38#ifndef LOG_TAG
39#define LOG_TAG "LatinIME: "
Ken Wakasa6cee61d2013-01-15 16:15:48 +090040#endif // LOG_TAG
Ken Wakasae3f26dd2012-07-27 18:06:06 +090041#define AKLOGE(fmt, ...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, fmt, ##__VA_ARGS__)
42#define AKLOGI(fmt, ...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, fmt, ##__VA_ARGS__)
satok6ad15fc2012-01-16 16:21:21 +090043
Ken Wakasa5db594a2013-01-12 01:18:00 +090044#define DUMP_RESULT(words, frequencies) do { dumpResult(words, frequencies); } while (0)
Ken Wakasaf2789812012-09-04 12:49:46 +090045#define DUMP_WORD(word, length) do { dumpWord(word, length); } while (0)
Ken Wakasa1e614932012-10-29 18:06:22 +090046#define INTS_TO_CHARS(input, length, output) do { \
47 intArrayToCharArray(input, length, output); } while (0)
satok6ad15fc2012-01-16 16:21:21 +090048
Satoshi Kataoka42217382012-12-17 23:28:17 +090049// TODO: Support full UTF-8 conversion
50AK_FORCE_INLINE static int intArrayToCharArray(const int *source, const int sourceSize,
51 char *dest) {
52 int si = 0;
53 int di = 0;
Ken Wakasa5db594a2013-01-12 01:18:00 +090054 while (si < sourceSize && di < MAX_WORD_LENGTH - 1 && 0 != source[si]) {
Satoshi Kataoka42217382012-12-17 23:28:17 +090055 const int codePoint = source[si++];
56 if (codePoint < 0x7F) {
57 dest[di++] = codePoint;
58 } else if (codePoint < 0x7FF) {
59 dest[di++] = 0xC0 + (codePoint >> 6);
60 dest[di++] = 0x80 + (codePoint & 0x3F);
61 } else if (codePoint < 0xFFFF) {
62 dest[di++] = 0xE0 + (codePoint >> 12);
63 dest[di++] = 0x80 + ((codePoint & 0xFC0) >> 6);
64 dest[di++] = 0x80 + (codePoint & 0x3F);
65 }
66 }
67 dest[di] = 0;
68 return di;
69}
70
Ken Wakasa1e614932012-10-29 18:06:22 +090071static inline void dumpWordInfo(const int *word, const int length, const int rank,
72 const int frequency) {
Satoshi Kataoka586b0ca2012-08-06 11:20:54 +090073 static char charBuf[50];
Satoshi Kataoka42217382012-12-17 23:28:17 +090074 const int N = intArrayToCharArray(word, length, charBuf);
75 if (N > 1) {
Satoshi Kataoka586b0ca2012-08-06 11:20:54 +090076 AKLOGI("%2d [ %s ] (%d)", rank, charBuf, frequency);
77 }
78}
79
Ken Wakasa5db594a2013-01-12 01:18:00 +090080static inline void dumpResult(const int *outWords, const int *frequencies) {
Satoshi Kataoka586b0ca2012-08-06 11:20:54 +090081 AKLOGI("--- DUMP RESULT ---------");
Ken Wakasa5db594a2013-01-12 01:18:00 +090082 for (int i = 0; i < MAX_RESULTS; ++i) {
83 dumpWordInfo(&outWords[i * MAX_WORD_LENGTH], MAX_WORD_LENGTH, i, frequencies[i]);
Satoshi Kataoka586b0ca2012-08-06 11:20:54 +090084 }
85 AKLOGI("-------------------------");
86}
87
Ken Wakasa1ce96fe2012-11-15 19:09:11 +090088static AK_FORCE_INLINE void dumpWord(const int *word, const int length) {
Tadashi G. Takaokad1dbdb62012-03-06 15:35:46 +090089 static char charBuf[50];
Satoshi Kataoka42217382012-12-17 23:28:17 +090090 const int N = intArrayToCharArray(word, length, charBuf);
91 if (N > 1) {
Satoshi Kataoka586b0ca2012-08-06 11:20:54 +090092 AKLOGI("[ %s ]", charBuf);
93 }
satok6ad15fc2012-01-16 16:21:21 +090094}
95
Satoshi Kataoka5540acb2012-09-03 18:35:32 +090096#ifndef __ANDROID__
Satoshi Kataoka1c8fc832012-09-06 21:31:54 +090097#include <cassert>
Satoshi Kataoka5540acb2012-09-03 18:35:32 +090098#include <execinfo.h>
99#include <stdlib.h>
Satoshi Kataoka1c8fc832012-09-06 21:31:54 +0900100
Satoshi Kataokadd4d9382013-01-09 12:54:39 +0900101#define DO_ASSERT_TEST
Satoshi Kataoka1c8fc832012-09-06 21:31:54 +0900102#define ASSERT(success) do { if (!(success)) { showStackTrace(); assert(success);} } while (0)
103#define SHOW_STACK_TRACE do { showStackTrace(); } while (0)
104
Satoshi Kataoka5540acb2012-09-03 18:35:32 +0900105static inline void showStackTrace() {
106 void *callstack[128];
107 int i, frames = backtrace(callstack, 128);
108 char **strs = backtrace_symbols(callstack, frames);
109 for (i = 0; i < frames; ++i) {
110 if (i == 0) {
111 AKLOGI("=== Trace ===");
112 continue;
113 }
114 AKLOGI("%s", strs[i]);
115 }
116 free(strs);
117}
Ken Wakasa6cee61d2013-01-15 16:15:48 +0900118#else // __ANDROID__
Satoshi Kataoka1c8fc832012-09-06 21:31:54 +0900119#include <cassert>
Satoshi Kataokadd4d9382013-01-09 12:54:39 +0900120#define DO_ASSERT_TEST
Satoshi Kataoka1c8fc832012-09-06 21:31:54 +0900121#define ASSERT(success) assert(success)
Satoshi Kataoka5540acb2012-09-03 18:35:32 +0900122#define SHOW_STACK_TRACE
Ken Wakasa6cee61d2013-01-15 16:15:48 +0900123#endif // __ANDROID__
Satoshi Kataoka5540acb2012-09-03 18:35:32 +0900124
Ken Wakasa6cee61d2013-01-15 16:15:48 +0900125#else // defined(FLAG_DO_PROFILE) || defined(FLAG_DBG)
satok9fb6f472012-01-13 18:01:22 +0900126#define AKLOGE(fmt, ...)
127#define AKLOGI(fmt, ...)
Ken Wakasa5db594a2013-01-12 01:18:00 +0900128#define DUMP_RESULT(words, frequencies)
satok6ad15fc2012-01-16 16:21:21 +0900129#define DUMP_WORD(word, length)
Satoshi Kataokadd4d9382013-01-09 12:54:39 +0900130#undef DO_ASSERT_TEST
Satoshi Kataoka5540acb2012-09-03 18:35:32 +0900131#define ASSERT(success)
132#define SHOW_STACK_TRACE
Ken Wakasa1e614932012-10-29 18:06:22 +0900133#define INTS_TO_CHARS(input, length, output)
Ken Wakasa6cee61d2013-01-15 16:15:48 +0900134#endif // defined(FLAG_DO_PROFILE) || defined(FLAG_DBG)
satok827ced82011-07-14 09:01:09 +0900135
satok20d9fda2011-07-13 14:40:30 +0900136#ifdef FLAG_DO_PROFILE
satok61e2f852011-01-05 14:13:07 +0900137// Profiler
138#include <time.h>
satok9fb6f472012-01-13 18:01:22 +0900139
satok61e2f852011-01-05 14:13:07 +0900140#define PROF_BUF_SIZE 100
satok0028ed32012-05-16 20:42:12 +0900141static float profile_buf[PROF_BUF_SIZE];
142static float profile_old[PROF_BUF_SIZE];
satok61e2f852011-01-05 14:13:07 +0900143static unsigned int profile_counter[PROF_BUF_SIZE];
144
Ken Wakasae90b3332011-01-07 15:01:51 +0900145#define PROF_RESET prof_reset()
146#define PROF_COUNT(prof_buf_id) ++profile_counter[prof_buf_id]
Ken Wakasaf2789812012-09-04 12:49:46 +0900147#define PROF_OPEN do { PROF_RESET; PROF_START(PROF_BUF_SIZE - 1); } while (0)
Ken Wakasae90b3332011-01-07 15:01:51 +0900148#define PROF_START(prof_buf_id) do { \
Ken Wakasaf2789812012-09-04 12:49:46 +0900149 PROF_COUNT(prof_buf_id); profile_old[prof_buf_id] = (clock()); } while (0)
150#define PROF_CLOSE do { PROF_END(PROF_BUF_SIZE - 1); PROF_OUTALL; } while (0)
Ken Wakasae90b3332011-01-07 15:01:51 +0900151#define PROF_END(prof_buf_id) profile_buf[prof_buf_id] += ((clock()) - profile_old[prof_buf_id])
152#define PROF_CLOCKOUT(prof_buf_id) \
satok9fb6f472012-01-13 18:01:22 +0900153 AKLOGI("%s : clock is %f", __FUNCTION__, (clock() - profile_old[prof_buf_id]))
Ken Wakasaf2789812012-09-04 12:49:46 +0900154#define PROF_OUTALL do { AKLOGI("--- %s ---", __FUNCTION__); prof_out(); } while (0)
satok61e2f852011-01-05 14:13:07 +0900155
Tadashi G. Takaokad1dbdb62012-03-06 15:35:46 +0900156static inline void prof_reset(void) {
Ken Wakasae90b3332011-01-07 15:01:51 +0900157 for (int i = 0; i < PROF_BUF_SIZE; ++i) {
satok61e2f852011-01-05 14:13:07 +0900158 profile_buf[i] = 0;
159 profile_old[i] = 0;
160 profile_counter[i] = 0;
161 }
162}
163
Tadashi G. Takaokad1dbdb62012-03-06 15:35:46 +0900164static inline void prof_out(void) {
satok61e2f852011-01-05 14:13:07 +0900165 if (profile_counter[PROF_BUF_SIZE - 1] != 1) {
satok9fb6f472012-01-13 18:01:22 +0900166 AKLOGI("Error: You must call PROF_OPEN before PROF_CLOSE.");
satok61e2f852011-01-05 14:13:07 +0900167 }
satok9fb6f472012-01-13 18:01:22 +0900168 AKLOGI("Total time is %6.3f ms.",
Ken Wakasa77e8e812012-08-02 19:48:08 +0900169 profile_buf[PROF_BUF_SIZE - 1] * 1000.0f / static_cast<float>(CLOCKS_PER_SEC));
Ken Wakasa2a6f58d2012-11-27 19:40:38 +0900170 float all = 0.0f;
Ken Wakasae90b3332011-01-07 15:01:51 +0900171 for (int i = 0; i < PROF_BUF_SIZE - 1; ++i) {
satok61e2f852011-01-05 14:13:07 +0900172 all += profile_buf[i];
173 }
Ken Wakasa2a6f58d2012-11-27 19:40:38 +0900174 if (all < 1.0f) all = 1.0f;
Ken Wakasae90b3332011-01-07 15:01:51 +0900175 for (int i = 0; i < PROF_BUF_SIZE - 1; ++i) {
Ken Wakasa2a6f58d2012-11-27 19:40:38 +0900176 if (profile_buf[i] > 0.0f) {
satok9fb6f472012-01-13 18:01:22 +0900177 AKLOGI("(%d): Used %4.2f%%, %8.4f ms. Called %d times.",
Ken Wakasa2a6f58d2012-11-27 19:40:38 +0900178 i, (profile_buf[i] * 100.0f / all),
Ken Wakasa77e8e812012-08-02 19:48:08 +0900179 profile_buf[i] * 1000.0f / static_cast<float>(CLOCKS_PER_SEC),
180 profile_counter[i]);
Ken Wakasae90b3332011-01-07 15:01:51 +0900181 }
satok61e2f852011-01-05 14:13:07 +0900182 }
183}
184
satok20d9fda2011-07-13 14:40:30 +0900185#else // FLAG_DO_PROFILE
satok61e2f852011-01-05 14:13:07 +0900186#define PROF_BUF_SIZE 0
187#define PROF_RESET
188#define PROF_COUNT(prof_buf_id)
189#define PROF_OPEN
190#define PROF_START(prof_buf_id)
191#define PROF_CLOSE
192#define PROF_END(prof_buf_id)
193#define PROF_CLOCK_OUT(prof_buf_id)
194#define PROF_CLOCKOUT(prof_buf_id)
195#define PROF_OUTALL
196
satok20d9fda2011-07-13 14:40:30 +0900197#endif // FLAG_DO_PROFILE
198
199#ifdef FLAG_DBG
satok20d9fda2011-07-13 14:40:30 +0900200#define DEBUG_DICT true
201#define DEBUG_DICT_FULL false
satok0cedd2b2011-08-12 01:05:27 +0900202#define DEBUG_EDIT_DISTANCE false
satok10266c02011-08-19 22:05:59 +0900203#define DEBUG_SHOW_FOUND_WORD false
satok20d9fda2011-07-13 14:40:30 +0900204#define DEBUG_NODE DEBUG_DICT_FULL
205#define DEBUG_TRACE DEBUG_DICT_FULL
satok1a6da632011-12-16 23:15:06 +0900206#define DEBUG_PROXIMITY_INFO false
satok0cb20972012-03-13 22:07:56 +0900207#define DEBUG_PROXIMITY_CHARS false
satok10266c02011-08-19 22:05:59 +0900208#define DEBUG_CORRECTION false
satok29dc8062012-01-17 15:59:15 +0900209#define DEBUG_CORRECTION_FREQ false
210#define DEBUG_WORDS_PRIORITY_QUEUE false
Satoshi Kataoka0ed8c6e2012-12-04 16:28:06 +0900211#define DEBUG_SAMPLING_POINTS false
212#define DEBUG_POINTS_PROBABILITY false
213#define DEBUG_DOUBLE_LETTER false
satok20d9fda2011-07-13 14:40:30 +0900214
Satoshi Kataoka952ec492012-09-11 15:51:38 +0900215#ifdef FLAG_FULL_DBG
216#define DEBUG_GEO_FULL true
217#else
Satoshi Kataoka23a57ea2012-09-10 17:59:17 +0900218#define DEBUG_GEO_FULL false
Satoshi Kataoka952ec492012-09-11 15:51:38 +0900219#endif
Satoshi Kataoka23a57ea2012-09-10 17:59:17 +0900220
satok20d9fda2011-07-13 14:40:30 +0900221#else // FLAG_DBG
satok827ced82011-07-14 09:01:09 +0900222
satok20d9fda2011-07-13 14:40:30 +0900223#define DEBUG_DICT false
224#define DEBUG_DICT_FULL false
satok0cedd2b2011-08-12 01:05:27 +0900225#define DEBUG_EDIT_DISTANCE false
satok20d9fda2011-07-13 14:40:30 +0900226#define DEBUG_SHOW_FOUND_WORD false
227#define DEBUG_NODE false
228#define DEBUG_TRACE false
229#define DEBUG_PROXIMITY_INFO false
satok0cb20972012-03-13 22:07:56 +0900230#define DEBUG_PROXIMITY_CHARS false
satok10266c02011-08-19 22:05:59 +0900231#define DEBUG_CORRECTION false
232#define DEBUG_CORRECTION_FREQ false
satok16379df2011-12-12 20:53:22 +0900233#define DEBUG_WORDS_PRIORITY_QUEUE false
Keisuke Kuroyanagi806eba42012-10-09 19:57:08 +0900234#define DEBUG_SAMPLING_POINTS false
235#define DEBUG_POINTS_PROBABILITY false
Satoshi Kataoka9af53352012-11-16 23:06:41 +0900236#define DEBUG_DOUBLE_LETTER false
satok20d9fda2011-07-13 14:40:30 +0900237
Satoshi Kataoka23a57ea2012-09-10 17:59:17 +0900238#define DEBUG_GEO_FULL false
239
satoke808e432010-12-02 14:53:24 +0900240#endif // FLAG_DBG
241
Jean Chalarda5d58492011-02-18 17:50:58 +0900242#ifndef S_INT_MAX
satok3c4bb772011-03-04 22:50:19 -0800243#define S_INT_MAX 2147483647 // ((1 << 31) - 1)
Jean Chalarda5d58492011-02-18 17:50:58 +0900244#endif
Jean Chalard592f2b32012-11-20 21:19:26 +0900245#ifndef S_INT_MIN
246// The literal constant -2147483648 does not work in C prior C90, because
247// the compiler tries to fit the positive number into an int and then negate it.
248// GCC warns about this.
249#define S_INT_MIN (-2147483647 - 1) // -(1 << 31)
250#endif
Satoshi Kataokaee62b782013-01-21 18:29:27 +0900251
252#define MAX_PERCENTILE 100
253
Jean Chalard22025c62012-11-29 17:33:53 +0900254// Number of base-10 digits in the largest integer + 1 to leave room for a zero terminator.
255// As such, this is the maximum number of characters will be needed to represent an int as a
256// string, including the terminator; this is used as the size of a string buffer large enough to
257// hold any value that is intended to fit in an integer, e.g. in the code that reads the header
258// of the binary dictionary where a {key,value} string pair scheme is used.
259#define LARGEST_INT_DIGIT_COUNT 11
satok662fe692010-12-08 17:05:39 +0900260
Ken Wakasae90b3332011-01-07 15:01:51 +0900261// Define this to use mmap() for dictionary loading. Undefine to use malloc() instead of mmap().
262// We measured and compared performance of both, and found mmap() is fairly good in terms of
263// loading time, and acceptable even for several initial lookups which involve page faults.
264#define USE_MMAP_FOR_DICTIONARY
265
satoke808e432010-12-02 14:53:24 +0900266// 22-bit address = ~4MB dictionary size limit, which on average would be about 200k-300k words
267#define ADDRESS_MASK 0x3FFFFF
268
269// The bit that decides if an address follows in the next 22 bits
270#define FLAG_ADDRESS_MASK 0x40
271// The bit that decides if this is a terminal node for a word. The node could still have children,
272// if the word has other endings.
273#define FLAG_TERMINAL_MASK 0x80
274
275#define FLAG_BIGRAM_READ 0x80
276#define FLAG_BIGRAM_CHILDEXIST 0x40
277#define FLAG_BIGRAM_CONTINUED 0x80
278#define FLAG_BIGRAM_FREQ 0x7F
279
280#define DICTIONARY_VERSION_MIN 200
Ken Wakasaf2789812012-09-04 12:49:46 +0900281#define NOT_VALID_WORD (-99)
282#define NOT_A_CODE_POINT (-1)
283#define NOT_A_DISTANCE (-1)
284#define NOT_A_COORDINATE (-1)
285#define EQUIVALENT_CHAR_WITHOUT_DISTANCE_INFO (-2)
286#define PROXIMITY_CHAR_WITHOUT_DISTANCE_INFO (-3)
287#define ADDITIONAL_PROXIMITY_CHAR_DISTANCE_INFO (-4)
288#define NOT_AN_INDEX (-1)
289#define NOT_A_PROBABILITY (-1)
satoke808e432010-12-02 14:53:24 +0900290
satok817e5172011-03-04 06:06:45 -0800291#define KEYCODE_SPACE ' '
Ken Wakasa1e614932012-10-29 18:06:22 +0900292#define KEYCODE_SINGLE_QUOTE '\''
293#define KEYCODE_HYPHEN_MINUS '-'
satok817e5172011-03-04 06:06:45 -0800294
Yusuke Nojimaafb90762011-10-05 18:11:42 +0900295#define CALIBRATE_SCORE_BY_TOUCH_COORDINATES true
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900296
satok662fe692010-12-08 17:05:39 +0900297#define SUGGEST_WORDS_WITH_MISSING_CHARACTER true
satok662fe692010-12-08 17:05:39 +0900298#define SUGGEST_WORDS_WITH_EXCESSIVE_CHARACTER true
satoka3d78f62010-12-09 22:08:33 +0900299#define SUGGEST_WORDS_WITH_TRANSPOSED_CHARACTERS true
satok99557162012-01-26 22:49:13 +0900300#define SUGGEST_MULTIPLE_WORDS true
satoka3d78f62010-12-09 22:08:33 +0900301
Jean Chalard8dc754a2011-01-27 14:20:22 +0900302// The following "rate"s are used as a multiplier before dividing by 100, so they are in percent.
satok72bc17e2011-04-13 17:23:27 +0900303#define WORDS_WITH_MISSING_CHARACTER_DEMOTION_RATE 80
satokdc5301e2011-04-11 16:14:45 +0900304#define WORDS_WITH_MISSING_CHARACTER_DEMOTION_START_POS_10X 12
satok54af64a2012-01-17 15:58:23 +0900305#define WORDS_WITH_MISSING_SPACE_CHARACTER_DEMOTION_RATE 58
satok8330b482012-01-23 16:52:37 +0900306#define WORDS_WITH_MISTYPED_SPACE_DEMOTION_RATE 50
satoka3d78f62010-12-09 22:08:33 +0900307#define WORDS_WITH_EXCESSIVE_CHARACTER_DEMOTION_RATE 75
satok54fe9e02010-12-13 14:42:35 +0900308#define WORDS_WITH_EXCESSIVE_CHARACTER_OUT_OF_PROXIMITY_DEMOTION_RATE 75
satoka161a4a2012-01-16 18:38:32 +0900309#define WORDS_WITH_TRANSPOSED_CHARACTERS_DEMOTION_RATE 70
satok58c49b92011-01-27 03:23:39 +0900310#define FULL_MATCHED_WORDS_PROMOTION_RATE 120
satok9d2a3022011-04-14 19:13:34 +0900311#define WORDS_WITH_PROXIMITY_CHARACTER_DEMOTION_RATE 90
satok1b9fa942012-02-02 18:49:22 +0900312#define WORDS_WITH_ADDITIONAL_PROXIMITY_CHARACTER_DEMOTION_RATE 70
satok635f68e2011-08-10 22:19:33 +0900313#define WORDS_WITH_MATCH_SKIP_PROMOTION_RATE 105
satok1b9fa942012-02-02 18:49:22 +0900314#define WORDS_WITH_JUST_ONE_CORRECTION_PROMOTION_RATE 148
315#define WORDS_WITH_JUST_ONE_CORRECTION_PROMOTION_MULTIPLIER 3
satok10266c02011-08-19 22:05:59 +0900316#define CORRECTION_COUNT_RATE_DEMOTION_RATE_BASE 45
317#define INPUT_EXCEEDS_OUTPUT_DEMOTION_RATE 70
318#define FIRST_CHAR_DIFFERENT_DEMOTION_RATE 96
satokeb050fc2011-10-03 19:21:13 +0900319#define TWO_WORDS_CAPITALIZED_DEMOTION_RATE 50
satok54af64a2012-01-17 15:58:23 +0900320#define TWO_WORDS_CORRECTION_DEMOTION_BASE 80
321#define TWO_WORDS_PLUS_OTHER_ERROR_CORRECTION_DEMOTION_DIVIDER 1
Ken Wakasaf25e7052013-01-16 01:29:43 +0900322#define ZERO_DISTANCE_PROMOTION_RATE 110.0f
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900323#define NEUTRAL_SCORE_SQUARED_RADIUS 8.0f
324#define HALF_SCORE_SQUARED_RADIUS 32.0f
satoka85f4922012-01-30 18:18:30 +0900325#define MAX_FREQ 255
Jean Chalard9416c812012-05-15 19:24:47 +0900326#define MAX_BIGRAM_FREQ 15
satoke808e432010-12-02 14:53:24 +0900327
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900328// This must be the same as ProximityInfo#MAX_PROXIMITY_CHARS_SIZE, currently it's 16.
329#define MAX_PROXIMITY_CHARS_SIZE_INTERNAL 16
330
satoke05b3f42012-01-31 17:15:43 +0900331// This must be equal to ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE in KeyDetector.java
332#define ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE 2
333
Ken Wakasa9e0c7112012-08-09 22:26:58 +0900334// Assuming locale strings such as en_US, sr-Latn etc.
335#define MAX_LOCALE_STRING_LENGTH 10
336
satoka7e5a5a2011-12-15 16:49:12 +0900337// Word limit for sub queues used in WordsPriorityQueuePool. Sub queues are temporary queues used
338// for better performance.
satok6ad15fc2012-01-16 16:21:21 +0900339// Holds up to 1 candidate for each word
340#define SUB_QUEUE_MAX_WORDS 1
satokb9604772012-01-13 15:41:17 +0900341#define SUB_QUEUE_MAX_COUNT 10
satok54af64a2012-01-17 15:58:23 +0900342#define SUB_QUEUE_MIN_WORD_LENGTH 4
Satoshi Kataoka67e3cc82012-05-31 15:04:58 +0900343// TODO: Extend this limitation
344#define MULTIPLE_WORDS_SUGGESTION_MAX_WORDS 5
Satoshi Kataoka6cbe2042012-05-30 17:28:34 +0900345// TODO: Remove this limitation
346#define MULTIPLE_WORDS_SUGGESTION_MAX_WORD_LENGTH 12
347// TODO: Remove this limitation
Satoshi Kataoka67e3cc82012-05-31 15:04:58 +0900348#define MULTIPLE_WORDS_SUGGESTION_MAX_TOTAL_TRAVERSE_COUNT 45
satoka85f4922012-01-30 18:18:30 +0900349#define MULTIPLE_WORDS_DEMOTION_RATE 80
350#define MIN_INPUT_LENGTH_FOR_THREE_OR_MORE_WORDS_CORRECTION 6
satoka7e5a5a2011-12-15 16:49:12 +0900351
Ken Wakasab02ee3d2012-10-08 11:46:14 +0900352#define TWO_WORDS_CORRECTION_WITH_OTHER_ERROR_THRESHOLD 0.35f
353#define START_TWO_WORDS_CORRECTION_THRESHOLD 0.185f
satoka0ac31f2012-05-23 19:55:27 +0900354/* heuristic... This should be changed if we change the unit of the frequency. */
355#define SUPPRESS_SHORT_MULTIPLE_WORDS_THRESHOLD_FREQ (MAX_FREQ * 58 / 100)
satok29dc8062012-01-17 15:59:15 +0900356
satok68319262010-12-03 19:38:08 +0900357#define MAX_DEPTH_MULTIPLIER 3
satok1f6b52e2012-01-30 13:53:58 +0900358#define FIRST_WORD_INDEX 0
Satoshi Kataoka5817b6b2013-01-09 20:05:26 +0900359
Satoshi Kataoka687a2442012-08-23 15:46:43 +0900360// Max Distance between point to key
361#define MAX_POINT_TO_KEY_LENGTH 10000000
362
Keisuke Kuroyanagi95a49a52012-09-04 17:00:24 +0900363// The max number of the keys in one keyboard layout
364#define MAX_KEY_COUNT_IN_A_KEYBOARD 64
365
Jean Chalard6c300612012-03-06 19:54:03 +0900366// TODO: Reduce this constant if possible; check the maximum number of digraphs in the same
367// word in the dictionary for languages with digraphs, like German and French
368#define DEFAULT_MAX_DIGRAPH_SEARCH_DEPTH 5
Jean Chalarda787dba2011-03-04 12:17:48 +0900369
satok99557162012-01-26 22:49:13 +0900370#define MIN_USER_TYPED_LENGTH_FOR_MULTIPLE_WORD_SUGGESTION 3
satok54fe9e02010-12-13 14:42:35 +0900371#define MIN_USER_TYPED_LENGTH_FOR_EXCESSIVE_CHARACTER_SUGGESTION 3
satok662fe692010-12-08 17:05:39 +0900372
Ken Wakasa5150e152012-09-27 19:21:25 +0900373// TODO: Remove
Satoshi Kataokafe4f1ce2012-12-10 17:17:52 +0900374#define MAX_POINTER_COUNT 1
Ken Wakasa5150e152012-09-27 19:21:25 +0900375#define MAX_POINTER_COUNT_FOR_G 2
376
Jean Chalardf1634c82012-05-02 19:05:27 +0900377// Size, in bytes, of the bloom filter index for bigrams
378// 128 gives us 1024 buckets. The probability of false positive is (1 - e ** (-kn/m))**k,
379// where k is the number of hash functions, n the number of bigrams, and m the number of
380// bits we can test.
381// At the moment 100 is the maximum number of bigrams for a word with the current
382// dictionaries, so n = 100. 1024 buckets give us m = 1024.
383// With 1 hash function, our false positive rate is about 9.3%, which should be enough for
384// our uses since we are only using this to increase average performance. For the record,
385// k = 2 gives 3.1% and k = 3 gives 1.6%. With k = 1, making m = 2048 gives 4.8%,
386// and m = 4096 gives 2.4%.
387#define BIGRAM_FILTER_BYTE_SIZE 128
388// Must be smaller than BIGRAM_FILTER_BYTE_SIZE * 8, and preferably prime. 1021 is the largest
389// prime under 128 * 8.
390#define BIGRAM_FILTER_MODULO 1021
391#if BIGRAM_FILTER_BYTE_SIZE * 8 < BIGRAM_FILTER_MODULO
392#error "BIGRAM_FILTER_MODULO is larger than BIGRAM_FILTER_BYTE_SIZE"
393#endif
394
Tadashi G. Takaoka09baa362012-02-03 21:24:53 +0900395template<typename T> inline T min(T a, T b) { return a < b ? a : b; }
396template<typename T> inline T max(T a, T b) { return a > b ? a : b; }
satokf5cded12010-12-06 21:28:24 +0900397
Ken Wakasa0c2227a2013-01-21 11:37:54 +0900398#define M_PI_F 3.14159265f
399
Ken Wakasab02ee3d2012-10-08 11:46:14 +0900400#define NELEMS(x) (sizeof(x) / sizeof((x)[0]))
401
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900402// The ratio of neutral area radius to sweet spot radius.
403#define NEUTRAL_AREA_RADIUS_RATIO 1.3f
404
satoke05b3f42012-01-31 17:15:43 +0900405// DEBUG
Ken Wakasaf2789812012-09-04 12:49:46 +0900406#define INPUTLENGTH_FOR_DEBUG (-1)
407#define MIN_OUTPUT_INDEX_FOR_DEBUG (-1)
satoke05b3f42012-01-31 17:15:43 +0900408
satok1bc038c2012-06-14 11:25:50 -0700409#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
410 TypeName(const TypeName&); \
411 void operator=(const TypeName&)
412
413#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
414 TypeName(); \
415 DISALLOW_COPY_AND_ASSIGN(TypeName)
416
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900417// Used as a return value for character comparison
418typedef enum {
419 // Same char, possibly with different case or accent
420 EQUIVALENT_CHAR,
421 // It is a char located nearby on the keyboard
422 NEAR_PROXIMITY_CHAR,
423 // It is an unrelated char
424 UNRELATED_CHAR,
425 // Additional proximity char which can differ by language.
426 ADDITIONAL_PROXIMITY_CHAR
427} ProximityType;
Satoshi Kataoka6ae8dd42012-11-22 20:15:40 +0900428
429typedef enum {
430 NOT_A_DOUBLE_LETTER,
431 A_DOUBLE_LETTER,
432 A_STRONG_DOUBLE_LETTER
433} DoubleLetterLevel;
satoke808e432010-12-02 14:53:24 +0900434#endif // LATINIME_DEFINES_H