defines.h revision f473f4b1ebfd30756b520f1d1233c3391e1d35b8
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
39#if defined(FLAG_DO_PROFILE) || defined(FLAG_DBG)
40#include <android/log.h>
41#ifndef LOG_TAG
42#define LOG_TAG "LatinIME: "
43#endif // LOG_TAG
44#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__)
46
47#define DUMP_RESULT(words, frequencies) do { dumpResult(words, frequencies); } while (0)
48#define DUMP_WORD(word, length) do { dumpWord(word, length); } while (0)
49#define INTS_TO_CHARS(input, length, output) do { \
50        intArrayToCharArray(input, length, output); } while (0)
51
52// 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;
57    while (si < sourceSize && di < MAX_WORD_LENGTH - 1 && 0 != source[si]) {
58        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
74static inline void dumpWordInfo(const int *word, const int length, const int rank,
75        const int probability) {
76    static char charBuf[50];
77    const int N = intArrayToCharArray(word, length, charBuf);
78    if (N > 1) {
79        AKLOGI("%2d [ %s ] (%d)", rank, charBuf, probability);
80    }
81}
82
83static inline void dumpResult(const int *outWords, const int *frequencies) {
84    AKLOGI("--- DUMP RESULT ---------");
85    for (int i = 0; i < MAX_RESULTS; ++i) {
86        dumpWordInfo(&outWords[i * MAX_WORD_LENGTH], MAX_WORD_LENGTH, i, frequencies[i]);
87    }
88    AKLOGI("-------------------------");
89}
90
91static AK_FORCE_INLINE void dumpWord(const int *word, const int length) {
92    static char charBuf[50];
93    const int N = intArrayToCharArray(word, length, charBuf);
94    if (N > 1) {
95        AKLOGI("[ %s ]", charBuf);
96    }
97}
98
99#ifndef __ANDROID__
100#include <cassert>
101#include <execinfo.h>
102#include <stdlib.h>
103
104#define DO_ASSERT_TEST
105#define ASSERT(success) do { if (!(success)) { showStackTrace(); assert(success);} } while (0)
106#define SHOW_STACK_TRACE do { showStackTrace(); } while (0)
107
108static 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}
121#else // __ANDROID__
122#include <cassert>
123#define DO_ASSERT_TEST
124#define ASSERT(success) assert(success)
125#define SHOW_STACK_TRACE
126#endif // __ANDROID__
127
128#else // defined(FLAG_DO_PROFILE) || defined(FLAG_DBG)
129#define AKLOGE(fmt, ...)
130#define AKLOGI(fmt, ...)
131#define DUMP_RESULT(words, frequencies)
132#define DUMP_WORD(word, length)
133#undef DO_ASSERT_TEST
134#define ASSERT(success)
135#define SHOW_STACK_TRACE
136#define INTS_TO_CHARS(input, length, output)
137#endif // defined(FLAG_DO_PROFILE) || defined(FLAG_DBG)
138
139#ifdef FLAG_DO_PROFILE
140// Profiler
141#include <time.h>
142
143#define PROF_BUF_SIZE 100
144static float profile_buf[PROF_BUF_SIZE];
145static float profile_old[PROF_BUF_SIZE];
146static unsigned int profile_counter[PROF_BUF_SIZE];
147
148#define PROF_RESET               prof_reset()
149#define PROF_COUNT(prof_buf_id)  ++profile_counter[prof_buf_id]
150#define PROF_OPEN                do { PROF_RESET; PROF_START(PROF_BUF_SIZE - 1); } while (0)
151#define PROF_START(prof_buf_id)  do { \
152        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)
154#define PROF_END(prof_buf_id)    profile_buf[prof_buf_id] += ((clock()) - profile_old[prof_buf_id])
155#define PROF_CLOCKOUT(prof_buf_id) \
156        AKLOGI("%s : clock is %f", __FUNCTION__, (clock() - profile_old[prof_buf_id]))
157#define PROF_OUTALL              do { AKLOGI("--- %s ---", __FUNCTION__); prof_out(); } while (0)
158
159static inline void prof_reset(void) {
160    for (int i = 0; i < PROF_BUF_SIZE; ++i) {
161        profile_buf[i] = 0;
162        profile_old[i] = 0;
163        profile_counter[i] = 0;
164    }
165}
166
167static inline void prof_out(void) {
168    if (profile_counter[PROF_BUF_SIZE - 1] != 1) {
169        AKLOGI("Error: You must call PROF_OPEN before PROF_CLOSE.");
170    }
171    AKLOGI("Total time is %6.3f ms.",
172            profile_buf[PROF_BUF_SIZE - 1] * 1000.0f / static_cast<float>(CLOCKS_PER_SEC));
173    float all = 0.0f;
174    for (int i = 0; i < PROF_BUF_SIZE - 1; ++i) {
175        all += profile_buf[i];
176    }
177    if (all < 1.0f) all = 1.0f;
178    for (int i = 0; i < PROF_BUF_SIZE - 1; ++i) {
179        if (profile_buf[i] > 0.0f) {
180            AKLOGI("(%d): Used %4.2f%%, %8.4f ms. Called %d times.",
181                    i, (profile_buf[i] * 100.0f / all),
182                    profile_buf[i] * 1000.0f / static_cast<float>(CLOCKS_PER_SEC),
183                    profile_counter[i]);
184        }
185    }
186}
187
188#else // FLAG_DO_PROFILE
189#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
200#endif // FLAG_DO_PROFILE
201
202#ifdef FLAG_DBG
203#define DEBUG_DICT true
204#define DEBUG_DICT_FULL false
205#define DEBUG_EDIT_DISTANCE false
206#define DEBUG_NODE DEBUG_DICT_FULL
207#define DEBUG_TRACE DEBUG_DICT_FULL
208#define DEBUG_PROXIMITY_INFO false
209#define DEBUG_PROXIMITY_CHARS false
210#define DEBUG_CORRECTION false
211#define DEBUG_CORRECTION_FREQ false
212#define DEBUG_SAMPLING_POINTS false
213#define DEBUG_POINTS_PROBABILITY false
214#define DEBUG_DOUBLE_LETTER false
215#define DEBUG_CACHE false
216#define DEBUG_DUMP_ERROR false
217#define DEBUG_EVALUATE_MOST_PROBABLE_STRING false
218
219#ifdef FLAG_FULL_DBG
220#define DEBUG_GEO_FULL true
221#else
222#define DEBUG_GEO_FULL false
223#endif
224
225#else // FLAG_DBG
226
227#define DEBUG_DICT false
228#define DEBUG_DICT_FULL false
229#define DEBUG_EDIT_DISTANCE false
230#define DEBUG_NODE false
231#define DEBUG_TRACE false
232#define DEBUG_PROXIMITY_INFO false
233#define DEBUG_PROXIMITY_CHARS false
234#define DEBUG_CORRECTION false
235#define DEBUG_CORRECTION_FREQ false
236#define DEBUG_SAMPLING_POINTS false
237#define DEBUG_POINTS_PROBABILITY false
238#define DEBUG_DOUBLE_LETTER false
239#define DEBUG_CACHE false
240#define DEBUG_DUMP_ERROR false
241#define DEBUG_EVALUATE_MOST_PROBABLE_STRING false
242
243#define DEBUG_GEO_FULL false
244
245#endif // FLAG_DBG
246
247#ifndef S_INT_MAX
248#define S_INT_MAX 2147483647 // ((1 << 31) - 1)
249#endif
250#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
256
257#define M_PI_F 3.14159265f
258#define MAX_PERCENTILE 100
259
260// 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
266
267#define NOT_VALID_WORD (-99)
268#define NOT_A_CODE_POINT (-1)
269#define NOT_A_DISTANCE (-1)
270#define NOT_A_COORDINATE (-1)
271#define NOT_AN_INDEX (-1)
272#define NOT_A_PROBABILITY (-1)
273
274#define KEYCODE_SPACE ' '
275#define KEYCODE_SINGLE_QUOTE '\''
276#define KEYCODE_HYPHEN_MINUS '-'
277
278#define SUGGEST_INTERFACE_OUTPUT_SCALE 1000000.0f
279#define MAX_PROBABILITY 255
280#define MAX_BIGRAM_ENCODED_PROBABILITY 15
281
282// Assuming locale strings such as en_US, sr-Latn etc.
283#define MAX_LOCALE_STRING_LENGTH 10
284
285// Max value for length, distance and probability which are used in weighting
286// TODO: Remove
287#define MAX_VALUE_FOR_WEIGHTING 10000000
288
289// The max number of the keys in one keyboard layout
290#define MAX_KEY_COUNT_IN_A_KEYBOARD 64
291
292// TODO: Remove
293#define MAX_POINTER_COUNT 1
294#define MAX_POINTER_COUNT_G 2
295
296// Queue IDs and size for DicNodesCache
297#define DIC_NODES_CACHE_INITIAL_QUEUE_ID_ACTIVE 0
298#define DIC_NODES_CACHE_INITIAL_QUEUE_ID_NEXT_ACTIVE 1
299#define DIC_NODES_CACHE_INITIAL_QUEUE_ID_TERMINAL 2
300#define DIC_NODES_CACHE_INITIAL_QUEUE_ID_CACHE_FOR_CONTINUOUS_SUGGESTION 3
301#define DIC_NODES_CACHE_PRIORITY_QUEUES_SIZE 4
302
303// Size, in bytes, of the bloom filter index for bigrams
304// 128 gives us 1024 buckets. The probability of false positive is (1 - e ** (-kn/m))**k,
305// where k is the number of hash functions, n the number of bigrams, and m the number of
306// bits we can test.
307// At the moment 100 is the maximum number of bigrams for a word with the current
308// dictionaries, so n = 100. 1024 buckets give us m = 1024.
309// With 1 hash function, our false positive rate is about 9.3%, which should be enough for
310// our uses since we are only using this to increase average performance. For the record,
311// k = 2 gives 3.1% and k = 3 gives 1.6%. With k = 1, making m = 2048 gives 4.8%,
312// and m = 4096 gives 2.4%.
313#define BIGRAM_FILTER_BYTE_SIZE 128
314// Must be smaller than BIGRAM_FILTER_BYTE_SIZE * 8, and preferably prime. 1021 is the largest
315// prime under 128 * 8.
316#define BIGRAM_FILTER_MODULO 1021
317#if BIGRAM_FILTER_BYTE_SIZE * 8 < BIGRAM_FILTER_MODULO
318#error "BIGRAM_FILTER_MODULO is larger than BIGRAM_FILTER_BYTE_SIZE"
319#endif
320
321// Max number of bigram maps (previous word contexts) to be cached. Increasing this number could
322// improve bigram lookup speed for multi-word suggestions, but at the cost of more memory usage.
323// Also, there are diminishing returns since the most frequently used bigrams are typically near
324// the beginning of the input and are thus the first ones to be cached. Note that these bigrams
325// are reset for each new composing word.
326#define MAX_CACHED_PREV_WORDS_IN_BIGRAM_MAP 25
327// Most common previous word contexts currently have 100 bigrams
328#define DEFAULT_HASH_MAP_SIZE_FOR_EACH_BIGRAM_MAP 100
329
330template<typename T> AK_FORCE_INLINE const T &min(const T &a, const T &b) { return a < b ? a : b; }
331template<typename T> AK_FORCE_INLINE const T &max(const T &a, const T &b) { return a > b ? a : b; }
332
333#define NELEMS(x) (sizeof(x) / sizeof((x)[0]))
334
335// DEBUG
336#define INPUTLENGTH_FOR_DEBUG (-1)
337#define MIN_OUTPUT_INDEX_FOR_DEBUG (-1)
338
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
347// Used as a return value for character comparison
348typedef enum {
349    // Same char, possibly with different case or accent
350    MATCH_CHAR,
351    // It is a char located nearby on the keyboard
352    PROXIMITY_CHAR,
353    // Additional proximity char which can differ by language.
354    ADDITIONAL_PROXIMITY_CHAR,
355    // It is a substitution char
356    SUBSTITUTION_CHAR,
357    // It is an unrelated char
358    UNRELATED_CHAR,
359} ProximityType;
360
361typedef enum {
362    NOT_A_DOUBLE_LETTER,
363    A_DOUBLE_LETTER,
364    A_STRONG_DOUBLE_LETTER
365} DoubleLetterLevel;
366
367typedef enum {
368    // Correction for MATCH_CHAR
369    CT_MATCH,
370    // Correction for PROXIMITY_CHAR
371    CT_PROXIMITY,
372    // Correction for ADDITIONAL_PROXIMITY_CHAR
373    CT_ADDITIONAL_PROXIMITY,
374    // Correction for SUBSTITUTION_CHAR
375    CT_SUBSTITUTION,
376    // Skip one omitted letter
377    CT_OMISSION,
378    // Delete an unnecessarily inserted letter
379    CT_INSERTION,
380    // Swap the order of next two touch points
381    CT_TRANSPOSITION,
382    CT_COMPLETION,
383    CT_TERMINAL,
384    // Create new word with space omission
385    CT_NEW_WORD_SPACE_OMITTION,
386    // Create new word with space substitution
387    CT_NEW_WORD_SPACE_SUBSTITUTION,
388} CorrectionType;
389
390// ErrorType is mainly decided by CorrectionType but it is also depending on if
391// the correction has really been performed or not.
392typedef enum {
393    // Substitution, omission and transposition
394    ET_EDIT_CORRECTION,
395    // Proximity error
396    ET_PROXIMITY_CORRECTION,
397    // Completion
398    ET_COMPLETION,
399    // New word
400    // TODO: Remove.
401    // A new word error should be an edit correction error or a proximity correction error.
402    ET_NEW_WORD,
403    // Treat error as an intentional omission when the CorrectionType is omission and the node can
404    // be intentional omission.
405    ET_INTENTIONAL_OMISSION,
406    // Not treated as an error. Tracked for checking exact match
407    ET_NOT_AN_ERROR
408} ErrorType;
409#endif // LATINIME_DEFINES_H
410