1/*
2**
3** Copyright 2010, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9**     http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#ifndef LATINIME_DEFINES_H
19#define LATINIME_DEFINES_H
20
21#if defined(FLAG_DO_PROFILE) || defined(FLAG_DBG)
22#include <cutils/log.h>
23#else
24#define LOGE(fmt, ...)
25#define LOGI(fmt, ...)
26#endif
27
28#ifdef FLAG_DO_PROFILE
29// Profiler
30#include <cutils/log.h>
31#include <time.h>
32#define PROF_BUF_SIZE 100
33static double profile_buf[PROF_BUF_SIZE];
34static double profile_old[PROF_BUF_SIZE];
35static unsigned int profile_counter[PROF_BUF_SIZE];
36
37#define PROF_RESET               prof_reset()
38#define PROF_COUNT(prof_buf_id)  ++profile_counter[prof_buf_id]
39#define PROF_OPEN                do { PROF_RESET; PROF_START(PROF_BUF_SIZE - 1); } while(0)
40#define PROF_START(prof_buf_id)  do { \
41        PROF_COUNT(prof_buf_id); profile_old[prof_buf_id] = (clock()); } while(0)
42#define PROF_CLOSE               do { PROF_END(PROF_BUF_SIZE - 1); PROF_OUTALL; } while(0)
43#define PROF_END(prof_buf_id)    profile_buf[prof_buf_id] += ((clock()) - profile_old[prof_buf_id])
44#define PROF_CLOCKOUT(prof_buf_id) \
45        LOGI("%s : clock is %f", __FUNCTION__, (clock() - profile_old[prof_buf_id]))
46#define PROF_OUTALL              do { LOGI("--- %s ---", __FUNCTION__); prof_out(); } while(0)
47
48static void prof_reset(void) {
49    for (int i = 0; i < PROF_BUF_SIZE; ++i) {
50        profile_buf[i] = 0;
51        profile_old[i] = 0;
52        profile_counter[i] = 0;
53    }
54}
55
56static void prof_out(void) {
57    if (profile_counter[PROF_BUF_SIZE - 1] != 1) {
58        LOGI("Error: You must call PROF_OPEN before PROF_CLOSE.");
59    }
60    LOGI("Total time is %6.3f ms.",
61            profile_buf[PROF_BUF_SIZE - 1] * 1000 / (double)CLOCKS_PER_SEC);
62    double all = 0;
63    for (int i = 0; i < PROF_BUF_SIZE - 1; ++i) {
64        all += profile_buf[i];
65    }
66    if (all == 0) all = 1;
67    for (int i = 0; i < PROF_BUF_SIZE - 1; ++i) {
68        if (profile_buf[i] != 0) {
69            LOGI("(%d): Used %4.2f%%, %8.4f ms. Called %d times.",
70                    i, (profile_buf[i] * 100 / all),
71                    profile_buf[i] * 1000 / (double)CLOCKS_PER_SEC, profile_counter[i]);
72        }
73    }
74}
75
76#else // FLAG_DO_PROFILE
77#define PROF_BUF_SIZE 0
78#define PROF_RESET
79#define PROF_COUNT(prof_buf_id)
80#define PROF_OPEN
81#define PROF_START(prof_buf_id)
82#define PROF_CLOSE
83#define PROF_END(prof_buf_id)
84#define PROF_CLOCK_OUT(prof_buf_id)
85#define PROF_CLOCKOUT(prof_buf_id)
86#define PROF_OUTALL
87
88#endif // FLAG_DO_PROFILE
89
90#ifdef FLAG_DBG
91#include <cutils/log.h>
92#ifndef LOG_TAG
93#define LOG_TAG "LatinIME: "
94#endif
95#define DEBUG_DICT true
96#define DEBUG_DICT_FULL false
97#define DEBUG_EDIT_DISTANCE false
98#define DEBUG_SHOW_FOUND_WORD false
99#define DEBUG_NODE DEBUG_DICT_FULL
100#define DEBUG_TRACE DEBUG_DICT_FULL
101#define DEBUG_PROXIMITY_INFO true
102#define DEBUG_CORRECTION false
103#define DEBUG_CORRECTION_FREQ true
104
105#define DUMP_WORD(word, length) do { dumpWord(word, length); } while(0)
106
107static char charBuf[50];
108
109static void dumpWord(const unsigned short* word, const int length) {
110    for (int i = 0; i < length; ++i) {
111        charBuf[i] = word[i];
112    }
113    charBuf[length] = 0;
114    LOGI("[ %s ]", charBuf);
115}
116
117#else // FLAG_DBG
118
119#define DEBUG_DICT false
120#define DEBUG_DICT_FULL false
121#define DEBUG_EDIT_DISTANCE false
122#define DEBUG_SHOW_FOUND_WORD false
123#define DEBUG_NODE false
124#define DEBUG_TRACE false
125#define DEBUG_PROXIMITY_INFO false
126#define DEBUG_CORRECTION false
127#define DEBUG_CORRECTION_FREQ false
128
129#define DUMP_WORD(word, length)
130
131#endif // FLAG_DBG
132
133#ifndef U_SHORT_MAX
134#define U_SHORT_MAX 65535    // ((1 << 16) - 1)
135#endif
136#ifndef S_INT_MAX
137#define S_INT_MAX 2147483647 // ((1 << 31) - 1)
138#endif
139
140// Define this to use mmap() for dictionary loading.  Undefine to use malloc() instead of mmap().
141// We measured and compared performance of both, and found mmap() is fairly good in terms of
142// loading time, and acceptable even for several initial lookups which involve page faults.
143#define USE_MMAP_FOR_DICTIONARY
144
145// 22-bit address = ~4MB dictionary size limit, which on average would be about 200k-300k words
146#define ADDRESS_MASK 0x3FFFFF
147
148// The bit that decides if an address follows in the next 22 bits
149#define FLAG_ADDRESS_MASK 0x40
150// The bit that decides if this is a terminal node for a word. The node could still have children,
151// if the word has other endings.
152#define FLAG_TERMINAL_MASK 0x80
153
154#define FLAG_BIGRAM_READ 0x80
155#define FLAG_BIGRAM_CHILDEXIST 0x40
156#define FLAG_BIGRAM_CONTINUED 0x80
157#define FLAG_BIGRAM_FREQ 0x7F
158
159#define DICTIONARY_VERSION_MIN 200
160// TODO: remove this constant when the switch to the new dict format is over
161#define DICTIONARY_HEADER_SIZE 2
162#define NEW_DICTIONARY_HEADER_SIZE 5
163#define NOT_VALID_WORD -99
164#define NOT_A_CHARACTER -1
165#define NOT_A_DISTANCE -1
166#define EQUIVALENT_CHAR_WITHOUT_DISTANCE_INFO -2
167#define PROXIMITY_CHAR_WITHOUT_DISTANCE_INFO -3
168#define NOT_A_INDEX -1
169
170#define KEYCODE_SPACE ' '
171
172#define CALIBRATE_SCORE_BY_TOUCH_COORDINATES true
173
174#define SUGGEST_WORDS_WITH_MISSING_CHARACTER true
175#define SUGGEST_WORDS_WITH_MISSING_SPACE_CHARACTER true
176#define SUGGEST_WORDS_WITH_EXCESSIVE_CHARACTER true
177#define SUGGEST_WORDS_WITH_TRANSPOSED_CHARACTERS true
178#define SUGGEST_WORDS_WITH_SPACE_PROXIMITY true
179
180// The following "rate"s are used as a multiplier before dividing by 100, so they are in percent.
181#define WORDS_WITH_MISSING_CHARACTER_DEMOTION_RATE 80
182#define WORDS_WITH_MISSING_CHARACTER_DEMOTION_START_POS_10X 12
183#define WORDS_WITH_MISSING_SPACE_CHARACTER_DEMOTION_RATE 67
184#define WORDS_WITH_EXCESSIVE_CHARACTER_DEMOTION_RATE 75
185#define WORDS_WITH_EXCESSIVE_CHARACTER_OUT_OF_PROXIMITY_DEMOTION_RATE 75
186#define WORDS_WITH_TRANSPOSED_CHARACTERS_DEMOTION_RATE 60
187#define FULL_MATCHED_WORDS_PROMOTION_RATE 120
188#define WORDS_WITH_PROXIMITY_CHARACTER_DEMOTION_RATE 90
189#define WORDS_WITH_MATCH_SKIP_PROMOTION_RATE 105
190#define WORDS_WITH_JUST_ONE_CORRECTION_PROMOTION_RATE 160
191#define CORRECTION_COUNT_RATE_DEMOTION_RATE_BASE 45
192#define INPUT_EXCEEDS_OUTPUT_DEMOTION_RATE 70
193#define FIRST_CHAR_DIFFERENT_DEMOTION_RATE 96
194#define TWO_WORDS_CAPITALIZED_DEMOTION_RATE 50
195#define ZERO_DISTANCE_PROMOTION_RATE 110
196#define NEUTRAL_SCORE_SQUARED_RADIUS 8.0f
197#define HALF_SCORE_SQUARED_RADIUS 32.0f
198
199// This should be greater than or equal to MAX_WORD_LENGTH defined in BinaryDictionary.java
200// This is only used for the size of array. Not to be used in c functions.
201#define MAX_WORD_LENGTH_INTERNAL 48
202
203#define MAX_DEPTH_MULTIPLIER 3
204
205// TODO: Reduce this constant if possible; check the maximum number of umlauts in the same German
206// word in the dictionary
207#define DEFAULT_MAX_UMLAUT_SEARCH_DEPTH 5
208
209// Minimum suggest depth for one word for all cases except for missing space suggestions.
210#define MIN_SUGGEST_DEPTH 1
211#define MIN_USER_TYPED_LENGTH_FOR_MISSING_SPACE_SUGGESTION 3
212#define MIN_USER_TYPED_LENGTH_FOR_EXCESSIVE_CHARACTER_SUGGESTION 3
213
214#define min(a,b) ((a)<(b)?(a):(b))
215#define max(a,b) ((a)>(b)?(a):(b))
216
217// The ratio of neutral area radius to sweet spot radius.
218#define NEUTRAL_AREA_RADIUS_RATIO 1.3f
219
220#endif // LATINIME_DEFINES_H
221