blob: e460f1ef4316fe089787a6e74647a387de9b3727 [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
satok20d9fda2011-07-13 14:40:30 +090021#ifdef FLAG_DO_PROFILE
satok61e2f852011-01-05 14:13:07 +090022// Profiler
satok787945b2011-07-14 08:32:57 +090023#include <cutils/log.h>
satok61e2f852011-01-05 14:13:07 +090024#include <time.h>
25#define PROF_BUF_SIZE 100
26static double profile_buf[PROF_BUF_SIZE];
27static double profile_old[PROF_BUF_SIZE];
28static unsigned int profile_counter[PROF_BUF_SIZE];
29
Ken Wakasae90b3332011-01-07 15:01:51 +090030#define PROF_RESET prof_reset()
31#define PROF_COUNT(prof_buf_id) ++profile_counter[prof_buf_id]
32#define PROF_OPEN do { PROF_RESET; PROF_START(PROF_BUF_SIZE - 1); } while(0)
33#define PROF_START(prof_buf_id) do { \
34 PROF_COUNT(prof_buf_id); profile_old[prof_buf_id] = (clock()); } while(0)
35#define PROF_CLOSE do { PROF_END(PROF_BUF_SIZE - 1); PROF_OUTALL; } while(0)
36#define PROF_END(prof_buf_id) profile_buf[prof_buf_id] += ((clock()) - profile_old[prof_buf_id])
37#define PROF_CLOCKOUT(prof_buf_id) \
38 LOGI("%s : clock is %f", __FUNCTION__, (clock() - profile_old[prof_buf_id]))
39#define PROF_OUTALL do { LOGI("--- %s ---", __FUNCTION__); prof_out(); } while(0)
satok61e2f852011-01-05 14:13:07 +090040
Ken Wakasae90b3332011-01-07 15:01:51 +090041static void prof_reset(void) {
42 for (int i = 0; i < PROF_BUF_SIZE; ++i) {
satok61e2f852011-01-05 14:13:07 +090043 profile_buf[i] = 0;
44 profile_old[i] = 0;
45 profile_counter[i] = 0;
46 }
47}
48
Ken Wakasae90b3332011-01-07 15:01:51 +090049static void prof_out(void) {
satok61e2f852011-01-05 14:13:07 +090050 if (profile_counter[PROF_BUF_SIZE - 1] != 1) {
51 LOGI("Error: You must call PROF_OPEN before PROF_CLOSE.");
52 }
53 LOGI("Total time is %6.3f ms.",
Ken Wakasae90b3332011-01-07 15:01:51 +090054 profile_buf[PROF_BUF_SIZE - 1] * 1000 / (double)CLOCKS_PER_SEC);
satok61e2f852011-01-05 14:13:07 +090055 double all = 0;
Ken Wakasae90b3332011-01-07 15:01:51 +090056 for (int i = 0; i < PROF_BUF_SIZE - 1; ++i) {
satok61e2f852011-01-05 14:13:07 +090057 all += profile_buf[i];
58 }
Ken Wakasae90b3332011-01-07 15:01:51 +090059 if (all == 0) all = 1;
60 for (int i = 0; i < PROF_BUF_SIZE - 1; ++i) {
61 if (profile_buf[i] != 0) {
satok61e2f852011-01-05 14:13:07 +090062 LOGI("(%d): Used %4.2f%%, %8.4f ms. Called %d times.",
Ken Wakasae90b3332011-01-07 15:01:51 +090063 i, (profile_buf[i] * 100 / all),
64 profile_buf[i] * 1000 / (double)CLOCKS_PER_SEC, profile_counter[i]);
65 }
satok61e2f852011-01-05 14:13:07 +090066 }
67}
68
satok20d9fda2011-07-13 14:40:30 +090069#else // FLAG_DO_PROFILE
satok61e2f852011-01-05 14:13:07 +090070#define PROF_BUF_SIZE 0
71#define PROF_RESET
72#define PROF_COUNT(prof_buf_id)
73#define PROF_OPEN
74#define PROF_START(prof_buf_id)
75#define PROF_CLOSE
76#define PROF_END(prof_buf_id)
77#define PROF_CLOCK_OUT(prof_buf_id)
78#define PROF_CLOCKOUT(prof_buf_id)
79#define PROF_OUTALL
80
satok20d9fda2011-07-13 14:40:30 +090081#endif // FLAG_DO_PROFILE
82
83#ifdef FLAG_DBG
84#include <cutils/log.h>
85#ifndef LOG_TAG
86#define LOG_TAG "LatinIME: "
87#endif
88#define DEBUG_DICT true
89#define DEBUG_DICT_FULL false
90#define DEBUG_SHOW_FOUND_WORD DEBUG_DICT_FULL
91#define DEBUG_NODE DEBUG_DICT_FULL
92#define DEBUG_TRACE DEBUG_DICT_FULL
93#define DEBUG_PROXIMITY_INFO true
94
95#else // FLAG_DBG
satok787945b2011-07-14 08:32:57 +090096#ifndef FLAG_DO_PROFILE
satok20d9fda2011-07-13 14:40:30 +090097#define LOGE(fmt, ...)
98#define LOGI(fmt, ...)
satok787945b2011-07-14 08:32:57 +090099#endif // FLAG_DO_PROFILE
satok20d9fda2011-07-13 14:40:30 +0900100#define DEBUG_DICT false
101#define DEBUG_DICT_FULL false
102#define DEBUG_SHOW_FOUND_WORD false
103#define DEBUG_NODE false
104#define DEBUG_TRACE false
105#define DEBUG_PROXIMITY_INFO false
106
satoke808e432010-12-02 14:53:24 +0900107#endif // FLAG_DBG
108
satok662fe692010-12-08 17:05:39 +0900109#ifndef U_SHORT_MAX
110#define U_SHORT_MAX 1 << 16
111#endif
Jean Chalarda5d58492011-02-18 17:50:58 +0900112#ifndef S_INT_MAX
satok3c4bb772011-03-04 22:50:19 -0800113#define S_INT_MAX 2147483647 // ((1 << 31) - 1)
Jean Chalarda5d58492011-02-18 17:50:58 +0900114#endif
satok662fe692010-12-08 17:05:39 +0900115
Ken Wakasae90b3332011-01-07 15:01:51 +0900116// Define this to use mmap() for dictionary loading. Undefine to use malloc() instead of mmap().
117// We measured and compared performance of both, and found mmap() is fairly good in terms of
118// loading time, and acceptable even for several initial lookups which involve page faults.
119#define USE_MMAP_FOR_DICTIONARY
120
satoke808e432010-12-02 14:53:24 +0900121// 22-bit address = ~4MB dictionary size limit, which on average would be about 200k-300k words
122#define ADDRESS_MASK 0x3FFFFF
123
124// The bit that decides if an address follows in the next 22 bits
125#define FLAG_ADDRESS_MASK 0x40
126// The bit that decides if this is a terminal node for a word. The node could still have children,
127// if the word has other endings.
128#define FLAG_TERMINAL_MASK 0x80
129
130#define FLAG_BIGRAM_READ 0x80
131#define FLAG_BIGRAM_CHILDEXIST 0x40
132#define FLAG_BIGRAM_CONTINUED 0x80
133#define FLAG_BIGRAM_FREQ 0x7F
134
135#define DICTIONARY_VERSION_MIN 200
Jean Chalard1059f272011-06-28 20:45:05 +0900136// TODO: remove this constant when the switch to the new dict format is over
satoke808e432010-12-02 14:53:24 +0900137#define DICTIONARY_HEADER_SIZE 2
Jean Chalard1059f272011-06-28 20:45:05 +0900138#define NEW_DICTIONARY_HEADER_SIZE 5
satoke808e432010-12-02 14:53:24 +0900139#define NOT_VALID_WORD -99
Jean Chalard1059f272011-06-28 20:45:05 +0900140#define NOT_A_CHARACTER -1
satoke808e432010-12-02 14:53:24 +0900141
satok817e5172011-03-04 06:06:45 -0800142#define KEYCODE_SPACE ' '
143
satok662fe692010-12-08 17:05:39 +0900144#define SUGGEST_WORDS_WITH_MISSING_CHARACTER true
145#define SUGGEST_WORDS_WITH_MISSING_SPACE_CHARACTER true
146#define SUGGEST_WORDS_WITH_EXCESSIVE_CHARACTER true
satoka3d78f62010-12-09 22:08:33 +0900147#define SUGGEST_WORDS_WITH_TRANSPOSED_CHARACTERS true
satok817e5172011-03-04 06:06:45 -0800148#define SUGGEST_WORDS_WITH_SPACE_PROXIMITY true
satoka3d78f62010-12-09 22:08:33 +0900149
Jean Chalard8dc754a2011-01-27 14:20:22 +0900150// The following "rate"s are used as a multiplier before dividing by 100, so they are in percent.
satok72bc17e2011-04-13 17:23:27 +0900151#define WORDS_WITH_MISSING_CHARACTER_DEMOTION_RATE 80
satokdc5301e2011-04-11 16:14:45 +0900152#define WORDS_WITH_MISSING_CHARACTER_DEMOTION_START_POS_10X 12
satok99c908a2011-05-24 14:28:13 +0900153#define WORDS_WITH_MISSING_SPACE_CHARACTER_DEMOTION_RATE 67
satoka3d78f62010-12-09 22:08:33 +0900154#define WORDS_WITH_EXCESSIVE_CHARACTER_DEMOTION_RATE 75
satok54fe9e02010-12-13 14:42:35 +0900155#define WORDS_WITH_EXCESSIVE_CHARACTER_OUT_OF_PROXIMITY_DEMOTION_RATE 75
satoka3d78f62010-12-09 22:08:33 +0900156#define WORDS_WITH_TRANSPOSED_CHARACTERS_DEMOTION_RATE 60
satok58c49b92011-01-27 03:23:39 +0900157#define FULL_MATCHED_WORDS_PROMOTION_RATE 120
satok9d2a3022011-04-14 19:13:34 +0900158#define WORDS_WITH_PROXIMITY_CHARACTER_DEMOTION_RATE 90
satoke808e432010-12-02 14:53:24 +0900159
satokf5cded12010-12-06 21:28:24 +0900160// This should be greater than or equal to MAX_WORD_LENGTH defined in BinaryDictionary.java
161// This is only used for the size of array. Not to be used in c functions.
162#define MAX_WORD_LENGTH_INTERNAL 48
satok715514d2010-12-02 20:19:59 +0900163
satok68319262010-12-03 19:38:08 +0900164#define MAX_DEPTH_MULTIPLIER 3
165
Jean Chalarda787dba2011-03-04 12:17:48 +0900166// TODO: Reduce this constant if possible; check the maximum number of umlauts in the same German
167// word in the dictionary
168#define DEFAULT_MAX_UMLAUT_SEARCH_DEPTH 5
169
satok54fe9e02010-12-13 14:42:35 +0900170// Minimum suggest depth for one word for all cases except for missing space suggestions.
171#define MIN_SUGGEST_DEPTH 1
172#define MIN_USER_TYPED_LENGTH_FOR_MISSING_SPACE_SUGGESTION 3
173#define MIN_USER_TYPED_LENGTH_FOR_EXCESSIVE_CHARACTER_SUGGESTION 3
satok662fe692010-12-08 17:05:39 +0900174
Tadashi G. Takaoka887f11e2011-02-10 20:53:58 +0900175// The size of next letters frequency array. Zero will disable the feature.
176#define NEXT_LETTERS_SIZE 0
177
satokf5cded12010-12-06 21:28:24 +0900178#define min(a,b) ((a)<(b)?(a):(b))
179
satoke808e432010-12-02 14:53:24 +0900180#endif // LATINIME_DEFINES_H