satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 1 | /* |
Ken Wakasa | a10b1a8 | 2013-01-08 17:23:43 +0900 | [diff] [blame] | 2 | * Copyright (C) 2010 The Android Open Source Project |
Ken Wakasa | 0bbb917 | 2012-07-25 17:51:43 +0900 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
Ken Wakasa | a10b1a8 | 2013-01-08 17:23:43 +0900 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Ken Wakasa | 0bbb917 | 2012-07-25 17:51:43 +0900 | [diff] [blame] | 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 16 | |
| 17 | #ifndef LATINIME_DEFINES_H |
| 18 | #define LATINIME_DEFINES_H |
| 19 | |
Ken Wakasa | 1ce96fe | 2012-11-15 19:09:11 +0900 | [diff] [blame] | 20 | #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 Wakasa | 6cee61d | 2013-01-15 16:15:48 +0900 | [diff] [blame] | 26 | #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 Wakasa | 6c22439 | 2013-01-22 13:14:53 +0900 | [diff] [blame] | 31 | // Must be equal to Constants.Dictionary.MAX_WORD_LENGTH in Java |
Ken Wakasa | 5db594a | 2013-01-12 01:18:00 +0900 | [diff] [blame] | 32 | #define MAX_WORD_LENGTH 48 |
Ken Wakasa | 6c22439 | 2013-01-22 13:14:53 +0900 | [diff] [blame] | 33 | // Must be equal to BinaryDictionary.MAX_RESULTS in Java |
Ken Wakasa | 5db594a | 2013-01-12 01:18:00 +0900 | [diff] [blame] | 34 | #define MAX_RESULTS 18 |
Ken Wakasa | 6c22439 | 2013-01-22 13:14:53 +0900 | [diff] [blame] | 35 | // 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 |
Jean Chalard | 7eba019 | 2013-06-27 17:31:13 +0900 | [diff] [blame] | 38 | #define NELEMS(x) (sizeof(x) / sizeof((x)[0])) |
| 39 | |
| 40 | AK_FORCE_INLINE static int intArrayToCharArray(const int *const source, const int sourceSize, |
| 41 | char *dest, const int destSize) { |
| 42 | // We want to always terminate with a 0 char, so stop one short of the length to make |
| 43 | // sure there is room. |
| 44 | const int destLimit = destSize - 1; |
| 45 | int si = 0; |
| 46 | int di = 0; |
| 47 | while (si < sourceSize && di < destLimit && 0 != source[si]) { |
| 48 | const int codePoint = source[si++]; |
| 49 | if (codePoint < 0x7F) { // One byte |
| 50 | dest[di++] = codePoint; |
| 51 | } else if (codePoint < 0x7FF) { // Two bytes |
| 52 | if (di + 1 >= destLimit) break; |
| 53 | dest[di++] = 0xC0 + (codePoint >> 6); |
| 54 | dest[di++] = 0x80 + (codePoint & 0x3F); |
| 55 | } else if (codePoint < 0xFFFF) { // Three bytes |
| 56 | if (di + 2 >= destLimit) break; |
| 57 | dest[di++] = 0xE0 + (codePoint >> 12); |
| 58 | dest[di++] = 0x80 + ((codePoint >> 6) & 0x3F); |
| 59 | dest[di++] = 0x80 + (codePoint & 0x3F); |
| 60 | } else if (codePoint <= 0x1FFFFF) { // Four bytes |
| 61 | if (di + 3 >= destLimit) break; |
| 62 | dest[di++] = 0xF0 + (codePoint >> 18); |
| 63 | dest[di++] = 0x80 + ((codePoint >> 12) & 0x3F); |
| 64 | dest[di++] = 0x80 + ((codePoint >> 6) & 0x3F); |
| 65 | dest[di++] = 0x80 + (codePoint & 0x3F); |
| 66 | } else if (codePoint <= 0x3FFFFFF) { // Five bytes |
| 67 | if (di + 4 >= destLimit) break; |
| 68 | dest[di++] = 0xF8 + (codePoint >> 24); |
| 69 | dest[di++] = 0x80 + ((codePoint >> 18) & 0x3F); |
| 70 | dest[di++] = 0x80 + ((codePoint >> 12) & 0x3F); |
| 71 | dest[di++] = 0x80 + ((codePoint >> 6) & 0x3F); |
| 72 | dest[di++] = codePoint & 0x3F; |
| 73 | } else if (codePoint <= 0x7FFFFFFF) { // Six bytes |
| 74 | if (di + 5 >= destLimit) break; |
| 75 | dest[di++] = 0xFC + (codePoint >> 30); |
| 76 | dest[di++] = 0x80 + ((codePoint >> 24) & 0x3F); |
| 77 | dest[di++] = 0x80 + ((codePoint >> 18) & 0x3F); |
| 78 | dest[di++] = 0x80 + ((codePoint >> 12) & 0x3F); |
| 79 | dest[di++] = 0x80 + ((codePoint >> 6) & 0x3F); |
| 80 | dest[di++] = codePoint & 0x3F; |
| 81 | } else { |
| 82 | // Not a code point... skip. |
| 83 | } |
| 84 | } |
| 85 | dest[di] = 0; |
| 86 | return di; |
| 87 | } |
Satoshi Kataoka | 4221738 | 2012-12-17 23:28:17 +0900 | [diff] [blame] | 88 | |
satok | 827ced8 | 2011-07-14 09:01:09 +0900 | [diff] [blame] | 89 | #if defined(FLAG_DO_PROFILE) || defined(FLAG_DBG) |
Ken Wakasa | e3f26dd | 2012-07-27 18:06:06 +0900 | [diff] [blame] | 90 | #include <android/log.h> |
| 91 | #ifndef LOG_TAG |
| 92 | #define LOG_TAG "LatinIME: " |
Ken Wakasa | 6cee61d | 2013-01-15 16:15:48 +0900 | [diff] [blame] | 93 | #endif // LOG_TAG |
Ken Wakasa | e3f26dd | 2012-07-27 18:06:06 +0900 | [diff] [blame] | 94 | #define AKLOGE(fmt, ...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, fmt, ##__VA_ARGS__) |
| 95 | #define AKLOGI(fmt, ...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, fmt, ##__VA_ARGS__) |
satok | 6ad15fc | 2012-01-16 16:21:21 +0900 | [diff] [blame] | 96 | |
Ken Wakasa | 5db594a | 2013-01-12 01:18:00 +0900 | [diff] [blame] | 97 | #define DUMP_RESULT(words, frequencies) do { dumpResult(words, frequencies); } while (0) |
Ken Wakasa | f278981 | 2012-09-04 12:49:46 +0900 | [diff] [blame] | 98 | #define DUMP_WORD(word, length) do { dumpWord(word, length); } while (0) |
Jean Chalard | 7eba019 | 2013-06-27 17:31:13 +0900 | [diff] [blame] | 99 | #define INTS_TO_CHARS(input, length, output, outlength) do { \ |
| 100 | intArrayToCharArray(input, length, output, outlength); } while (0) |
Satoshi Kataoka | 4221738 | 2012-12-17 23:28:17 +0900 | [diff] [blame] | 101 | |
Ken Wakasa | 1e61493 | 2012-10-29 18:06:22 +0900 | [diff] [blame] | 102 | static inline void dumpWordInfo(const int *word, const int length, const int rank, |
Satoshi Kataoka | e0e6737 | 2013-03-18 13:08:31 +0900 | [diff] [blame] | 103 | const int probability) { |
Satoshi Kataoka | 586b0ca | 2012-08-06 11:20:54 +0900 | [diff] [blame] | 104 | static char charBuf[50]; |
Jean Chalard | 7eba019 | 2013-06-27 17:31:13 +0900 | [diff] [blame] | 105 | const int N = intArrayToCharArray(word, length, charBuf, NELEMS(charBuf)); |
Satoshi Kataoka | 4221738 | 2012-12-17 23:28:17 +0900 | [diff] [blame] | 106 | if (N > 1) { |
Satoshi Kataoka | e0e6737 | 2013-03-18 13:08:31 +0900 | [diff] [blame] | 107 | AKLOGI("%2d [ %s ] (%d)", rank, charBuf, probability); |
Satoshi Kataoka | 586b0ca | 2012-08-06 11:20:54 +0900 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
Ken Wakasa | 5db594a | 2013-01-12 01:18:00 +0900 | [diff] [blame] | 111 | static inline void dumpResult(const int *outWords, const int *frequencies) { |
Satoshi Kataoka | 586b0ca | 2012-08-06 11:20:54 +0900 | [diff] [blame] | 112 | AKLOGI("--- DUMP RESULT ---------"); |
Ken Wakasa | 5db594a | 2013-01-12 01:18:00 +0900 | [diff] [blame] | 113 | for (int i = 0; i < MAX_RESULTS; ++i) { |
| 114 | dumpWordInfo(&outWords[i * MAX_WORD_LENGTH], MAX_WORD_LENGTH, i, frequencies[i]); |
Satoshi Kataoka | 586b0ca | 2012-08-06 11:20:54 +0900 | [diff] [blame] | 115 | } |
| 116 | AKLOGI("-------------------------"); |
| 117 | } |
| 118 | |
Ken Wakasa | 1ce96fe | 2012-11-15 19:09:11 +0900 | [diff] [blame] | 119 | static AK_FORCE_INLINE void dumpWord(const int *word, const int length) { |
Tadashi G. Takaoka | d1dbdb6 | 2012-03-06 15:35:46 +0900 | [diff] [blame] | 120 | static char charBuf[50]; |
Jean Chalard | 7eba019 | 2013-06-27 17:31:13 +0900 | [diff] [blame] | 121 | const int N = intArrayToCharArray(word, length, charBuf, NELEMS(charBuf)); |
Satoshi Kataoka | 4221738 | 2012-12-17 23:28:17 +0900 | [diff] [blame] | 122 | if (N > 1) { |
Satoshi Kataoka | 586b0ca | 2012-08-06 11:20:54 +0900 | [diff] [blame] | 123 | AKLOGI("[ %s ]", charBuf); |
| 124 | } |
satok | 6ad15fc | 2012-01-16 16:21:21 +0900 | [diff] [blame] | 125 | } |
| 126 | |
Satoshi Kataoka | 5540acb | 2012-09-03 18:35:32 +0900 | [diff] [blame] | 127 | #ifndef __ANDROID__ |
Satoshi Kataoka | 1c8fc83 | 2012-09-06 21:31:54 +0900 | [diff] [blame] | 128 | #include <cassert> |
Satoshi Kataoka | 5540acb | 2012-09-03 18:35:32 +0900 | [diff] [blame] | 129 | #include <execinfo.h> |
| 130 | #include <stdlib.h> |
Satoshi Kataoka | 1c8fc83 | 2012-09-06 21:31:54 +0900 | [diff] [blame] | 131 | |
Satoshi Kataoka | dd4d938 | 2013-01-09 12:54:39 +0900 | [diff] [blame] | 132 | #define DO_ASSERT_TEST |
Satoshi Kataoka | 1c8fc83 | 2012-09-06 21:31:54 +0900 | [diff] [blame] | 133 | #define ASSERT(success) do { if (!(success)) { showStackTrace(); assert(success);} } while (0) |
| 134 | #define SHOW_STACK_TRACE do { showStackTrace(); } while (0) |
| 135 | |
Satoshi Kataoka | 5540acb | 2012-09-03 18:35:32 +0900 | [diff] [blame] | 136 | static inline void showStackTrace() { |
| 137 | void *callstack[128]; |
| 138 | int i, frames = backtrace(callstack, 128); |
| 139 | char **strs = backtrace_symbols(callstack, frames); |
| 140 | for (i = 0; i < frames; ++i) { |
| 141 | if (i == 0) { |
| 142 | AKLOGI("=== Trace ==="); |
| 143 | continue; |
| 144 | } |
| 145 | AKLOGI("%s", strs[i]); |
| 146 | } |
| 147 | free(strs); |
| 148 | } |
Ken Wakasa | 6cee61d | 2013-01-15 16:15:48 +0900 | [diff] [blame] | 149 | #else // __ANDROID__ |
Satoshi Kataoka | 1c8fc83 | 2012-09-06 21:31:54 +0900 | [diff] [blame] | 150 | #include <cassert> |
Satoshi Kataoka | dd4d938 | 2013-01-09 12:54:39 +0900 | [diff] [blame] | 151 | #define DO_ASSERT_TEST |
Satoshi Kataoka | 1c8fc83 | 2012-09-06 21:31:54 +0900 | [diff] [blame] | 152 | #define ASSERT(success) assert(success) |
Satoshi Kataoka | 5540acb | 2012-09-03 18:35:32 +0900 | [diff] [blame] | 153 | #define SHOW_STACK_TRACE |
Ken Wakasa | 6cee61d | 2013-01-15 16:15:48 +0900 | [diff] [blame] | 154 | #endif // __ANDROID__ |
Satoshi Kataoka | 5540acb | 2012-09-03 18:35:32 +0900 | [diff] [blame] | 155 | |
Ken Wakasa | 6cee61d | 2013-01-15 16:15:48 +0900 | [diff] [blame] | 156 | #else // defined(FLAG_DO_PROFILE) || defined(FLAG_DBG) |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 157 | #define AKLOGE(fmt, ...) |
| 158 | #define AKLOGI(fmt, ...) |
Ken Wakasa | 5db594a | 2013-01-12 01:18:00 +0900 | [diff] [blame] | 159 | #define DUMP_RESULT(words, frequencies) |
satok | 6ad15fc | 2012-01-16 16:21:21 +0900 | [diff] [blame] | 160 | #define DUMP_WORD(word, length) |
Satoshi Kataoka | dd4d938 | 2013-01-09 12:54:39 +0900 | [diff] [blame] | 161 | #undef DO_ASSERT_TEST |
Satoshi Kataoka | 5540acb | 2012-09-03 18:35:32 +0900 | [diff] [blame] | 162 | #define ASSERT(success) |
| 163 | #define SHOW_STACK_TRACE |
Ken Wakasa | 1e61493 | 2012-10-29 18:06:22 +0900 | [diff] [blame] | 164 | #define INTS_TO_CHARS(input, length, output) |
Ken Wakasa | 6cee61d | 2013-01-15 16:15:48 +0900 | [diff] [blame] | 165 | #endif // defined(FLAG_DO_PROFILE) || defined(FLAG_DBG) |
satok | 827ced8 | 2011-07-14 09:01:09 +0900 | [diff] [blame] | 166 | |
satok | 20d9fda | 2011-07-13 14:40:30 +0900 | [diff] [blame] | 167 | #ifdef FLAG_DO_PROFILE |
satok | 61e2f85 | 2011-01-05 14:13:07 +0900 | [diff] [blame] | 168 | // Profiler |
| 169 | #include <time.h> |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 170 | |
satok | 61e2f85 | 2011-01-05 14:13:07 +0900 | [diff] [blame] | 171 | #define PROF_BUF_SIZE 100 |
satok | 0028ed3 | 2012-05-16 20:42:12 +0900 | [diff] [blame] | 172 | static float profile_buf[PROF_BUF_SIZE]; |
| 173 | static float profile_old[PROF_BUF_SIZE]; |
satok | 61e2f85 | 2011-01-05 14:13:07 +0900 | [diff] [blame] | 174 | static unsigned int profile_counter[PROF_BUF_SIZE]; |
| 175 | |
Ken Wakasa | e90b333 | 2011-01-07 15:01:51 +0900 | [diff] [blame] | 176 | #define PROF_RESET prof_reset() |
| 177 | #define PROF_COUNT(prof_buf_id) ++profile_counter[prof_buf_id] |
Ken Wakasa | f278981 | 2012-09-04 12:49:46 +0900 | [diff] [blame] | 178 | #define PROF_OPEN do { PROF_RESET; PROF_START(PROF_BUF_SIZE - 1); } while (0) |
Ken Wakasa | e90b333 | 2011-01-07 15:01:51 +0900 | [diff] [blame] | 179 | #define PROF_START(prof_buf_id) do { \ |
Ken Wakasa | f278981 | 2012-09-04 12:49:46 +0900 | [diff] [blame] | 180 | PROF_COUNT(prof_buf_id); profile_old[prof_buf_id] = (clock()); } while (0) |
| 181 | #define PROF_CLOSE do { PROF_END(PROF_BUF_SIZE - 1); PROF_OUTALL; } while (0) |
Ken Wakasa | e90b333 | 2011-01-07 15:01:51 +0900 | [diff] [blame] | 182 | #define PROF_END(prof_buf_id) profile_buf[prof_buf_id] += ((clock()) - profile_old[prof_buf_id]) |
| 183 | #define PROF_CLOCKOUT(prof_buf_id) \ |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 184 | AKLOGI("%s : clock is %f", __FUNCTION__, (clock() - profile_old[prof_buf_id])) |
Ken Wakasa | f278981 | 2012-09-04 12:49:46 +0900 | [diff] [blame] | 185 | #define PROF_OUTALL do { AKLOGI("--- %s ---", __FUNCTION__); prof_out(); } while (0) |
satok | 61e2f85 | 2011-01-05 14:13:07 +0900 | [diff] [blame] | 186 | |
Tadashi G. Takaoka | d1dbdb6 | 2012-03-06 15:35:46 +0900 | [diff] [blame] | 187 | static inline void prof_reset(void) { |
Ken Wakasa | e90b333 | 2011-01-07 15:01:51 +0900 | [diff] [blame] | 188 | for (int i = 0; i < PROF_BUF_SIZE; ++i) { |
satok | 61e2f85 | 2011-01-05 14:13:07 +0900 | [diff] [blame] | 189 | profile_buf[i] = 0; |
| 190 | profile_old[i] = 0; |
| 191 | profile_counter[i] = 0; |
| 192 | } |
| 193 | } |
| 194 | |
Tadashi G. Takaoka | d1dbdb6 | 2012-03-06 15:35:46 +0900 | [diff] [blame] | 195 | static inline void prof_out(void) { |
satok | 61e2f85 | 2011-01-05 14:13:07 +0900 | [diff] [blame] | 196 | if (profile_counter[PROF_BUF_SIZE - 1] != 1) { |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 197 | AKLOGI("Error: You must call PROF_OPEN before PROF_CLOSE."); |
satok | 61e2f85 | 2011-01-05 14:13:07 +0900 | [diff] [blame] | 198 | } |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 199 | AKLOGI("Total time is %6.3f ms.", |
Ken Wakasa | 77e8e81 | 2012-08-02 19:48:08 +0900 | [diff] [blame] | 200 | profile_buf[PROF_BUF_SIZE - 1] * 1000.0f / static_cast<float>(CLOCKS_PER_SEC)); |
Ken Wakasa | 2a6f58d | 2012-11-27 19:40:38 +0900 | [diff] [blame] | 201 | float all = 0.0f; |
Ken Wakasa | e90b333 | 2011-01-07 15:01:51 +0900 | [diff] [blame] | 202 | for (int i = 0; i < PROF_BUF_SIZE - 1; ++i) { |
satok | 61e2f85 | 2011-01-05 14:13:07 +0900 | [diff] [blame] | 203 | all += profile_buf[i]; |
| 204 | } |
Ken Wakasa | 2a6f58d | 2012-11-27 19:40:38 +0900 | [diff] [blame] | 205 | if (all < 1.0f) all = 1.0f; |
Ken Wakasa | e90b333 | 2011-01-07 15:01:51 +0900 | [diff] [blame] | 206 | for (int i = 0; i < PROF_BUF_SIZE - 1; ++i) { |
Ken Wakasa | 2a6f58d | 2012-11-27 19:40:38 +0900 | [diff] [blame] | 207 | if (profile_buf[i] > 0.0f) { |
satok | 9fb6f47 | 2012-01-13 18:01:22 +0900 | [diff] [blame] | 208 | AKLOGI("(%d): Used %4.2f%%, %8.4f ms. Called %d times.", |
Ken Wakasa | 2a6f58d | 2012-11-27 19:40:38 +0900 | [diff] [blame] | 209 | i, (profile_buf[i] * 100.0f / all), |
Ken Wakasa | 77e8e81 | 2012-08-02 19:48:08 +0900 | [diff] [blame] | 210 | profile_buf[i] * 1000.0f / static_cast<float>(CLOCKS_PER_SEC), |
| 211 | profile_counter[i]); |
Ken Wakasa | e90b333 | 2011-01-07 15:01:51 +0900 | [diff] [blame] | 212 | } |
satok | 61e2f85 | 2011-01-05 14:13:07 +0900 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
satok | 20d9fda | 2011-07-13 14:40:30 +0900 | [diff] [blame] | 216 | #else // FLAG_DO_PROFILE |
satok | 61e2f85 | 2011-01-05 14:13:07 +0900 | [diff] [blame] | 217 | #define PROF_BUF_SIZE 0 |
| 218 | #define PROF_RESET |
| 219 | #define PROF_COUNT(prof_buf_id) |
| 220 | #define PROF_OPEN |
| 221 | #define PROF_START(prof_buf_id) |
| 222 | #define PROF_CLOSE |
| 223 | #define PROF_END(prof_buf_id) |
| 224 | #define PROF_CLOCK_OUT(prof_buf_id) |
| 225 | #define PROF_CLOCKOUT(prof_buf_id) |
| 226 | #define PROF_OUTALL |
| 227 | |
satok | 20d9fda | 2011-07-13 14:40:30 +0900 | [diff] [blame] | 228 | #endif // FLAG_DO_PROFILE |
| 229 | |
| 230 | #ifdef FLAG_DBG |
satok | 20d9fda | 2011-07-13 14:40:30 +0900 | [diff] [blame] | 231 | #define DEBUG_DICT true |
| 232 | #define DEBUG_DICT_FULL false |
satok | 0cedd2b | 2011-08-12 01:05:27 +0900 | [diff] [blame] | 233 | #define DEBUG_EDIT_DISTANCE false |
satok | 20d9fda | 2011-07-13 14:40:30 +0900 | [diff] [blame] | 234 | #define DEBUG_NODE DEBUG_DICT_FULL |
| 235 | #define DEBUG_TRACE DEBUG_DICT_FULL |
satok | 1a6da63 | 2011-12-16 23:15:06 +0900 | [diff] [blame] | 236 | #define DEBUG_PROXIMITY_INFO false |
satok | 0cb2097 | 2012-03-13 22:07:56 +0900 | [diff] [blame] | 237 | #define DEBUG_PROXIMITY_CHARS false |
satok | 10266c0 | 2011-08-19 22:05:59 +0900 | [diff] [blame] | 238 | #define DEBUG_CORRECTION false |
satok | 29dc806 | 2012-01-17 15:59:15 +0900 | [diff] [blame] | 239 | #define DEBUG_CORRECTION_FREQ false |
Satoshi Kataoka | 0ed8c6e | 2012-12-04 16:28:06 +0900 | [diff] [blame] | 240 | #define DEBUG_SAMPLING_POINTS false |
| 241 | #define DEBUG_POINTS_PROBABILITY false |
| 242 | #define DEBUG_DOUBLE_LETTER false |
Satoshi Kataoka | bc25b80 | 2013-02-21 21:54:21 +0900 | [diff] [blame] | 243 | #define DEBUG_CACHE false |
| 244 | #define DEBUG_DUMP_ERROR false |
Satoshi Kataoka | 8b3009e | 2013-04-10 11:45:12 +0900 | [diff] [blame] | 245 | #define DEBUG_EVALUATE_MOST_PROBABLE_STRING false |
satok | 20d9fda | 2011-07-13 14:40:30 +0900 | [diff] [blame] | 246 | |
Satoshi Kataoka | 952ec49 | 2012-09-11 15:51:38 +0900 | [diff] [blame] | 247 | #ifdef FLAG_FULL_DBG |
| 248 | #define DEBUG_GEO_FULL true |
| 249 | #else |
Satoshi Kataoka | 23a57ea | 2012-09-10 17:59:17 +0900 | [diff] [blame] | 250 | #define DEBUG_GEO_FULL false |
Satoshi Kataoka | 952ec49 | 2012-09-11 15:51:38 +0900 | [diff] [blame] | 251 | #endif |
Satoshi Kataoka | 23a57ea | 2012-09-10 17:59:17 +0900 | [diff] [blame] | 252 | |
satok | 20d9fda | 2011-07-13 14:40:30 +0900 | [diff] [blame] | 253 | #else // FLAG_DBG |
satok | 827ced8 | 2011-07-14 09:01:09 +0900 | [diff] [blame] | 254 | |
satok | 20d9fda | 2011-07-13 14:40:30 +0900 | [diff] [blame] | 255 | #define DEBUG_DICT false |
| 256 | #define DEBUG_DICT_FULL false |
satok | 0cedd2b | 2011-08-12 01:05:27 +0900 | [diff] [blame] | 257 | #define DEBUG_EDIT_DISTANCE false |
satok | 20d9fda | 2011-07-13 14:40:30 +0900 | [diff] [blame] | 258 | #define DEBUG_NODE false |
| 259 | #define DEBUG_TRACE false |
| 260 | #define DEBUG_PROXIMITY_INFO false |
satok | 0cb2097 | 2012-03-13 22:07:56 +0900 | [diff] [blame] | 261 | #define DEBUG_PROXIMITY_CHARS false |
satok | 10266c0 | 2011-08-19 22:05:59 +0900 | [diff] [blame] | 262 | #define DEBUG_CORRECTION false |
| 263 | #define DEBUG_CORRECTION_FREQ false |
Keisuke Kuroyanagi | 806eba4 | 2012-10-09 19:57:08 +0900 | [diff] [blame] | 264 | #define DEBUG_SAMPLING_POINTS false |
| 265 | #define DEBUG_POINTS_PROBABILITY false |
Satoshi Kataoka | 9af5335 | 2012-11-16 23:06:41 +0900 | [diff] [blame] | 266 | #define DEBUG_DOUBLE_LETTER false |
Satoshi Kataoka | bc25b80 | 2013-02-21 21:54:21 +0900 | [diff] [blame] | 267 | #define DEBUG_CACHE false |
| 268 | #define DEBUG_DUMP_ERROR false |
Satoshi Kataoka | 8b3009e | 2013-04-10 11:45:12 +0900 | [diff] [blame] | 269 | #define DEBUG_EVALUATE_MOST_PROBABLE_STRING false |
satok | 20d9fda | 2011-07-13 14:40:30 +0900 | [diff] [blame] | 270 | |
Satoshi Kataoka | 23a57ea | 2012-09-10 17:59:17 +0900 | [diff] [blame] | 271 | #define DEBUG_GEO_FULL false |
| 272 | |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 273 | #endif // FLAG_DBG |
| 274 | |
Jean Chalard | a5d5849 | 2011-02-18 17:50:58 +0900 | [diff] [blame] | 275 | #ifndef S_INT_MAX |
satok | 3c4bb77 | 2011-03-04 22:50:19 -0800 | [diff] [blame] | 276 | #define S_INT_MAX 2147483647 // ((1 << 31) - 1) |
Jean Chalard | a5d5849 | 2011-02-18 17:50:58 +0900 | [diff] [blame] | 277 | #endif |
Jean Chalard | 592f2b3 | 2012-11-20 21:19:26 +0900 | [diff] [blame] | 278 | #ifndef S_INT_MIN |
| 279 | // The literal constant -2147483648 does not work in C prior C90, because |
| 280 | // the compiler tries to fit the positive number into an int and then negate it. |
| 281 | // GCC warns about this. |
| 282 | #define S_INT_MIN (-2147483647 - 1) // -(1 << 31) |
| 283 | #endif |
Satoshi Kataoka | ee62b78 | 2013-01-21 18:29:27 +0900 | [diff] [blame] | 284 | |
Ken Wakasa | 6c22439 | 2013-01-22 13:14:53 +0900 | [diff] [blame] | 285 | #define M_PI_F 3.14159265f |
Satoshi Kataoka | ee62b78 | 2013-01-21 18:29:27 +0900 | [diff] [blame] | 286 | #define MAX_PERCENTILE 100 |
| 287 | |
Jean Chalard | 22025c6 | 2012-11-29 17:33:53 +0900 | [diff] [blame] | 288 | // Number of base-10 digits in the largest integer + 1 to leave room for a zero terminator. |
| 289 | // As such, this is the maximum number of characters will be needed to represent an int as a |
| 290 | // string, including the terminator; this is used as the size of a string buffer large enough to |
| 291 | // hold any value that is intended to fit in an integer, e.g. in the code that reads the header |
| 292 | // of the binary dictionary where a {key,value} string pair scheme is used. |
| 293 | #define LARGEST_INT_DIGIT_COUNT 11 |
satok | 662fe69 | 2010-12-08 17:05:39 +0900 | [diff] [blame] | 294 | |
Keisuke Kuroynagi | 8a71295 | 2013-07-16 15:22:31 +0900 | [diff] [blame] | 295 | #define NOT_A_VALID_WORD_POS (-99) |
Ken Wakasa | f278981 | 2012-09-04 12:49:46 +0900 | [diff] [blame] | 296 | #define NOT_A_CODE_POINT (-1) |
| 297 | #define NOT_A_DISTANCE (-1) |
| 298 | #define NOT_A_COORDINATE (-1) |
Ken Wakasa | f278981 | 2012-09-04 12:49:46 +0900 | [diff] [blame] | 299 | #define NOT_AN_INDEX (-1) |
| 300 | #define NOT_A_PROBABILITY (-1) |
Keisuke Kuroynagi | 68e7edf | 2013-06-27 12:53:31 +0900 | [diff] [blame] | 301 | #define NOT_A_DICT_POS (S_INT_MIN) |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 302 | |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 303 | #define KEYCODE_SPACE ' ' |
Ken Wakasa | 1e61493 | 2012-10-29 18:06:22 +0900 | [diff] [blame] | 304 | #define KEYCODE_SINGLE_QUOTE '\'' |
| 305 | #define KEYCODE_HYPHEN_MINUS '-' |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 306 | |
Tom Ouyang | 31f7ece | 2013-02-21 15:42:05 -0800 | [diff] [blame] | 307 | #define SUGGEST_INTERFACE_OUTPUT_SCALE 1000000.0f |
Satoshi Kataoka | e0e6737 | 2013-03-18 13:08:31 +0900 | [diff] [blame] | 308 | #define MAX_PROBABILITY 255 |
| 309 | #define MAX_BIGRAM_ENCODED_PROBABILITY 15 |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 310 | |
Ken Wakasa | 9e0c711 | 2012-08-09 22:26:58 +0900 | [diff] [blame] | 311 | // Assuming locale strings such as en_US, sr-Latn etc. |
| 312 | #define MAX_LOCALE_STRING_LENGTH 10 |
| 313 | |
Satoshi Kataoka | 830ba67 | 2013-03-05 14:12:06 +0900 | [diff] [blame] | 314 | // Max value for length, distance and probability which are used in weighting |
| 315 | // TODO: Remove |
| 316 | #define MAX_VALUE_FOR_WEIGHTING 10000000 |
Satoshi Kataoka | 687a244 | 2012-08-23 15:46:43 +0900 | [diff] [blame] | 317 | |
Keisuke Kuroyanagi | 95a49a5 | 2012-09-04 17:00:24 +0900 | [diff] [blame] | 318 | // The max number of the keys in one keyboard layout |
| 319 | #define MAX_KEY_COUNT_IN_A_KEYBOARD 64 |
| 320 | |
Ken Wakasa | 5150e15 | 2012-09-27 19:21:25 +0900 | [diff] [blame] | 321 | // TODO: Remove |
Satoshi Kataoka | fe4f1ce | 2012-12-10 17:17:52 +0900 | [diff] [blame] | 322 | #define MAX_POINTER_COUNT 1 |
Satoshi Kataoka | 99eae8e | 2013-01-30 20:55:31 +0900 | [diff] [blame] | 323 | #define MAX_POINTER_COUNT_G 2 |
Ken Wakasa | 5150e15 | 2012-09-27 19:21:25 +0900 | [diff] [blame] | 324 | |
Ken Wakasa | 11dc3a3 | 2013-06-04 15:40:30 +0900 | [diff] [blame] | 325 | // Queue IDs and size for DicNodesCache |
| 326 | #define DIC_NODES_CACHE_INITIAL_QUEUE_ID_ACTIVE 0 |
| 327 | #define DIC_NODES_CACHE_INITIAL_QUEUE_ID_NEXT_ACTIVE 1 |
| 328 | #define DIC_NODES_CACHE_INITIAL_QUEUE_ID_TERMINAL 2 |
| 329 | #define DIC_NODES_CACHE_INITIAL_QUEUE_ID_CACHE_FOR_CONTINUOUS_SUGGESTION 3 |
| 330 | #define DIC_NODES_CACHE_PRIORITY_QUEUES_SIZE 4 |
| 331 | |
Ken Wakasa | 871b8c9 | 2013-01-31 18:05:26 +0900 | [diff] [blame] | 332 | template<typename T> AK_FORCE_INLINE const T &min(const T &a, const T &b) { return a < b ? a : b; } |
| 333 | template<typename T> AK_FORCE_INLINE const T &max(const T &a, const T &b) { return a > b ? a : b; } |
satok | f5cded1 | 2010-12-06 21:28:24 +0900 | [diff] [blame] | 334 | |
satok | e05b3f4 | 2012-01-31 17:15:43 +0900 | [diff] [blame] | 335 | // DEBUG |
Ken Wakasa | f278981 | 2012-09-04 12:49:46 +0900 | [diff] [blame] | 336 | #define INPUTLENGTH_FOR_DEBUG (-1) |
| 337 | #define MIN_OUTPUT_INDEX_FOR_DEBUG (-1) |
satok | e05b3f4 | 2012-01-31 17:15:43 +0900 | [diff] [blame] | 338 | |
satok | 1bc038c | 2012-06-14 11:25:50 -0700 | [diff] [blame] | 339 | #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ |
| 340 | TypeName(const TypeName&); \ |
| 341 | void operator=(const TypeName&) |
| 342 | |
| 343 | #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \ |
| 344 | TypeName(); \ |
| 345 | DISALLOW_COPY_AND_ASSIGN(TypeName) |
| 346 | |
Satoshi Kataoka | 3e8c58f | 2012-06-05 17:55:52 +0900 | [diff] [blame] | 347 | // Used as a return value for character comparison |
| 348 | typedef enum { |
| 349 | // Same char, possibly with different case or accent |
Satoshi Kataoka | f4425aa | 2013-03-07 13:06:32 +0900 | [diff] [blame] | 350 | MATCH_CHAR, |
Satoshi Kataoka | 3e8c58f | 2012-06-05 17:55:52 +0900 | [diff] [blame] | 351 | // It is a char located nearby on the keyboard |
Satoshi Kataoka | f4425aa | 2013-03-07 13:06:32 +0900 | [diff] [blame] | 352 | PROXIMITY_CHAR, |
Satoshi Kataoka | 3e8c58f | 2012-06-05 17:55:52 +0900 | [diff] [blame] | 353 | // Additional proximity char which can differ by language. |
Satoshi Kataoka | f4425aa | 2013-03-07 13:06:32 +0900 | [diff] [blame] | 354 | ADDITIONAL_PROXIMITY_CHAR, |
| 355 | // It is a substitution char |
| 356 | SUBSTITUTION_CHAR, |
| 357 | // It is an unrelated char |
| 358 | UNRELATED_CHAR, |
Satoshi Kataoka | 3e8c58f | 2012-06-05 17:55:52 +0900 | [diff] [blame] | 359 | } ProximityType; |
Satoshi Kataoka | 6ae8dd4 | 2012-11-22 20:15:40 +0900 | [diff] [blame] | 360 | |
| 361 | typedef enum { |
| 362 | NOT_A_DOUBLE_LETTER, |
| 363 | A_DOUBLE_LETTER, |
| 364 | A_STRONG_DOUBLE_LETTER |
| 365 | } DoubleLetterLevel; |
Satoshi Kataoka | d870891 | 2013-03-04 17:05:28 +0900 | [diff] [blame] | 366 | |
| 367 | typedef enum { |
Keisuke Kuroynagi | 911f326 | 2013-04-24 20:21:18 +0900 | [diff] [blame] | 368 | // Correction for MATCH_CHAR |
Satoshi Kataoka | d870891 | 2013-03-04 17:05:28 +0900 | [diff] [blame] | 369 | CT_MATCH, |
Keisuke Kuroynagi | 911f326 | 2013-04-24 20:21:18 +0900 | [diff] [blame] | 370 | // Correction for PROXIMITY_CHAR |
Satoshi Kataoka | f4425aa | 2013-03-07 13:06:32 +0900 | [diff] [blame] | 371 | CT_PROXIMITY, |
Keisuke Kuroynagi | 911f326 | 2013-04-24 20:21:18 +0900 | [diff] [blame] | 372 | // Correction for ADDITIONAL_PROXIMITY_CHAR |
Satoshi Kataoka | f4425aa | 2013-03-07 13:06:32 +0900 | [diff] [blame] | 373 | CT_ADDITIONAL_PROXIMITY, |
Keisuke Kuroynagi | 911f326 | 2013-04-24 20:21:18 +0900 | [diff] [blame] | 374 | // Correction for SUBSTITUTION_CHAR |
Satoshi Kataoka | f4425aa | 2013-03-07 13:06:32 +0900 | [diff] [blame] | 375 | CT_SUBSTITUTION, |
Keisuke Kuroynagi | 911f326 | 2013-04-24 20:21:18 +0900 | [diff] [blame] | 376 | // Skip one omitted letter |
Satoshi Kataoka | d870891 | 2013-03-04 17:05:28 +0900 | [diff] [blame] | 377 | CT_OMISSION, |
Keisuke Kuroynagi | 911f326 | 2013-04-24 20:21:18 +0900 | [diff] [blame] | 378 | // Delete an unnecessarily inserted letter |
Satoshi Kataoka | d870891 | 2013-03-04 17:05:28 +0900 | [diff] [blame] | 379 | CT_INSERTION, |
Keisuke Kuroynagi | 911f326 | 2013-04-24 20:21:18 +0900 | [diff] [blame] | 380 | // Swap the order of next two touch points |
Satoshi Kataoka | d870891 | 2013-03-04 17:05:28 +0900 | [diff] [blame] | 381 | CT_TRANSPOSITION, |
Satoshi Kataoka | d870891 | 2013-03-04 17:05:28 +0900 | [diff] [blame] | 382 | CT_COMPLETION, |
| 383 | CT_TERMINAL, |
Satoshi Kataoka | 75322ce | 2013-06-14 18:40:59 +0900 | [diff] [blame^] | 384 | CT_TERMINAL_INSERTION, |
Keisuke Kuroynagi | 911f326 | 2013-04-24 20:21:18 +0900 | [diff] [blame] | 385 | // Create new word with space omission |
Satoshi Kataoka | 252412d | 2013-04-16 14:39:30 +0900 | [diff] [blame] | 386 | CT_NEW_WORD_SPACE_OMITTION, |
Keisuke Kuroynagi | 911f326 | 2013-04-24 20:21:18 +0900 | [diff] [blame] | 387 | // Create new word with space substitution |
Satoshi Kataoka | 252412d | 2013-04-16 14:39:30 +0900 | [diff] [blame] | 388 | CT_NEW_WORD_SPACE_SUBSTITUTION, |
Satoshi Kataoka | d870891 | 2013-03-04 17:05:28 +0900 | [diff] [blame] | 389 | } CorrectionType; |
Keisuke Kuroynagi | 60a169f | 2013-04-25 16:47:38 +0900 | [diff] [blame] | 390 | |
| 391 | // ErrorType is mainly decided by CorrectionType but it is also depending on if |
| 392 | // the correction has really been performed or not. |
| 393 | typedef enum { |
| 394 | // Substitution, omission and transposition |
| 395 | ET_EDIT_CORRECTION, |
| 396 | // Proximity error |
| 397 | ET_PROXIMITY_CORRECTION, |
| 398 | // Completion |
| 399 | ET_COMPLETION, |
| 400 | // New word |
| 401 | // TODO: Remove. |
| 402 | // A new word error should be an edit correction error or a proximity correction error. |
| 403 | ET_NEW_WORD, |
| 404 | // Treat error as an intentional omission when the CorrectionType is omission and the node can |
| 405 | // be intentional omission. |
| 406 | ET_INTENTIONAL_OMISSION, |
| 407 | // Not treated as an error. Tracked for checking exact match |
| 408 | ET_NOT_AN_ERROR |
| 409 | } ErrorType; |
satok | e808e43 | 2010-12-02 14:53:24 +0900 | [diff] [blame] | 410 | #endif // LATINIME_DEFINES_H |