update test_phrase_lookup.cpp
[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
58     setlocale(LC_ALL, "");
59
60     /* init phrase table */
61     FacadePhraseTable2 phrase_table;
62     MemoryChunk * chunk = new MemoryChunk;
63     chunk->load("../../data/phrase_index.bin");
64     phrase_table.load(chunk, NULL);
65
66     /* init phrase index */
67     FacadePhraseIndex phrase_index;
68     if (!load_phrase_index(&phrase_index))
69         exit(ENOENT);
70
71     /* init bi-gram */
72     Bigram system_bigram;
73     system_bigram.attach("../../data/bigram.db", ATTACH_READONLY);
74     Bigram user_bigram;
75
76     /* init phrase lookup */
77     PhraseLookup phrase_lookup(&phrase_table, &phrase_index,
78                                &system_bigram, &user_bigram);
79
80     /* try one sentence */
81     char * linebuf = NULL;
82     size_t size = 0;
83     ssize_t read;
84     while( (read = getline(&linebuf, &size, stdin)) != -1 ){
85         if ( '\n' == linebuf[strlen(linebuf) - 1] ) {
86             linebuf[strlen(linebuf) - 1] = '\0';
87         }
88
89         if ( strcmp ( linebuf, "quit" ) == 0)
90             break;
91
92         /* check non-ucs4 characters */
93         const glong num_of_chars = g_utf8_strlen(linebuf, -1);
94         glong len = 0;
95         ucs4_t * sentence = g_utf8_to_ucs4(linebuf, -1, NULL, &len, NULL);
96         if ( len != num_of_chars ) {
97             fprintf(stderr, "non-ucs4 characters are not accepted.\n");
98             g_free(sentence);
99             continue;
100         }
101
102         try_phrase_lookup(&phrase_lookup, sentence, len);
103         g_free(sentence);
104     }
105
106     free(linebuf);
107     return 0;
108 }