blob: 01ef65678ee815f937d72f66ce86eda44ef86991 [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
satok827ced82011-07-14 09:01:09 +090025#else
satok9fb6f472012-01-13 18:01:22 +090026#define AKLOGE(fmt, ...)
27#define AKLOGI(fmt, ...)
satok827ced82011-07-14 09:01:09 +090028#endif
29
satok20d9fda2011-07-13 14:40:30 +090030#ifdef FLAG_DO_PROFILE
satok61e2f852011-01-05 14:13:07 +090031// Profiler
satok787945b2011-07-14 08:32:57 +090032#include <cutils/log.h>
satok61e2f852011-01-05 14:13:07 +090033#include <time.h>
satok9fb6f472012-01-13 18:01:22 +090034
satok61e2f852011-01-05 14:13:07 +090035#define PROF_BUF_SIZE 100
36static double profile_buf[PROF_BUF_SIZE];
37static double profile_old[PROF_BUF_SIZE];
38static unsigned int profile_counter[PROF_BUF_SIZE];
39
Ken Wakasae90b3332011-01-07 15:01:51 +090040#define PROF_RESET prof_reset()
41#define PROF_COUNT(prof_buf_id) ++profile_counter[prof_buf_id]
42#define PROF_OPEN do { PROF_RESET; PROF_START(PROF_BUF_SIZE - 1); } while(0)
43#define PROF_START(prof_buf_id) do { \
44 PROF_COUNT(prof_buf_id); profile_old[prof_buf_id] = (clock()); } while(0)
45#define PROF_CLOSE do { PROF_END(PROF_BUF_SIZE - 1); PROF_OUTALL; } while(0)
46#define PROF_END(prof_buf_id) profile_buf[prof_buf_id] += ((clock()) - profile_old[prof_buf_id])
47#define PROF_CLOCKOUT(prof_buf_id) \
satok9fb6f472012-01-13 18:01:22 +090048 AKLOGI("%s : clock is %f", __FUNCTION__, (clock() - profile_old[prof_buf_id]))
49#define PROF_OUTALL do { AKLOGI("--- %s ---", __FUNCTION__); prof_out(); } while(0)
satok61e2f852011-01-05 14:13:07 +090050
Ken Wakasae90b3332011-01-07 15:01:51 +090051static void prof_reset(void) {
52 for (int i = 0; i < PROF_BUF_SIZE; ++i) {
satok61e2f852011-01-05 14:13:07 +090053 profile_buf[i] = 0;
54 profile_old[i] = 0;
55 profile_counter[i] = 0;
56 }
57}
58
Ken Wakasae90b3332011-01-07 15:01:51 +090059static void prof_out(void) {
satok61e2f852011-01-05 14:13:07 +090060 if (profile_counter[PROF_BUF_SIZE - 1] != 1) {
satok9fb6f472012-01-13 18:01:22 +090061 AKLOGI("Error: You must call PROF_OPEN before PROF_CLOSE.");
satok61e2f852011-01-05 14:13:07 +090062 }
satok9fb6f472012-01-13 18:01:22 +090063 AKLOGI("Total time is %6.3f ms.",
Ken Wakasae90b3332011-01-07 15:01:51 +090064 profile_buf[PROF_BUF_SIZE - 1] * 1000 / (double)CLOCKS_PER_SEC);
satok61e2f852011-01-05 14:13:07 +090065 double all = 0;
Ken Wakasae90b3332011-01-07 15:01:51 +090066 for (int i = 0; i < PROF_BUF_SIZE - 1; ++i) {
satok61e2f852011-01-05 14:13:07 +090067 all += profile_buf[i];
68 }
Ken Wakasae90b3332011-01-07 15:01:51 +090069 if (all == 0) all = 1;
70 for (int i = 0; i < PROF_BUF_SIZE - 1; ++i) {
71 if (profile_buf[i] != 0) {
satok9fb6f472012-01-13 18:01:22 +090072 AKLOGI("(%d): Used %4.2f%%, %8.4f ms. Called %d times.",
Ken Wakasae90b3332011-01-07 15:01:51 +090073 i, (profile_buf[i] * 100 / all),
74 profile_buf[i] * 1000 / (double)CLOCKS_PER_SEC, profile_counter[i]);
75 }
satok61e2f852011-01-05 14:13:07 +090076 }
77}
78
satok20d9fda2011-07-13 14:40:30 +090079#else // FLAG_DO_PROFILE
satok61e2f852011-01-05 14:13:07 +090080#define PROF_BUF_SIZE 0
81#define PROF_RESET
82#define PROF_COUNT(prof_buf_id)
83#define PROF_OPEN
84#define PROF_START(prof_buf_id)
85#define PROF_CLOSE
86#define PROF_END(prof_buf_id)
87#define PROF_CLOCK_OUT(prof_buf_id)
88#define PROF_CLOCKOUT(prof_buf_id)
89#define PROF_OUTALL
90
satok20d9fda2011-07-13 14:40:30 +090091#endif // FLAG_DO_PROFILE
92
93#ifdef FLAG_DBG
94#include <cutils/log.h>
95#ifndef LOG_TAG
96#define LOG_TAG "LatinIME: "
97#endif
98#define DEBUG_DICT true
99#define DEBUG_DICT_FULL false
satok0cedd2b2011-08-12 01:05:27 +0900100#define DEBUG_EDIT_DISTANCE false
satok10266c02011-08-19 22:05:59 +0900101#define DEBUG_SHOW_FOUND_WORD false
satok20d9fda2011-07-13 14:40:30 +0900102#define DEBUG_NODE DEBUG_DICT_FULL
103#define DEBUG_TRACE DEBUG_DICT_FULL
satok1a6da632011-12-16 23:15:06 +0900104#define DEBUG_PROXIMITY_INFO false
satok10266c02011-08-19 22:05:59 +0900105#define DEBUG_CORRECTION false
106#define DEBUG_CORRECTION_FREQ true
satok16379df2011-12-12 20:53:22 +0900107#define DEBUG_WORDS_PRIORITY_QUEUE true
satok20d9fda2011-07-13 14:40:30 +0900108
satok0cedd2b2011-08-12 01:05:27 +0900109#define DUMP_WORD(word, length) do { dumpWord(word, length); } while(0)
110
111static char charBuf[50];
112
113static void dumpWord(const unsigned short* word, const int length) {
114 for (int i = 0; i < length; ++i) {
115 charBuf[i] = word[i];
116 }
117 charBuf[length] = 0;
satok9fb6f472012-01-13 18:01:22 +0900118 AKLOGI("[ %s ]", charBuf);
satok0cedd2b2011-08-12 01:05:27 +0900119}
120
satok20d9fda2011-07-13 14:40:30 +0900121#else // FLAG_DBG
satok827ced82011-07-14 09:01:09 +0900122
satok20d9fda2011-07-13 14:40:30 +0900123#define DEBUG_DICT false
124#define DEBUG_DICT_FULL false
satok0cedd2b2011-08-12 01:05:27 +0900125#define DEBUG_EDIT_DISTANCE false
satok20d9fda2011-07-13 14:40:30 +0900126#define DEBUG_SHOW_FOUND_WORD false
127#define DEBUG_NODE false
128#define DEBUG_TRACE false
129#define DEBUG_PROXIMITY_INFO false
satok10266c02011-08-19 22:05:59 +0900130#define DEBUG_CORRECTION false
131#define DEBUG_CORRECTION_FREQ false
satok16379df2011-12-12 20:53:22 +0900132#define DEBUG_WORDS_PRIORITY_QUEUE false
satok20d9fda2011-07-13 14:40:30 +0900133
satok0cedd2b2011-08-12 01:05:27 +0900134#define DUMP_WORD(word, length)
135
satoke808e432010-12-02 14:53:24 +0900136#endif // FLAG_DBG
137
satok662fe692010-12-08 17:05:39 +0900138#ifndef U_SHORT_MAX
Yusuke Nojima63095932011-09-20 14:03:42 +0900139#define U_SHORT_MAX 65535 // ((1 << 16) - 1)
satok662fe692010-12-08 17:05:39 +0900140#endif
Jean Chalarda5d58492011-02-18 17:50:58 +0900141#ifndef S_INT_MAX
satok3c4bb772011-03-04 22:50:19 -0800142#define S_INT_MAX 2147483647 // ((1 << 31) - 1)
Jean Chalarda5d58492011-02-18 17:50:58 +0900143#endif
satok662fe692010-12-08 17:05:39 +0900144
Ken Wakasae90b3332011-01-07 15:01:51 +0900145// Define this to use mmap() for dictionary loading. Undefine to use malloc() instead of mmap().
146// We measured and compared performance of both, and found mmap() is fairly good in terms of
147// loading time, and acceptable even for several initial lookups which involve page faults.
148#define USE_MMAP_FOR_DICTIONARY
149
satoke808e432010-12-02 14:53:24 +0900150// 22-bit address = ~4MB dictionary size limit, which on average would be about 200k-300k words
151#define ADDRESS_MASK 0x3FFFFF
152
153// The bit that decides if an address follows in the next 22 bits
154#define FLAG_ADDRESS_MASK 0x40
155// The bit that decides if this is a terminal node for a word. The node could still have children,
156// if the word has other endings.
157#define FLAG_TERMINAL_MASK 0x80
158
159#define FLAG_BIGRAM_READ 0x80
160#define FLAG_BIGRAM_CHILDEXIST 0x40
161#define FLAG_BIGRAM_CONTINUED 0x80
162#define FLAG_BIGRAM_FREQ 0x7F
163
164#define DICTIONARY_VERSION_MIN 200
Jean Chalard1059f272011-06-28 20:45:05 +0900165// TODO: remove this constant when the switch to the new dict format is over
satoke808e432010-12-02 14:53:24 +0900166#define DICTIONARY_HEADER_SIZE 2
Jean Chalard1059f272011-06-28 20:45:05 +0900167#define NEW_DICTIONARY_HEADER_SIZE 5
satoke808e432010-12-02 14:53:24 +0900168#define NOT_VALID_WORD -99
Jean Chalard1059f272011-06-28 20:45:05 +0900169#define NOT_A_CHARACTER -1
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900170#define NOT_A_DISTANCE -1
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900171#define EQUIVALENT_CHAR_WITHOUT_DISTANCE_INFO -2
172#define PROXIMITY_CHAR_WITHOUT_DISTANCE_INFO -3
173#define NOT_A_INDEX -1
satoke808e432010-12-02 14:53:24 +0900174
satok817e5172011-03-04 06:06:45 -0800175#define KEYCODE_SPACE ' '
176
Yusuke Nojimaafb90762011-10-05 18:11:42 +0900177#define CALIBRATE_SCORE_BY_TOUCH_COORDINATES true
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900178
satok662fe692010-12-08 17:05:39 +0900179#define SUGGEST_WORDS_WITH_MISSING_CHARACTER true
180#define SUGGEST_WORDS_WITH_MISSING_SPACE_CHARACTER true
181#define SUGGEST_WORDS_WITH_EXCESSIVE_CHARACTER true
satoka3d78f62010-12-09 22:08:33 +0900182#define SUGGEST_WORDS_WITH_TRANSPOSED_CHARACTERS true
satok817e5172011-03-04 06:06:45 -0800183#define SUGGEST_WORDS_WITH_SPACE_PROXIMITY true
satoka3d78f62010-12-09 22:08:33 +0900184
Jean Chalard8dc754a2011-01-27 14:20:22 +0900185// The following "rate"s are used as a multiplier before dividing by 100, so they are in percent.
satok72bc17e2011-04-13 17:23:27 +0900186#define WORDS_WITH_MISSING_CHARACTER_DEMOTION_RATE 80
satokdc5301e2011-04-11 16:14:45 +0900187#define WORDS_WITH_MISSING_CHARACTER_DEMOTION_START_POS_10X 12
satok99c908a2011-05-24 14:28:13 +0900188#define WORDS_WITH_MISSING_SPACE_CHARACTER_DEMOTION_RATE 67
satoka3d78f62010-12-09 22:08:33 +0900189#define WORDS_WITH_EXCESSIVE_CHARACTER_DEMOTION_RATE 75
satok54fe9e02010-12-13 14:42:35 +0900190#define WORDS_WITH_EXCESSIVE_CHARACTER_OUT_OF_PROXIMITY_DEMOTION_RATE 75
satoka3d78f62010-12-09 22:08:33 +0900191#define WORDS_WITH_TRANSPOSED_CHARACTERS_DEMOTION_RATE 60
satok58c49b92011-01-27 03:23:39 +0900192#define FULL_MATCHED_WORDS_PROMOTION_RATE 120
satok9d2a3022011-04-14 19:13:34 +0900193#define WORDS_WITH_PROXIMITY_CHARACTER_DEMOTION_RATE 90
satok635f68e2011-08-10 22:19:33 +0900194#define WORDS_WITH_MATCH_SKIP_PROMOTION_RATE 105
satokbcac0e92011-08-15 22:30:33 +0900195#define WORDS_WITH_JUST_ONE_CORRECTION_PROMOTION_RATE 160
satok10266c02011-08-19 22:05:59 +0900196#define CORRECTION_COUNT_RATE_DEMOTION_RATE_BASE 45
197#define INPUT_EXCEEDS_OUTPUT_DEMOTION_RATE 70
198#define FIRST_CHAR_DIFFERENT_DEMOTION_RATE 96
satokeb050fc2011-10-03 19:21:13 +0900199#define TWO_WORDS_CAPITALIZED_DEMOTION_RATE 50
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900200#define ZERO_DISTANCE_PROMOTION_RATE 110
201#define NEUTRAL_SCORE_SQUARED_RADIUS 8.0f
202#define HALF_SCORE_SQUARED_RADIUS 32.0f
satoke808e432010-12-02 14:53:24 +0900203
satok1147c7b2011-12-14 15:04:58 +0900204// This must be greater than or equal to MAX_WORD_LENGTH defined in BinaryDictionary.java
satokf5cded12010-12-06 21:28:24 +0900205// This is only used for the size of array. Not to be used in c functions.
206#define MAX_WORD_LENGTH_INTERNAL 48
satok715514d2010-12-02 20:19:59 +0900207
satoka7e5a5a2011-12-15 16:49:12 +0900208// Word limit for sub queues used in WordsPriorityQueuePool. Sub queues are temporary queues used
209// for better performance.
210#define SUB_QUEUE_MAX_WORDS 5
satokb9604772012-01-13 15:41:17 +0900211#define SUB_QUEUE_MAX_COUNT 10
satoka7e5a5a2011-12-15 16:49:12 +0900212
satok68319262010-12-03 19:38:08 +0900213#define MAX_DEPTH_MULTIPLIER 3
214
Jean Chalarda787dba2011-03-04 12:17:48 +0900215// TODO: Reduce this constant if possible; check the maximum number of umlauts in the same German
216// word in the dictionary
217#define DEFAULT_MAX_UMLAUT_SEARCH_DEPTH 5
218
satok54fe9e02010-12-13 14:42:35 +0900219// Minimum suggest depth for one word for all cases except for missing space suggestions.
220#define MIN_SUGGEST_DEPTH 1
221#define MIN_USER_TYPED_LENGTH_FOR_MISSING_SPACE_SUGGESTION 3
222#define MIN_USER_TYPED_LENGTH_FOR_EXCESSIVE_CHARACTER_SUGGESTION 3
satok662fe692010-12-08 17:05:39 +0900223
satokf5cded12010-12-06 21:28:24 +0900224#define min(a,b) ((a)<(b)?(a):(b))
satokbcac0e92011-08-15 22:30:33 +0900225#define max(a,b) ((a)>(b)?(a):(b))
satokf5cded12010-12-06 21:28:24 +0900226
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900227// The ratio of neutral area radius to sweet spot radius.
228#define NEUTRAL_AREA_RADIUS_RATIO 1.3f
229
satoke808e432010-12-02 14:53:24 +0900230#endif // LATINIME_DEFINES_H