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 /* methods for Chewing Keys to access pinyin parser table. */
76 const char * ChewingKeyRest::get_pinyin_string(){
77 if (m_table_index == 0)
80 /* check end boundary. */
81 assert(m_table_index < G_N_ELEMENTS(content_table));
82 return content_table[m_table_index].m_pinyin_str;
85 const char * ChewingKeyRest::get_chewing_string(){
86 if (m_table_index == 0)
89 /* check end boundary. */
90 assert(m_table_index < G_N_ELEMENTS(content_table));
91 return content_table[m_table_index].m_chewing_str;
97 /* internal information for pinyin parsers. */
100 ChewingKeyRest m_key_rest;
114 const guint16 max_full_pinyin_length = 7; /* include tone. */
116 const guint16 max_double_pinyin_length = 3; /* include tone. */
118 const guint16 max_chewing_length = 4; /* include tone. */
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);
125 static inline bool search_pinyin_index(pinyin_option_t options, const char * pinyin,
127 ChewingKeyRest & key_rest){
128 pinyin_index_item_t item;
129 memset(&item, 0, sizeof(item));
130 item.m_pinyin_input = pinyin;
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);
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;
143 if (!check_pinyin_options(options, index))
146 key_rest.m_table_index = index->m_table_index;
147 key = content_table[key_rest.m_table_index].m_chewing_key;
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);
159 static inline bool search_chewing_index(pinyin_option_t options, const char * chewing,
161 ChewingKeyRest & key_rest){
162 chewing_index_item_t item;
163 memset(&item, 0, sizeof(item));
164 item.m_chewing_input = chewing;
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);
172 guint16 range_len = range.second - range.first;
173 assert (range_len <= 1);
175 if (range_len == 1) {
176 const chewing_index_item_t * index = range.first;
178 if (!check_chewing_options(options, index))
181 key_rest.m_table_index = index->m_table_index;
182 key = content_table[key_rest.m_table_index].m_chewing_key;
189 /* Full Pinyin Parser */
190 FullPinyinParser2::FullPinyinParser2 (){
191 m_parse_steps = g_array_new(TRUE, FALSE, sizeof(parse_value_t));
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, '\''));
202 guint16 tone = CHEWING_ZERO_TONE; guint16 tone_pos = 0;
203 guint16 parsed_len = len;
204 key = ChewingKey(); key_rest = ChewingKeyRest();
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' ) {
212 tone_pos = parsed_len;
216 /* parse pinyin core staff here. */
218 /* Note: optimize here? */
219 input[parsed_len] = '\0';
220 if (!search_pinyin_index(options, input, key, key_rest))
223 if (options & USE_TONE) {
224 /* post processing tone. */
225 if ( parsed_len == tone_pos ) {
226 if (tone != CHEWING_ZERO_TONE) {
233 key_rest.m_raw_begin = 0; key_rest.m_raw_end = parsed_len;
235 return parsed_len == len;
239 int FullPinyinParser2::parse (pinyin_option_t options, ChewingKeyVector & keys,
240 ChewingKeyRestVector & key_rests,
241 const char *str, int len) const {
244 g_array_set_size(keys, 0);
245 g_array_set_size(key_rests, 0);
247 /* init m_parse_steps, and prepare dynamic programming. */
248 int step_len = len + 1;
249 g_array_set_size(m_parse_steps, 0);
251 for (i = 0; i < step_len; ++i) {
252 g_array_append_val(m_parse_steps, value);
256 gchar * input = g_strndup(str, len);
257 parse_value_t * curstep = NULL, * nextstep = NULL;
259 for (i = 0; i < len; ++i) {
260 if (input[i] == '\'') {
261 curstep = &g_array_index(m_parse_steps, parse_value_t, i);
262 nextstep = &g_array_index(m_parse_steps, parse_value_t, i + 1);
264 /* propagate current step into next step. */
265 nextstep->m_key = ChewingKey();
266 nextstep->m_key_rest = ChewingKeyRest();
267 nextstep->m_num_keys = curstep->m_num_keys;
268 nextstep->m_parsed_len = curstep->m_parsed_len + 1;
269 nextstep->m_last_step = i;
274 /* forward to next "'" */
275 if ( 0 == next_sep ) {
277 for (k = i; k < len; ++k) {
278 if (input[k] == '\'')
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);
293 const char * onepinyin = input + m;
294 gint16 onepinyinlen = n - m;
295 value = parse_value_t();
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;
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;
309 if (-1 == nextstep->m_last_step)
311 if (value.m_parsed_len > nextstep->m_parsed_len)
313 if (value.m_parsed_len == nextstep->m_parsed_len &&
314 value.m_num_keys < nextstep->m_num_keys)
320 /* final step for back tracing. */
321 gint16 parsed_len = final_step(step_len, keys, key_rests);
323 /* post processing for re-split table. */
324 if (options & USE_RESPLIT_TABLE) {
325 post_process(options, keys, key_rests);
332 int FullPinyinParser2::final_step(size_t step_len, ChewingKeyVector & keys,
333 ChewingKeyRestVector & key_rests) const{
335 gint16 parsed_len = 0;
336 parse_value_t * curstep = NULL;
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)
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);
350 /* save the match. */
351 while (curstep->m_last_step != -1) {
352 gint16 pos = curstep->m_num_keys - 1;
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;
363 curstep = &g_array_index(m_parse_steps, parse_value_t,
364 curstep->m_last_step);
370 bool FullPinyinParser2::post_process(pinyin_option_t options,
371 ChewingKeyVector & keys,
372 ChewingKeyRestVector & key_rests) const {
374 assert(keys->len == key_rests->len);
375 gint16 num_keys = keys->len;
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;
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);
386 if (cur_rest->m_raw_end != next_rest->m_raw_begin)
389 cur_key = &g_array_index(keys, ChewingKey, i);
390 next_key = &g_array_index(keys, ChewingKey, i + 1);
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;
398 /* lookup re-split table */
400 const resplit_table_item_t * item = NULL;
401 for (k = 0; k < G_N_ELEMENTS(resplit_table); ++k) {
402 item = resplit_table + k;
404 if (item->m_orig_freq >= item->m_new_freq)
407 /* use pinyin_exact_compare2 here. */
408 if (0 == pinyin_exact_compare2(item->m_orig_keys,
415 if (k < G_N_ELEMENTS(resplit_table)) {
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 --;
425 /* save back tones */
426 if (options & USE_TONE) {
427 cur_key->m_tone = cur_tone;
428 next_key->m_tone = next_tone;
435 #define IS_KEY(x) (('a' <= x && x <= 'z') || x == ';')
437 bool DoublePinyinParser2::parse_one_key(pinyin_option_t options, ChewingKey & key,
438 ChewingKeyRest & key_rest,
439 const char *str, int len) const {
442 if (!(options & PINYIN_INCOMPLETE))
449 int charid = ch == ';' ? 26 : ch - 'a';
450 const char * sheng = m_shengmu_table[charid].m_shengmu;
451 if (NULL == sheng || strcmp(sheng, "'") == 0)
454 if (search_pinyin_index(options, sheng, key, key_rest)) {
455 key_rest.m_raw_begin = 0; key_rest.m_raw_end = len;
462 ChewingTone tone = CHEWING_ZERO_TONE;
463 options &= ~(PINYIN_CORRECT_ALL|PINYIN_AMB_ALL);
467 if (!(options & USE_TONE))
470 if (!('0' < ch && ch <= '5'))
472 tone = (ChewingTone) (ch - '0');
475 if (2 == len || 3 == len) {
476 /* parse shengmu here. */
481 int charid = ch == ';' ? 26 : ch - 'a';
482 const char * sheng = m_shengmu_table[charid].m_shengmu;
485 if (strcmp(sheng, "'") == 0)
488 /* parse yunmu here. */
493 charid = ch == ';' ? 26 : ch - 'a';
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;
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;
522 /* only 'a'-'z' and ';' are accepted here. */
523 int DoublePinyinParser2::parse(pinyin_option_t 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);
529 int maximum_len = 0; int i;
530 /* probe the longest possible double pinyin string. */
531 for (i = 0; i < len; ++i) {
532 const char ch = str[i];
533 if (!(IS_KEY(ch) || ('0' < ch && ch <= '5')))
538 /* maximum forward match for double pinyin. */
540 while (parsed_len < maximum_len) {
541 const char * cur_str = str + parsed_len;
542 i = std_lite::min(maximum_len - parsed_len,
543 (int)max_double_pinyin_length);
545 ChewingKey key; ChewingKeyRest key_rest;
547 bool success = parse_one_key(options, key, key_rest, cur_str, i);
552 if (0 == i) /* no more possible double pinyins. */
555 key_rest.m_raw_begin = parsed_len; key_rest.m_raw_end = parsed_len + i;
558 /* save the pinyin */
559 g_array_append_val(keys, key);
560 g_array_append_val(key_rests, key_rest);
568 bool DoublePinyinParser2::set_scheme(DoublePinyinScheme scheme) {
571 case DOUBLE_PINYIN_ZRM:
572 m_shengmu_table = double_pinyin_zrm_sheng;
573 m_yunmu_table = double_pinyin_zrm_yun;
575 case DOUBLE_PINYIN_MS:
576 m_shengmu_table = double_pinyin_mspy_sheng;
577 m_yunmu_table = double_pinyin_mspy_yun;
579 case DOUBLE_PINYIN_ZIGUANG:
580 m_shengmu_table = double_pinyin_zgpy_sheng;
581 m_yunmu_table = double_pinyin_zgpy_yun;
583 case DOUBLE_PINYIN_ABC:
584 m_shengmu_table = double_pinyin_abc_sheng;
585 m_yunmu_table = double_pinyin_abc_yun;
587 case DOUBLE_PINYIN_PYJJ:
588 m_shengmu_table = double_pinyin_pyjj_sheng;
589 m_yunmu_table = double_pinyin_pyjj_yun;
591 case DOUBLE_PINYIN_XHE:
592 m_shengmu_table = double_pinyin_xhe_sheng;
593 m_yunmu_table = double_pinyin_xhe_yun;
595 case DOUBLE_PINYIN_CUSTOMIZED:
599 return false; /* no such scheme. */
602 /* the chewing string must be freed with g_free. */
603 static bool search_chewing_symbols(const chewing_symbol_item_t * symbol_table,
604 const char key, char ** chewing) {
606 /* just iterate the table, as we only have < 50 items. */
607 while (symbol_table->m_input != '\0') {
608 if (symbol_table->m_input == key) {
609 *chewing = g_strdup(symbol_table->m_chewing);
617 static bool search_chewing_tones(const chewing_tone_item_t * tone_table,
618 const char key, char * tone) {
619 *tone = CHEWING_ZERO_TONE;
620 /* just iterate the table, as we only have < 10 items. */
621 while (tone_table->m_input != '\0') {
622 if (tone_table->m_input == key) {
623 *tone = tone_table->m_tone;
632 bool ChewingParser2::parse_one_key(pinyin_option_t options, ChewingKey & key, ChewingKeyRest & key_rest, const char *str, int len) const {
633 char tone = CHEWING_ZERO_TONE;
635 int symbols_len = len;
636 /* probe whether the last key is tone key in str. */
637 if (options & USE_TONE) {
638 char ch = str[len - 1];
639 /* remove tone from input */
640 if (search_chewing_tones(m_tone_table, ch, &tone))
645 gchar * chewing = NULL, * onechar = NULL;
647 /* probe the possible chewing map in the rest of str. */
648 for (i = 0; i < symbols_len; ++i) {
649 if (!search_chewing_symbols(m_symbol_table, str[i], &onechar)) {
656 chewing = g_strdup(onechar);
658 gchar * tmp = chewing;
659 chewing = g_strconcat(chewing, onechar, NULL);
665 /* search the chewing in the chewing index table. */
666 if (search_chewing_index(options, chewing, key, key_rest)) {
667 key_rest.m_raw_begin = 0; key_rest.m_raw_end = len;
668 /* save back tone if available. */
679 /* only characters in chewing keyboard scheme are accepted here. */
680 int ChewingParser2::parse(pinyin_option_t options, ChewingKeyVector & keys,
681 ChewingKeyRestVector & key_rests,
682 const char *str, int len) const {
683 g_array_set_size(keys, 0);
684 g_array_set_size(key_rests, 0);
686 int maximum_len = 0; int i;
687 /* probe the longest possible chewing string. */
688 for (i = 0; i < len; ++i) {
689 if (!in_chewing_scheme(str[i]))
694 /* maximum forward match for chewing. */
696 while (parsed_len < maximum_len) {
697 const char * cur_str = str + parsed_len;
698 i = std_lite::min(maximum_len - parsed_len,
699 (int)max_chewing_length);
701 ChewingKey key; ChewingKeyRest key_rest;
703 bool success = parse_one_key(options, key, key_rest, cur_str, i);
708 if (0 == i) /* no more possible chewings. */
711 key_rest.m_raw_begin = parsed_len; key_rest.m_raw_end = parsed_len + i;
714 /* save the pinyin. */
715 g_array_append_val(keys, key);
716 g_array_append_val(key_rests, key_rest);
723 bool ChewingParser2::set_scheme(ChewingScheme scheme) {
725 case CHEWING_STANDARD:
726 m_symbol_table = chewing_standard_symbols;
727 m_tone_table = chewing_standard_tones;
730 m_symbol_table = chewing_ibm_symbols;
731 m_tone_table = chewing_ibm_tones;
733 case CHEWING_GINYIEH:
734 m_symbol_table = chewing_ginyieh_symbols;
735 m_tone_table = chewing_ginyieh_tones;
738 m_symbol_table = chewing_eten_symbols;
739 m_tone_table = chewing_eten_tones;
747 bool ChewingParser2::in_chewing_scheme(const char key) const {
748 gchar * chewing = NULL;
749 char tone = CHEWING_ZERO_TONE;
751 bool retval = search_chewing_symbols(m_symbol_table, key, &chewing) ||
752 search_chewing_tones(m_tone_table, key, &tone);