b4ee19c7a2b2d38aa7fd27dae233aa6b2692312a
[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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20  */
21
22
23 #include <stdio.h>
24 #include <locale.h>
25 #include "pinyin_internal.h"
26 #include "tests_helper.h"
27
28 void print_help(){
29     printf("Usage: test_phrase_lookup\n");
30 }
31
32 bool try_phrase_lookup(PhraseLookup * phrase_lookup,
33                        ucs4_t * ucs4_str, glong ucs4_len){
34     char * result_string = NULL;
35     MatchResults results = g_array_new(FALSE, FALSE, sizeof(phrase_token_t));
36     phrase_lookup->get_best_match(ucs4_len, ucs4_str, 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     phrase_lookup->convert_to_utf8(results, 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     FacadePhraseTable2 phrase_table;
75     MemoryChunk * chunk = new MemoryChunk;
76     chunk->load("../../data/phrase_index.bin");
77     phrase_table.load(chunk, NULL);
78
79     /* init phrase index */
80     FacadePhraseIndex phrase_index;
81     if (!load_phrase_index(&phrase_index))
82         exit(ENOENT);
83
84     /* init bi-gram */
85     Bigram system_bigram;
86     system_bigram.attach("../../data/bigram.db", ATTACH_READONLY);
87     Bigram user_bigram;
88
89     /* init phrase lookup */
90     PhraseLookup phrase_lookup(&phrase_table, &phrase_index,
91                                &system_bigram, &user_bigram);
92
93     /* try one sentence */
94     char * linebuf = NULL;
95     size_t size = 0;
96     ssize_t read;
97     while( (read = getline(&linebuf, &size, stdin)) != -1 ){
98         if ( '\n' == linebuf[strlen(linebuf) - 1] ) {
99             linebuf[strlen(linebuf) - 1] = '\0';
100         }
101
102         if ( strcmp ( linebuf, "quit" ) == 0)
103             break;
104
105         /* check non-ucs4 characters */
106         const glong num_of_chars = g_utf8_strlen(linebuf, -1);
107         glong len = 0;
108         ucs4_t * sentence = g_utf8_to_ucs4(linebuf, -1, NULL, &len, NULL);
109         if ( len != num_of_chars ) {
110             fprintf(stderr, "non-ucs4 characters are not accepted.\n");
111             g_free(sentence);
112             continue;
113         }
114
115         try_phrase_lookup(&phrase_lookup, sentence, len);
116         g_free(sentence);
117     }
118
119     free(linebuf);
120     return 0;
121 }