1*4882a593Smuzhiyun /* 2*4882a593Smuzhiyun * Copyright (C) 2009 The Android Open Source Project 3*4882a593Smuzhiyun * 4*4882a593Smuzhiyun * Licensed under the Apache License, Version 2.0 (the "License"); 5*4882a593Smuzhiyun * you may not use this file except in compliance with the License. 6*4882a593Smuzhiyun * You may obtain a copy of the License at 7*4882a593Smuzhiyun * 8*4882a593Smuzhiyun * http://www.apache.org/licenses/LICENSE-2.0 9*4882a593Smuzhiyun * 10*4882a593Smuzhiyun * Unless required by applicable law or agreed to in writing, software 11*4882a593Smuzhiyun * distributed under the License is distributed on an "AS IS" BASIS, 12*4882a593Smuzhiyun * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*4882a593Smuzhiyun * See the License for the specific language governing permissions and 14*4882a593Smuzhiyun * limitations under the License. 15*4882a593Smuzhiyun */ 16*4882a593Smuzhiyun 17*4882a593Smuzhiyun #ifndef PINYINIME_INCLUDE_SPELLINGTABLE_H__ 18*4882a593Smuzhiyun #define PINYINIME_INCLUDE_SPELLINGTABLE_H__ 19*4882a593Smuzhiyun 20*4882a593Smuzhiyun #include <stdlib.h> 21*4882a593Smuzhiyun #include "./dictdef.h" 22*4882a593Smuzhiyun 23*4882a593Smuzhiyun namespace ime_pinyin { 24*4882a593Smuzhiyun 25*4882a593Smuzhiyun #ifdef ___BUILD_MODEL___ 26*4882a593Smuzhiyun 27*4882a593Smuzhiyun const size_t kMaxSpellingSize = kMaxPinyinSize; 28*4882a593Smuzhiyun 29*4882a593Smuzhiyun typedef struct { 30*4882a593Smuzhiyun char str[kMaxSpellingSize + 1]; 31*4882a593Smuzhiyun double freq; 32*4882a593Smuzhiyun } RawSpelling, *PRawSpelling; 33*4882a593Smuzhiyun 34*4882a593Smuzhiyun // This class is used to store the spelling strings 35*4882a593Smuzhiyun // The length of the input spelling string should be less or equal to the 36*4882a593Smuzhiyun // spelling_size_ (set by init_table). If the input string is too long, 37*4882a593Smuzhiyun // we only keep its first spelling_size_ chars. 38*4882a593Smuzhiyun class SpellingTable { 39*4882a593Smuzhiyun private: 40*4882a593Smuzhiyun static const size_t kNotSupportNum = 3; 41*4882a593Smuzhiyun static const char kNotSupportList[kNotSupportNum][kMaxSpellingSize + 1]; 42*4882a593Smuzhiyun 43*4882a593Smuzhiyun bool need_score_; 44*4882a593Smuzhiyun 45*4882a593Smuzhiyun size_t spelling_max_num_; 46*4882a593Smuzhiyun 47*4882a593Smuzhiyun RawSpelling *raw_spellings_; 48*4882a593Smuzhiyun 49*4882a593Smuzhiyun // Used to store spelling strings. If the spelling table needs to calculate 50*4882a593Smuzhiyun // score, an extra char after each spelling string is the score. 51*4882a593Smuzhiyun // An item with a lower score has a higher probability. 52*4882a593Smuzhiyun char *spelling_buf_; 53*4882a593Smuzhiyun size_t spelling_size_; 54*4882a593Smuzhiyun 55*4882a593Smuzhiyun double total_freq_; 56*4882a593Smuzhiyun 57*4882a593Smuzhiyun size_t spelling_num_; 58*4882a593Smuzhiyun 59*4882a593Smuzhiyun double score_amplifier_; 60*4882a593Smuzhiyun 61*4882a593Smuzhiyun unsigned char average_score_; 62*4882a593Smuzhiyun 63*4882a593Smuzhiyun // If frozen is true, put_spelling() and contain() are not allowed to call. 64*4882a593Smuzhiyun bool frozen_; 65*4882a593Smuzhiyun 66*4882a593Smuzhiyun size_t get_hash_pos(const char* spelling_str); 67*4882a593Smuzhiyun size_t hash_pos_next(size_t hash_pos); 68*4882a593Smuzhiyun void free_resource(); 69*4882a593Smuzhiyun public: 70*4882a593Smuzhiyun SpellingTable(); 71*4882a593Smuzhiyun ~SpellingTable(); 72*4882a593Smuzhiyun 73*4882a593Smuzhiyun // pure_spl_size is the pure maximum spelling string size. For example, 74*4882a593Smuzhiyun // "zhuang" is the longgest item in Pinyin, so pure_spl_size should be 6. 75*4882a593Smuzhiyun // spl_max_num is the maximum number of spelling strings to store. 76*4882a593Smuzhiyun // need_score is used to indicate whether the caller needs to calculate a 77*4882a593Smuzhiyun // score for each spelling. 78*4882a593Smuzhiyun bool init_table(size_t pure_spl_size, size_t spl_max_num, bool need_score); 79*4882a593Smuzhiyun 80*4882a593Smuzhiyun // Put a spelling string to the table. 81*4882a593Smuzhiyun // It always returns false if called after arrange() withtout a new 82*4882a593Smuzhiyun // init_table() operation. 83*4882a593Smuzhiyun // freq is the spelling's occuring count. 84*4882a593Smuzhiyun // If the spelling has been in the table, occuring count will accumulated. 85*4882a593Smuzhiyun bool put_spelling(const char* spelling_str, double spl_count); 86*4882a593Smuzhiyun 87*4882a593Smuzhiyun // Test whether a spelling string is in the table. 88*4882a593Smuzhiyun // It always returns false, when being called after arrange() withtout a new 89*4882a593Smuzhiyun // init_table() operation. 90*4882a593Smuzhiyun bool contain(const char* spelling_str); 91*4882a593Smuzhiyun 92*4882a593Smuzhiyun // Sort the spelling strings and put them from the begin of the buffer. 93*4882a593Smuzhiyun // Return the pointer of the sorted spelling strings. 94*4882a593Smuzhiyun // item_size and spl_num return the item size and number of spelling. 95*4882a593Smuzhiyun // Because each spelling uses a '\0' as terminator, the returned item_size is 96*4882a593Smuzhiyun // at least one char longer than the spl_size parameter specified by 97*4882a593Smuzhiyun // init_table(). If the table is initialized to calculate score, item_size 98*4882a593Smuzhiyun // will be increased by 1, and current_spl_str[item_size - 1] stores an 99*4882a593Smuzhiyun // unsinged char score. 100*4882a593Smuzhiyun // An item with a lower score has a higher probability. 101*4882a593Smuzhiyun // Do not call put_spelling() and contains() after arrange(). 102*4882a593Smuzhiyun const char* arrange(size_t *item_size, size_t *spl_num); 103*4882a593Smuzhiyun 104*4882a593Smuzhiyun float get_score_amplifier(); 105*4882a593Smuzhiyun 106*4882a593Smuzhiyun unsigned char get_average_score(); 107*4882a593Smuzhiyun }; 108*4882a593Smuzhiyun #endif // ___BUILD_MODEL___ 109*4882a593Smuzhiyun } 110*4882a593Smuzhiyun 111*4882a593Smuzhiyun #endif // PINYINIME_INCLUDE_SPELLINGTABLE_H__ 112