fixes phrase_index.h
[platform/upstream/libpinyin.git] / src / storage / pinyin_parser2.cpp
index 568380c..131231c 100644 (file)
@@ -23,6 +23,7 @@
 #include "pinyin_parser2.h"
 #include <ctype.h>
 #include <assert.h>
+#include <stdio.h>
 #include <string.h>
 #include "stl_lite.h"
 #include "pinyin_phrase2.h"
@@ -71,23 +72,40 @@ static bool check_chewing_options(pinyin_option_t options, const chewing_index_i
 }
 
 
-/* methods for Chewing Keys to access pinyin parser table. */
-const char * ChewingKeyRest::get_pinyin_string(){
-    if (m_table_index == 0)
-        return NULL;
+gint ChewingKey::get_table_index() {
+    assert(m_initial <  CHEWING_NUMBER_OF_INITIALS);
+    assert(m_middle < CHEWING_NUMBER_OF_MIDDLES);
+    assert(m_final < CHEWING_NUMBER_OF_FINALS);
 
-    /* check end boundary. */
-    assert(m_table_index < G_N_ELEMENTS(content_table));
-    return content_table[m_table_index].m_pinyin_str;
+    gint index = chewing_key_table[(m_initial * CHEWING_NUMBER_OF_MIDDLES + m_middle) * CHEWING_NUMBER_OF_FINALS + m_final];
+    return index == -1 ? 0 : index;
 }
 
-const char * ChewingKeyRest::get_chewing_string(){
-    if (m_table_index == 0)
-        return NULL;
+gchar * ChewingKey::get_pinyin_string() {
+    assert(m_tone < CHEWING_NUMBER_OF_TONES);
+    gint index = get_table_index();
+    assert(index < G_N_ELEMENTS(content_table));
+    const content_table_item_t & item = content_table[index];
 
-    /* check end boundary. */
-    assert(m_table_index < G_N_ELEMENTS(content_table));
-    return content_table[m_table_index].m_chewing_str;
+    if (CHEWING_ZERO_TONE == m_tone) {
+        return g_strdup(item.m_pinyin_str);
+    } else {
+        return g_strdup_printf("%s%d", item.m_pinyin_str, m_tone);
+    }
+}
+
+gchar * ChewingKey::get_chewing_string() {
+    assert(m_tone < CHEWING_NUMBER_OF_TONES);
+    gint index = get_table_index();
+    assert(index < G_N_ELEMENTS(content_table));
+    const content_table_item_t & item = content_table[index];
+
+    if (CHEWING_ZERO_TONE == m_tone) {
+        return g_strdup(item.m_chewing_str);
+    } else {
+        return g_strdup_printf("%s%s", item.m_chewing_str,
+                               chewing_tone_table[m_tone]);
+    }
 }
 
 
@@ -121,9 +139,9 @@ static bool compare_pinyin_less_than(const pinyin_index_item_t & lhs,
     return 0 > strcmp(lhs.m_pinyin_input, rhs.m_pinyin_input);
 }
 
-static inline bool search_pinyin_index(pinyin_option_t options, const char * pinyin,
-                                       ChewingKey & key,
-                                       ChewingKeyRest & key_rest){
+static inline bool search_pinyin_index(pinyin_option_t options,
+                                       const char * pinyin,
+                                       ChewingKey & key){
     pinyin_index_item_t item;
     memset(&item, 0, sizeof(item));
     item.m_pinyin_input = pinyin;
@@ -142,8 +160,8 @@ static inline bool search_pinyin_index(pinyin_option_t options, const char * pin
         if (!check_pinyin_options(options, index))
             return false;
 
-        key_rest.m_table_index = index->m_table_index;
-        key = content_table[key_rest.m_table_index].m_chewing_key;
+        key = content_table[index->m_table_index].m_chewing_key;
+        assert(key.get_table_index() == index->m_table_index);
         return true;
     }
 
@@ -155,9 +173,9 @@ static bool compare_chewing_less_than(const chewing_index_item_t & lhs,
     return 0 > strcmp(lhs.m_chewing_input, rhs.m_chewing_input);
 }
 
-static inline bool search_chewing_index(pinyin_option_t options, const char * chewing,
-                                        ChewingKey & key,
-                                        ChewingKeyRest & key_rest){
+static inline bool search_chewing_index(pinyin_option_t options,
+                                        const char * chewing,
+                                        ChewingKey & key){
     chewing_index_item_t item;
     memset(&item, 0, sizeof(item));
     item.m_chewing_input = chewing;
@@ -177,8 +195,8 @@ static inline bool search_chewing_index(pinyin_option_t options, const char * ch
         if (!check_chewing_options(options, index))
             return false;
 
-        key_rest.m_table_index = index->m_table_index;
-        key = content_table[key_rest.m_table_index].m_chewing_key;
+        key = content_table[index->m_table_index].m_chewing_key;
+        assert(key.get_table_index() == index->m_table_index);
         return true;
     }
 
@@ -191,8 +209,8 @@ FullPinyinParser2::FullPinyinParser2 (){
 }
 
 
-bool FullPinyinParser2::parse_one_key (pinyin_option_t options, ChewingKey & key,
-                                       ChewingKeyRest & key_rest,
+bool FullPinyinParser2::parse_one_key (pinyin_option_t options,
+                                       ChewingKey & key,
                                        const char * pinyin, int len) const {
     /* "'" are not accepted in parse_one_key. */
     gchar * input = g_strndup(pinyin, len);
@@ -200,7 +218,7 @@ bool FullPinyinParser2::parse_one_key (pinyin_option_t options, ChewingKey & key
 
     guint16 tone = CHEWING_ZERO_TONE; guint16 tone_pos = 0;
     guint16 parsed_len = len;
-    key = ChewingKey(); key_rest = ChewingKeyRest();
+    key = ChewingKey();
 
     if (options & USE_TONE) {
         /* find the tone in the last character. */
@@ -216,8 +234,10 @@ bool FullPinyinParser2::parse_one_key (pinyin_option_t options, ChewingKey & key
 
     /* Note: optimize here? */
     input[parsed_len] = '\0';
-    if (!search_pinyin_index(options, input, key, key_rest))
-        --parsed_len;
+    if (!search_pinyin_index(options, input, key)) {
+        g_free(input);
+        return false;
+    }
 
     if (options & USE_TONE) {
         /* post processing tone. */
@@ -229,7 +249,6 @@ bool FullPinyinParser2::parse_one_key (pinyin_option_t options, ChewingKey & key
         }
     }
 
-    key_rest.m_raw_begin = 0; key_rest.m_raw_end = parsed_len;
     g_free(input);
     return parsed_len == len;
 }
@@ -281,7 +300,9 @@ int FullPinyinParser2::parse (pinyin_option_t options, ChewingKeyVector & keys,
         }
 
         /* dynamic programming here. */
-        for (size_t m = i; m < next_sep; ++m) {
+        /* for (size_t m = i; m < next_sep; ++m) */
+        {
+            size_t m = i;
             curstep = &g_array_index(m_parse_steps, parse_value_t, m);
             size_t try_len = std_lite::min
                 (m + max_full_pinyin_length, next_sep);
@@ -295,23 +316,69 @@ int FullPinyinParser2::parse (pinyin_option_t options, ChewingKeyVector & keys,
 
                 ChewingKey key; ChewingKeyRest rest;
                 bool parsed = parse_one_key
-                    (options, key, rest, onepinyin, onepinyinlen);
+                    (options, key, onepinyin, onepinyinlen);
                 rest.m_raw_begin = m; rest.m_raw_end = n;
                 if (!parsed)
                     continue;
+
+                //printf("onepinyin:%s len:%d\n", onepinyin, onepinyinlen);
+
                 value.m_key = key; value.m_key_rest = rest;
                 value.m_num_keys = curstep->m_num_keys + 1;
                 value.m_parsed_len = curstep->m_parsed_len + onepinyinlen;
                 value.m_last_step = m;
 
                 /* save next step */
+                /* no previous result */
                 if (-1 == nextstep->m_last_step)
                     *nextstep = value;
+                /* prefer the longest pinyin */
                 if (value.m_parsed_len > nextstep->m_parsed_len)
                     *nextstep = value;
+                /* prefer the shortest keys with the same pinyin length */
                 if (value.m_parsed_len == nextstep->m_parsed_len &&
                     value.m_num_keys < nextstep->m_num_keys)
                     *nextstep = value;
+
+                /* handle with the same pinyin length and the number of keys */
+                if (value.m_parsed_len == nextstep->m_parsed_len &&
+                    value.m_num_keys == nextstep->m_num_keys) {
+
+#if 0
+                    /* prefer the complete pinyin with shengmu
+                     * over without shengmu,
+                     * ex: "kaneiji" -> "ka'nei'ji".
+                     */
+                    if ((value.m_key.m_initial != CHEWING_ZERO_INITIAL &&
+                         !(value.m_key.m_middle == CHEWING_ZERO_MIDDLE &&
+                           value.m_key.m_final == CHEWING_ZERO_FINAL)) &&
+                        nextstep->m_key.m_initial == CHEWING_ZERO_INITIAL)
+                        *nextstep = value;
+
+                    /* prefer the complete pinyin 'er'
+                     * over the in-complete pinyin 'r',
+                     * ex: "xierqi" -> "xi'er'qi."
+                     */
+                    if ((value.m_key.m_initial == CHEWING_ZERO_INITIAL &&
+                        value.m_key.m_middle == CHEWING_ZERO_MIDDLE &&
+                        value.m_key.m_final == CHEWING_ER) &&
+                        (nextstep->m_key.m_initial == CHEWING_R &&
+                         nextstep->m_key.m_middle == CHEWING_ZERO_MIDDLE &&
+                         nextstep->m_key.m_final == CHEWING_ZERO_FINAL))
+                        *nextstep = value;
+#endif
+
+                    /* prefer the 'a' at the end of clause,
+                     * ex: "zheyanga$" -> "zhe'yang'a$".
+                     */
+                    if (value.m_parsed_len == len &&
+                        (nextstep->m_key.m_initial != CHEWING_ZERO_INITIAL &&
+                         nextstep->m_key.m_final == CHEWING_A) &&
+                        (value.m_key.m_initial == CHEWING_ZERO_INITIAL &&
+                         value.m_key.m_middle == CHEWING_ZERO_MIDDLE &&
+                         value.m_key.m_final == CHEWING_A))
+                        *nextstep = value;
+                }
             }
         }
     }
@@ -321,7 +388,7 @@ int FullPinyinParser2::parse (pinyin_option_t options, ChewingKeyVector & keys,
 
     /* post processing for re-split table. */
     if (options & USE_RESPLIT_TABLE) {
-        post_process(options, keys, key_rests);
+        post_process2(options, keys, key_rests, str, len);
     }
 
     g_free(input);
@@ -351,7 +418,7 @@ int FullPinyinParser2::final_step(size_t step_len, ChewingKeyVector & keys,
         gint16 pos = curstep->m_num_keys - 1;
 
         /* skip "'" */
-        if (0 != curstep->m_key_rest.m_table_index) {
+        if (0 != curstep->m_key.get_table_index()) {
             ChewingKey * key = &g_array_index(keys, ChewingKey, pos);
             ChewingKeyRest * rest = &g_array_index
                 (key_rests, ChewingKeyRest, pos);
@@ -365,17 +432,18 @@ int FullPinyinParser2::final_step(size_t step_len, ChewingKeyVector & keys,
     return parsed_len;
 }
 
-
-bool FullPinyinParser2::post_process(pinyin_option_t options,
-                                     ChewingKeyVector & keys,
-                                     ChewingKeyRestVector & key_rests) const {
+bool FullPinyinParser2::post_process2(pinyin_option_t options,
+                                      ChewingKeyVector & keys,
+                                      ChewingKeyRestVector & key_rests,
+                                      const char * str,
+                                      int len) const {
     int i;
     assert(keys->len == key_rests->len);
-    gint16 num_keys = keys->len;
+    gint num_keys = keys->len;
 
     ChewingKey * cur_key = NULL, * next_key = NULL;
     ChewingKeyRest * cur_rest = NULL, * next_rest = NULL;
-    guint16 cur_tone = CHEWING_ZERO_TONE, next_tone = CHEWING_ZERO_TONE;
+    guint16 next_tone = CHEWING_ZERO_TONE;
 
     for (i = 0; i < num_keys - 1; ++i) {
         cur_rest = &g_array_index(key_rests, ChewingKeyRest, i);
@@ -388,54 +456,173 @@ bool FullPinyinParser2::post_process(pinyin_option_t options,
         cur_key = &g_array_index(keys, ChewingKey, i);
         next_key = &g_array_index(keys, ChewingKey, i + 1);
 
+        /* some tone here */
+        if (CHEWING_ZERO_TONE != cur_key->m_tone)
+            continue;
+
+        /* back up tone */
         if (options & USE_TONE) {
-            cur_tone = cur_key->m_tone;
             next_tone = next_key->m_tone;
-            cur_key->m_tone = next_key->m_tone = CHEWING_ZERO_TONE;
+            if (CHEWING_ZERO_TONE != next_tone) {
+                next_key->m_tone = CHEWING_ZERO_TONE;
+                next_rest->m_raw_end --;
+            }
         }
 
         /* lookup re-split table */
-        size_t k;
         const resplit_table_item_t * item = NULL;
-        for (k = 0; k < G_N_ELEMENTS(resplit_table); ++k) {
-            item = resplit_table + k;
+
+        item = retrieve_resplit_item_by_original_pinyins
+            (options, cur_key, cur_rest, next_key, next_rest, str, len);
+
+        if (item) {
             /* no ops */
             if (item->m_orig_freq >= item->m_new_freq)
                 continue;
 
-            /* use pinyin_exact_compare2 here. */
-            if (0 == pinyin_exact_compare2(item->m_orig_keys,
-                                           cur_key, 2))
-                break;
+            /* do re-split */
+            const char * onepinyin = str + cur_rest->m_raw_begin;
+            size_t len = strlen(item->m_new_keys[0]);
 
-        }
+            assert(parse_one_key(options, *cur_key, onepinyin, len));
+            cur_rest->m_raw_end = cur_rest->m_raw_begin + len;
 
-        /* find the match */
-        if (k < G_N_ELEMENTS(resplit_table)) {
-            /* do re-split */
-            item = resplit_table + k;
-            *cur_key = item->m_new_keys[0];
-            *next_key = item->m_new_keys[1];
-            /* assumes only moved one char in gen_all_resplit script. */
-            cur_rest->m_raw_end --;
-            next_rest->m_raw_begin --;
+            next_rest->m_raw_begin = cur_rest->m_raw_end;
+            onepinyin = str + next_rest->m_raw_begin;
+            len = strlen(item->m_new_keys[1]);
+
+            assert(parse_one_key(options, *next_key, onepinyin, len));
         }
 
-        /* save back tones */
+        /* restore tones */
         if (options & USE_TONE) {
-            cur_key->m_tone = cur_tone;
-            next_key->m_tone = next_tone;
+            if (CHEWING_ZERO_TONE != next_tone) {
+                next_key->m_tone = next_tone;
+                next_rest->m_raw_end ++;
+            }
         }
     }
 
     return true;
 }
 
+const divided_table_item_t * FullPinyinParser2::retrieve_divided_item
+(pinyin_option_t options, ChewingKey * key, ChewingKeyRest * rest,
+ const char * str, int len) const {
+
+    /* lookup divided table */
+    size_t k;
+    const divided_table_item_t * item = NULL;
+    for (k = 0; k < G_N_ELEMENTS(divided_table); ++k) {
+        item = divided_table + k;
+
+        const char * onepinyin = str + rest->m_raw_begin;
+        size_t len = strlen(item->m_orig_key);
+
+        if (rest->length() != len)
+            continue;
+
+        if (0 == strncmp(onepinyin, item->m_orig_key, len))
+            break;
+    }
+
+    /* found the match */
+    if (k < G_N_ELEMENTS(divided_table)) {
+        /* do divided */
+        item = divided_table + k;
+        return item;
+    }
+
+    return NULL;
+}
+
+
+const resplit_table_item_t * FullPinyinParser2::retrieve_resplit_item_by_original_pinyins
+(pinyin_option_t options,
+ ChewingKey * cur_key, ChewingKeyRest * cur_rest,
+ ChewingKey * next_key, ChewingKeyRest * next_rest,
+ const char * str, int len) const{
+    /* lookup re-split table */
+    size_t k;
+    const resplit_table_item_t * item = NULL;
+
+    for (k = 0; k < G_N_ELEMENTS(resplit_table); ++k) {
+        item = resplit_table + k;
+
+        const char * onepinyin = str + cur_rest->m_raw_begin;
+        size_t len = strlen(item->m_orig_keys[0]);
+
+        if (cur_rest->length() != len)
+            continue;
+
+        if (0 != strncmp(onepinyin, item->m_orig_keys[0], len))
+            continue;
+
+        onepinyin = str + next_rest->m_raw_begin;
+        len = strlen(item->m_orig_keys[1]);
+
+        if (next_rest->length() != len)
+            continue;
+
+        if (0 == strncmp(onepinyin, item->m_orig_keys[1], len))
+            break;
+    }
+
+    /* found the match */
+    if (k < G_N_ELEMENTS(resplit_table)) {
+        item = resplit_table + k;
+        return item;
+    }
+
+    return NULL;
+}
+
+const resplit_table_item_t * FullPinyinParser2::retrieve_resplit_item_by_resplit_pinyins
+(pinyin_option_t options,
+ ChewingKey * cur_key, ChewingKeyRest * cur_rest,
+ ChewingKey * next_key, ChewingKeyRest * next_rest,
+ const char * str, int len) const {
+    /* lookup divide table */
+    size_t k;
+    const resplit_table_item_t * item = NULL;
+
+    for (k = 0; k < G_N_ELEMENTS(resplit_table); ++k) {
+        item = resplit_table + k;
+
+        const char * onepinyin = str + cur_rest->m_raw_begin;
+        size_t len = strlen(item->m_new_keys[0]);
+
+        if (cur_rest->length() != len)
+            continue;
+
+        if (0 != strncmp(onepinyin, item->m_new_keys[0], len))
+            continue;
+
+        onepinyin = str + next_rest->m_raw_begin;
+        len = strlen(item->m_new_keys[1]);
+
+        if (next_rest->length() != len)
+            continue;
+
+        if (0 == strncmp(onepinyin, item->m_new_keys[1], len))
+            break;
+    }
+
+    /* found the match */
+    if (k < G_N_ELEMENTS(resplit_table)) {
+        item = resplit_table + k;
+        return item;
+    }
+
+    return NULL;
+}
+
 #define IS_KEY(x)   (('a' <= x && x <= 'z') || x == ';')
 
-bool DoublePinyinParser2::parse_one_key(pinyin_option_t options, ChewingKey & key,
-                                        ChewingKeyRest & key_rest,
+bool DoublePinyinParser2::parse_one_key(pinyin_option_t options,
+                                        ChewingKey & key,
                                         const char *str, int len) const {
+    options &= ~(PINYIN_CORRECT_ALL|PINYIN_AMB_ALL);
 
     if (1 == len) {
         if (!(options & PINYIN_INCOMPLETE))
@@ -450,8 +637,7 @@ bool DoublePinyinParser2::parse_one_key(pinyin_option_t options, ChewingKey & ke
         if (NULL == sheng || strcmp(sheng, "'") == 0)
             return false;
 
-        if (search_pinyin_index(options, sheng, key, key_rest)) {
-            key_rest.m_raw_begin = 0; key_rest.m_raw_end = len;
+        if (search_pinyin_index(options, sheng, key)) {
             return true;
         } else {
             return false;
@@ -459,7 +645,8 @@ bool DoublePinyinParser2::parse_one_key(pinyin_option_t options, ChewingKey & ke
     }
 
     ChewingTone tone = CHEWING_ZERO_TONE;
-    options &= ~(PINYIN_CORRECT_ALL|PINYIN_AMB_ALL);
+    options &= ~(PINYIN_INCOMPLETE|CHEWING_INCOMPLETE);
+    options |= PINYIN_CORRECT_UE_VE | PINYIN_CORRECT_V_U;
 
     /* parse tone */
     if (3 == len) {
@@ -492,9 +679,11 @@ bool DoublePinyinParser2::parse_one_key(pinyin_option_t options, ChewingKey & ke
         charid = ch == ';' ? 26 : ch - 'a';
         /* first yunmu */
         const char * yun = m_yunmu_table[charid].m_yunmus[0];
+        if (NULL == yun)
+            return false;
+
         gchar * pinyin = g_strdup_printf("%s%s", sheng, yun);
-        if (search_pinyin_index(options, pinyin, key, key_rest)) {
-            key_rest.m_raw_begin = 0; key_rest.m_raw_end = len;
+        if (search_pinyin_index(options, pinyin, key)) {
             key.m_tone = tone;
             g_free(pinyin);
             return true;
@@ -503,9 +692,11 @@ bool DoublePinyinParser2::parse_one_key(pinyin_option_t options, ChewingKey & ke
 
         /* second yunmu */
         yun = m_yunmu_table[charid].m_yunmus[1];
+        if (NULL == yun)
+            return false;
+
         pinyin = g_strdup_printf("%s%s", sheng, yun);
-        if (search_pinyin_index(options, pinyin, key, key_rest)) {
-            key_rest.m_raw_begin = 0; key_rest.m_raw_end = len;
+        if (search_pinyin_index(options, pinyin, key)) {
             key.m_tone = tone;
             g_free(pinyin);
             return true;
@@ -528,7 +719,8 @@ int DoublePinyinParser2::parse(pinyin_option_t options, ChewingKeyVector & keys,
     int maximum_len = 0; int i;
     /* probe the longest possible double pinyin string. */
     for (i = 0; i < len; ++i) {
-        if (!IS_KEY(str[i]))
+        const char ch = str[i];
+        if (!(IS_KEY(ch) || ('0' < ch && ch <= '5')))
             break;
     }
     maximum_len = i;
@@ -542,7 +734,7 @@ int DoublePinyinParser2::parse(pinyin_option_t options, ChewingKeyVector & keys,
 
         ChewingKey key; ChewingKeyRest key_rest;
         for (; i > 0; --i) {
-            bool success = parse_one_key(options, key, key_rest, cur_str, i);
+            bool success = parse_one_key(options, key, cur_str, i);
             if (success)
                 break;
         }
@@ -599,12 +791,12 @@ bool DoublePinyinParser2::set_scheme(DoublePinyinScheme scheme) {
 
 /* the chewing string must be freed with g_free. */
 static bool search_chewing_symbols(const chewing_symbol_item_t * symbol_table,
-                                   const char key, char ** chewing) {
+                                   const char key, const char ** chewing) {
     *chewing = NULL;
     /* just iterate the table, as we only have < 50 items. */
     while (symbol_table->m_input != '\0') {
         if (symbol_table->m_input == key) {
-            *chewing = g_strdup(symbol_table->m_chewing);
+            *chewing = symbol_table->m_chewing;
             return true;
         }
         symbol_table ++;
@@ -627,7 +819,10 @@ static bool search_chewing_tones(const chewing_tone_item_t * tone_table,
 }
 
 
-bool ChewingParser2::parse_one_key(pinyin_option_t options, ChewingKey & key, ChewingKeyRest & key_rest, const char *str, int len) const {
+bool ChewingParser2::parse_one_key(pinyin_option_t options,
+                                   ChewingKey & key,
+                                   const char *str, int len) const {
+    options &= ~(PINYIN_CORRECT_ALL|PINYIN_AMB_ALL);
     char tone = CHEWING_ZERO_TONE;
 
     int symbols_len = len;
@@ -640,12 +835,11 @@ bool ChewingParser2::parse_one_key(pinyin_option_t options, ChewingKey & key, Ch
     }
 
     int i;
-    gchar * chewing = NULL, * onechar = NULL;
+    gchar * chewing = NULL; const char * onechar = NULL;
 
     /* probe the possible chewing map in the rest of str. */
     for (i = 0; i < symbols_len; ++i) {
         if (!search_chewing_symbols(m_symbol_table, str[i], &onechar)) {
-            g_free(onechar);
             g_free(chewing);
             return false;
         }
@@ -657,12 +851,10 @@ bool ChewingParser2::parse_one_key(pinyin_option_t options, ChewingKey & key, Ch
             chewing = g_strconcat(chewing, onechar, NULL);
             g_free(tmp);
         }
-        g_free(onechar);
     }
 
     /* search the chewing in the chewing index table. */
-    if (search_chewing_index(options, chewing, key, key_rest)) {
-        key_rest.m_raw_begin = 0; key_rest.m_raw_end = len;
+    if (chewing && search_chewing_index(options, chewing, key)) {
         /* save back tone if available. */
         key.m_tone = tone;
         g_free(chewing);
@@ -684,7 +876,7 @@ int ChewingParser2::parse(pinyin_option_t options, ChewingKeyVector & keys,
     int maximum_len = 0; int i;
     /* probe the longest possible chewing string. */
     for (i = 0; i < len; ++i) {
-        if (!in_chewing_scheme(str[i]))
+        if (!in_chewing_scheme(options, str[i], NULL))
             break;
     }
     maximum_len = i;
@@ -698,7 +890,7 @@ int ChewingParser2::parse(pinyin_option_t options, ChewingKeyVector & keys,
 
         ChewingKey key; ChewingKeyRest key_rest;
         for (; i > 0; --i) {
-            bool success = parse_one_key(options, key, key_rest, cur_str, i);
+            bool success = parse_one_key(options, key, cur_str, i);
             if (success)
                 break;
         }
@@ -742,13 +934,26 @@ bool ChewingParser2::set_scheme(ChewingScheme scheme) {
 }
 
 
-bool ChewingParser2::in_chewing_scheme(const char key) const {
-    gchar * chewing = NULL;
+bool ChewingParser2::in_chewing_scheme(pinyin_option_t options,
+                                       const char key, const char ** symbol)
+ const {
+    const gchar * chewing = NULL;
     char tone = CHEWING_ZERO_TONE;
 
-    bool retval = search_chewing_symbols(m_symbol_table, key, &chewing) ||
-        search_chewing_tones(m_tone_table, key, &tone);
-    g_free(chewing);
+    if (search_chewing_symbols(m_symbol_table, key, &chewing)) {
+        if (symbol)
+            *symbol = chewing;
+        return true;
+    }
 
-    return retval;
+    if (!(options & USE_TONE))
+        return false;
+
+    if (search_chewing_tones(m_tone_table, key, &tone)) {
+        if (symbol)
+            *symbol = chewing_tone_table[tone];
+        return true;
+    }
+
+    return false;
 }