From cbe325008a4413d154eebf521ba5bfb25bae075d Mon Sep 17 00:00:00 2001 From: Peng Wu Date: Wed, 23 Nov 2011 17:37:02 +0800 Subject: write parse method for chewing parser2 --- src/storage/pinyin_parser2.cpp | 54 +++++++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 9 deletions(-) (limited to 'src/storage/pinyin_parser2.cpp') diff --git a/src/storage/pinyin_parser2.cpp b/src/storage/pinyin_parser2.cpp index 727f939..1e67aed 100644 --- a/src/storage/pinyin_parser2.cpp +++ b/src/storage/pinyin_parser2.cpp @@ -434,9 +434,9 @@ bool FullPinyinParser2::post_process(guint32 options, #define IS_KEY(x) (('a' <= x && x <= 'z') || x == ';') -bool DoublePinyinParser2::parse_one_key (guint32 options, ChewingKey & key, - ChewingKeyRest & key_rest, - const char *str, int len) const{ +bool DoublePinyinParser2::parse_one_key(guint32 options, ChewingKey & key, + ChewingKeyRest & key_rest, + const char *str, int len) const { if (1 == len) { if (!(options & PINYIN_INCOMPLETE)) @@ -520,9 +520,9 @@ bool DoublePinyinParser2::parse_one_key (guint32 options, ChewingKey & key, /* only 'a'-'z' and ';' are accepted here. */ -int DoublePinyinParser2::parse (guint32 options, ChewingKeyVector & keys, - ChewingKeyRestVector & key_rests, - const char *str, int len) const{ +int DoublePinyinParser2::parse(guint32 options, ChewingKeyVector & keys, + ChewingKeyRestVector & key_rests, + const char *str, int len) const { g_array_set_size(keys, 0); g_array_set_size(key_rests, 0); @@ -674,10 +674,46 @@ bool ChewingParser2::parse_one_key(guint32 options, ChewingKey & key, ChewingKey /* only characters in chewing keyboard scheme are accepted here. */ -int ChewingParser2::parse(guint32 options, ChewingKeyVector & keys, ChewingKeyRestVector & key_rests, const char *str, int len) const { +int ChewingParser2::parse(guint32 options, ChewingKeyVector & keys, + ChewingKeyRestVector & key_rests, + const char *str, int len) const { + g_array_set_size(keys, 0); + g_array_set_size(key_rests, 0); + + int maximum_len = 0; int i; /* probe the longest possible chewing string. */ + for (i = 0; i < len; ++i) { + if (!in_chewing_scheme(str[i])) + break; + } + maximum_len = i; + /* maximum forward match for chewing. */ - assert(FALSE); + int parsed_len = 0; + while (parsed_len < maximum_len) { + const char * cur_str = str + parsed_len; + i = std_lite::min(maximum_len - parsed_len, + (int)max_chewing_length); + + ChewingKey key; ChewingKeyRest key_rest; + for (; i > 0; --i) { + bool success = parse_one_key(options, key, key_rest, cur_str, i); + if (success) + break; + } + + if (0 == i) /* no more possible chewings. */ + break; + + key_rest.m_raw_begin = parsed_len; key_rest.m_raw_end = parsed_len + i; + parsed_len += i; + + /* save the pinyin. */ + g_array_append_val(keys, key); + g_array_append_val(key_rests, key_rest); + } + + return parsed_len; } @@ -705,7 +741,7 @@ bool ChewingParser2::set_scheme(ChewingScheme scheme) { } -bool ChewingParser2::in_chewing_scheme(const char key){ +bool ChewingParser2::in_chewing_scheme(const char key) const { gchar * chewing = NULL; char tone = CHEWING_ZERO_TONE; -- cgit