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