polish code
[platform/upstream/libpinyin.git] / tests / storage / test_parser2.cpp
1 /* 
2  *  libpinyin
3  *  Library to deal with pinyin.
4  *  
5  *  Copyright (C) 2011 Peng Wu <alexepico@gmail.com>
6  *  
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  * 
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  *  GNU General Public License for more details.
16  *  
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20  */
21
22
23 #include "timer.h"
24 #include <errno.h>
25 #include <stdio.h>
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include "pinyin_parser2.h"
30
31
32 static const gchar * parsername = "";
33 static gboolean incomplete = FALSE;
34
35 static GOptionEntry entries[] =
36 {
37     {"parser", 'p', 0, G_OPTION_ARG_STRING, &parsername, "parser", "fullpinyin doublepinyin chewing"},
38     {"incomplete", 'i', 0, G_OPTION_ARG_NONE, &incomplete, "incomplete pinyin", NULL},
39     {NULL}
40 };
41
42 #if 0
43     "  -s <scheme> specify scheme for doublepinyin/chewing.\n"
44     "     schemes for doublepinyin: zrm, ms, ziguang, abc, pyjj, xhe.\n"
45     "     schemes for chewing: standard, ibm, ginyieh, eten.\n"
46 #endif
47
48
49 size_t bench_times = 1000;
50
51 using namespace pinyin;
52
53
54 int main(int argc, char * argv[]) {
55     GError * error = NULL;
56     GOptionContext * context;
57
58     context = g_option_context_new("- test pinyin parser");
59     g_option_context_add_main_entries(context, entries, NULL);
60     if (!g_option_context_parse(context, &argc, &argv, &error)) {
61         g_print("option parsing failed:%s\n", error->message);
62         exit(EINVAL);
63     }
64
65     pinyin_option_t options = PINYIN_CORRECT_ALL | USE_TONE | USE_RESPLIT_TABLE;
66     if (incomplete)
67         options |= PINYIN_INCOMPLETE | CHEWING_INCOMPLETE;
68
69     PinyinParser2 * parser = NULL;
70     ChewingKeyVector keys = g_array_new(FALSE, FALSE, sizeof(ChewingKey));
71     ChewingKeyRestVector key_rests =
72         g_array_new(FALSE, FALSE, sizeof(ChewingKeyRest));
73
74     /* create the parser */
75     if (strcmp("fullpinyin", parsername) == 0) {
76         parser = new FullPinyinParser2();
77     } else if (strcmp("doublepinyin", parsername) == 0) {
78         parser = new DoublePinyinParser2();
79     } else if (strcmp("chewing", parsername) == 0) {
80         parser = new ChewingParser2();
81     }
82
83     if (!parser)
84         parser = new FullPinyinParser2();
85
86     char* linebuf = NULL; size_t size = 0; ssize_t read;
87     while( (read = getline(&linebuf, &size, stdin)) != -1 ){
88         if ( '\n' == linebuf[strlen(linebuf) - 1] ) {
89             linebuf[strlen(linebuf) - 1] = '\0';
90         }
91
92         if ( strcmp ( linebuf, "quit" ) == 0)
93             break;
94
95 #if 0
96         ChewingKey key;
97         bool success = parser->parse_one_key(options, key,
98                                              linebuf, strlen(linebuf));
99         if (success) {
100             gchar * pinyins = key.get_pinyin_string();
101             printf("pinyin:%s\n", pinyins);
102             g_free(pinyins);
103         }
104 #endif
105
106 #if 1
107         int len = 0;
108         guint32 start_time = record_time();
109         for ( size_t i = 0; i < bench_times; ++i)
110             len = parser->parse(options, keys, key_rests,
111                                 linebuf, strlen(linebuf));
112
113         print_time(start_time, bench_times);
114
115         printf("parsed %d chars, %d keys.\n", len, keys->len);
116
117         assert(keys->len == key_rests->len);
118
119         for (size_t i = 0; i < keys->len; ++i) {
120             ChewingKey * key =
121                 &g_array_index(keys, ChewingKey, i);
122             ChewingKeyRest * key_rest =
123                 &g_array_index(key_rests, ChewingKeyRest, i);
124
125             gchar * pinyins = key->get_pinyin_string();
126             printf("%s %d %d\t", pinyins,
127                    key_rest->m_raw_begin, key_rest->m_raw_end);
128             g_free(pinyins);
129         }
130         printf("\n");
131 #endif
132
133     }
134
135     if (linebuf)
136         free(linebuf);
137
138     delete parser;
139
140     g_array_free(key_rests, TRUE);
141     g_array_free(keys, TRUE);
142
143     return 0;
144 }