refine ChewingKeyRest
[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 size_t bench_times = 1000;
33
34 using namespace pinyin;
35
36 static const char * help_msg =
37     "Usage:\n"
38     "  test-parser -p <parser> [-s <scheme>] [options].\n\n"
39     "  -p <parser> fullpinyin/doublepinyin/chewing.\n"
40 #if 0
41     "  -s <scheme> specify scheme for doublepinyin/chewing.\n"
42     "     schemes for doublepinyin: zrm, ms, ziguang, abc, pyjj, xhe.\n"
43     "     schemes for chewing: standard, ibm, ginyieh, eten.\n"
44 #endif
45     "  -i          Use incomplete pinyin.\n"
46     "  -h          print this help msg.\n"
47     ;
48
49
50 void print_help(){
51     printf(help_msg);
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     pinyin_option_t options = PINYIN_CORRECT_ALL | USE_TONE | USE_RESPLIT_TABLE;
60
61     int i = 1;
62     while(i < argc) {
63         if (strcmp("-h", argv[i]) == 0) {
64             print_help();
65             exit(0);
66         } else if (strcmp("-i", argv[i]) == 0) {
67             options |= PINYIN_INCOMPLETE | CHEWING_INCOMPLETE;
68         } else if (strcmp("-p", argv[i]) == 0) {
69             if ( ++i >= argc ) {
70                 print_help();
71                 exit(EINVAL);
72             }
73             const char * name = argv[i];
74             if (strcmp("fullpinyin", name) == 0) {
75                 parser = new FullPinyinParser2();
76             } else if (strcmp("doublepinyin", name) == 0) {
77                 parser = new DoublePinyinParser2();
78             } else if (strcmp("chewing", name) == 0) {
79                 parser = new ChewingParser2();
80             } else {
81                 print_help();
82                 exit(EINVAL);
83             }
84         } else {
85             print_help();
86             exit(EINVAL);
87         }
88         ++i;
89     }
90
91     if (!parser)
92         parser = new FullPinyinParser2();
93
94     char* linebuf = NULL; size_t size = 0; ssize_t read;
95     while( (read = getline(&linebuf, &size, stdin)) != -1 ){
96         if ( '\n' == linebuf[strlen(linebuf) - 1] ) {
97             linebuf[strlen(linebuf) - 1] = '\0';
98         }
99
100         if ( strcmp ( linebuf, "quit" ) == 0)
101             break;
102
103 #if 0
104         ChewingKey key; ChewingKeyRest key_rest;
105         bool success = parser->parse_one_key(options, key, key_rest,
106                                              linebuf, strlen(linebuf));
107         if (success)
108             printf("pinyin:%s\t%d\t%d\n", key.get_pinyin_string(),
109                    key_rest.m_raw_begin, key_rest.m_raw_end);
110 #endif
111
112 #if 1
113         int len = 0;
114         guint32 start_time = record_time();
115         for ( size_t i = 0; i < bench_times; ++i)
116             len = parser->parse(options, keys, key_rests,
117                                 linebuf, strlen(linebuf));
118
119         print_time(start_time, bench_times);
120
121         printf("parsed %d chars, %d keys.\n", len, keys->len);
122
123         assert(keys->len == key_rests->len);
124
125         for (size_t i = 0; i < keys->len; ++i) {
126             ChewingKey * key =
127                 &g_array_index(keys, ChewingKey, i);
128             ChewingKeyRest * key_rest =
129                 &g_array_index(key_rests, ChewingKeyRest, i);
130             printf("%s %d %d\t", key->get_pinyin_string(),
131                    key_rest->m_raw_begin, key_rest->m_raw_end);
132         }
133         printf("\n");
134 #endif
135
136     }
137
138     if (linebuf)
139         free(linebuf);
140
141     return 0;
142 }