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