3 * Library to deal with pinyin.
5 * Copyright (C) 2011 Peng Wu <alexepico@gmail.com>
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.
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.
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.
23 #include "pinyin_parser2.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"
37 using namespace pinyin;
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);
43 /* handle incomplete pinyin. */
44 if (flags & PINYIN_INCOMPLETE) {
45 if (!(options & PINYIN_INCOMPLETE))
49 /* handle correct pinyin, currently only one flag per item. */
50 flags &= PINYIN_CORRECT_ALL;
51 options &= PINYIN_CORRECT_ALL;
54 if ((flags & options) != flags)
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);
65 /* handle incomplete chewing. */
66 if (flags & CHEWING_INCOMPLETE) {
67 if (!(options & CHEWING_INCOMPLETE))
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);
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;
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];
90 if (CHEWING_ZERO_TONE == m_tone) {
91 return g_strdup(item.m_pinyin_str);
93 return g_strdup_printf("%s%d", item.m_pinyin_str, m_tone);
97 gchar * _ChewingKey::get_shengmu_string() {
98 gint index = get_table_index();
99 assert(index < G_N_ELEMENTS(content_table));
100 const content_table_item_t & item = content_table[index];
101 return g_strdup(item.m_shengmu_str);
104 gchar * _ChewingKey::get_yunmu_string() {
105 gint index = get_table_index();
106 assert(index < G_N_ELEMENTS(content_table));
107 const content_table_item_t & item = content_table[index];
108 return g_strdup(item.m_yunmu_str);
111 gchar * _ChewingKey::get_chewing_string() {
112 assert(m_tone < CHEWING_NUMBER_OF_TONES);
113 gint index = get_table_index();
114 assert(index < G_N_ELEMENTS(content_table));
115 const content_table_item_t & item = content_table[index];
117 if (CHEWING_ZERO_TONE == m_tone) {
118 return g_strdup(item.m_chewing_str);
120 return g_strdup_printf("%s%s", item.m_chewing_str,
121 chewing_tone_table[m_tone]);
128 /* internal information for pinyin parsers. */
129 struct parse_value_t{
131 ChewingKeyRest m_key_rest;
145 const guint16 max_full_pinyin_length = 7; /* include tone. */
147 const guint16 max_double_pinyin_length = 3; /* include tone. */
149 const guint16 max_chewing_length = 4; /* include tone. */
151 static bool compare_pinyin_less_than(const pinyin_index_item_t & lhs,
152 const pinyin_index_item_t & rhs){
153 return 0 > strcmp(lhs.m_pinyin_input, rhs.m_pinyin_input);
156 static inline bool search_pinyin_index(pinyin_option_t options,
159 pinyin_index_item_t item;
160 memset(&item, 0, sizeof(item));
161 item.m_pinyin_input = pinyin;
163 std_lite::pair<const pinyin_index_item_t *,
164 const pinyin_index_item_t *> range;
165 range = std_lite::equal_range
166 (pinyin_index, pinyin_index + G_N_ELEMENTS(pinyin_index),
167 item, compare_pinyin_less_than);
169 guint16 range_len = range.second - range.first;
170 assert(range_len <= 1);
171 if (range_len == 1) {
172 const pinyin_index_item_t * index = range.first;
174 if (!check_pinyin_options(options, index))
177 key = content_table[index->m_table_index].m_chewing_key;
178 assert(key.get_table_index() == index->m_table_index);
185 static bool compare_chewing_less_than(const chewing_index_item_t & lhs,
186 const chewing_index_item_t & rhs){
187 return 0 > strcmp(lhs.m_chewing_input, rhs.m_chewing_input);
190 static inline bool search_chewing_index(pinyin_option_t options,
191 const char * chewing,
193 chewing_index_item_t item;
194 memset(&item, 0, sizeof(item));
195 item.m_chewing_input = chewing;
197 std_lite::pair<const chewing_index_item_t *,
198 const chewing_index_item_t *> range;
199 range = std_lite::equal_range
200 (chewing_index, chewing_index + G_N_ELEMENTS(chewing_index),
201 item, compare_chewing_less_than);
203 guint16 range_len = range.second - range.first;
204 assert (range_len <= 1);
206 if (range_len == 1) {
207 const chewing_index_item_t * index = range.first;
209 if (!check_chewing_options(options, index))
212 key = content_table[index->m_table_index].m_chewing_key;
213 assert(key.get_table_index() == index->m_table_index);
220 /* Full Pinyin Parser */
221 FullPinyinParser2::FullPinyinParser2 (){
222 m_parse_steps = g_array_new(TRUE, FALSE, sizeof(parse_value_t));
226 bool FullPinyinParser2::parse_one_key (pinyin_option_t options,
228 const char * pinyin, int len) const {
229 /* "'" are not accepted in parse_one_key. */
230 gchar * input = g_strndup(pinyin, len);
231 assert(NULL == strchr(input, '\''));
233 guint16 tone = CHEWING_ZERO_TONE; guint16 tone_pos = 0;
234 guint16 parsed_len = len;
237 if (options & USE_TONE) {
238 /* find the tone in the last character. */
239 char chr = input[parsed_len - 1];
240 if ( '0' < chr && chr <= '5' ) {
243 tone_pos = parsed_len;
247 /* parse pinyin core staff here. */
249 /* Note: optimize here? */
250 input[parsed_len] = '\0';
251 if (!search_pinyin_index(options, input, key)) {
256 if (options & USE_TONE) {
257 /* post processing tone. */
258 if ( parsed_len == tone_pos ) {
259 if (tone != CHEWING_ZERO_TONE) {
267 return parsed_len == len;
271 int FullPinyinParser2::parse (pinyin_option_t options, ChewingKeyVector & keys,
272 ChewingKeyRestVector & key_rests,
273 const char *str, int len) const {
276 g_array_set_size(keys, 0);
277 g_array_set_size(key_rests, 0);
279 /* init m_parse_steps, and prepare dynamic programming. */
280 int step_len = len + 1;
281 g_array_set_size(m_parse_steps, 0);
283 for (i = 0; i < step_len; ++i) {
284 g_array_append_val(m_parse_steps, value);
288 gchar * input = g_strndup(str, len);
289 parse_value_t * curstep = NULL, * nextstep = NULL;
291 for (i = 0; i < len; ++i) {
292 if (input[i] == '\'') {
293 curstep = &g_array_index(m_parse_steps, parse_value_t, i);
294 nextstep = &g_array_index(m_parse_steps, parse_value_t, i + 1);
296 /* propagate current step into next step. */
297 nextstep->m_key = ChewingKey();
298 nextstep->m_key_rest = ChewingKeyRest();
299 nextstep->m_num_keys = curstep->m_num_keys;
300 nextstep->m_parsed_len = curstep->m_parsed_len + 1;
301 nextstep->m_last_step = i;
306 /* forward to next "'" */
307 if ( 0 == next_sep ) {
309 for (k = i; k < len; ++k) {
310 if (input[k] == '\'')
316 /* dynamic programming here. */
317 /* for (size_t m = i; m < next_sep; ++m) */
320 curstep = &g_array_index(m_parse_steps, parse_value_t, m);
321 size_t try_len = std_lite::min
322 (m + max_full_pinyin_length, next_sep);
323 for (size_t n = m + 1; n < try_len + 1; ++n) {
324 nextstep = &g_array_index(m_parse_steps, parse_value_t, n);
327 const char * onepinyin = input + m;
328 gint16 onepinyinlen = n - m;
329 value = parse_value_t();
331 ChewingKey key; ChewingKeyRest rest;
332 bool parsed = parse_one_key
333 (options, key, onepinyin, onepinyinlen);
334 rest.m_raw_begin = m; rest.m_raw_end = n;
338 //printf("onepinyin:%s len:%d\n", onepinyin, onepinyinlen);
340 value.m_key = key; value.m_key_rest = rest;
341 value.m_num_keys = curstep->m_num_keys + 1;
342 value.m_parsed_len = curstep->m_parsed_len + onepinyinlen;
343 value.m_last_step = m;
346 /* no previous result */
347 if (-1 == nextstep->m_last_step)
349 /* prefer the longest pinyin */
350 if (value.m_parsed_len > nextstep->m_parsed_len)
352 /* prefer the shortest keys with the same pinyin length */
353 if (value.m_parsed_len == nextstep->m_parsed_len &&
354 value.m_num_keys < nextstep->m_num_keys)
357 /* handle with the same pinyin length and the number of keys */
358 if (value.m_parsed_len == nextstep->m_parsed_len &&
359 value.m_num_keys == nextstep->m_num_keys) {
362 /* prefer the complete pinyin with shengmu
363 * over without shengmu,
364 * ex: "kaneiji" -> "ka'nei'ji".
366 if ((value.m_key.m_initial != CHEWING_ZERO_INITIAL &&
367 !(value.m_key.m_middle == CHEWING_ZERO_MIDDLE &&
368 value.m_key.m_final == CHEWING_ZERO_FINAL)) &&
369 nextstep->m_key.m_initial == CHEWING_ZERO_INITIAL)
372 /* prefer the complete pinyin 'er'
373 * over the in-complete pinyin 'r',
374 * ex: "xierqi" -> "xi'er'qi."
376 if ((value.m_key.m_initial == CHEWING_ZERO_INITIAL &&
377 value.m_key.m_middle == CHEWING_ZERO_MIDDLE &&
378 value.m_key.m_final == CHEWING_ER) &&
379 (nextstep->m_key.m_initial == CHEWING_R &&
380 nextstep->m_key.m_middle == CHEWING_ZERO_MIDDLE &&
381 nextstep->m_key.m_final == CHEWING_ZERO_FINAL))
385 /* prefer the 'a' at the end of clause,
386 * ex: "zheyanga$" -> "zhe'yang'a$".
388 if (value.m_parsed_len == len &&
389 (nextstep->m_key.m_initial != CHEWING_ZERO_INITIAL &&
390 nextstep->m_key.m_final == CHEWING_A) &&
391 (value.m_key.m_initial == CHEWING_ZERO_INITIAL &&
392 value.m_key.m_middle == CHEWING_ZERO_MIDDLE &&
393 value.m_key.m_final == CHEWING_A))
400 /* final step for back tracing. */
401 gint16 parsed_len = final_step(step_len, keys, key_rests);
403 /* post processing for re-split table. */
404 if (options & USE_RESPLIT_TABLE) {
405 post_process2(options, keys, key_rests, str, len);
412 int FullPinyinParser2::final_step(size_t step_len, ChewingKeyVector & keys,
413 ChewingKeyRestVector & key_rests) const{
415 gint16 parsed_len = 0;
416 parse_value_t * curstep = NULL;
418 /* find longest match, which starts from the beginning of input. */
419 for (i = step_len - 1; i >= 0; --i) {
420 curstep = &g_array_index(m_parse_steps, parse_value_t, i);
421 if (i == curstep->m_parsed_len)
424 /* prepare saving. */
425 parsed_len = curstep->m_parsed_len;
426 gint16 num_keys = curstep->m_num_keys;
427 g_array_set_size(keys, num_keys);
428 g_array_set_size(key_rests, num_keys);
430 /* save the match. */
431 while (curstep->m_last_step != -1) {
432 gint16 pos = curstep->m_num_keys - 1;
435 if (0 != curstep->m_key.get_table_index()) {
436 ChewingKey * key = &g_array_index(keys, ChewingKey, pos);
437 ChewingKeyRest * rest = &g_array_index
438 (key_rests, ChewingKeyRest, pos);
439 *key = curstep->m_key; *rest = curstep->m_key_rest;
443 curstep = &g_array_index(m_parse_steps, parse_value_t,
444 curstep->m_last_step);
449 bool FullPinyinParser2::post_process2(pinyin_option_t options,
450 ChewingKeyVector & keys,
451 ChewingKeyRestVector & key_rests,
455 assert(keys->len == key_rests->len);
456 gint num_keys = keys->len;
458 ChewingKey * cur_key = NULL, * next_key = NULL;
459 ChewingKeyRest * cur_rest = NULL, * next_rest = NULL;
460 guint16 next_tone = CHEWING_ZERO_TONE;
462 for (i = 0; i < num_keys - 1; ++i) {
463 cur_rest = &g_array_index(key_rests, ChewingKeyRest, i);
464 next_rest = &g_array_index(key_rests, ChewingKeyRest, i + 1);
467 if (cur_rest->m_raw_end != next_rest->m_raw_begin)
470 cur_key = &g_array_index(keys, ChewingKey, i);
471 next_key = &g_array_index(keys, ChewingKey, i + 1);
474 if (CHEWING_ZERO_TONE != cur_key->m_tone)
478 if (options & USE_TONE) {
479 next_tone = next_key->m_tone;
480 if (CHEWING_ZERO_TONE != next_tone) {
481 next_key->m_tone = CHEWING_ZERO_TONE;
482 next_rest->m_raw_end --;
486 /* lookup re-split table */
487 const resplit_table_item_t * item = NULL;
489 item = retrieve_resplit_item_by_original_pinyins
490 (options, cur_key, cur_rest, next_key, next_rest, str, len);
494 if (item->m_orig_freq >= item->m_new_freq)
498 const char * onepinyin = str + cur_rest->m_raw_begin;
499 size_t len = strlen(item->m_new_keys[0]);
501 assert(parse_one_key(options, *cur_key, onepinyin, len));
502 cur_rest->m_raw_end = cur_rest->m_raw_begin + len;
504 next_rest->m_raw_begin = cur_rest->m_raw_end;
505 onepinyin = str + next_rest->m_raw_begin;
506 len = strlen(item->m_new_keys[1]);
508 assert(parse_one_key(options, *next_key, onepinyin, len));
512 if (options & USE_TONE) {
513 if (CHEWING_ZERO_TONE != next_tone) {
514 next_key->m_tone = next_tone;
515 next_rest->m_raw_end ++;
523 const divided_table_item_t * FullPinyinParser2::retrieve_divided_item
524 (pinyin_option_t options, ChewingKey * key, ChewingKeyRest * rest,
525 const char * str, int len) const {
527 /* lookup divided table */
529 const divided_table_item_t * item = NULL;
530 for (k = 0; k < G_N_ELEMENTS(divided_table); ++k) {
531 item = divided_table + k;
533 const char * onepinyin = str + rest->m_raw_begin;
534 size_t len = strlen(item->m_orig_key);
536 if (rest->length() != len)
539 if (0 == strncmp(onepinyin, item->m_orig_key, len))
543 /* found the match */
544 if (k < G_N_ELEMENTS(divided_table)) {
546 item = divided_table + k;
554 const resplit_table_item_t * FullPinyinParser2::retrieve_resplit_item_by_original_pinyins
555 (pinyin_option_t options,
556 ChewingKey * cur_key, ChewingKeyRest * cur_rest,
557 ChewingKey * next_key, ChewingKeyRest * next_rest,
558 const char * str, int len) const{
559 /* lookup re-split table */
561 const resplit_table_item_t * item = NULL;
563 for (k = 0; k < G_N_ELEMENTS(resplit_table); ++k) {
564 item = resplit_table + k;
566 const char * onepinyin = str + cur_rest->m_raw_begin;
567 size_t len = strlen(item->m_orig_keys[0]);
569 if (cur_rest->length() != len)
572 if (0 != strncmp(onepinyin, item->m_orig_keys[0], len))
575 onepinyin = str + next_rest->m_raw_begin;
576 len = strlen(item->m_orig_keys[1]);
578 if (next_rest->length() != len)
581 if (0 == strncmp(onepinyin, item->m_orig_keys[1], len))
585 /* found the match */
586 if (k < G_N_ELEMENTS(resplit_table)) {
587 item = resplit_table + k;
594 const resplit_table_item_t * FullPinyinParser2::retrieve_resplit_item_by_resplit_pinyins
595 (pinyin_option_t options,
596 ChewingKey * cur_key, ChewingKeyRest * cur_rest,
597 ChewingKey * next_key, ChewingKeyRest * next_rest,
598 const char * str, int len) const {
599 /* lookup divide table */
601 const resplit_table_item_t * item = NULL;
603 for (k = 0; k < G_N_ELEMENTS(resplit_table); ++k) {
604 item = resplit_table + k;
606 const char * onepinyin = str + cur_rest->m_raw_begin;
607 size_t len = strlen(item->m_new_keys[0]);
609 if (cur_rest->length() != len)
612 if (0 != strncmp(onepinyin, item->m_new_keys[0], len))
615 onepinyin = str + next_rest->m_raw_begin;
616 len = strlen(item->m_new_keys[1]);
618 if (next_rest->length() != len)
621 if (0 == strncmp(onepinyin, item->m_new_keys[1], len))
625 /* found the match */
626 if (k < G_N_ELEMENTS(resplit_table)) {
627 item = resplit_table + k;
634 #define IS_KEY(x) (('a' <= x && x <= 'z') || x == ';')
636 bool DoublePinyinParser2::parse_one_key(pinyin_option_t options,
638 const char *str, int len) const {
639 options &= ~(PINYIN_CORRECT_ALL|PINYIN_AMB_ALL);
642 if (!(options & PINYIN_INCOMPLETE))
649 int charid = ch == ';' ? 26 : ch - 'a';
650 const char * sheng = m_shengmu_table[charid].m_shengmu;
651 if (NULL == sheng || strcmp(sheng, "'") == 0)
654 if (search_pinyin_index(options, sheng, key)) {
661 ChewingTone tone = CHEWING_ZERO_TONE;
662 options &= ~(PINYIN_INCOMPLETE|CHEWING_INCOMPLETE);
663 options |= PINYIN_CORRECT_UE_VE | PINYIN_CORRECT_V_U;
667 if (!(options & USE_TONE))
670 if (!('0' < ch && ch <= '5'))
672 tone = (ChewingTone) (ch - '0');
675 if (2 == len || 3 == len) {
676 /* parse shengmu here. */
681 int charid = ch == ';' ? 26 : ch - 'a';
682 const char * sheng = m_shengmu_table[charid].m_shengmu;
685 if (strcmp(sheng, "'") == 0)
688 /* parse yunmu here. */
693 charid = ch == ';' ? 26 : ch - 'a';
695 const char * yun = m_yunmu_table[charid].m_yunmus[0];
699 gchar * pinyin = g_strdup_printf("%s%s", sheng, yun);
700 if (search_pinyin_index(options, pinyin, key)) {
708 yun = m_yunmu_table[charid].m_yunmus[1];
712 pinyin = g_strdup_printf("%s%s", sheng, yun);
713 if (search_pinyin_index(options, pinyin, key)) {
726 /* only 'a'-'z' and ';' are accepted here. */
727 int DoublePinyinParser2::parse(pinyin_option_t options, ChewingKeyVector & keys,
728 ChewingKeyRestVector & key_rests,
729 const char *str, int len) const {
730 g_array_set_size(keys, 0);
731 g_array_set_size(key_rests, 0);
733 int maximum_len = 0; int i;
734 /* probe the longest possible double pinyin string. */
735 for (i = 0; i < len; ++i) {
736 const char ch = str[i];
737 if (!(IS_KEY(ch) || ('0' < ch && ch <= '5')))
742 /* maximum forward match for double pinyin. */
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_double_pinyin_length);
749 ChewingKey key; ChewingKeyRest key_rest;
751 bool success = parse_one_key(options, key, cur_str, i);
756 if (0 == i) /* no more possible double pinyins. */
759 key_rest.m_raw_begin = parsed_len; key_rest.m_raw_end = parsed_len + i;
762 /* save the pinyin */
763 g_array_append_val(keys, key);
764 g_array_append_val(key_rests, key_rest);
772 bool DoublePinyinParser2::set_scheme(DoublePinyinScheme scheme) {
775 case DOUBLE_PINYIN_ZRM:
776 m_shengmu_table = double_pinyin_zrm_sheng;
777 m_yunmu_table = double_pinyin_zrm_yun;
779 case DOUBLE_PINYIN_MS:
780 m_shengmu_table = double_pinyin_mspy_sheng;
781 m_yunmu_table = double_pinyin_mspy_yun;
783 case DOUBLE_PINYIN_ZIGUANG:
784 m_shengmu_table = double_pinyin_zgpy_sheng;
785 m_yunmu_table = double_pinyin_zgpy_yun;
787 case DOUBLE_PINYIN_ABC:
788 m_shengmu_table = double_pinyin_abc_sheng;
789 m_yunmu_table = double_pinyin_abc_yun;
791 case DOUBLE_PINYIN_PYJJ:
792 m_shengmu_table = double_pinyin_pyjj_sheng;
793 m_yunmu_table = double_pinyin_pyjj_yun;
795 case DOUBLE_PINYIN_XHE:
796 m_shengmu_table = double_pinyin_xhe_sheng;
797 m_yunmu_table = double_pinyin_xhe_yun;
799 case DOUBLE_PINYIN_CUSTOMIZED:
803 return false; /* no such scheme. */
806 /* the chewing string must be freed with g_free. */
807 static bool search_chewing_symbols(const chewing_symbol_item_t * symbol_table,
808 const char key, const char ** chewing) {
810 /* just iterate the table, as we only have < 50 items. */
811 while (symbol_table->m_input != '\0') {
812 if (symbol_table->m_input == key) {
813 *chewing = symbol_table->m_chewing;
821 static bool search_chewing_tones(const chewing_tone_item_t * tone_table,
822 const char key, char * tone) {
823 *tone = CHEWING_ZERO_TONE;
824 /* just iterate the table, as we only have < 10 items. */
825 while (tone_table->m_input != '\0') {
826 if (tone_table->m_input == key) {
827 *tone = tone_table->m_tone;
836 bool ChewingParser2::parse_one_key(pinyin_option_t options,
838 const char *str, int len) const {
839 options &= ~(PINYIN_CORRECT_ALL|PINYIN_AMB_ALL);
840 char tone = CHEWING_ZERO_TONE;
842 int symbols_len = len;
843 /* probe whether the last key is tone key in str. */
844 if (options & USE_TONE) {
845 char ch = str[len - 1];
846 /* remove tone from input */
847 if (search_chewing_tones(m_tone_table, ch, &tone))
852 gchar * chewing = NULL; const char * onechar = NULL;
854 /* probe the possible chewing map in the rest of str. */
855 for (i = 0; i < symbols_len; ++i) {
856 if (!search_chewing_symbols(m_symbol_table, str[i], &onechar)) {
862 chewing = g_strdup(onechar);
864 gchar * tmp = chewing;
865 chewing = g_strconcat(chewing, onechar, NULL);
870 /* search the chewing in the chewing index table. */
871 if (chewing && search_chewing_index(options, chewing, key)) {
872 /* save back tone if available. */
883 /* only characters in chewing keyboard scheme are accepted here. */
884 int ChewingParser2::parse(pinyin_option_t options, ChewingKeyVector & keys,
885 ChewingKeyRestVector & key_rests,
886 const char *str, int len) const {
887 g_array_set_size(keys, 0);
888 g_array_set_size(key_rests, 0);
890 int maximum_len = 0; int i;
891 /* probe the longest possible chewing string. */
892 for (i = 0; i < len; ++i) {
893 if (!in_chewing_scheme(options, str[i], NULL))
898 /* maximum forward match for chewing. */
900 while (parsed_len < maximum_len) {
901 const char * cur_str = str + parsed_len;
902 i = std_lite::min(maximum_len - parsed_len,
903 (int)max_chewing_length);
905 ChewingKey key; ChewingKeyRest key_rest;
907 bool success = parse_one_key(options, key, cur_str, i);
912 if (0 == i) /* no more possible chewings. */
915 key_rest.m_raw_begin = parsed_len; key_rest.m_raw_end = parsed_len + i;
918 /* save the pinyin. */
919 g_array_append_val(keys, key);
920 g_array_append_val(key_rests, key_rest);
927 bool ChewingParser2::set_scheme(ChewingScheme scheme) {
929 case CHEWING_STANDARD:
930 m_symbol_table = chewing_standard_symbols;
931 m_tone_table = chewing_standard_tones;
934 m_symbol_table = chewing_ibm_symbols;
935 m_tone_table = chewing_ibm_tones;
937 case CHEWING_GINYIEH:
938 m_symbol_table = chewing_ginyieh_symbols;
939 m_tone_table = chewing_ginyieh_tones;
942 m_symbol_table = chewing_eten_symbols;
943 m_tone_table = chewing_eten_tones;
951 bool ChewingParser2::in_chewing_scheme(pinyin_option_t options,
952 const char key, const char ** symbol)
954 const gchar * chewing = NULL;
955 char tone = CHEWING_ZERO_TONE;
957 if (search_chewing_symbols(m_symbol_table, key, &chewing)) {
963 if (!(options & USE_TONE))
966 if (search_chewing_tones(m_tone_table, key, &tone)) {
968 *symbol = chewing_tone_table[tone];