From 4d0e7eebec72ed95b4278161b115f78b0550753a Mon Sep 17 00:00:00 2001 From: Peng Wu Date: Fri, 24 Jul 2020 14:59:16 +0800 Subject: refactor parse_one_key method --- src/storage/pinyin_parser2.h | 9 +++++---- src/storage/zhuyin_parser2.cpp | 21 ++++++++++++++------- src/storage/zhuyin_parser2.h | 8 ++++---- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/storage/pinyin_parser2.h b/src/storage/pinyin_parser2.h index 4a2f0f2..790c0cb 100644 --- a/src/storage/pinyin_parser2.h +++ b/src/storage/pinyin_parser2.h @@ -42,6 +42,7 @@ typedef struct { const char * m_pinyin_input; guint32 m_flags; guint16 m_table_index; + guint16 m_distance; } pinyin_index_item_t; typedef struct { @@ -114,7 +115,7 @@ public: * Parse only one struct ChewingKey from a string. * */ - virtual bool parse_one_key(pinyin_option_t options, ChewingKey & key, const char *str, int len) const = 0; + virtual bool parse_one_key(pinyin_option_t options, ChewingKey & key, gint16 & distance, const char *str, int len) const = 0; /** * PhoneticParser2::parse: @@ -156,7 +157,7 @@ public: g_array_free(m_parse_steps, TRUE); } - virtual bool parse_one_key(pinyin_option_t options, ChewingKey & key, const char *str, int len) const; + virtual bool parse_one_key(pinyin_option_t options, ChewingKey & key, gint16 & distance, const char *str, int len) const; /* Note: * the parse method will use dynamic programming to drive parse_one_key. @@ -195,7 +196,7 @@ public: virtual ~DoublePinyinParser2() {} - virtual bool parse_one_key(pinyin_option_t options, ChewingKey & key, const char *str, int len) const; + virtual bool parse_one_key(pinyin_option_t options, ChewingKey & key, gint16 & distance, const char *str, int len) const; virtual int parse(pinyin_option_t options, ChewingKeyVector & keys, ChewingKeyRestVector & key_rests, const char *str, int len) const; @@ -215,7 +216,7 @@ public: virtual ~PinyinDirectParser2() {} - virtual bool parse_one_key(pinyin_option_t options, ChewingKey & key, const char *str, int len) const; + virtual bool parse_one_key(pinyin_option_t options, ChewingKey & key, gint16 & distance, const char *str, int len) const; virtual int parse(pinyin_option_t options, ChewingKeyVector & keys, ChewingKeyRestVector & key_rests, const char *str, int len) const; }; diff --git a/src/storage/zhuyin_parser2.cpp b/src/storage/zhuyin_parser2.cpp index 99ae4d2..e711b76 100644 --- a/src/storage/zhuyin_parser2.cpp +++ b/src/storage/zhuyin_parser2.cpp @@ -160,6 +160,7 @@ static int search_chewing_symbols2(const zhuyin_symbol_item_t * symbol_table, bool ZhuyinSimpleParser2::parse_one_key(pinyin_option_t options, ChewingKey & key, + gint16 & distance, const char * str, int len) const { options &= ~PINYIN_AMB_ALL; unsigned char tone = CHEWING_ZERO_TONE; @@ -240,9 +241,10 @@ int ZhuyinSimpleParser2::parse(pinyin_option_t options, i = std_lite::min(maximum_len - parsed_len, (int)max_chewing_length); + gint16 distance = 0; ChewingKey key; ChewingKeyRest key_rest; for (; i > 0; --i) { - bool success = parse_one_key(options, key, cur_str, i); + bool success = parse_one_key(options, key, distance, cur_str, i); if (success) break; } @@ -331,6 +333,7 @@ bool ZhuyinSimpleParser2::in_chewing_scheme(pinyin_option_t options, bool ZhuyinDiscreteParser2::parse_one_key(pinyin_option_t options, ChewingKey & key, + gint16 & distance, const char * str, int len) const { if (0 == len) return false; @@ -430,9 +433,10 @@ int ZhuyinDiscreteParser2::parse(pinyin_option_t options, i = std_lite::min(maximum_len - parsed_len, (int)max_chewing_length); + gint16 distance = 0; ChewingKey key; ChewingKeyRest key_rest; for (; i > 0; --i) { - bool success = parse_one_key(options, key, cur_str, i); + bool success = parse_one_key(options, key, distance, cur_str, i); if (success) break; } @@ -567,6 +571,7 @@ static int count_same_chars(const char * str, int len) { bool ZhuyinDaChenCP26Parser2::parse_one_key(pinyin_option_t options, ChewingKey & key, + gint16 & distance, const char *str, int len) const { if (0 == len) return false; @@ -731,15 +736,16 @@ int ZhuyinDaChenCP26Parser2::parse(pinyin_option_t options, /* maximum forward match for chewing. */ int parsed_len = 0; const char * cur_str = NULL; - ChewingKey key; ChewingKeyRest key_rest; while (parsed_len < maximum_len) { cur_str = str + parsed_len; i = std_lite::min(maximum_len - parsed_len, (int)max_chewing_dachen26_length); + gint16 distance = 0; + ChewingKey key; ChewingKeyRest key_rest; for (; i > 0; --i) { - bool success = parse_one_key(options, key, cur_str, i); + bool success = parse_one_key(options, key, distance, cur_str, i); if (success) break; } @@ -851,6 +857,7 @@ ZhuyinDirectParser2::ZhuyinDirectParser2 (){ bool ZhuyinDirectParser2::parse_one_key(pinyin_option_t options, ChewingKey & key, + gint16 & distance, const char *str, int len) const { options &= ~PINYIN_AMB_ALL; /* by default, chewing will use the first tone. */ @@ -910,8 +917,6 @@ int ZhuyinDirectParser2::parse(pinyin_option_t options, g_array_set_size(keys, 0); g_array_set_size(key_rests, 0); - ChewingKey key; ChewingKeyRest key_rest; - int parsed_len = 0; int i = 0, cur = 0, next = 0; while (cur < len) { @@ -922,7 +927,9 @@ int ZhuyinDirectParser2::parse(pinyin_option_t options, } next = i; - if (parse_one_key(options, key, str + cur, next - cur)) { + gint16 distance = 0; + ChewingKey key; ChewingKeyRest key_rest; + if (parse_one_key(options, key, distance, str + cur, next - cur)) { #if 0 /* as direct parser handles data source, assume the data is correct when loading. */ diff --git a/src/storage/zhuyin_parser2.h b/src/storage/zhuyin_parser2.h index 45af804..8a9c550 100644 --- a/src/storage/zhuyin_parser2.h +++ b/src/storage/zhuyin_parser2.h @@ -98,7 +98,7 @@ public: virtual ~ZhuyinSimpleParser2() {} - virtual bool parse_one_key(pinyin_option_t options, ChewingKey & key, const char *str, int len) const; + virtual bool parse_one_key(pinyin_option_t options, ChewingKey & key, gint16 & distance, const char *str, int len) const; virtual int parse(pinyin_option_t options, ChewingKeyVector & keys, ChewingKeyRestVector & key_rests, const char *str, int len) const; @@ -142,7 +142,7 @@ public: virtual ~ZhuyinDiscreteParser2() {} - virtual bool parse_one_key(pinyin_option_t options, ChewingKey & key, const char *str, int len) const; + virtual bool parse_one_key(pinyin_option_t options, ChewingKey & key, gint16 & distance, const char *str, int len) const; virtual int parse(pinyin_option_t options, ChewingKeyVector & keys, ChewingKeyRestVector & key_rests, const char *str, int len) const; @@ -167,7 +167,7 @@ public: virtual ~ZhuyinDaChenCP26Parser2() {} - virtual bool parse_one_key(pinyin_option_t options, ChewingKey & key, const char *str, int len) const; + virtual bool parse_one_key(pinyin_option_t options, ChewingKey & key, gint16 & distance, const char *str, int len) const; virtual int parse(pinyin_option_t options, ChewingKeyVector & keys, ChewingKeyRestVector & key_rests, const char *str, int len) const; @@ -187,7 +187,7 @@ public: virtual ~ZhuyinDirectParser2() {} - virtual bool parse_one_key(pinyin_option_t options, ChewingKey & key, const char *str, int len) const; + virtual bool parse_one_key(pinyin_option_t options, ChewingKey & key, gint16 & distance, const char *str, int len) const; virtual int parse(pinyin_option_t options, ChewingKeyVector & keys, ChewingKeyRestVector & key_rests, const char *str, int len) const; }; -- cgit