3 * Library to deal with pinyin.
5 * Copyright (C) 2011 Peng Wu <alexepico@gmail.com>
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.
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.
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.
29 #include "pinyin_parser2.h"
32 size_t bench_times = 1000;
34 using namespace pinyin;
36 static const char * help_msg =
38 " test-parser -p <parser> [-s <scheme>] [options].\n\n"
39 " -p <parser> fullpinyin/doublepinyin/chewing.\n"
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"
45 " -i Use incomplete pinyin.\n"
46 " -h print this help msg.\n"
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;
63 if (strcmp("-h", argv[i]) == 0) {
66 } else if (strcmp("-i", argv[i]) == 0) {
67 options |= PINYIN_INCOMPLETE | CHEWING_INCOMPLETE;
68 } else if (strcmp("-p", argv[i]) == 0) {
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();
92 parser = new FullPinyinParser2();
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';
100 if ( strcmp ( linebuf, "quit" ) == 0)
104 ChewingKey key; ChewingKeyRest key_rest;
105 bool success = parser->parse_one_key(options, key, key_rest,
106 linebuf, strlen(linebuf));
108 printf("pinyin:%s\t%d\t%d\n", key.get_pinyin_string(),
109 key_rest.m_raw_begin, key_rest.m_raw_end);
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));
119 print_time(start_time, bench_times);
121 printf("parsed %d chars, %d keys.\n", len, keys->len);
123 assert(keys->len == key_rests->len);
125 for (size_t i = 0; i < keys->len; ++i) {
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);