update gen_binary_files.cpp
[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     PinyinParser2 * parser = NULL;
56     ChewingKeyVector keys = g_array_new(FALSE, FALSE, sizeof(ChewingKey));
57     ChewingKeyRestVector key_rests =
58         g_array_new(FALSE, FALSE, sizeof(ChewingKeyRest));
59
60     pinyin_option_t options = PINYIN_CORRECT_ALL | USE_TONE | USE_RESPLIT_TABLE;
61
62     GError * error = NULL;
63     GOptionContext * context;
64
65     context = g_option_context_new("- test pinyin parser");
66     g_option_context_add_main_entries(context, entries, NULL);
67     if (!g_option_context_parse(context, &argc, &argv, &error)) {
68         g_print("option parsing failed:%s\n", error->message);
69         exit(EINVAL);
70     }
71
72     if (incomplete)
73         options |= PINYIN_INCOMPLETE | CHEWING_INCOMPLETE;
74
75     /* create the parser */
76     if (strcmp("fullpinyin", parsername) == 0) {
77         parser = new FullPinyinParser2();
78     } else if (strcmp("doublepinyin", parsername) == 0) {
79         parser = new DoublePinyinParser2();
80     } else if (strcmp("chewing", parsername) == 0) {
81         parser = new ChewingParser2();
82     }
83
84     if (!parser)
85         parser = new FullPinyinParser2();
86
87     char* linebuf = NULL; size_t size = 0; ssize_t read;
88     while( (read = getline(&linebuf, &size, stdin)) != -1 ){
89         if ( '\n' == linebuf[strlen(linebuf) - 1] ) {
90             linebuf[strlen(linebuf) - 1] = '\0';
91         }
92
93         if ( strcmp ( linebuf, "quit" ) == 0)
94             break;
95
96 #if 0
97         ChewingKey key;
98         bool success = parser->parse_one_key(options, key,
99                                              linebuf, strlen(linebuf));
100         if (success) {
101             gchar * pinyins = key.get_pinyin_string();
102             printf("pinyin:%s\n", pinyins);
103             g_free(pinyins);
104         }
105 #endif
106
107 #if 1
108         int len = 0;
109         guint32 start_time = record_time();
110         for ( size_t i = 0; i < bench_times; ++i)
111             len = parser->parse(options, keys, key_rests,
112                                 linebuf, strlen(linebuf));
113
114         print_time(start_time, bench_times);
115
116         printf("parsed %d chars, %d keys.\n", len, keys->len);
117
118         assert(keys->len == key_rests->len);
119
120         for (size_t i = 0; i < keys->len; ++i) {
121             ChewingKey * key =
122                 &g_array_index(keys, ChewingKey, i);
123             ChewingKeyRest * key_rest =
124                 &g_array_index(key_rests, ChewingKeyRest, i);
125
126             gchar * pinyins = key->get_pinyin_string();
127             printf("%s %d %d\t", pinyins,
128                    key_rest->m_raw_begin, key_rest->m_raw_end);
129             g_free(pinyins);
130         }
131         printf("\n");
132 #endif
133
134     }
135
136     if (linebuf)
137         free(linebuf);
138
139     delete parser;
140
141     g_array_free(key_rests, TRUE);
142     g_array_free(keys, TRUE);
143
144     return 0;
145 }