refine ChewingKeyRest
authorPeng Wu <alexepico@gmail.com>
Wed, 14 Dec 2011 05:39:06 +0000 (13:39 +0800)
committerPeng Wu <alexepico@gmail.com>
Wed, 14 Dec 2011 05:39:06 +0000 (13:39 +0800)
src/storage/chewing_key.h
src/storage/pinyin_parser2.cpp
tests/storage/test_parser2.cpp
utils/storage/gen_chewing_table.cpp

index 9f6526e..68733d4 100644 (file)
@@ -212,21 +212,8 @@ struct ChewingKeyRest
         m_raw_begin = 0;
         m_raw_end = 0;
     }
-
-    const char * get_pinyin_string();
-    const char * get_chewing_string();
 };
 
-static inline gchar * get_pinyin_string(ChewingKey key,
-                                        ChewingKeyRest key_rest) {
-    if (CHEWING_ZERO_TONE != key.m_tone) {
-        return g_strdup_printf
-            ("%s%d", key_rest.get_pinyin_string(), key.m_tone);
-    } else {
-        return g_strdup(key_rest.get_pinyin_string());
-    }
-}
-
 };
 
 #endif
index 7adf9a7..f79695f 100644 (file)
@@ -84,6 +84,7 @@ gint ChewingKey::get_table_index() {
 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];
 
     if (CHEWING_ZERO_TONE == m_tone) {
@@ -96,6 +97,7 @@ gchar * ChewingKey::get_pinyin_string() {
 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) {
@@ -107,26 +109,6 @@ gchar * ChewingKey::get_chewing_string() {
 }
 
 
-/* methods for Chewing Keys to access pinyin parser table. */
-const char * ChewingKeyRest::get_pinyin_string(){
-    if (m_table_index == 0)
-        return NULL;
-
-    /* check end boundary. */
-    assert(m_table_index < G_N_ELEMENTS(content_table));
-    return content_table[m_table_index].m_pinyin_str;
-}
-
-const char * ChewingKeyRest::get_chewing_string(){
-    if (m_table_index == 0)
-        return NULL;
-
-    /* check end boundary. */
-    assert(m_table_index < G_N_ELEMENTS(content_table));
-    return content_table[m_table_index].m_chewing_str;
-}
-
-
 /* Pinyin Parsers */
 
 /* internal information for pinyin parsers. */
index f42db49..276a3a9 100644 (file)
@@ -105,7 +105,7 @@ int main(int argc, char * argv[]) {
         bool success = parser->parse_one_key(options, key, key_rest,
                                              linebuf, strlen(linebuf));
         if (success)
-            printf("pinyin:%s\t%d\t%d\n", key_rest.get_pinyin_string(),
+            printf("pinyin:%s\t%d\t%d\n", key.get_pinyin_string(),
                    key_rest.m_raw_begin, key_rest.m_raw_end);
 #endif
 
@@ -123,9 +123,11 @@ int main(int argc, char * argv[]) {
         assert(keys->len == key_rests->len);
 
         for (size_t i = 0; i < keys->len; ++i) {
+            ChewingKey * key =
+                &g_array_index(keys, ChewingKey, i);
             ChewingKeyRest * key_rest =
                 &g_array_index(key_rests, ChewingKeyRest, i);
-            printf("%s %d %d\t", key_rest->get_pinyin_string(),
+            printf("%s %d %d\t", key->get_pinyin_string(),
                    key_rest->m_raw_begin, key_rest->m_raw_end);
         }
         printf("\n");
index 30bc0da..d74be6a 100644 (file)
@@ -293,7 +293,7 @@ void gen_phrase_file(const char * outfilename, int phrase_index){
                         (key_rests, ChewingKeyRest, k);
 
                     //assert (CHEWING_ZERO_TONE != key.m_tone);
-                    pinyin = get_pinyin_string(key, key_rest);
+                    pinyin = key.get_pinyin_string();
                     g_array_append_val(pinyins, pinyin);
                 }
                 gchar * pinyin_str = g_strjoinv("'", (gchar **)pinyins->data);