fixes compile warnings
[platform/upstream/libpinyin.git] / src / storage / pinyin_parser2.cpp
1 /* 
2  *  libpinyin
3  *  Library to deal with pinyin.
4  *  
5  *  Copyright (C) 2011 Peng Wu <alexepico@gmail.com>
6  *  
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  * 
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  *  GNU General Public License for more details.
16  *  
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20  */
21
22
23 #include <ctype.h>
24 #include <assert.h>
25 #include <string.h>
26 #include "stl_lite.h"
27 #include "pinyin_parser2.h"
28 #include "pinyin_phrase2.h"
29 #include "pinyin_custom2.h"
30 #include "chewing_key.h"
31 #include "pinyin_parser_table.h"
32 #include "double_pinyin_table.h"
33 #include "chewing_table.h"
34
35
36 using namespace pinyin;
37
38 static bool check_pinyin_options(guint32 options, const pinyin_index_item_t * item) {
39     guint32 flags = item->m_flags;
40     assert (flags & IS_PINYIN);
41
42     /* handle incomplete pinyin. */
43     if (flags & PINYIN_INCOMPLETE) {
44         if (!(options & PINYIN_INCOMPLETE))
45             return false;
46     }
47
48     /* handle correct pinyin, currently only one flag per item. */
49     flags &= PINYIN_CORRECT_ALL;
50     options &= PINYIN_CORRECT_ALL;
51
52     if (flags) {
53         if ((flags & options) != flags)
54             return false;
55     }
56
57     return true;
58 }
59
60 static bool check_chewing_options(guint32 options, const chewing_index_item_t * item) {
61     guint32 flags = item->m_flags;
62     assert (flags & IS_CHEWING);
63
64     /* handle incomplete chewing. */
65     if (flags & CHEWING_INCOMPLETE) {
66         if (!(options & CHEWING_INCOMPLETE))
67             return false;
68     }
69
70     return true;
71 }
72
73
74 /* methods for Chewing Keys to access pinyin parser table. */
75 const char * ChewingKeyRest::get_pinyin_string(){
76     if (m_table_index == 0)
77         return NULL;
78
79     /* check end boundary. */
80     assert(m_table_index < G_N_ELEMENTS(content_table));
81     return content_table[m_table_index].m_pinyin_str;
82 }
83
84 const char * ChewingKeyRest::get_chewing_string(){
85     if (m_table_index == 0)
86         return NULL;
87
88     /* check end boundary. */
89     assert(m_table_index < G_N_ELEMENTS(content_table));
90     return content_table[m_table_index].m_chewing_str;
91 }
92
93
94 /* Pinyin Parsers */
95
96 /* internal information for pinyin parsers. */
97 struct parse_value_t{
98     ChewingKey m_key;
99     ChewingKeyRest m_key_rest;
100     gint16 m_num_keys;
101     gint16 m_parsed_len;
102     gint16 m_last_step;
103
104     /* constructor */
105 public:
106     parse_value_t(){
107         m_num_keys = 0;
108         m_parsed_len = 0;
109         m_last_step = -1;
110     }
111 };
112
113 const guint16 max_full_pinyin_length = 7;  /* include tone. */
114
115
116 static bool compare_less_than(const pinyin_index_item_t & lhs,
117                               const pinyin_index_item_t & rhs){
118     return 0 > strcmp(lhs.m_pinyin_input, rhs.m_pinyin_input);
119 }
120
121 static inline bool search_pinyin_index(guint32 options, const char * pinyin,
122                                        ChewingKey & key,
123                                        ChewingKeyRest & key_rest){
124     pinyin_index_item_t item;
125     memset(&item, 0, sizeof(item));
126     item.m_pinyin_input = pinyin;
127
128     std_lite::pair<const pinyin_index_item_t *,
129                    const pinyin_index_item_t *> range;
130     range = std_lite::equal_range
131         (pinyin_index, pinyin_index + G_N_ELEMENTS(pinyin_index),
132          item, compare_less_than);
133
134     guint16 range_len = range.second - range.first;
135     assert (range_len <= 1);
136     if ( range_len == 1 ) {
137         const pinyin_index_item_t * index = range.first;
138
139         if (!check_pinyin_options(options, index))
140             return false;
141
142         key_rest.m_table_index = index->m_table_index;
143         key = content_table[key_rest.m_table_index].m_chewing_key;
144         return true;
145     }
146
147     return false;
148 }
149
150 /* Full Pinyin Parser */
151 FullPinyinParser2::FullPinyinParser2 (){
152     m_parse_steps = g_array_new(TRUE, FALSE, sizeof(parse_value_t));
153 }
154
155
156 bool FullPinyinParser2::parse_one_key (guint32 options, ChewingKey & key,
157                                        ChewingKeyRest & key_rest,
158                                        const char * pinyin, int len) const {
159     /* "'" are not accepted in parse_one_key. */
160     assert(NULL == strchr(pinyin, '\''));
161     gchar * input = g_strndup(pinyin, len);
162
163     guint16 tone = CHEWING_ZERO_TONE; guint16 tone_pos = 0;
164     guint16 parsed_len = len;
165     key = ChewingKey(); key_rest = ChewingKeyRest();
166
167     if (options & USE_TONE) {
168         /* find the tone in the last character. */
169         char chr = input[parsed_len - 1];
170         if ( '0' < chr && chr <= '5' ) {
171             tone = chr - '0';
172             parsed_len --;
173             tone_pos = parsed_len;
174         }
175     }
176
177     /* parse pinyin core staff here. */
178
179     /* Note: optimize here? */
180     input[parsed_len] = '\0';
181     if (!search_pinyin_index(options, input, key, key_rest))
182         --parsed_len;
183
184     if (options & USE_TONE) {
185         /* post processing tone. */
186         if ( parsed_len == tone_pos ) {
187             if (tone != CHEWING_ZERO_TONE) {
188                 key.m_tone = tone;
189                 parsed_len ++;
190             }
191         }
192     }
193
194     key_rest.m_raw_begin = 0; key_rest.m_raw_end = parsed_len;
195     g_free(input);
196     return parsed_len == len;
197 }
198
199
200 int FullPinyinParser2::parse (guint32 options, ChewingKeyVector & keys,
201                               ChewingKeyRestVector & key_rests,
202                               const char *str, int len) const {
203     int i;
204     /* clear arrays. */
205     g_array_set_size(keys, 0);
206     g_array_set_size(key_rests, 0);
207
208     /* init m_parse_steps, and prepare dynamic programming. */
209     int step_len = len + 1;
210     g_array_set_size(m_parse_steps, 0);
211     parse_value_t value;
212     for (i = 0; i < step_len; ++i) {
213         g_array_append_val(m_parse_steps, value);
214     }
215
216     size_t next_sep = 0;
217     gchar * input = g_strndup(str, len);
218     parse_value_t * curstep = NULL, * nextstep = NULL;
219
220     for (i = 0; i < len; ) {
221         if (input[i] == '\'') {
222             curstep = &g_array_index(m_parse_steps, parse_value_t, i);
223             nextstep = &g_array_index(m_parse_steps, parse_value_t, i + 1);
224
225             /* propagate current step into next step. */
226             nextstep->m_key = ChewingKey();
227             nextstep->m_key_rest = ChewingKeyRest();
228             nextstep->m_num_keys = curstep->m_num_keys;
229             nextstep->m_parsed_len = curstep->m_parsed_len + 1;
230             nextstep->m_last_step = i;
231             next_sep = 0;
232             continue;
233         }
234
235         /* forward to next "'" */
236         if ( 0 == next_sep ) {
237             int k;
238             for (k = i;  k < len; ++k) {
239                 if (input[k] == '\'')
240                     break;
241             }
242             next_sep = k;
243             i = next_sep;
244         }
245
246         /* dynamic programming here. */
247         for (size_t m = i; m < next_sep; ++m) {
248             curstep = &g_array_index(m_parse_steps, parse_value_t, m);
249             size_t try_len = std_lite::min
250                 (m + max_full_pinyin_length, next_sep);
251             for (size_t n = m + 1; n < try_len + 1; ++n) {
252                 nextstep = &g_array_index(m_parse_steps, parse_value_t, n);
253
254                 /* gen next step */
255                 const char * onepinyin = input + m;
256                 gint16 onepinyinlen = n - m;
257                 value = parse_value_t();
258
259                 ChewingKey key; ChewingKeyRest rest;
260                 bool parsed = parse_one_key
261                     (options, key, rest, onepinyin, onepinyinlen);
262                 rest.m_raw_begin = m; rest.m_raw_end = n;
263                 if (!parsed)
264                     continue;
265                 value.m_key = key; value.m_key_rest = rest;
266                 value.m_num_keys = curstep->m_num_keys + 1;
267                 value.m_parsed_len = curstep->m_parsed_len + onepinyinlen;
268                 value.m_last_step = m;
269
270                 /* save next step */
271                 if (-1 == nextstep->m_last_step)
272                     *nextstep = value;
273                 if (value.m_parsed_len > nextstep->m_parsed_len)
274                     *nextstep = value;
275                 if (value.m_parsed_len == nextstep->m_parsed_len &&
276                     value.m_num_keys < nextstep->m_num_keys)
277                     *nextstep = value;
278             }
279         }
280     }
281
282     /* final step for back tracing. */
283     gint16 parsed_len = final_step(step_len, keys, key_rests);
284
285     /* post processing for re-split table. */
286     if (options & USE_RESPLIT_TABLE) {
287         post_process(options, keys, key_rests);
288     }
289
290     g_free(input);
291     return parsed_len;
292 }
293
294 int FullPinyinParser2::final_step(size_t step_len, ChewingKeyVector & keys,
295                                   ChewingKeyRestVector & key_rests) const{
296     int i;
297     gint16 parsed_len = 0;
298     parse_value_t * curstep = NULL;
299
300     /* find longest match, which starts from the beginning of input. */
301     for (i = step_len - 1; i >= 0; --i) {
302         curstep = &g_array_index(m_parse_steps, parse_value_t, i);
303         if (i == curstep->m_parsed_len)
304             break;
305     }
306     /* prepare saving. */
307     parsed_len = curstep->m_parsed_len;
308     gint16 num_keys = curstep->m_num_keys;
309     g_array_set_size(keys, num_keys);
310     g_array_set_size(key_rests, num_keys);
311
312     /* save the match. */
313     while (curstep->m_last_step != -1) {
314         gint16 pos = curstep->m_num_keys - 1;
315
316         /* skip "'" */
317         if (0 != curstep->m_key_rest.m_table_index) {
318             ChewingKey * key = &g_array_index(keys, ChewingKey, pos);
319             ChewingKeyRest * rest = &g_array_index
320                 (key_rests, ChewingKeyRest, pos);
321             *key = curstep->m_key; *rest = curstep->m_key_rest;
322         }
323
324         /* back ward */
325         curstep = &g_array_index(m_parse_steps, parse_value_t,
326                                  curstep->m_last_step);
327     }
328     return parsed_len;
329 }
330
331
332 bool FullPinyinParser2::post_process(guint32 options,
333                                      ChewingKeyVector & keys,
334                                      ChewingKeyRestVector & key_rests) const {
335     int i;
336     assert(keys->len == key_rests->len);
337     gint16 num_keys = keys->len;
338
339     ChewingKey * cur_key = NULL, * next_key = NULL;
340     ChewingKeyRest * cur_rest = NULL, * next_rest = NULL;
341     guint16 cur_tone = CHEWING_ZERO_TONE, next_tone = CHEWING_ZERO_TONE;
342
343     for (i = 0; i < num_keys - 1; ++i) {
344         cur_rest = &g_array_index(key_rests, ChewingKeyRest, i);
345         next_rest = &g_array_index(key_rests, ChewingKeyRest, i + 1);
346
347         /* some "'" here */
348         if (cur_rest->m_raw_end != next_rest->m_raw_begin)
349             continue;
350
351         cur_key = &g_array_index(keys, ChewingKey, i);
352         next_key = &g_array_index(keys, ChewingKey, i + 1);
353
354         if (options & USE_TONE) {
355             cur_tone = cur_key->m_tone;
356             next_tone = next_key->m_tone;
357             cur_key->m_tone = next_key->m_tone = CHEWING_ZERO_TONE;
358         }
359
360         /* lookup re-split table */
361         size_t k;
362         const resplit_table_item_t * item = NULL;
363         for (k = 0; k < G_N_ELEMENTS(resplit_table); ++k) {
364             item = resplit_table + k;
365             /* no ops */
366             if (item->m_orig_freq >= item->m_new_freq)
367                 continue;
368
369             /* use pinyin_exact_compare2 here. */
370             if (0 == pinyin_exact_compare2(item->m_orig_keys,
371                                            cur_key, 2))
372                 break;
373
374         }
375
376         /* find the match */
377         if (k < G_N_ELEMENTS(resplit_table)) {
378             /* do re-split */
379             item = resplit_table + k;
380             *cur_key = item->m_new_keys[0];
381             *next_key = item->m_new_keys[1];
382             /* assumes only moved one char in gen_all_resplit script. */
383             cur_rest->m_raw_end --;
384             next_rest->m_raw_begin --;
385         }
386
387         /* save back tones */
388         if (options & USE_TONE) {
389             cur_key->m_tone = cur_tone;
390             next_key->m_tone = next_tone;
391         }
392     }
393
394     return true;
395 }
396
397
398 bool DoublePinyinParser2::parse_one_key (guint32 options, ChewingKey & key,
399                                          ChewingKeyRest & key_rest,
400                                          const char *str, int len) const{
401 #define IS_KEY(x)   (('a' <= x && x <= 'z') || x == ';')
402
403     if (1 == len) {
404         if (!(options & PINYIN_INCOMPLETE))
405             return false;
406
407         char ch = str[0];
408         if (!IS_KEY(ch))
409             return false;
410
411         int charid = ch == ';' ? 26 : ch - 'a';
412         const char * sheng = m_shengmu_table[charid].m_shengmu;
413         if (NULL == sheng || strcmp(sheng, "'") == 0)
414             return false;
415
416         if (search_pinyin_index(options, sheng, key, key_rest)) {
417             key_rest.m_raw_begin = 0; key_rest.m_raw_end = len;
418             return true;
419         } else {
420             return false;
421         }
422     }
423
424     ChewingTone tone = CHEWING_ZERO_TONE;
425     options &= ~(PINYIN_CORRECT_ALL|PINYIN_AMB_ALL);
426
427     /* parse tone */
428     if (3 == len) {
429         if (!(options & USE_TONE))
430             return false;
431         char ch = str[2];
432         if (!('0' < ch && ch <= '5'))
433             return false;
434         tone = (ChewingTone) (ch - '0');
435     }
436
437     if (2 == len || 3 == len) {
438         /* parse shengmu here. */
439         char ch = str[0];
440         if (!IS_KEY(ch))
441             return false;
442
443         int charid = ch == ';' ? 26 : ch - 'a';
444         const char * sheng = m_shengmu_table[charid].m_shengmu;
445         if (NULL == sheng)
446             return false;
447         if (strcmp(sheng, "'") == 0)
448             sheng = "";
449
450         /* parse yunmu here. */
451         ch = str[1];
452         if (!IS_KEY(ch))
453             return false;
454
455         charid = ch == ';' ? 26 : ch - 'a';
456         /* first yunmu */
457         const char * yun = m_yunmu_table[charid].m_yunmus[0];
458         gchar * pinyin = g_strdup_printf("%s%s", sheng, yun);
459         if (search_pinyin_index(options, pinyin, key, key_rest)) {
460             key_rest.m_raw_begin = 0; key_rest.m_raw_end = len;
461             key.m_tone = tone;
462             g_free(pinyin);
463             return true;
464         }
465
466         /* second yunmu */
467         yun = m_yunmu_table[charid].m_yunmus[1];
468         pinyin = g_strdup_printf("%s%s", sheng, yun);
469         if (search_pinyin_index(options, pinyin, key, key_rest)) {
470             key_rest.m_raw_begin = 0; key_rest.m_raw_end = len;
471             key.m_tone = tone;
472             g_free(pinyin);
473             return true;
474         }
475
476         g_free(pinyin);
477     }
478
479 #undef IS_KEY
480
481     return false;
482 }
483
484
485 int DoublePinyinParser2::parse (guint32 options, ChewingKeyVector & keys,
486                                 ChewingKeyRestVector & key_rests,
487                                 const char *str, int len) const{
488     assert(FALSE);
489 }
490
491 bool DoublePinyinParser2::set_scheme(DoublePinyinScheme scheme) {
492
493     switch (scheme) {
494     case DOUBLE_PINYIN_ZRM:
495         m_shengmu_table = double_pinyin_zrm_sheng;
496         m_yunmu_table   = double_pinyin_zrm_yun;
497         return true;
498     case DOUBLE_PINYIN_MS:
499         m_shengmu_table = double_pinyin_mspy_sheng;
500         m_yunmu_table   = double_pinyin_mspy_yun;
501         return true;
502     case DOUBLE_PINYIN_ZIGUANG:
503         m_shengmu_table = double_pinyin_zgpy_sheng;
504         m_yunmu_table   = double_pinyin_zgpy_yun;
505         return true;
506     case DOUBLE_PINYIN_ABC:
507         m_shengmu_table = double_pinyin_abc_sheng;
508         m_yunmu_table   = double_pinyin_abc_yun;
509         return true;
510     case DOUBLE_PINYIN_PYJJ:
511         m_shengmu_table = double_pinyin_pyjj_sheng;
512         m_yunmu_table   = double_pinyin_pyjj_yun;
513         return true;
514     case DOUBLE_PINYIN_XHE:
515         m_shengmu_table = double_pinyin_xhe_sheng;
516         m_yunmu_table   = double_pinyin_xhe_yun;
517         return true;
518     case DOUBLE_PINYIN_CUSTOMIZED:
519         assert(FALSE);
520     };
521
522     return false; /* no such scheme. */
523 }