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.
24 #include "pinyin_internal.h"
25 #include "tests_helper.h"
27 size_t bench_times = 1000;
29 int main(int argc, char * argv[]) {
30 pinyin_option_t options = USE_TONE | PINYIN_INCOMPLETE;
31 ChewingLargeTable largetable(options);
32 FacadePhraseIndex phrase_index;
34 if (!load_phrase_table(&largetable, NULL, &phrase_index))
37 MemoryChunk * new_chunk = new MemoryChunk;
38 largetable.store(new_chunk);
39 largetable.load(new_chunk);
41 char* linebuf = NULL; size_t size = 0;
42 while( getline(&linebuf, &size, stdin) ){
43 linebuf[strlen(linebuf)-1] = '\0';
44 if ( strcmp ( linebuf, "quit" ) == 0)
47 FullPinyinParser2 parser;
48 ChewingKeyVector keys = g_array_new(FALSE, FALSE, sizeof(ChewingKey));
49 ChewingKeyRestVector key_rests =
50 g_array_new(FALSE, FALSE, sizeof(ChewingKeyRest));
52 parser.parse(options, keys, key_rests, linebuf, strlen(linebuf));
54 fprintf(stderr, "Invalid input.\n");
58 guint32 start = record_time();
59 PhraseIndexRanges ranges;
60 memset(ranges, 0, sizeof(PhraseIndexRanges));
62 phrase_index.prepare_ranges(ranges);
64 for (size_t i = 0; i < bench_times; ++i) {
65 largetable.search(keys->len, (ChewingKey *)keys->data, ranges);
68 phrase_index.clear_ranges(ranges);
70 print_time(start, bench_times);
72 largetable.search(keys->len, (ChewingKey *)keys->data, ranges);
74 for (size_t i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) {
75 GArray * & range = ranges[i];
80 printf("range items number:%d\n", range->len);
82 for (size_t k = 0; k < range->len; ++k) {
83 PhraseIndexRange * onerange =
84 &g_array_index(range, PhraseIndexRange, k);
85 printf("start:%d\tend:%d\n", onerange->m_range_begin,
86 onerange->m_range_end);
89 for ( phrase_token_t token = onerange->m_range_begin;
90 token != onerange->m_range_end; ++token){
92 phrase_index.get_phrase_item( token, item);
94 /* get phrase string */
95 ucs4_t buffer[MAX_PHRASE_LENGTH + 1];
96 item.get_phrase_string(buffer);
97 char * string = g_ucs4_to_utf8
98 ( buffer, item.get_phrase_length(),
100 printf("%s\t", string);
103 ChewingKey chewing_buffer[MAX_PHRASE_LENGTH];
104 size_t npron = item.get_n_pronunciation();
106 for (size_t m = 0; m < npron; ++m){
107 item.get_nth_pronunciation(m, chewing_buffer, freq);
108 for (size_t n = 0; n < item.get_phrase_length();
111 chewing_buffer[n].get_pinyin_string());
113 printf("\b\t%d\t", freq);
118 g_array_set_size(range, 0);
121 phrase_index.destroy_ranges(ranges);
122 g_array_free(keys, TRUE);
123 g_array_free(key_rests, TRUE);