write test phrase lookup
[platform/upstream/libpinyin.git] / tests / lookup / test_phrase_lookup.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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */
21
22
23 #include <stdio.h>
24 #include <locale.h>
25 #include "pinyin.h"
26
27 PhraseLookup * g_phrase_lookup = NULL;
28
29 void print_help(){
30     printf("Usage: test_phrase_lookup\n");
31 }
32
33 bool try_phrase_lookup(utf16_t * utf16, glong utf16_len){
34     char * result_string = NULL;
35     MatchResults results = g_array_new(FALSE, FALSE, sizeof(phrase_token_t));
36     g_phrase_lookup->get_best_match(utf16_len, utf16, results);
37 #if 0
38     for ( size_t i = 0; i < results->len; ++i) {
39         phrase_token_t * token = &g_array_index(results, phrase_token_t, i);
40         if ( *token == null_token )
41             continue;
42         printf("%d:%d\t", i, *token);
43     }
44     printf("\n");
45 #endif
46     g_phrase_lookup->convert_to_utf8(results, "\n", result_string);
47     if (result_string)
48         printf("%s\n", result_string);
49     else
50         fprintf(stderr, "Error: Un-segmentable sentence encountered!\n");
51     g_array_free(results, TRUE);
52     g_free(result_string);
53     return true;
54 }
55
56 int main(int argc, char * argv[]){
57     int i = 1;
58
59     setlocale(LC_ALL, "");
60     //deal with options.
61     while ( i < argc ){
62         if ( strcmp ("--help", argv[i]) == 0 ){
63             print_help();
64             exit(0);
65         } else {
66             print_help();
67             exit(EINVAL);
68         }
69         ++i;
70     }
71
72
73     //init phrase table
74     PhraseLargeTable phrase_table;
75     MemoryChunk * chunk = new MemoryChunk;
76     chunk->load("../../data/phrase_index.bin");
77     phrase_table.load(chunk);
78
79     //init phrase index
80     FacadePhraseIndex phrase_index;
81     chunk = new MemoryChunk;
82     chunk->load("../../data/gb_char.bin");
83     phrase_index.load(1, chunk);
84     chunk = new MemoryChunk;
85     chunk->load("../../data/gbk_char.bin");
86     phrase_index.load(2, chunk);
87
88     //init bi-gram
89     Bigram system_bigram;
90     system_bigram.attach("../../data/bigram.db", ATTACH_READONLY);
91     Bigram user_bigram;
92
93     //init phrase lookup
94     g_phrase_lookup = new PhraseLookup(&phrase_table, &phrase_index,
95                                        &system_bigram, &user_bigram);
96
97     //try one sentence
98     char * linebuf = NULL;
99     size_t size = 0;
100     ssize_t read;
101     while( (read = getline(&linebuf, &size, stdin)) != -1 ){
102         if ( '\n' == linebuf[strlen(linebuf) - 1] ) {
103             linebuf[strlen(linebuf) - 1] = '\0';
104         }
105
106         if ( strcmp ( linebuf, "quit" ) == 0)
107             break;
108
109         //check non-ucs2 characters
110         const glong num_of_chars = g_utf8_strlen(linebuf, -1);
111         glong len = 0;
112         utf16_t * sentence = g_utf8_to_utf16(linebuf, -1, NULL, &len, NULL);
113         if ( len != num_of_chars ) {
114             fprintf(stderr, "non-ucs2 characters are not accepted.\n");
115             g_free(sentence);
116             continue;
117         }
118
119         try_phrase_lookup(sentence, len);
120         g_free(sentence);
121     }
122
123     delete g_phrase_lookup;
124     free(linebuf);
125     return 0;
126 }