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[]){
59 setlocale(LC_ALL, "");
60 /* deal with options. */
62 if ( strcmp ("--help", argv[i]) == 0 ){
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);
79 /* init phrase index */
80 FacadePhraseIndex phrase_index;
81 if (!load_phrase_index(&phrase_index))
86 system_bigram.attach("../../data/bigram.db", ATTACH_READONLY);
89 /* init phrase lookup */
90 PhraseLookup phrase_lookup(&phrase_table, &phrase_index,
91 &system_bigram, &user_bigram);
93 /* try one sentence */
94 char * linebuf = NULL;
97 while( (read = getline(&linebuf, &size, stdin)) != -1 ){
98 if ( '\n' == linebuf[strlen(linebuf) - 1] ) {
99 linebuf[strlen(linebuf) - 1] = '\0';
102 if ( strcmp ( linebuf, "quit" ) == 0)
105 /* check non-ucs4 characters */
106 const glong num_of_chars = g_utf8_strlen(linebuf, -1);
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");
115 try_phrase_lookup(&phrase_lookup, sentence, len);