refine parse_one_key method
[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 "pinyin_parser2.h"
24 #include <ctype.h>
25 #include <assert.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include "stl_lite.h"
29 #include "pinyin_phrase2.h"
30 #include "pinyin_custom2.h"
31 #include "chewing_key.h"
32 #include "pinyin_parser_table.h"
33 #include "double_pinyin_table.h"
34 #include "chewing_table.h"
35
36
37 using namespace pinyin;
38
39 static bool check_pinyin_options(pinyin_option_t options, const pinyin_index_item_t * item) {
40     guint32 flags = item->m_flags;
41     assert (flags & IS_PINYIN);
42
43     /* handle incomplete pinyin. */
44     if (flags & PINYIN_INCOMPLETE) {
45         if (!(options & PINYIN_INCOMPLETE))
46             return false;
47     }
48
49     /* handle correct pinyin, currently only one flag per item. */
50     flags &= PINYIN_CORRECT_ALL;
51     options &= PINYIN_CORRECT_ALL;
52
53     if (flags) {
54         if ((flags & options) != flags)
55             return false;
56     }
57
58     return true;
59 }
60
61 static bool check_chewing_options(pinyin_option_t options, const chewing_index_item_t * item) {
62     guint32 flags = item->m_flags;
63     assert (flags & IS_CHEWING);
64
65     /* handle incomplete chewing. */
66     if (flags & CHEWING_INCOMPLETE) {
67         if (!(options & CHEWING_INCOMPLETE))
68             return false;
69     }
70
71     return true;
72 }
73
74
75 gint ChewingKey::get_table_index() {
76     assert(m_initial <  CHEWING_NUMBER_OF_INITIALS);
77     assert(m_middle < CHEWING_NUMBER_OF_MIDDLES);
78     assert(m_final < CHEWING_NUMBER_OF_FINALS);
79
80     gint index = chewing_key_table[(m_initial * CHEWING_NUMBER_OF_MIDDLES + m_middle) * CHEWING_NUMBER_OF_FINALS + m_final];
81     return index == -1 ? 0 : index;
82 }
83
84 gchar * ChewingKey::get_pinyin_string() {
85     assert(m_tone < CHEWING_NUMBER_OF_TONES);
86     gint index = get_table_index();
87     assert(index < G_N_ELEMENTS(content_table));
88     const content_table_item_t & item = content_table[index];
89
90     if (CHEWING_ZERO_TONE == m_tone) {
91         return g_strdup(item.m_pinyin_str);
92     } else {
93         return g_strdup_printf("%s%d", item.m_pinyin_str, m_tone);
94     }
95 }
96
97 gchar * ChewingKey::get_chewing_string() {
98     assert(m_tone < CHEWING_NUMBER_OF_TONES);
99     gint index = get_table_index();
100     assert(index < G_N_ELEMENTS(content_table));
101     const content_table_item_t & item = content_table[index];
102
103     if (CHEWING_ZERO_TONE == m_tone) {
104         return g_strdup(item.m_chewing_str);
105     } else {
106         return g_strdup_printf("%s%s", item.m_chewing_str,
107                                chewing_tone_table[m_tone]);
108     }
109 }
110
111
112 /* Pinyin Parsers */
113
114 /* internal information for pinyin parsers. */
115 struct parse_value_t{
116     ChewingKey m_key;
117     ChewingKeyRest m_key_rest;
118     gint16 m_num_keys;
119     gint16 m_parsed_len;
120     gint16 m_last_step;
121
122     /* constructor */
123 public:
124     parse_value_t(){
125         m_num_keys = 0;
126         m_parsed_len = 0;
127         m_last_step = -1;
128     }
129 };
130
131 const guint16 max_full_pinyin_length   = 7;  /* include tone. */
132
133 const guint16 max_double_pinyin_length = 3;  /* include tone. */
134
135 const guint16 max_chewing_length       = 4;  /* include tone. */
136
137 static bool compare_pinyin_less_than(const pinyin_index_item_t & lhs,
138                                      const pinyin_index_item_t & rhs){
139     return 0 > strcmp(lhs.m_pinyin_input, rhs.m_pinyin_input);
140 }
141
142 static inline bool search_pinyin_index(pinyin_option_t options,
143                                        const char * pinyin,
144                                        ChewingKey & key){
145     pinyin_index_item_t item;
146     memset(&item, 0, sizeof(item));
147     item.m_pinyin_input = pinyin;
148
149     std_lite::pair<const pinyin_index_item_t *,
150                    const pinyin_index_item_t *> range;
151     range = std_lite::equal_range
152         (pinyin_index, pinyin_index + G_N_ELEMENTS(pinyin_index),
153          item, compare_pinyin_less_than);
154
155     guint16 range_len = range.second - range.first;
156     assert(range_len <= 1);
157     if (range_len == 1) {
158         const pinyin_index_item_t * index = range.first;
159
160         if (!check_pinyin_options(options, index))
161             return false;
162
163         key = content_table[index->m_table_index].m_chewing_key;
164         assert(key.get_table_index() == index->m_table_index);
165         return true;
166     }
167
168     return false;
169 }
170
171 static bool compare_chewing_less_than(const chewing_index_item_t & lhs,
172                                       const chewing_index_item_t & rhs){
173     return 0 > strcmp(lhs.m_chewing_input, rhs.m_chewing_input);
174 }
175
176 static inline bool search_chewing_index(pinyin_option_t options,
177                                         const char * chewing,
178                                         ChewingKey & key){
179     chewing_index_item_t item;
180     memset(&item, 0, sizeof(item));
181     item.m_chewing_input = chewing;
182
183     std_lite::pair<const chewing_index_item_t *,
184                    const chewing_index_item_t *> range;
185     range = std_lite::equal_range
186         (chewing_index, chewing_index + G_N_ELEMENTS(chewing_index),
187          item, compare_chewing_less_than);
188
189     guint16 range_len = range.second - range.first;
190     assert (range_len <= 1);
191
192     if (range_len == 1) {
193         const chewing_index_item_t * index = range.first;
194
195         if (!check_chewing_options(options, index))
196             return false;
197
198         key = content_table[index->m_table_index].m_chewing_key;
199         assert(key.get_table_index() == index->m_table_index);
200         return true;
201     }
202
203     return false;
204 }
205
206 /* Full Pinyin Parser */
207 FullPinyinParser2::FullPinyinParser2 (){
208     m_parse_steps = g_array_new(TRUE, FALSE, sizeof(parse_value_t));
209 }
210
211
212 bool FullPinyinParser2::parse_one_key (pinyin_option_t options,
213                                        ChewingKey & key,
214                                        const char * pinyin, int len) const {
215     /* "'" are not accepted in parse_one_key. */
216     gchar * input = g_strndup(pinyin, len);
217     assert(NULL == strchr(input, '\''));
218
219     guint16 tone = CHEWING_ZERO_TONE; guint16 tone_pos = 0;
220     guint16 parsed_len = len;
221     key = ChewingKey();
222
223     if (options & USE_TONE) {
224         /* find the tone in the last character. */
225         char chr = input[parsed_len - 1];
226         if ( '0' < chr && chr <= '5' ) {
227             tone = chr - '0';
228             parsed_len --;
229             tone_pos = parsed_len;
230         }
231     }
232
233     /* parse pinyin core staff here. */
234
235     /* Note: optimize here? */
236     input[parsed_len] = '\0';
237     if (!search_pinyin_index(options, input, key)) {
238         g_free(input);
239         return false;
240     }
241
242     if (options & USE_TONE) {
243         /* post processing tone. */
244         if ( parsed_len == tone_pos ) {
245             if (tone != CHEWING_ZERO_TONE) {
246                 key.m_tone = tone;
247                 parsed_len ++;
248             }
249         }
250     }
251
252     g_free(input);
253     return parsed_len == len;
254 }
255
256
257 int FullPinyinParser2::parse (pinyin_option_t options, ChewingKeyVector & keys,
258                               ChewingKeyRestVector & key_rests,
259                               const char *str, int len) const {
260     int i;
261     /* clear arrays. */
262     g_array_set_size(keys, 0);
263     g_array_set_size(key_rests, 0);
264
265     /* init m_parse_steps, and prepare dynamic programming. */
266     int step_len = len + 1;
267     g_array_set_size(m_parse_steps, 0);
268     parse_value_t value;
269     for (i = 0; i < step_len; ++i) {
270         g_array_append_val(m_parse_steps, value);
271     }
272
273     size_t next_sep = 0;
274     gchar * input = g_strndup(str, len);
275     parse_value_t * curstep = NULL, * nextstep = NULL;
276
277     for (i = 0; i < len; ++i) {
278         if (input[i] == '\'') {
279             curstep = &g_array_index(m_parse_steps, parse_value_t, i);
280             nextstep = &g_array_index(m_parse_steps, parse_value_t, i + 1);
281
282             /* propagate current step into next step. */
283             nextstep->m_key = ChewingKey();
284             nextstep->m_key_rest = ChewingKeyRest();
285             nextstep->m_num_keys = curstep->m_num_keys;
286             nextstep->m_parsed_len = curstep->m_parsed_len + 1;
287             nextstep->m_last_step = i;
288             next_sep = 0;
289             continue;
290         }
291
292         /* forward to next "'" */
293         if ( 0 == next_sep ) {
294             int k;
295             for (k = i;  k < len; ++k) {
296                 if (input[k] == '\'')
297                     break;
298             }
299             next_sep = k;
300         }
301
302         /* Heuristic Method:
303          *   do maximum forward match first. */
304         for (size_t pos = i; pos < next_sep; ++pos) {
305             curstep = &g_array_index(m_parse_steps, parse_value_t, pos);
306             size_t try_len = std_lite::min
307                 (pos + max_full_pinyin_length, next_sep);
308             for (size_t n = try_len; n > pos; --n) {
309                 nextstep = &g_array_index(m_parse_steps, parse_value_t, n);
310
311                 /* gen next step */
312                 const char * onepinyin = input + pos;
313                 gint16 onepinyinlen = n - pos;
314                 value = parse_value_t();
315
316                 ChewingKey key; ChewingKeyRest rest;
317                 bool parsed = parse_one_key
318                     (options, key, onepinyin, onepinyinlen);
319                 rest.m_raw_begin = pos; rest.m_raw_end = n;
320
321                 if (!parsed)
322                     continue;
323
324                 //printf("onepinyin:%s len:%d\n", onepinyin, onepinyinlen);
325                 value.m_key = key; value.m_key_rest = rest;
326                 value.m_num_keys = curstep->m_num_keys + 1;
327                 value.m_parsed_len = curstep->m_parsed_len + onepinyinlen;
328                 value.m_last_step = pos;
329
330                 /* save next step */
331                 if (-1 == nextstep->m_last_step)
332                     *nextstep = value;
333                 if (value.m_parsed_len > nextstep->m_parsed_len)
334                     *nextstep = value;
335                 if (value.m_parsed_len == nextstep->m_parsed_len &&
336                     value.m_num_keys < nextstep->m_num_keys)
337                     *nextstep = value;
338
339                 /* maximum forward, set pos to n in next iteration. */
340                 pos = n - 1;
341                 break;
342             }
343         }
344
345         /* dynamic programming here. */
346         for (size_t m = i; m < next_sep; ++m) {
347             curstep = &g_array_index(m_parse_steps, parse_value_t, m);
348             size_t try_len = std_lite::min
349                 (m + max_full_pinyin_length, next_sep);
350             for (size_t n = m + 1; n < try_len + 1; ++n) {
351                 nextstep = &g_array_index(m_parse_steps, parse_value_t, n);
352
353                 /* gen next step */
354                 const char * onepinyin = input + m;
355                 gint16 onepinyinlen = n - m;
356                 value = parse_value_t();
357
358                 ChewingKey key; ChewingKeyRest rest;
359                 bool parsed = parse_one_key
360                     (options, key, onepinyin, onepinyinlen);
361                 rest.m_raw_begin = m; rest.m_raw_end = n;
362                 if (!parsed)
363                     continue;
364
365                 //printf("onepinyin:%s len:%d\n", onepinyin, onepinyinlen);
366
367                 value.m_key = key; value.m_key_rest = rest;
368                 value.m_num_keys = curstep->m_num_keys + 1;
369                 value.m_parsed_len = curstep->m_parsed_len + onepinyinlen;
370                 value.m_last_step = m;
371
372                 /* save next step */
373                 if (-1 == nextstep->m_last_step)
374                     *nextstep = value;
375                 if (value.m_parsed_len > nextstep->m_parsed_len)
376                     *nextstep = value;
377                 if (value.m_parsed_len == nextstep->m_parsed_len &&
378                     value.m_num_keys < nextstep->m_num_keys)
379                     *nextstep = value;
380             }
381         }
382     }
383
384     /* final step for back tracing. */
385     gint16 parsed_len = final_step(step_len, keys, key_rests);
386
387     /* post processing for re-split table. */
388     if (options & USE_RESPLIT_TABLE) {
389         post_process(options, keys, key_rests);
390     }
391
392     g_free(input);
393     return parsed_len;
394 }
395
396 int FullPinyinParser2::final_step(size_t step_len, ChewingKeyVector & keys,
397                                   ChewingKeyRestVector & key_rests) const{
398     int i;
399     gint16 parsed_len = 0;
400     parse_value_t * curstep = NULL;
401
402     /* find longest match, which starts from the beginning of input. */
403     for (i = step_len - 1; i >= 0; --i) {
404         curstep = &g_array_index(m_parse_steps, parse_value_t, i);
405         if (i == curstep->m_parsed_len)
406             break;
407     }
408     /* prepare saving. */
409     parsed_len = curstep->m_parsed_len;
410     gint16 num_keys = curstep->m_num_keys;
411     g_array_set_size(keys, num_keys);
412     g_array_set_size(key_rests, num_keys);
413
414     /* save the match. */
415     while (curstep->m_last_step != -1) {
416         gint16 pos = curstep->m_num_keys - 1;
417
418         /* skip "'" */
419         if (0 != curstep->m_key_rest.m_table_index) {
420             ChewingKey * key = &g_array_index(keys, ChewingKey, pos);
421             ChewingKeyRest * rest = &g_array_index
422                 (key_rests, ChewingKeyRest, pos);
423             *key = curstep->m_key; *rest = curstep->m_key_rest;
424         }
425
426         /* back ward */
427         curstep = &g_array_index(m_parse_steps, parse_value_t,
428                                  curstep->m_last_step);
429     }
430     return parsed_len;
431 }
432
433
434 bool FullPinyinParser2::post_process(pinyin_option_t options,
435                                      ChewingKeyVector & keys,
436                                      ChewingKeyRestVector & key_rests) const {
437     int i;
438     assert(keys->len == key_rests->len);
439     gint16 num_keys = keys->len;
440
441     ChewingKey * cur_key = NULL, * next_key = NULL;
442     ChewingKeyRest * cur_rest = NULL, * next_rest = NULL;
443     guint16 cur_tone = CHEWING_ZERO_TONE, next_tone = CHEWING_ZERO_TONE;
444
445     for (i = 0; i < num_keys - 1; ++i) {
446         cur_rest = &g_array_index(key_rests, ChewingKeyRest, i);
447         next_rest = &g_array_index(key_rests, ChewingKeyRest, i + 1);
448
449         /* some "'" here */
450         if (cur_rest->m_raw_end != next_rest->m_raw_begin)
451             continue;
452
453         cur_key = &g_array_index(keys, ChewingKey, i);
454         next_key = &g_array_index(keys, ChewingKey, i + 1);
455
456         if (options & USE_TONE) {
457             cur_tone = cur_key->m_tone;
458             next_tone = next_key->m_tone;
459             cur_key->m_tone = next_key->m_tone = CHEWING_ZERO_TONE;
460         }
461
462         /* lookup re-split table */
463         size_t k;
464         const resplit_table_item_t * item = NULL;
465         for (k = 0; k < G_N_ELEMENTS(resplit_table); ++k) {
466             item = resplit_table + k;
467             /* no ops */
468             if (item->m_orig_freq >= item->m_new_freq)
469                 continue;
470
471             /* use pinyin_exact_compare2 here. */
472             if (0 == pinyin_exact_compare2(item->m_orig_keys,
473                                            cur_key, 2))
474                 break;
475
476         }
477
478         /* find the match */
479         if (k < G_N_ELEMENTS(resplit_table)) {
480             /* do re-split */
481             item = resplit_table + k;
482             *cur_key = item->m_new_keys[0];
483             *next_key = item->m_new_keys[1];
484             /* assumes only moved one char in gen_all_resplit script. */
485             cur_rest->m_raw_end --;
486             next_rest->m_raw_begin --;
487         }
488
489         /* save back tones */
490         if (options & USE_TONE) {
491             cur_key->m_tone = cur_tone;
492             next_key->m_tone = next_tone;
493         }
494     }
495
496     return true;
497 }
498
499 #define IS_KEY(x)   (('a' <= x && x <= 'z') || x == ';')
500
501 bool DoublePinyinParser2::parse_one_key(pinyin_option_t options,
502                                         ChewingKey & key,
503                                         const char *str, int len) const {
504
505     if (1 == len) {
506         if (!(options & PINYIN_INCOMPLETE))
507             return false;
508
509         char ch = str[0];
510         if (!IS_KEY(ch))
511             return false;
512
513         int charid = ch == ';' ? 26 : ch - 'a';
514         const char * sheng = m_shengmu_table[charid].m_shengmu;
515         if (NULL == sheng || strcmp(sheng, "'") == 0)
516             return false;
517
518         if (search_pinyin_index(options, sheng, key)) {
519             return true;
520         } else {
521             return false;
522         }
523     }
524
525     ChewingTone tone = CHEWING_ZERO_TONE;
526     options &= ~(PINYIN_CORRECT_ALL|PINYIN_AMB_ALL);
527
528     /* parse tone */
529     if (3 == len) {
530         if (!(options & USE_TONE))
531             return false;
532         char ch = str[2];
533         if (!('0' < ch && ch <= '5'))
534             return false;
535         tone = (ChewingTone) (ch - '0');
536     }
537
538     if (2 == len || 3 == len) {
539         /* parse shengmu here. */
540         char ch = str[0];
541         if (!IS_KEY(ch))
542             return false;
543
544         int charid = ch == ';' ? 26 : ch - 'a';
545         const char * sheng = m_shengmu_table[charid].m_shengmu;
546         if (NULL == sheng)
547             return false;
548         if (strcmp(sheng, "'") == 0)
549             sheng = "";
550
551         /* parse yunmu here. */
552         ch = str[1];
553         if (!IS_KEY(ch))
554             return false;
555
556         charid = ch == ';' ? 26 : ch - 'a';
557         /* first yunmu */
558         const char * yun = m_yunmu_table[charid].m_yunmus[0];
559         gchar * pinyin = g_strdup_printf("%s%s", sheng, yun);
560         if (search_pinyin_index(options, pinyin, key)) {
561             key.m_tone = tone;
562             g_free(pinyin);
563             return true;
564         }
565         g_free(pinyin);
566
567         /* second yunmu */
568         yun = m_yunmu_table[charid].m_yunmus[1];
569         pinyin = g_strdup_printf("%s%s", sheng, yun);
570         if (search_pinyin_index(options, pinyin, key)) {
571             key.m_tone = tone;
572             g_free(pinyin);
573             return true;
574         }
575         g_free(pinyin);
576
577     }
578
579     return false;
580 }
581
582
583 /* only 'a'-'z' and ';' are accepted here. */
584 int DoublePinyinParser2::parse(pinyin_option_t options, ChewingKeyVector & keys,
585                                ChewingKeyRestVector & key_rests,
586                                const char *str, int len) const {
587     g_array_set_size(keys, 0);
588     g_array_set_size(key_rests, 0);
589
590     int maximum_len = 0; int i;
591     /* probe the longest possible double pinyin string. */
592     for (i = 0; i < len; ++i) {
593         const char ch = str[i];
594         if (!(IS_KEY(ch) || ('0' < ch && ch <= '5')))
595             break;
596     }
597     maximum_len = i;
598
599     /* maximum forward match for double pinyin. */
600     int parsed_len = 0;
601     while (parsed_len < maximum_len) {
602         const char * cur_str = str + parsed_len;
603         i = std_lite::min(maximum_len - parsed_len,
604                           (int)max_double_pinyin_length);
605
606         ChewingKey key; ChewingKeyRest key_rest;
607         for (; i > 0; --i) {
608             bool success = parse_one_key(options, key, cur_str, i);
609             if (success)
610                 break;
611         }
612
613         if (0 == i)        /* no more possible double pinyins. */
614             break;
615
616         key_rest.m_raw_begin = parsed_len; key_rest.m_raw_end = parsed_len + i;
617         parsed_len += i;
618
619         /* save the pinyin */
620         g_array_append_val(keys, key);
621         g_array_append_val(key_rests, key_rest);
622     }
623
624     return parsed_len;
625 }
626
627 #undef IS_KEY
628
629 bool DoublePinyinParser2::set_scheme(DoublePinyinScheme scheme) {
630
631     switch (scheme) {
632     case DOUBLE_PINYIN_ZRM:
633         m_shengmu_table = double_pinyin_zrm_sheng;
634         m_yunmu_table   = double_pinyin_zrm_yun;
635         return true;
636     case DOUBLE_PINYIN_MS:
637         m_shengmu_table = double_pinyin_mspy_sheng;
638         m_yunmu_table   = double_pinyin_mspy_yun;
639         return true;
640     case DOUBLE_PINYIN_ZIGUANG:
641         m_shengmu_table = double_pinyin_zgpy_sheng;
642         m_yunmu_table   = double_pinyin_zgpy_yun;
643         return true;
644     case DOUBLE_PINYIN_ABC:
645         m_shengmu_table = double_pinyin_abc_sheng;
646         m_yunmu_table   = double_pinyin_abc_yun;
647         return true;
648     case DOUBLE_PINYIN_PYJJ:
649         m_shengmu_table = double_pinyin_pyjj_sheng;
650         m_yunmu_table   = double_pinyin_pyjj_yun;
651         return true;
652     case DOUBLE_PINYIN_XHE:
653         m_shengmu_table = double_pinyin_xhe_sheng;
654         m_yunmu_table   = double_pinyin_xhe_yun;
655         return true;
656     case DOUBLE_PINYIN_CUSTOMIZED:
657         assert(FALSE);
658     };
659
660     return false; /* no such scheme. */
661 }
662
663 /* the chewing string must be freed with g_free. */
664 static bool search_chewing_symbols(const chewing_symbol_item_t * symbol_table,
665                                    const char key, char ** chewing) {
666     *chewing = NULL;
667     /* just iterate the table, as we only have < 50 items. */
668     while (symbol_table->m_input != '\0') {
669         if (symbol_table->m_input == key) {
670             *chewing = g_strdup(symbol_table->m_chewing);
671             return true;
672         }
673         symbol_table ++;
674     }
675     return false;
676 }
677
678 static bool search_chewing_tones(const chewing_tone_item_t * tone_table,
679                                  const char key, char * tone) {
680     *tone = CHEWING_ZERO_TONE;
681     /* just iterate the table, as we only have < 10 items. */
682     while (tone_table->m_input != '\0') {
683         if (tone_table->m_input == key) {
684             *tone = tone_table->m_tone;
685             return true;
686         }
687         tone_table ++;
688     }
689     return false;
690 }
691
692
693 bool ChewingParser2::parse_one_key(pinyin_option_t options,
694                                    ChewingKey & key,
695                                    const char *str, int len) const {
696     char tone = CHEWING_ZERO_TONE;
697
698     int symbols_len = len;
699     /* probe whether the last key is tone key in str. */
700     if (options & USE_TONE) {
701         char ch = str[len - 1];
702         /* remove tone from input */
703         if (search_chewing_tones(m_tone_table, ch, &tone))
704             symbols_len --;
705     }
706
707     int i;
708     gchar * chewing = NULL, * onechar = NULL;
709
710     /* probe the possible chewing map in the rest of str. */
711     for (i = 0; i < symbols_len; ++i) {
712         if (!search_chewing_symbols(m_symbol_table, str[i], &onechar)) {
713             g_free(onechar);
714             g_free(chewing);
715             return false;
716         }
717
718         if (!chewing) {
719             chewing = g_strdup(onechar);
720         } else {
721             gchar * tmp = chewing;
722             chewing = g_strconcat(chewing, onechar, NULL);
723             g_free(tmp);
724         }
725         g_free(onechar);
726     }
727
728     /* search the chewing in the chewing index table. */
729     if (search_chewing_index(options, chewing, key)) {
730         /* save back tone if available. */
731         key.m_tone = tone;
732         g_free(chewing);
733         return true;
734     }
735
736     g_free(chewing);
737     return false;
738 }
739
740
741 /* only characters in chewing keyboard scheme are accepted here. */
742 int ChewingParser2::parse(pinyin_option_t options, ChewingKeyVector & keys,
743                           ChewingKeyRestVector & key_rests,
744                           const char *str, int len) const {
745     g_array_set_size(keys, 0);
746     g_array_set_size(key_rests, 0);
747
748     int maximum_len = 0; int i;
749     /* probe the longest possible chewing string. */
750     for (i = 0; i < len; ++i) {
751         if (!in_chewing_scheme(str[i]))
752             break;
753     }
754     maximum_len = i;
755
756     /* maximum forward match for chewing. */
757     int parsed_len = 0;
758     while (parsed_len < maximum_len) {
759         const char * cur_str = str + parsed_len;
760         i = std_lite::min(maximum_len - parsed_len,
761                           (int)max_chewing_length);
762
763         ChewingKey key; ChewingKeyRest key_rest;
764         for (; i > 0; --i) {
765             bool success = parse_one_key(options, key, cur_str, i);
766             if (success)
767                 break;
768         }
769
770         if (0 == i)        /* no more possible chewings. */
771             break;
772
773         key_rest.m_raw_begin = parsed_len; key_rest.m_raw_end = parsed_len + i;
774         parsed_len += i;
775
776         /* save the pinyin. */
777         g_array_append_val(keys, key);
778         g_array_append_val(key_rests, key_rest);
779     }
780
781     return parsed_len;
782 }
783
784
785 bool ChewingParser2::set_scheme(ChewingScheme scheme) {
786     switch(scheme) {
787     case CHEWING_STANDARD:
788         m_symbol_table = chewing_standard_symbols;
789         m_tone_table   = chewing_standard_tones;
790         return true;
791     case CHEWING_IBM:
792         m_symbol_table = chewing_ibm_symbols;
793         m_tone_table   = chewing_ibm_tones;
794         return true;
795     case CHEWING_GINYIEH:
796         m_symbol_table = chewing_ginyieh_symbols;
797         m_tone_table   = chewing_ginyieh_tones;
798         return true;
799     case CHEWING_ETEN:
800         m_symbol_table = chewing_eten_symbols;
801         m_tone_table   = chewing_eten_tones;
802         return true;
803     }
804
805     return false;
806 }
807
808
809 bool ChewingParser2::in_chewing_scheme(const char key) const {
810     gchar * chewing = NULL;
811     char tone = CHEWING_ZERO_TONE;
812
813     bool retval = search_chewing_symbols(m_symbol_table, key, &chewing) ||
814         search_chewing_tones(m_tone_table, key, &tone);
815     g_free(chewing);
816
817     return retval;
818 }