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 static const gchar * parsername = "";
33 static gboolean incomplete = FALSE;
35 static GOptionEntry entries[] =
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},
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"
49 size_t bench_times = 1000;
51 using namespace pinyin;
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));
60 pinyin_option_t options = PINYIN_CORRECT_ALL | USE_TONE | USE_RESPLIT_TABLE;
62 GError * error = NULL;
63 GOptionContext * context;
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);
73 options |= PINYIN_INCOMPLETE | CHEWING_INCOMPLETE;
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();
85 parser = new FullPinyinParser2();
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';
93 if ( strcmp ( linebuf, "quit" ) == 0)
98 bool success = parser->parse_one_key(options, key,
99 linebuf, strlen(linebuf));
101 gchar * pinyins = key.get_pinyin_string();
102 printf("pinyin:%s\n", pinyins);
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));
114 print_time(start_time, bench_times);
116 printf("parsed %d chars, %d keys.\n", len, keys->len);
118 assert(keys->len == key_rests->len);
120 for (size_t i = 0; i < keys->len; ++i) {
122 &g_array_index(keys, ChewingKey, i);
123 ChewingKeyRest * key_rest =
124 &g_array_index(key_rests, ChewingKeyRest, i);
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);
141 g_array_free(key_rests, TRUE);
142 g_array_free(keys, TRUE);