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.
25 #include "pinyin_internal.h"
26 #include "tests_helper.h"
29 printf("Usage: test_phrase_lookup\n");
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);
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 )
42 printf("%d:%d\t", i, *token);
46 phrase_lookup->convert_to_utf8(results, result_string);
48 printf("%s\n", result_string);
50 fprintf(stderr, "Error: Un-segmentable sentence encountered!\n");
51 g_array_free(results, TRUE);
52 g_free(result_string);
56 int main(int argc, char * argv[]){
58 setlocale(LC_ALL, "");
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);
66 /* init phrase index */
67 FacadePhraseIndex phrase_index;
68 if (!load_phrase_index(&phrase_index))
73 system_bigram.attach("../../data/bigram.db", ATTACH_READONLY);
76 /* init phrase lookup */
77 PhraseLookup phrase_lookup(&phrase_table, &phrase_index,
78 &system_bigram, &user_bigram);
80 /* try one sentence */
81 char * linebuf = NULL;
84 while( (read = getline(&linebuf, &size, stdin)) != -1 ){
85 if ( '\n' == linebuf[strlen(linebuf) - 1] ) {
86 linebuf[strlen(linebuf) - 1] = '\0';
89 if ( strcmp ( linebuf, "quit" ) == 0)
92 /* check non-ucs4 characters */
93 const glong num_of_chars = g_utf8_strlen(linebuf, -1);
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");
102 try_phrase_lookup(&phrase_lookup, sentence, len);