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_DBG)
27 #undef AK_FORCE_INLINE
28 #define AK_FORCE_INLINE inline
29 #endif // 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
39 // TODO: Use size_t instead of int.
40 // Disclaimer: You will see a compile error if you use this macro against a variable-length array.
41 // Sorry for the inconvenience. It isn't supported.
42 template <typename T, int N>
43 char (&ArraySizeHelper(T (&array)[N]))[N];
44 #define NELEMS(x) (sizeof(ArraySizeHelper(x)))
45
intArrayToCharArray(const int * const source,const int sourceSize,char * dest,const int destSize)46 AK_FORCE_INLINE static int intArrayToCharArray(const int *const source, const int sourceSize,
47 char *dest, const int destSize) {
48 // We want to always terminate with a 0 char, so stop one short of the length to make
49 // sure there is room.
50 const int destLimit = destSize - 1;
51 int si = 0;
52 int di = 0;
53 while (si < sourceSize && di < destLimit && 0 != source[si]) {
54 const int codePoint = source[si++];
55 if (codePoint < 0x7F) { // One byte
56 dest[di++] = codePoint;
57 } else if (codePoint < 0x7FF) { // Two bytes
58 if (di + 1 >= destLimit) break;
59 dest[di++] = 0xC0 + (codePoint >> 6);
60 dest[di++] = 0x80 + (codePoint & 0x3F);
61 } else if (codePoint < 0xFFFF) { // Three bytes
62 if (di + 2 >= destLimit) break;
63 dest[di++] = 0xE0 + (codePoint >> 12);
64 dest[di++] = 0x80 + ((codePoint >> 6) & 0x3F);
65 dest[di++] = 0x80 + (codePoint & 0x3F);
66 } else if (codePoint <= 0x1FFFFF) { // Four bytes
67 if (di + 3 >= destLimit) break;
68 dest[di++] = 0xF0 + (codePoint >> 18);
69 dest[di++] = 0x80 + ((codePoint >> 12) & 0x3F);
70 dest[di++] = 0x80 + ((codePoint >> 6) & 0x3F);
71 dest[di++] = 0x80 + (codePoint & 0x3F);
72 } else if (codePoint <= 0x3FFFFFF) { // Five bytes
73 if (di + 4 >= destLimit) break;
74 dest[di++] = 0xF8 + (codePoint >> 24);
75 dest[di++] = 0x80 + ((codePoint >> 18) & 0x3F);
76 dest[di++] = 0x80 + ((codePoint >> 12) & 0x3F);
77 dest[di++] = 0x80 + ((codePoint >> 6) & 0x3F);
78 dest[di++] = codePoint & 0x3F;
79 } else if (codePoint <= 0x7FFFFFFF) { // Six bytes
80 if (di + 5 >= destLimit) break;
81 dest[di++] = 0xFC + (codePoint >> 30);
82 dest[di++] = 0x80 + ((codePoint >> 24) & 0x3F);
83 dest[di++] = 0x80 + ((codePoint >> 18) & 0x3F);
84 dest[di++] = 0x80 + ((codePoint >> 12) & 0x3F);
85 dest[di++] = 0x80 + ((codePoint >> 6) & 0x3F);
86 dest[di++] = codePoint & 0x3F;
87 } else {
88 // Not a code point... skip.
89 }
90 }
91 dest[di] = 0;
92 return di;
93 }
94
95 #if defined(FLAG_DO_PROFILE) || defined(FLAG_DBG)
96 #if defined(__ANDROID__)
97 #include <android/log.h>
98 #endif // defined(__ANDROID__)
99 #ifndef LOG_TAG
100 #define LOG_TAG "LatinIME: "
101 #endif // LOG_TAG
102
103 #if defined(HOST_TOOL)
104 #include <stdio.h>
105 #define AKLOGE(fmt, ...) printf(fmt "\n", ##__VA_ARGS__)
106 #define AKLOGI(fmt, ...) printf(fmt "\n", ##__VA_ARGS__)
107 #else // defined(HOST_TOOL)
108 #define AKLOGE(fmt, ...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, fmt, ##__VA_ARGS__)
109 #define AKLOGI(fmt, ...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, fmt, ##__VA_ARGS__)
110 #endif // defined(HOST_TOOL)
111
112 #define DUMP_SUGGESTION(words, frequencies, index, score) \
113 do { dumpWordInfo(words, frequencies, index, score); } while (0)
114 #define DUMP_WORD(word, length) do { dumpWord(word, length); } while (0)
115 #define INTS_TO_CHARS(input, length, output, outlength) do { \
116 intArrayToCharArray(input, length, output, outlength); } while (0)
117
dumpWordInfo(const int * word,const int length,const int rank,const int probability)118 static inline void dumpWordInfo(const int *word, const int length, const int rank,
119 const int probability) {
120 static char charBuf[50];
121 const int N = intArrayToCharArray(word, length, charBuf, NELEMS(charBuf));
122 if (N > 0) {
123 AKLOGI("%2d [ %s ] (%d)", rank, charBuf, probability);
124 }
125 }
126
dumpWord(const int * word,const int length)127 static AK_FORCE_INLINE void dumpWord(const int *word, const int length) {
128 static char charBuf[50];
129 const int N = intArrayToCharArray(word, length, charBuf, NELEMS(charBuf));
130 if (N > 1) {
131 AKLOGI("[ %s ]", charBuf);
132 }
133 }
134
135 #ifndef __ANDROID__
136 #include <cassert>
137 #include <execinfo.h>
138 #include <stdlib.h>
139
140 #define DO_ASSERT_TEST
141 #define ASSERT(success) do { if (!(success)) { showStackTrace(); assert(success);} } while (0)
142 #define SHOW_STACK_TRACE do { showStackTrace(); } while (0)
143
showStackTrace()144 static inline void showStackTrace() {
145 void *callstack[128];
146 int i, frames = backtrace(callstack, 128);
147 char **strs = backtrace_symbols(callstack, frames);
148 for (i = 0; i < frames; ++i) {
149 if (i == 0) {
150 AKLOGI("=== Trace ===");
151 continue;
152 }
153 AKLOGI("%s", strs[i]);
154 }
155 free(strs);
156 }
157 #else // __ANDROID__
158 #include <cassert>
159 #define DO_ASSERT_TEST
160 #define ASSERT(success) assert(success)
161 #define SHOW_STACK_TRACE
162 #endif // __ANDROID__
163
164 #else // defined(FLAG_DO_PROFILE) || defined(FLAG_DBG)
165 #define AKLOGE(fmt, ...)
166 #define AKLOGI(fmt, ...)
167 #define DUMP_SUGGESTION(words, frequencies, index, score)
168 #define DUMP_WORD(word, length)
169 #undef DO_ASSERT_TEST
170 #define ASSERT(success)
171 #define SHOW_STACK_TRACE
172 #define INTS_TO_CHARS(input, length, output)
173 #endif // defined(FLAG_DO_PROFILE) || defined(FLAG_DBG)
174
175 #ifdef FLAG_DBG
176 #define DEBUG_DICT true
177 #define DEBUG_DICT_FULL false
178 #define DEBUG_EDIT_DISTANCE false
179 #define DEBUG_NODE DEBUG_DICT_FULL
180 #define DEBUG_TRACE DEBUG_DICT_FULL
181 #define DEBUG_PROXIMITY_INFO false
182 #define DEBUG_PROXIMITY_CHARS false
183 #define DEBUG_CORRECTION false
184 #define DEBUG_CORRECTION_FREQ false
185 #define DEBUG_SAMPLING_POINTS false
186 #define DEBUG_POINTS_PROBABILITY false
187 #define DEBUG_DOUBLE_LETTER false
188 #define DEBUG_CACHE false
189 #define DEBUG_DUMP_ERROR false
190 #define DEBUG_EVALUATE_MOST_PROBABLE_STRING false
191
192 #ifdef FLAG_FULL_DBG
193 #define DEBUG_GEO_FULL true
194 #else
195 #define DEBUG_GEO_FULL false
196 #endif
197
198 #else // FLAG_DBG
199
200 #define DEBUG_DICT false
201 #define DEBUG_DICT_FULL false
202 #define DEBUG_EDIT_DISTANCE false
203 #define DEBUG_NODE false
204 #define DEBUG_TRACE false
205 #define DEBUG_PROXIMITY_INFO false
206 #define DEBUG_PROXIMITY_CHARS false
207 #define DEBUG_CORRECTION false
208 #define DEBUG_CORRECTION_FREQ false
209 #define DEBUG_SAMPLING_POINTS false
210 #define DEBUG_POINTS_PROBABILITY false
211 #define DEBUG_DOUBLE_LETTER false
212 #define DEBUG_CACHE false
213 #define DEBUG_DUMP_ERROR false
214 #define DEBUG_EVALUATE_MOST_PROBABLE_STRING false
215
216 #define DEBUG_GEO_FULL false
217
218 #endif // FLAG_DBG
219
220 #ifndef S_INT_MAX
221 #define S_INT_MAX 2147483647 // ((1 << 31) - 1)
222 #endif
223 #ifndef S_INT_MIN
224 // The literal constant -2147483648 does not work in C prior C90, because
225 // the compiler tries to fit the positive number into an int and then negate it.
226 // GCC warns about this.
227 #define S_INT_MIN (-2147483647 - 1) // -(1 << 31)
228 #endif
229
230 #define M_PI_F 3.14159265f
231 #define MAX_PERCENTILE 100
232
233 #define NOT_A_CODE_POINT (-1)
234 #define NOT_A_DISTANCE (-1)
235 #define NOT_A_COORDINATE (-1)
236 #define NOT_AN_INDEX (-1)
237 #define NOT_A_PROBABILITY (-1)
238 #define NOT_A_DICT_POS (S_INT_MIN)
239 #define NOT_A_WORD_ID (S_INT_MIN)
240 #define NOT_A_TIMESTAMP (-1)
241 #define NOT_A_WEIGHT_OF_LANG_MODEL_VS_SPATIAL_MODEL (-1.0f)
242
243 // A special value to mean the first word confidence makes no sense in this case,
244 // e.g. this is not a multi-word suggestion.
245 #define NOT_A_FIRST_WORD_CONFIDENCE (S_INT_MIN)
246 // How high the confidence needs to be for us to auto-commit. Arbitrary.
247 // This needs to be the same as CONFIDENCE_FOR_AUTO_COMMIT in BinaryDictionary.java
248 #define CONFIDENCE_FOR_AUTO_COMMIT (1000000)
249 // 80% of the full confidence
250 #define DISTANCE_WEIGHT_FOR_AUTO_COMMIT (80 * CONFIDENCE_FOR_AUTO_COMMIT / 100)
251 // 100% of the full confidence
252 #define LENGTH_WEIGHT_FOR_AUTO_COMMIT (CONFIDENCE_FOR_AUTO_COMMIT)
253 // 80% of the full confidence
254 #define SPACE_COUNT_WEIGHT_FOR_AUTO_COMMIT (80 * CONFIDENCE_FOR_AUTO_COMMIT / 100)
255
256 #define KEYCODE_SPACE ' '
257 #define KEYCODE_SINGLE_QUOTE '\''
258 #define KEYCODE_HYPHEN_MINUS '-'
259 // Code point to indicate beginning-of-sentence. This is not in the code point space of unicode.
260 #define CODE_POINT_BEGINNING_OF_SENTENCE 0x110000
261
262 #define SUGGEST_INTERFACE_OUTPUT_SCALE 1000000.0f
263 #define MAX_PROBABILITY 255
264 #define MAX_BIGRAM_ENCODED_PROBABILITY 15
265
266 // Max value for length, distance and probability which are used in weighting
267 // TODO: Remove
268 #define MAX_VALUE_FOR_WEIGHTING 10000000
269
270 // The max number of the keys in one keyboard layout
271 #define MAX_KEY_COUNT_IN_A_KEYBOARD 64
272
273 // TODO: Remove
274 #define MAX_POINTER_COUNT 1
275 #define MAX_POINTER_COUNT_G 2
276
277 // (MAX_PREV_WORD_COUNT_FOR_N_GRAM + 1)-gram is supported.
278 #define MAX_PREV_WORD_COUNT_FOR_N_GRAM 3
279
280 #define DISALLOW_DEFAULT_CONSTRUCTOR(TypeName) \
281 TypeName() = delete
282
283 #define DISALLOW_COPY_CONSTRUCTOR(TypeName) \
284 TypeName(const TypeName&) = delete
285
286 #define DISALLOW_ASSIGNMENT_OPERATOR(TypeName) \
287 void operator=(const TypeName&) = delete
288
289 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
290 DISALLOW_COPY_CONSTRUCTOR(TypeName); \
291 DISALLOW_ASSIGNMENT_OPERATOR(TypeName)
292
293 #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
294 DISALLOW_DEFAULT_CONSTRUCTOR(TypeName); \
295 DISALLOW_COPY_AND_ASSIGN(TypeName)
296
297 // Used as a return value for character comparison
298 typedef enum {
299 // Same char, possibly with different case or accent
300 MATCH_CHAR,
301 // It is a char located nearby on the keyboard
302 PROXIMITY_CHAR,
303 // Additional proximity char which can differ by language.
304 ADDITIONAL_PROXIMITY_CHAR,
305 // It is a substitution char
306 SUBSTITUTION_CHAR,
307 // It is an unrelated char
308 UNRELATED_CHAR,
309 } ProximityType;
310
311 typedef enum {
312 NOT_A_DOUBLE_LETTER,
313 A_DOUBLE_LETTER,
314 A_STRONG_DOUBLE_LETTER
315 } DoubleLetterLevel;
316
317 typedef enum {
318 // Correction for MATCH_CHAR
319 CT_MATCH,
320 // Correction for PROXIMITY_CHAR
321 CT_PROXIMITY,
322 // Correction for ADDITIONAL_PROXIMITY_CHAR
323 CT_ADDITIONAL_PROXIMITY,
324 // Correction for SUBSTITUTION_CHAR
325 CT_SUBSTITUTION,
326 // Skip one omitted letter
327 CT_OMISSION,
328 // Delete an unnecessarily inserted letter
329 CT_INSERTION,
330 // Swap the order of next two touch points
331 CT_TRANSPOSITION,
332 CT_COMPLETION,
333 CT_TERMINAL,
334 CT_TERMINAL_INSERTION,
335 // Create new word with space omission
336 CT_NEW_WORD_SPACE_OMISSION,
337 // Create new word with space substitution
338 CT_NEW_WORD_SPACE_SUBSTITUTION,
339 } CorrectionType;
340 #endif // LATINIME_DEFINES_H
341