Home | History | Annotate | Download | only in src
      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 #define AKLOGE ALOGE
     24 #define AKLOGI ALOGI
     25 
     26 #define DUMP_WORD(word, length) do { dumpWord(word, length); } while(0)
     27 #define DUMP_WORD_INT(word, length) do { dumpWordInt(word, length); } while(0)
     28 
     29 static inline void dumpWord(const unsigned short* word, const int length) {
     30     static char charBuf[50];
     31 
     32     for (int i = 0; i < length; ++i) {
     33         charBuf[i] = word[i];
     34     }
     35     charBuf[length] = 0;
     36     AKLOGI("[ %s ]", charBuf);
     37 }
     38 
     39 static inline void dumpWordInt(const int* word, const int length) {
     40     static char charBuf[50];
     41 
     42     for (int i = 0; i < length; ++i) {
     43         charBuf[i] = word[i];
     44     }
     45     charBuf[length] = 0;
     46     AKLOGI("i[ %s ]", charBuf);
     47 }
     48 
     49 #else
     50 #define AKLOGE(fmt, ...)
     51 #define AKLOGI(fmt, ...)
     52 #define DUMP_WORD(word, length)
     53 #define DUMP_WORD_INT(word, length)
     54 #endif
     55 
     56 #ifdef FLAG_DO_PROFILE
     57 // Profiler
     58 #include <time.h>
     59 
     60 #define PROF_BUF_SIZE 100
     61 static float profile_buf[PROF_BUF_SIZE];
     62 static float profile_old[PROF_BUF_SIZE];
     63 static unsigned int profile_counter[PROF_BUF_SIZE];
     64 
     65 #define PROF_RESET               prof_reset()
     66 #define PROF_COUNT(prof_buf_id)  ++profile_counter[prof_buf_id]
     67 #define PROF_OPEN                do { PROF_RESET; PROF_START(PROF_BUF_SIZE - 1); } while(0)
     68 #define PROF_START(prof_buf_id)  do { \
     69         PROF_COUNT(prof_buf_id); profile_old[prof_buf_id] = (clock()); } while(0)
     70 #define PROF_CLOSE               do { PROF_END(PROF_BUF_SIZE - 1); PROF_OUTALL; } while(0)
     71 #define PROF_END(prof_buf_id)    profile_buf[prof_buf_id] += ((clock()) - profile_old[prof_buf_id])
     72 #define PROF_CLOCKOUT(prof_buf_id) \
     73         AKLOGI("%s : clock is %f", __FUNCTION__, (clock() - profile_old[prof_buf_id]))
     74 #define PROF_OUTALL              do { AKLOGI("--- %s ---", __FUNCTION__); prof_out(); } while(0)
     75 
     76 static inline void prof_reset(void) {
     77     for (int i = 0; i < PROF_BUF_SIZE; ++i) {
     78         profile_buf[i] = 0;
     79         profile_old[i] = 0;
     80         profile_counter[i] = 0;
     81     }
     82 }
     83 
     84 static inline void prof_out(void) {
     85     if (profile_counter[PROF_BUF_SIZE - 1] != 1) {
     86         AKLOGI("Error: You must call PROF_OPEN before PROF_CLOSE.");
     87     }
     88     AKLOGI("Total time is %6.3f ms.",
     89             profile_buf[PROF_BUF_SIZE - 1] * 1000 / (float)CLOCKS_PER_SEC);
     90     float all = 0;
     91     for (int i = 0; i < PROF_BUF_SIZE - 1; ++i) {
     92         all += profile_buf[i];
     93     }
     94     if (all == 0) all = 1;
     95     for (int i = 0; i < PROF_BUF_SIZE - 1; ++i) {
     96         if (profile_buf[i] != 0) {
     97             AKLOGI("(%d): Used %4.2f%%, %8.4f ms. Called %d times.",
     98                     i, (profile_buf[i] * 100 / all),
     99                     profile_buf[i] * 1000 / (float)CLOCKS_PER_SEC, profile_counter[i]);
    100         }
    101     }
    102 }
    103 
    104 #else // FLAG_DO_PROFILE
    105 #define PROF_BUF_SIZE 0
    106 #define PROF_RESET
    107 #define PROF_COUNT(prof_buf_id)
    108 #define PROF_OPEN
    109 #define PROF_START(prof_buf_id)
    110 #define PROF_CLOSE
    111 #define PROF_END(prof_buf_id)
    112 #define PROF_CLOCK_OUT(prof_buf_id)
    113 #define PROF_CLOCKOUT(prof_buf_id)
    114 #define PROF_OUTALL
    115 
    116 #endif // FLAG_DO_PROFILE
    117 
    118 #ifdef FLAG_DBG
    119 #include <cutils/log.h>
    120 #ifndef LOG_TAG
    121 #define LOG_TAG "LatinIME: "
    122 #endif
    123 #define DEBUG_DICT true
    124 #define DEBUG_DICT_FULL false
    125 #define DEBUG_EDIT_DISTANCE false
    126 #define DEBUG_SHOW_FOUND_WORD false
    127 #define DEBUG_NODE DEBUG_DICT_FULL
    128 #define DEBUG_TRACE DEBUG_DICT_FULL
    129 #define DEBUG_PROXIMITY_INFO false
    130 #define DEBUG_PROXIMITY_CHARS false
    131 #define DEBUG_CORRECTION false
    132 #define DEBUG_CORRECTION_FREQ false
    133 #define DEBUG_WORDS_PRIORITY_QUEUE false
    134 
    135 #else // FLAG_DBG
    136 
    137 #define DEBUG_DICT false
    138 #define DEBUG_DICT_FULL false
    139 #define DEBUG_EDIT_DISTANCE false
    140 #define DEBUG_SHOW_FOUND_WORD false
    141 #define DEBUG_NODE false
    142 #define DEBUG_TRACE false
    143 #define DEBUG_PROXIMITY_INFO false
    144 #define DEBUG_PROXIMITY_CHARS false
    145 #define DEBUG_CORRECTION false
    146 #define DEBUG_CORRECTION_FREQ false
    147 #define DEBUG_WORDS_PRIORITY_QUEUE false
    148 
    149 
    150 #endif // FLAG_DBG
    151 
    152 #ifndef U_SHORT_MAX
    153 #define U_SHORT_MAX 65535    // ((1 << 16) - 1)
    154 #endif
    155 #ifndef S_INT_MAX
    156 #define S_INT_MAX 2147483647 // ((1 << 31) - 1)
    157 #endif
    158 
    159 // Define this to use mmap() for dictionary loading.  Undefine to use malloc() instead of mmap().
    160 // We measured and compared performance of both, and found mmap() is fairly good in terms of
    161 // loading time, and acceptable even for several initial lookups which involve page faults.
    162 #define USE_MMAP_FOR_DICTIONARY
    163 
    164 // 22-bit address = ~4MB dictionary size limit, which on average would be about 200k-300k words
    165 #define ADDRESS_MASK 0x3FFFFF
    166 
    167 // The bit that decides if an address follows in the next 22 bits
    168 #define FLAG_ADDRESS_MASK 0x40
    169 // The bit that decides if this is a terminal node for a word. The node could still have children,
    170 // if the word has other endings.
    171 #define FLAG_TERMINAL_MASK 0x80
    172 
    173 #define FLAG_BIGRAM_READ 0x80
    174 #define FLAG_BIGRAM_CHILDEXIST 0x40
    175 #define FLAG_BIGRAM_CONTINUED 0x80
    176 #define FLAG_BIGRAM_FREQ 0x7F
    177 
    178 #define DICTIONARY_VERSION_MIN 200
    179 #define NOT_VALID_WORD -99
    180 #define NOT_A_CHARACTER -1
    181 #define NOT_A_DISTANCE -1
    182 #define NOT_A_COORDINATE -1
    183 #define EQUIVALENT_CHAR_WITHOUT_DISTANCE_INFO -2
    184 #define PROXIMITY_CHAR_WITHOUT_DISTANCE_INFO -3
    185 #define ADDITIONAL_PROXIMITY_CHAR_DISTANCE_INFO -4
    186 #define NOT_AN_INDEX -1
    187 #define NOT_A_PROBABILITY -1
    188 
    189 #define KEYCODE_SPACE ' '
    190 
    191 #define CALIBRATE_SCORE_BY_TOUCH_COORDINATES true
    192 
    193 #define SUGGEST_WORDS_WITH_MISSING_CHARACTER true
    194 #define SUGGEST_WORDS_WITH_EXCESSIVE_CHARACTER true
    195 #define SUGGEST_WORDS_WITH_TRANSPOSED_CHARACTERS true
    196 #define SUGGEST_MULTIPLE_WORDS true
    197 
    198 // The following "rate"s are used as a multiplier before dividing by 100, so they are in percent.
    199 #define WORDS_WITH_MISSING_CHARACTER_DEMOTION_RATE 80
    200 #define WORDS_WITH_MISSING_CHARACTER_DEMOTION_START_POS_10X 12
    201 #define WORDS_WITH_MISSING_SPACE_CHARACTER_DEMOTION_RATE 58
    202 #define WORDS_WITH_MISTYPED_SPACE_DEMOTION_RATE 50
    203 #define WORDS_WITH_EXCESSIVE_CHARACTER_DEMOTION_RATE 75
    204 #define WORDS_WITH_EXCESSIVE_CHARACTER_OUT_OF_PROXIMITY_DEMOTION_RATE 75
    205 #define WORDS_WITH_TRANSPOSED_CHARACTERS_DEMOTION_RATE 70
    206 #define FULL_MATCHED_WORDS_PROMOTION_RATE 120
    207 #define WORDS_WITH_PROXIMITY_CHARACTER_DEMOTION_RATE 90
    208 #define WORDS_WITH_ADDITIONAL_PROXIMITY_CHARACTER_DEMOTION_RATE 70
    209 #define WORDS_WITH_MATCH_SKIP_PROMOTION_RATE 105
    210 #define WORDS_WITH_JUST_ONE_CORRECTION_PROMOTION_RATE 148
    211 #define WORDS_WITH_JUST_ONE_CORRECTION_PROMOTION_MULTIPLIER 3
    212 #define CORRECTION_COUNT_RATE_DEMOTION_RATE_BASE 45
    213 #define INPUT_EXCEEDS_OUTPUT_DEMOTION_RATE 70
    214 #define FIRST_CHAR_DIFFERENT_DEMOTION_RATE 96
    215 #define TWO_WORDS_CAPITALIZED_DEMOTION_RATE 50
    216 #define TWO_WORDS_CORRECTION_DEMOTION_BASE 80
    217 #define TWO_WORDS_PLUS_OTHER_ERROR_CORRECTION_DEMOTION_DIVIDER 1
    218 #define ZERO_DISTANCE_PROMOTION_RATE 110
    219 #define NEUTRAL_SCORE_SQUARED_RADIUS 8.0f
    220 #define HALF_SCORE_SQUARED_RADIUS 32.0f
    221 #define MAX_FREQ 255
    222 #define MAX_BIGRAM_FREQ 15
    223 
    224 // This must be greater than or equal to MAX_WORD_LENGTH defined in BinaryDictionary.java
    225 // This is only used for the size of array. Not to be used in c functions.
    226 #define MAX_WORD_LENGTH_INTERNAL 48
    227 
    228 // This must be equal to ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE in KeyDetector.java
    229 #define ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE 2
    230 
    231 // Word limit for sub queues used in WordsPriorityQueuePool.  Sub queues are temporary queues used
    232 // for better performance.
    233 // Holds up to 1 candidate for each word
    234 #define SUB_QUEUE_MAX_WORDS 1
    235 #define SUB_QUEUE_MAX_COUNT 10
    236 #define SUB_QUEUE_MIN_WORD_LENGTH 4
    237 // TODO: Extend this limitation
    238 #define MULTIPLE_WORDS_SUGGESTION_MAX_WORDS 5
    239 // TODO: Remove this limitation
    240 #define MULTIPLE_WORDS_SUGGESTION_MAX_WORD_LENGTH 12
    241 // TODO: Remove this limitation
    242 #define MULTIPLE_WORDS_SUGGESTION_MAX_TOTAL_TRAVERSE_COUNT 45
    243 #define MULTIPLE_WORDS_DEMOTION_RATE 80
    244 #define MIN_INPUT_LENGTH_FOR_THREE_OR_MORE_WORDS_CORRECTION 6
    245 
    246 #define TWO_WORDS_CORRECTION_WITH_OTHER_ERROR_THRESHOLD 0.35
    247 #define START_TWO_WORDS_CORRECTION_THRESHOLD 0.185
    248 /* heuristic... This should be changed if we change the unit of the frequency. */
    249 #define SUPPRESS_SHORT_MULTIPLE_WORDS_THRESHOLD_FREQ (MAX_FREQ * 58 / 100)
    250 
    251 #define MAX_DEPTH_MULTIPLIER 3
    252 
    253 #define FIRST_WORD_INDEX 0
    254 
    255 // TODO: Reduce this constant if possible; check the maximum number of digraphs in the same
    256 // word in the dictionary for languages with digraphs, like German and French
    257 #define DEFAULT_MAX_DIGRAPH_SEARCH_DEPTH 5
    258 
    259 // Minimum suggest depth for one word for all cases except for missing space suggestions.
    260 #define MIN_SUGGEST_DEPTH 1
    261 #define MIN_USER_TYPED_LENGTH_FOR_MULTIPLE_WORD_SUGGESTION 3
    262 #define MIN_USER_TYPED_LENGTH_FOR_EXCESSIVE_CHARACTER_SUGGESTION 3
    263 
    264 // Size, in bytes, of the bloom filter index for bigrams
    265 // 128 gives us 1024 buckets. The probability of false positive is (1 - e ** (-kn/m))**k,
    266 // where k is the number of hash functions, n the number of bigrams, and m the number of
    267 // bits we can test.
    268 // At the moment 100 is the maximum number of bigrams for a word with the current
    269 // dictionaries, so n = 100. 1024 buckets give us m = 1024.
    270 // With 1 hash function, our false positive rate is about 9.3%, which should be enough for
    271 // our uses since we are only using this to increase average performance. For the record,
    272 // k = 2 gives 3.1% and k = 3 gives 1.6%. With k = 1, making m = 2048 gives 4.8%,
    273 // and m = 4096 gives 2.4%.
    274 #define BIGRAM_FILTER_BYTE_SIZE 128
    275 // Must be smaller than BIGRAM_FILTER_BYTE_SIZE * 8, and preferably prime. 1021 is the largest
    276 // prime under 128 * 8.
    277 #define BIGRAM_FILTER_MODULO 1021
    278 #if BIGRAM_FILTER_BYTE_SIZE * 8 < BIGRAM_FILTER_MODULO
    279 #error "BIGRAM_FILTER_MODULO is larger than BIGRAM_FILTER_BYTE_SIZE"
    280 #endif
    281 
    282 template<typename T> inline T min(T a, T b) { return a < b ? a : b; }
    283 template<typename T> inline T max(T a, T b) { return a > b ? a : b; }
    284 
    285 // The ratio of neutral area radius to sweet spot radius.
    286 #define NEUTRAL_AREA_RADIUS_RATIO 1.3f
    287 
    288 // DEBUG
    289 #define INPUTLENGTH_FOR_DEBUG -1
    290 #define MIN_OUTPUT_INDEX_FOR_DEBUG -1
    291 
    292 #endif // LATINIME_DEFINES_H
    293