refine tests
[platform/upstream/libpinyin.git] / tests / storage / test_chewing_table.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 #include "timer.h"
23 #include <string.h>
24 #include "pinyin_internal.h"
25 #include "tests_helper.h"
26
27 size_t bench_times = 1000;
28
29 int main(int argc, char * argv[]) {
30     pinyin_option_t options = USE_TONE | PINYIN_INCOMPLETE;
31     ChewingLargeTable largetable(options);
32     FacadePhraseIndex phrase_index;
33
34     if (!load_phrase_table(&largetable, NULL, &phrase_index))
35         exit(ENOENT);
36
37     MemoryChunk * new_chunk = new MemoryChunk;
38     largetable.store(new_chunk);
39     largetable.load(new_chunk);
40
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)
45             break;
46
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));
51
52         parser.parse(options, keys, key_rests, linebuf, strlen(linebuf));
53         if (0 == keys->len) {
54             fprintf(stderr, "Invalid input.\n");
55             continue;
56         }
57
58         guint32 start = record_time();
59         PhraseIndexRanges ranges;
60         memset(ranges, 0, sizeof(PhraseIndexRanges));
61
62         phrase_index.prepare_ranges(ranges);
63
64         for (size_t i = 0; i < bench_times; ++i) {
65             largetable.search(keys->len, (ChewingKey *)keys->data, ranges);
66         }
67
68         phrase_index.clear_ranges(ranges);
69
70         print_time(start, bench_times);
71
72         largetable.search(keys->len, (ChewingKey *)keys->data, ranges);
73
74         for (size_t i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) {
75             GArray * & range = ranges[i];
76             if (!range)
77                 continue;
78
79             if (range->len)
80                 printf("range items number:%d\n", range->len);
81
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);
87
88                 PhraseItem item;
89                 for ( phrase_token_t token = onerange->m_range_begin;
90                       token != onerange->m_range_end; ++token){
91
92                     phrase_index.get_phrase_item( token, item);
93
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(),
99                           NULL, NULL, NULL);
100                     printf("%s\t", string);
101                     g_free(string);
102
103                     ChewingKey chewing_buffer[MAX_PHRASE_LENGTH];
104                     size_t npron = item.get_n_pronunciation();
105                     guint32 freq;
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();
109                              ++n){
110                             printf("%s'",
111                                    chewing_buffer[n].get_pinyin_string());
112                         }
113                         printf("\b\t%d\t", freq);
114                     }
115                 }
116                 printf("\n");
117             }
118             g_array_set_size(range, 0);
119         }
120
121         phrase_index.destroy_ranges(ranges);
122         g_array_free(keys, TRUE);
123         g_array_free(key_rests, TRUE);
124     }
125
126     if (linebuf)
127         free(linebuf);
128     return 0;
129 }