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 #ifdef __GNUC__ 21 #define AK_FORCE_INLINE __attribute__((always_inline)) __inline__ 22 #else // __GNUC__ 23 #define AK_FORCE_INLINE inline 24 #endif // __GNUC__ 25 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 31 // Must be equal to Constants.Dictionary.MAX_WORD_LENGTH in Java 32 #define MAX_WORD_LENGTH 48 33 // Must be equal to BinaryDictionary.MAX_RESULTS in Java 34 #define MAX_RESULTS 18 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 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 } 88 89 #if defined(FLAG_DO_PROFILE) || defined(FLAG_DBG) 90 #include <android/log.h> 91 #ifndef LOG_TAG 92 #define LOG_TAG "LatinIME: " 93 #endif // LOG_TAG 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__) 96 97 #define DUMP_RESULT(words, frequencies) do { dumpResult(words, frequencies); } while (0) 98 #define DUMP_WORD(word, length) do { dumpWord(word, length); } while (0) 99 #define INTS_TO_CHARS(input, length, output, outlength) do { \ 100 intArrayToCharArray(input, length, output, outlength); } while (0) 101 102 static inline void dumpWordInfo(const int *word, const int length, const int rank, 103 const int probability) { 104 static char charBuf[50]; 105 const int N = intArrayToCharArray(word, length, charBuf, NELEMS(charBuf)); 106 if (N > 1) { 107 AKLOGI("%2d [ %s ] (%d)", rank, charBuf, probability); 108 } 109 } 110 111 static inline void dumpResult(const int *outWords, const int *frequencies) { 112 AKLOGI("--- DUMP RESULT ---------"); 113 for (int i = 0; i < MAX_RESULTS; ++i) { 114 dumpWordInfo(&outWords[i * MAX_WORD_LENGTH], MAX_WORD_LENGTH, i, frequencies[i]); 115 } 116 AKLOGI("-------------------------"); 117 } 118 119 static AK_FORCE_INLINE void dumpWord(const int *word, const int length) { 120 static char charBuf[50]; 121 const int N = intArrayToCharArray(word, length, charBuf, NELEMS(charBuf)); 122 if (N > 1) { 123 AKLOGI("[ %s ]", charBuf); 124 } 125 } 126 127 #ifndef __ANDROID__ 128 #include <cassert> 129 #include <execinfo.h> 130 #include <stdlib.h> 131 132 #define DO_ASSERT_TEST 133 #define ASSERT(success) do { if (!(success)) { showStackTrace(); assert(success);} } while (0) 134 #define SHOW_STACK_TRACE do { showStackTrace(); } while (0) 135 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 } 149 #else // __ANDROID__ 150 #include <cassert> 151 #define DO_ASSERT_TEST 152 #define ASSERT(success) assert(success) 153 #define SHOW_STACK_TRACE 154 #endif // __ANDROID__ 155 156 #else // defined(FLAG_DO_PROFILE) || defined(FLAG_DBG) 157 #define AKLOGE(fmt, ...) 158 #define AKLOGI(fmt, ...) 159 #define DUMP_RESULT(words, frequencies) 160 #define DUMP_WORD(word, length) 161 #undef DO_ASSERT_TEST 162 #define ASSERT(success) 163 #define SHOW_STACK_TRACE 164 #define INTS_TO_CHARS(input, length, output) 165 #endif // defined(FLAG_DO_PROFILE) || defined(FLAG_DBG) 166 167 #ifdef FLAG_DO_PROFILE 168 // Profiler 169 #include <time.h> 170 171 #define PROF_BUF_SIZE 100 172 static float profile_buf[PROF_BUF_SIZE]; 173 static float profile_old[PROF_BUF_SIZE]; 174 static unsigned int profile_counter[PROF_BUF_SIZE]; 175 176 #define PROF_RESET prof_reset() 177 #define PROF_COUNT(prof_buf_id) ++profile_counter[prof_buf_id] 178 #define PROF_OPEN do { PROF_RESET; PROF_START(PROF_BUF_SIZE - 1); } while (0) 179 #define PROF_START(prof_buf_id) do { \ 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) 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) \ 184 AKLOGI("%s : clock is %f", __FUNCTION__, (clock() - profile_old[prof_buf_id])) 185 #define PROF_OUTALL do { AKLOGI("--- %s ---", __FUNCTION__); prof_out(); } while (0) 186 187 static inline void prof_reset(void) { 188 for (int i = 0; i < PROF_BUF_SIZE; ++i) { 189 profile_buf[i] = 0; 190 profile_old[i] = 0; 191 profile_counter[i] = 0; 192 } 193 } 194 195 static inline void prof_out(void) { 196 if (profile_counter[PROF_BUF_SIZE - 1] != 1) { 197 AKLOGI("Error: You must call PROF_OPEN before PROF_CLOSE."); 198 } 199 AKLOGI("Total time is %6.3f ms.", 200 profile_buf[PROF_BUF_SIZE - 1] * 1000.0f / static_cast<float>(CLOCKS_PER_SEC)); 201 float all = 0.0f; 202 for (int i = 0; i < PROF_BUF_SIZE - 1; ++i) { 203 all += profile_buf[i]; 204 } 205 if (all < 1.0f) all = 1.0f; 206 for (int i = 0; i < PROF_BUF_SIZE - 1; ++i) { 207 if (profile_buf[i] > 0.0f) { 208 AKLOGI("(%d): Used %4.2f%%, %8.4f ms. Called %d times.", 209 i, (profile_buf[i] * 100.0f / all), 210 profile_buf[i] * 1000.0f / static_cast<float>(CLOCKS_PER_SEC), 211 profile_counter[i]); 212 } 213 } 214 } 215 216 #else // FLAG_DO_PROFILE 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 228 #endif // FLAG_DO_PROFILE 229 230 #ifdef FLAG_DBG 231 #define DEBUG_DICT true 232 #define DEBUG_DICT_FULL false 233 #define DEBUG_EDIT_DISTANCE false 234 #define DEBUG_NODE DEBUG_DICT_FULL 235 #define DEBUG_TRACE DEBUG_DICT_FULL 236 #define DEBUG_PROXIMITY_INFO false 237 #define DEBUG_PROXIMITY_CHARS false 238 #define DEBUG_CORRECTION false 239 #define DEBUG_CORRECTION_FREQ false 240 #define DEBUG_SAMPLING_POINTS false 241 #define DEBUG_POINTS_PROBABILITY false 242 #define DEBUG_DOUBLE_LETTER false 243 #define DEBUG_CACHE false 244 #define DEBUG_DUMP_ERROR false 245 #define DEBUG_EVALUATE_MOST_PROBABLE_STRING false 246 247 #ifdef FLAG_FULL_DBG 248 #define DEBUG_GEO_FULL true 249 #else 250 #define DEBUG_GEO_FULL false 251 #endif 252 253 #else // FLAG_DBG 254 255 #define DEBUG_DICT false 256 #define DEBUG_DICT_FULL false 257 #define DEBUG_EDIT_DISTANCE false 258 #define DEBUG_NODE false 259 #define DEBUG_TRACE false 260 #define DEBUG_PROXIMITY_INFO false 261 #define DEBUG_PROXIMITY_CHARS false 262 #define DEBUG_CORRECTION false 263 #define DEBUG_CORRECTION_FREQ false 264 #define DEBUG_SAMPLING_POINTS false 265 #define DEBUG_POINTS_PROBABILITY false 266 #define DEBUG_DOUBLE_LETTER false 267 #define DEBUG_CACHE false 268 #define DEBUG_DUMP_ERROR false 269 #define DEBUG_EVALUATE_MOST_PROBABLE_STRING false 270 271 #define DEBUG_GEO_FULL false 272 273 #endif // FLAG_DBG 274 275 #ifndef S_INT_MAX 276 #define S_INT_MAX 2147483647 // ((1 << 31) - 1) 277 #endif 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 284 285 #define M_PI_F 3.14159265f 286 #define MAX_PERCENTILE 100 287 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 294 295 #define NOT_A_CODE_POINT (-1) 296 #define NOT_A_DISTANCE (-1) 297 #define NOT_A_COORDINATE (-1) 298 #define NOT_AN_INDEX (-1) 299 #define NOT_A_PROBABILITY (-1) 300 #define NOT_A_DICT_POS (S_INT_MIN) 301 302 // A special value to mean the first word confidence makes no sense in this case, 303 // e.g. this is not a multi-word suggestion. 304 #define NOT_A_FIRST_WORD_CONFIDENCE (S_INT_MAX) 305 // How high the confidence needs to be for us to auto-commit. Arbitrary. 306 // This needs to be the same as CONFIDENCE_FOR_AUTO_COMMIT in BinaryDictionary.java 307 #define CONFIDENCE_FOR_AUTO_COMMIT (1000000) 308 // 80% of the full confidence 309 #define DISTANCE_WEIGHT_FOR_AUTO_COMMIT (80 * CONFIDENCE_FOR_AUTO_COMMIT / 100) 310 // 100% of the full confidence 311 #define LENGTH_WEIGHT_FOR_AUTO_COMMIT (CONFIDENCE_FOR_AUTO_COMMIT) 312 // 80% of the full confidence 313 #define SPACE_COUNT_WEIGHT_FOR_AUTO_COMMIT (80 * CONFIDENCE_FOR_AUTO_COMMIT / 100) 314 315 #define KEYCODE_SPACE ' ' 316 #define KEYCODE_SINGLE_QUOTE '\'' 317 #define KEYCODE_HYPHEN_MINUS '-' 318 319 #define SUGGEST_INTERFACE_OUTPUT_SCALE 1000000.0f 320 #define MAX_PROBABILITY 255 321 #define MAX_BIGRAM_ENCODED_PROBABILITY 15 322 323 // Assuming locale strings such as en_US, sr-Latn etc. 324 #define MAX_LOCALE_STRING_LENGTH 10 325 326 // Max value for length, distance and probability which are used in weighting 327 // TODO: Remove 328 #define MAX_VALUE_FOR_WEIGHTING 10000000 329 330 // The max number of the keys in one keyboard layout 331 #define MAX_KEY_COUNT_IN_A_KEYBOARD 64 332 333 // TODO: Remove 334 #define MAX_POINTER_COUNT 1 335 #define MAX_POINTER_COUNT_G 2 336 337 template<typename T> AK_FORCE_INLINE const T &min(const T &a, const T &b) { return a < b ? a : b; } 338 template<typename T> AK_FORCE_INLINE const T &max(const T &a, const T &b) { return a > b ? a : b; } 339 340 // DEBUG 341 #define INPUTLENGTH_FOR_DEBUG (-1) 342 #define MIN_OUTPUT_INDEX_FOR_DEBUG (-1) 343 344 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ 345 TypeName(const TypeName&); \ 346 void operator=(const TypeName&) 347 348 #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \ 349 TypeName(); \ 350 DISALLOW_COPY_AND_ASSIGN(TypeName) 351 352 // Used as a return value for character comparison 353 typedef enum { 354 // Same char, possibly with different case or accent 355 MATCH_CHAR, 356 // It is a char located nearby on the keyboard 357 PROXIMITY_CHAR, 358 // Additional proximity char which can differ by language. 359 ADDITIONAL_PROXIMITY_CHAR, 360 // It is a substitution char 361 SUBSTITUTION_CHAR, 362 // It is an unrelated char 363 UNRELATED_CHAR, 364 } ProximityType; 365 366 typedef enum { 367 NOT_A_DOUBLE_LETTER, 368 A_DOUBLE_LETTER, 369 A_STRONG_DOUBLE_LETTER 370 } DoubleLetterLevel; 371 372 typedef enum { 373 // Correction for MATCH_CHAR 374 CT_MATCH, 375 // Correction for PROXIMITY_CHAR 376 CT_PROXIMITY, 377 // Correction for ADDITIONAL_PROXIMITY_CHAR 378 CT_ADDITIONAL_PROXIMITY, 379 // Correction for SUBSTITUTION_CHAR 380 CT_SUBSTITUTION, 381 // Skip one omitted letter 382 CT_OMISSION, 383 // Delete an unnecessarily inserted letter 384 CT_INSERTION, 385 // Swap the order of next two touch points 386 CT_TRANSPOSITION, 387 CT_COMPLETION, 388 CT_TERMINAL, 389 CT_TERMINAL_INSERTION, 390 // Create new word with space omission 391 CT_NEW_WORD_SPACE_OMISSION, 392 // Create new word with space substitution 393 CT_NEW_WORD_SPACE_SUBSTITUTION, 394 } CorrectionType; 395 396 // ErrorType is mainly decided by CorrectionType but it is also depending on if 397 // the correction has really been performed or not. 398 typedef enum { 399 // Substitution, omission and transposition 400 ET_EDIT_CORRECTION, 401 // Proximity error 402 ET_PROXIMITY_CORRECTION, 403 // Completion 404 ET_COMPLETION, 405 // New word 406 // TODO: Remove. 407 // A new word error should be an edit correction error or a proximity correction error. 408 ET_NEW_WORD, 409 // Treat error as an intentional omission when the CorrectionType is omission and the node can 410 // be intentional omission. 411 ET_INTENTIONAL_OMISSION, 412 // Not treated as an error. Tracked for checking exact match 413 ET_NOT_AN_ERROR 414 } ErrorType; 415 #endif // LATINIME_DEFINES_H 416