polish code
[platform/upstream/libpinyin.git] / tests / storage / test_parser2.cpp
index 9ab6bb3..638cd96 100644 (file)
@@ -20,6 +20,7 @@
  */
 
 
+#include "timer.h"
 #include <errno.h>
 #include <stdio.h>
 #include <assert.h>
 #include "pinyin_parser2.h"
 
 
-using namespace pinyin;
+static const gchar * parsername = "";
+static gboolean incomplete = FALSE;
+
+static GOptionEntry entries[] =
+{
+    {"parser", 'p', 0, G_OPTION_ARG_STRING, &parsername, "parser", "fullpinyin doublepinyin chewing"},
+    {"incomplete", 'i', 0, G_OPTION_ARG_NONE, &incomplete, "incomplete pinyin", NULL},
+    {NULL}
+};
 
-static const char * help_msg =
-    "Usage:\n"
-    "  test-parser -p <parser> [-s <scheme>] [options].\n\n"
-    "  -p <parser> fullpinyin/doublepinyin/chewing.\n"
 #if 0
     "  -s <scheme> specify scheme for doublepinyin/chewing.\n"
     "     schemes for doublepinyin: zrm, ms, ziguang, abc, pyjj, xhe.\n"
     "     schemes for chewing: standard, ibm, ginyieh, eten.\n"
 #endif
-    "  -i          Use incomplete pinyin.\n"
-    "  -h          print this help msg.\n"
-    ;
 
 
-void print_help(){
-    printf(help_msg);
-}
+size_t bench_times = 1000;
+
+using namespace pinyin;
+
 
 int main(int argc, char * argv[]) {
+    GError * error = NULL;
+    GOptionContext * context;
+
+    context = g_option_context_new("- test pinyin parser");
+    g_option_context_add_main_entries(context, entries, NULL);
+    if (!g_option_context_parse(context, &argc, &argv, &error)) {
+        g_print("option parsing failed:%s\n", error->message);
+        exit(EINVAL);
+    }
+
+    pinyin_option_t options = PINYIN_CORRECT_ALL | USE_TONE | USE_RESPLIT_TABLE;
+    if (incomplete)
+        options |= PINYIN_INCOMPLETE | CHEWING_INCOMPLETE;
+
     PinyinParser2 * parser = NULL;
     ChewingKeyVector keys = g_array_new(FALSE, FALSE, sizeof(ChewingKey));
     ChewingKeyRestVector key_rests =
         g_array_new(FALSE, FALSE, sizeof(ChewingKeyRest));
-    guint32 options = PINYIN_CORRECT_ALL;
-
-    int i = 1;
-    while(i < argc) {
-        if (strcmp("-h", argv[i]) == 0) {
-            print_help();
-            exit(0);
-        } else if (strcmp("-i", argv[i]) == 0) {
-            options |= PINYIN_INCOMPLETE | CHEWING_INCOMPLETE;
-        } else if (strcmp("-p", argv[i]) == 0) {
-            if ( ++i >= argc ) {
-                print_help();
-                exit(EINVAL);
-            }
-            const char * name = argv[i];
-            if (strcmp("fullpinyin", name) == 0) {
-                parser = new FullPinyinParser2();
-            } else if (strcmp("doublepinyin", name) == 0) {
-                parser = new DoublePinyinParser2();
-            } else if (strcmp("chewing", name) == 0) {
-                parser = new ChewingParser2();
-            } else {
-                print_help();
-                exit(EINVAL);
-            }
-        } else {
-            print_help();
-            exit(EINVAL);
-        }
-        ++i;
+
+    /* create the parser */
+    if (strcmp("fullpinyin", parsername) == 0) {
+        parser = new FullPinyinParser2();
+    } else if (strcmp("doublepinyin", parsername) == 0) {
+        parser = new DoublePinyinParser2();
+    } else if (strcmp("chewing", parsername) == 0) {
+        parser = new ChewingParser2();
     }
 
     if (!parser)
@@ -98,26 +93,39 @@ int main(int argc, char * argv[]) {
             break;
 
 #if 0
-        ChewingKey key; ChewingKeyRest key_rest;
-        bool success = parser->parse_one_key(options, key, key_rest,
+        ChewingKey key;
+        bool success = parser->parse_one_key(options, key,
                                              linebuf, strlen(linebuf));
-        if (success)
-            printf("pinyin:%s\t%d\t%d\n", key_rest.get_pinyin_string(),
-                   key_rest.m_raw_begin, key_rest.m_raw_end);
+        if (success) {
+            gchar * pinyins = key.get_pinyin_string();
+            printf("pinyin:%s\n", pinyins);
+            g_free(pinyins);
+        }
 #endif
 
 #if 1
-        int len = parser->parse(options, keys, key_rests,
+        int len = 0;
+        guint32 start_time = record_time();
+        for ( size_t i = 0; i < bench_times; ++i)
+            len = parser->parse(options, keys, key_rests,
                                 linebuf, strlen(linebuf));
+
+        print_time(start_time, bench_times);
+
         printf("parsed %d chars, %d keys.\n", len, keys->len);
 
         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(),
+
+            gchar * pinyins = key->get_pinyin_string();
+            printf("%s %d %d\t", pinyins,
                    key_rest->m_raw_begin, key_rest->m_raw_end);
+            g_free(pinyins);
         }
         printf("\n");
 #endif
@@ -127,5 +135,10 @@ int main(int argc, char * argv[]) {
     if (linebuf)
         free(linebuf);
 
+    delete parser;
+
+    g_array_free(key_rests, TRUE);
+    g_array_free(keys, TRUE);
+
     return 0;
 }