summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2011-11-23 17:37:02 +0800
committerPeng Wu <alexepico@gmail.com>2011-11-23 17:37:02 +0800
commitcbe325008a4413d154eebf521ba5bfb25bae075d (patch)
tree292d9ba4328ae492399deb42b26a67a3d3493cde
parentdd2f732abd8d16891995c7c12c2ae73f449f2395 (diff)
downloadlibpinyin-cbe325008a4413d154eebf521ba5bfb25bae075d.tar.gz
libpinyin-cbe325008a4413d154eebf521ba5bfb25bae075d.tar.xz
libpinyin-cbe325008a4413d154eebf521ba5bfb25bae075d.zip
write parse method for chewing parser2
-rw-r--r--src/storage/pinyin_parser2.cpp54
-rw-r--r--src/storage/pinyin_parser2.h2
2 files changed, 46 insertions, 10 deletions
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;
diff --git a/src/storage/pinyin_parser2.h b/src/storage/pinyin_parser2.h
index 6a31703..c200846 100644
--- a/src/storage/pinyin_parser2.h
+++ b/src/storage/pinyin_parser2.h
@@ -214,7 +214,7 @@ public:
public:
bool set_scheme(ChewingScheme scheme);
- bool in_chewing_scheme(const char key);
+ bool in_chewing_scheme(const char key) const;
};