blob: c25f963e00570440eed4799521367314db7bf6e2 [file] [log] [blame]
satoke808e432010-12-02 14:53:24 +09001/*
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
satok827ced82011-07-14 09:01:09 +090021#if defined(FLAG_DO_PROFILE) || defined(FLAG_DBG)
22#include <cutils/log.h>
satok9fb6f472012-01-13 18:01:22 +090023#define AKLOGE ALOGE
24#define AKLOGI ALOGI
satok6ad15fc2012-01-16 16:21:21 +090025
26#define DUMP_WORD(word, length) do { dumpWord(word, length); } while(0)
27
28static char charBuf[50];
29
30static void dumpWord(const unsigned short* word, const int length) {
31 for (int i = 0; i < length; ++i) {
32 charBuf[i] = word[i];
33 }
34 charBuf[length] = 0;
35 AKLOGI("[ %s ]", charBuf);
36}
37
satok827ced82011-07-14 09:01:09 +090038#else
satok9fb6f472012-01-13 18:01:22 +090039#define AKLOGE(fmt, ...)
40#define AKLOGI(fmt, ...)
satok6ad15fc2012-01-16 16:21:21 +090041#define DUMP_WORD(word, length)
satok827ced82011-07-14 09:01:09 +090042#endif
43
satok20d9fda2011-07-13 14:40:30 +090044#ifdef FLAG_DO_PROFILE
satok61e2f852011-01-05 14:13:07 +090045// Profiler
satok787945b2011-07-14 08:32:57 +090046#include <cutils/log.h>
satok61e2f852011-01-05 14:13:07 +090047#include <time.h>
satok9fb6f472012-01-13 18:01:22 +090048
satok61e2f852011-01-05 14:13:07 +090049#define PROF_BUF_SIZE 100
50static double profile_buf[PROF_BUF_SIZE];
51static double profile_old[PROF_BUF_SIZE];
52static unsigned int profile_counter[PROF_BUF_SIZE];
53
Ken Wakasae90b3332011-01-07 15:01:51 +090054#define PROF_RESET prof_reset()
55#define PROF_COUNT(prof_buf_id) ++profile_counter[prof_buf_id]
56#define PROF_OPEN do { PROF_RESET; PROF_START(PROF_BUF_SIZE - 1); } while(0)
57#define PROF_START(prof_buf_id) do { \
58 PROF_COUNT(prof_buf_id); profile_old[prof_buf_id] = (clock()); } while(0)
59#define PROF_CLOSE do { PROF_END(PROF_BUF_SIZE - 1); PROF_OUTALL; } while(0)
60#define PROF_END(prof_buf_id) profile_buf[prof_buf_id] += ((clock()) - profile_old[prof_buf_id])
61#define PROF_CLOCKOUT(prof_buf_id) \
satok9fb6f472012-01-13 18:01:22 +090062 AKLOGI("%s : clock is %f", __FUNCTION__, (clock() - profile_old[prof_buf_id]))
63#define PROF_OUTALL do { AKLOGI("--- %s ---", __FUNCTION__); prof_out(); } while(0)
satok61e2f852011-01-05 14:13:07 +090064
Ken Wakasae90b3332011-01-07 15:01:51 +090065static void prof_reset(void) {
66 for (int i = 0; i < PROF_BUF_SIZE; ++i) {
satok61e2f852011-01-05 14:13:07 +090067 profile_buf[i] = 0;
68 profile_old[i] = 0;
69 profile_counter[i] = 0;
70 }
71}
72
Ken Wakasae90b3332011-01-07 15:01:51 +090073static void prof_out(void) {
satok61e2f852011-01-05 14:13:07 +090074 if (profile_counter[PROF_BUF_SIZE - 1] != 1) {
satok9fb6f472012-01-13 18:01:22 +090075 AKLOGI("Error: You must call PROF_OPEN before PROF_CLOSE.");
satok61e2f852011-01-05 14:13:07 +090076 }
satok9fb6f472012-01-13 18:01:22 +090077 AKLOGI("Total time is %6.3f ms.",
Ken Wakasae90b3332011-01-07 15:01:51 +090078 profile_buf[PROF_BUF_SIZE - 1] * 1000 / (double)CLOCKS_PER_SEC);
satok61e2f852011-01-05 14:13:07 +090079 double all = 0;
Ken Wakasae90b3332011-01-07 15:01:51 +090080 for (int i = 0; i < PROF_BUF_SIZE - 1; ++i) {
satok61e2f852011-01-05 14:13:07 +090081 all += profile_buf[i];
82 }
Ken Wakasae90b3332011-01-07 15:01:51 +090083 if (all == 0) all = 1;
84 for (int i = 0; i < PROF_BUF_SIZE - 1; ++i) {
85 if (profile_buf[i] != 0) {
satok9fb6f472012-01-13 18:01:22 +090086 AKLOGI("(%d): Used %4.2f%%, %8.4f ms. Called %d times.",
Ken Wakasae90b3332011-01-07 15:01:51 +090087 i, (profile_buf[i] * 100 / all),
88 profile_buf[i] * 1000 / (double)CLOCKS_PER_SEC, profile_counter[i]);
89 }
satok61e2f852011-01-05 14:13:07 +090090 }
91}
92
satok20d9fda2011-07-13 14:40:30 +090093#else // FLAG_DO_PROFILE
satok61e2f852011-01-05 14:13:07 +090094#define PROF_BUF_SIZE 0
95#define PROF_RESET
96#define PROF_COUNT(prof_buf_id)
97#define PROF_OPEN
98#define PROF_START(prof_buf_id)
99#define PROF_CLOSE
100#define PROF_END(prof_buf_id)
101#define PROF_CLOCK_OUT(prof_buf_id)
102#define PROF_CLOCKOUT(prof_buf_id)
103#define PROF_OUTALL
104
satok20d9fda2011-07-13 14:40:30 +0900105#endif // FLAG_DO_PROFILE
106
107#ifdef FLAG_DBG
108#include <cutils/log.h>
109#ifndef LOG_TAG
110#define LOG_TAG "LatinIME: "
111#endif
112#define DEBUG_DICT true
113#define DEBUG_DICT_FULL false
satok0cedd2b2011-08-12 01:05:27 +0900114#define DEBUG_EDIT_DISTANCE false
satok10266c02011-08-19 22:05:59 +0900115#define DEBUG_SHOW_FOUND_WORD false
satok20d9fda2011-07-13 14:40:30 +0900116#define DEBUG_NODE DEBUG_DICT_FULL
117#define DEBUG_TRACE DEBUG_DICT_FULL
satok1a6da632011-12-16 23:15:06 +0900118#define DEBUG_PROXIMITY_INFO false
satok10266c02011-08-19 22:05:59 +0900119#define DEBUG_CORRECTION false
satok29dc8062012-01-17 15:59:15 +0900120#define DEBUG_CORRECTION_FREQ false
121#define DEBUG_WORDS_PRIORITY_QUEUE false
satok20d9fda2011-07-13 14:40:30 +0900122
123#else // FLAG_DBG
satok827ced82011-07-14 09:01:09 +0900124
satok20d9fda2011-07-13 14:40:30 +0900125#define DEBUG_DICT false
126#define DEBUG_DICT_FULL false
satok0cedd2b2011-08-12 01:05:27 +0900127#define DEBUG_EDIT_DISTANCE false
satok20d9fda2011-07-13 14:40:30 +0900128#define DEBUG_SHOW_FOUND_WORD false
129#define DEBUG_NODE false
130#define DEBUG_TRACE false
131#define DEBUG_PROXIMITY_INFO false
satok10266c02011-08-19 22:05:59 +0900132#define DEBUG_CORRECTION false
133#define DEBUG_CORRECTION_FREQ false
satok16379df2011-12-12 20:53:22 +0900134#define DEBUG_WORDS_PRIORITY_QUEUE false
satok20d9fda2011-07-13 14:40:30 +0900135
satok0cedd2b2011-08-12 01:05:27 +0900136
satoke808e432010-12-02 14:53:24 +0900137#endif // FLAG_DBG
138
satok662fe692010-12-08 17:05:39 +0900139#ifndef U_SHORT_MAX
Yusuke Nojima63095932011-09-20 14:03:42 +0900140#define U_SHORT_MAX 65535 // ((1 << 16) - 1)
satok662fe692010-12-08 17:05:39 +0900141#endif
Jean Chalarda5d58492011-02-18 17:50:58 +0900142#ifndef S_INT_MAX
satok3c4bb772011-03-04 22:50:19 -0800143#define S_INT_MAX 2147483647 // ((1 << 31) - 1)
Jean Chalarda5d58492011-02-18 17:50:58 +0900144#endif
satok662fe692010-12-08 17:05:39 +0900145
Ken Wakasae90b3332011-01-07 15:01:51 +0900146// Define this to use mmap() for dictionary loading. Undefine to use malloc() instead of mmap().
147// We measured and compared performance of both, and found mmap() is fairly good in terms of
148// loading time, and acceptable even for several initial lookups which involve page faults.
149#define USE_MMAP_FOR_DICTIONARY
150
satoke808e432010-12-02 14:53:24 +0900151// 22-bit address = ~4MB dictionary size limit, which on average would be about 200k-300k words
152#define ADDRESS_MASK 0x3FFFFF
153
154// The bit that decides if an address follows in the next 22 bits
155#define FLAG_ADDRESS_MASK 0x40
156// The bit that decides if this is a terminal node for a word. The node could still have children,
157// if the word has other endings.
158#define FLAG_TERMINAL_MASK 0x80
159
160#define FLAG_BIGRAM_READ 0x80
161#define FLAG_BIGRAM_CHILDEXIST 0x40
162#define FLAG_BIGRAM_CONTINUED 0x80
163#define FLAG_BIGRAM_FREQ 0x7F
164
165#define DICTIONARY_VERSION_MIN 200
Jean Chalard1059f272011-06-28 20:45:05 +0900166// TODO: remove this constant when the switch to the new dict format is over
satoke808e432010-12-02 14:53:24 +0900167#define DICTIONARY_HEADER_SIZE 2
Jean Chalard1059f272011-06-28 20:45:05 +0900168#define NEW_DICTIONARY_HEADER_SIZE 5
satoke808e432010-12-02 14:53:24 +0900169#define NOT_VALID_WORD -99
Jean Chalard1059f272011-06-28 20:45:05 +0900170#define NOT_A_CHARACTER -1
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900171#define NOT_A_DISTANCE -1
Jean Chalard0bfe3592012-01-25 18:11:26 +0900172#define NOT_A_COORDINATE -1
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900173#define EQUIVALENT_CHAR_WITHOUT_DISTANCE_INFO -2
174#define PROXIMITY_CHAR_WITHOUT_DISTANCE_INFO -3
175#define NOT_A_INDEX -1
Jean Chalard82ddd162012-01-16 18:48:19 +0900176#define NOT_A_FREQUENCY -1
satoke808e432010-12-02 14:53:24 +0900177
satok817e5172011-03-04 06:06:45 -0800178#define KEYCODE_SPACE ' '
179
Yusuke Nojimaafb90762011-10-05 18:11:42 +0900180#define CALIBRATE_SCORE_BY_TOUCH_COORDINATES true
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900181
satok662fe692010-12-08 17:05:39 +0900182#define SUGGEST_WORDS_WITH_MISSING_CHARACTER true
satok662fe692010-12-08 17:05:39 +0900183#define SUGGEST_WORDS_WITH_EXCESSIVE_CHARACTER true
satoka3d78f62010-12-09 22:08:33 +0900184#define SUGGEST_WORDS_WITH_TRANSPOSED_CHARACTERS true
satok99557162012-01-26 22:49:13 +0900185#define SUGGEST_MULTIPLE_WORDS true
satoka3d78f62010-12-09 22:08:33 +0900186
Jean Chalard8dc754a2011-01-27 14:20:22 +0900187// The following "rate"s are used as a multiplier before dividing by 100, so they are in percent.
satok72bc17e2011-04-13 17:23:27 +0900188#define WORDS_WITH_MISSING_CHARACTER_DEMOTION_RATE 80
satokdc5301e2011-04-11 16:14:45 +0900189#define WORDS_WITH_MISSING_CHARACTER_DEMOTION_START_POS_10X 12
satok54af64a2012-01-17 15:58:23 +0900190#define WORDS_WITH_MISSING_SPACE_CHARACTER_DEMOTION_RATE 58
satok8330b482012-01-23 16:52:37 +0900191#define WORDS_WITH_MISTYPED_SPACE_DEMOTION_RATE 50
satoka3d78f62010-12-09 22:08:33 +0900192#define WORDS_WITH_EXCESSIVE_CHARACTER_DEMOTION_RATE 75
satok54fe9e02010-12-13 14:42:35 +0900193#define WORDS_WITH_EXCESSIVE_CHARACTER_OUT_OF_PROXIMITY_DEMOTION_RATE 75
satoka161a4a2012-01-16 18:38:32 +0900194#define WORDS_WITH_TRANSPOSED_CHARACTERS_DEMOTION_RATE 70
satok58c49b92011-01-27 03:23:39 +0900195#define FULL_MATCHED_WORDS_PROMOTION_RATE 120
satok9d2a3022011-04-14 19:13:34 +0900196#define WORDS_WITH_PROXIMITY_CHARACTER_DEMOTION_RATE 90
satok635f68e2011-08-10 22:19:33 +0900197#define WORDS_WITH_MATCH_SKIP_PROMOTION_RATE 105
satokbcac0e92011-08-15 22:30:33 +0900198#define WORDS_WITH_JUST_ONE_CORRECTION_PROMOTION_RATE 160
satok10266c02011-08-19 22:05:59 +0900199#define CORRECTION_COUNT_RATE_DEMOTION_RATE_BASE 45
200#define INPUT_EXCEEDS_OUTPUT_DEMOTION_RATE 70
201#define FIRST_CHAR_DIFFERENT_DEMOTION_RATE 96
satokeb050fc2011-10-03 19:21:13 +0900202#define TWO_WORDS_CAPITALIZED_DEMOTION_RATE 50
satok54af64a2012-01-17 15:58:23 +0900203#define TWO_WORDS_CORRECTION_DEMOTION_BASE 80
204#define TWO_WORDS_PLUS_OTHER_ERROR_CORRECTION_DEMOTION_DIVIDER 1
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900205#define ZERO_DISTANCE_PROMOTION_RATE 110
206#define NEUTRAL_SCORE_SQUARED_RADIUS 8.0f
207#define HALF_SCORE_SQUARED_RADIUS 32.0f
satoke808e432010-12-02 14:53:24 +0900208
satok1147c7b2011-12-14 15:04:58 +0900209// This must be greater than or equal to MAX_WORD_LENGTH defined in BinaryDictionary.java
satokf5cded12010-12-06 21:28:24 +0900210// This is only used for the size of array. Not to be used in c functions.
211#define MAX_WORD_LENGTH_INTERNAL 48
satok715514d2010-12-02 20:19:59 +0900212
satoka7e5a5a2011-12-15 16:49:12 +0900213// Word limit for sub queues used in WordsPriorityQueuePool. Sub queues are temporary queues used
214// for better performance.
satok6ad15fc2012-01-16 16:21:21 +0900215// Holds up to 1 candidate for each word
216#define SUB_QUEUE_MAX_WORDS 1
satokb9604772012-01-13 15:41:17 +0900217#define SUB_QUEUE_MAX_COUNT 10
satok54af64a2012-01-17 15:58:23 +0900218#define SUB_QUEUE_MIN_WORD_LENGTH 4
satok7409d152012-01-26 16:13:25 +0900219#define SUB_QUEUE_MAX_WORD_INDEX 2
satoka7e5a5a2011-12-15 16:49:12 +0900220
satok54af64a2012-01-17 15:58:23 +0900221#define TWO_WORDS_CORRECTION_WITH_OTHER_ERROR_THRESHOLD 0.39
222#define START_TWO_WORDS_CORRECTION_THRESHOLD 0.22
satok29dc8062012-01-17 15:59:15 +0900223
satok68319262010-12-03 19:38:08 +0900224#define MAX_DEPTH_MULTIPLIER 3
225
satok8330b482012-01-23 16:52:37 +0900226#define FIRST_WORD_INDEX 1
227#define SECOND_WORD_INDEX 2
228
Jean Chalarda787dba2011-03-04 12:17:48 +0900229// TODO: Reduce this constant if possible; check the maximum number of umlauts in the same German
230// word in the dictionary
231#define DEFAULT_MAX_UMLAUT_SEARCH_DEPTH 5
232
satok54fe9e02010-12-13 14:42:35 +0900233// Minimum suggest depth for one word for all cases except for missing space suggestions.
234#define MIN_SUGGEST_DEPTH 1
satok99557162012-01-26 22:49:13 +0900235#define MIN_USER_TYPED_LENGTH_FOR_MULTIPLE_WORD_SUGGESTION 3
satok54fe9e02010-12-13 14:42:35 +0900236#define MIN_USER_TYPED_LENGTH_FOR_EXCESSIVE_CHARACTER_SUGGESTION 3
satok662fe692010-12-08 17:05:39 +0900237
satokf5cded12010-12-06 21:28:24 +0900238#define min(a,b) ((a)<(b)?(a):(b))
satokbcac0e92011-08-15 22:30:33 +0900239#define max(a,b) ((a)>(b)?(a):(b))
satokf5cded12010-12-06 21:28:24 +0900240
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900241// The ratio of neutral area radius to sweet spot radius.
242#define NEUTRAL_AREA_RADIUS_RATIO 1.3f
243
satoke808e432010-12-02 14:53:24 +0900244#endif // LATINIME_DEFINES_H