3 * Library to deal with pinyin.
5 * Copyright (C) 2010 Peng Wu
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 "utils_helper.h"
29 static const gchar * table_dir = ".";
31 static GOptionEntry entries[] =
33 {"table-dir", 0, 0, G_OPTION_ARG_FILENAME, &table_dir, "table directory", NULL},
47 static int line_type = 0;
48 static GPtrArray * values = NULL;
49 static GHashTable * required = NULL;
50 /* variables for line buffer. */
51 static char * linebuf = NULL;
52 static size_t len = 0;
54 bool parse_headline();
56 bool parse_unigram(FILE * input, PhraseLargeTable2 * phrase_table,
57 FacadePhraseIndex * phrase_index);
59 bool parse_bigram(FILE * input, PhraseLargeTable2 * phrase_table,
60 FacadePhraseIndex * phrase_index,
63 static ssize_t my_getline(FILE * input){
64 ssize_t result = getline(&linebuf, &len, input);
68 if ( '\n' == linebuf[strlen(linebuf) - 1] ) {
69 linebuf[strlen(linebuf) - 1] = '\0';
74 bool parse_headline(){
75 /* enter "\data" line */
76 assert(taglib_add_tag(BEGIN_LINE, "\\data", 0, "model", ""));
78 /* read "\data" line */
79 if ( !taglib_read(linebuf, line_type, values, required) ) {
80 fprintf(stderr, "error: interpolation model expected.\n");
84 assert(line_type == BEGIN_LINE);
86 TAGLIB_GET_TAGVALUE(const char *, model, (const char *));
87 if ( !( strcmp("interpolation", model) == 0 ) ) {
88 fprintf(stderr, "error: interpolation model expected.\n");
94 bool parse_body(FILE * input, PhraseLargeTable2 * phrase_table,
95 FacadePhraseIndex * phrase_index,
99 assert(taglib_add_tag(END_LINE, "\\end", 0, "", ""));
100 assert(taglib_add_tag(GRAM_1_LINE, "\\1-gram", 0, "", ""));
101 assert(taglib_add_tag(GRAM_2_LINE, "\\2-gram", 0, "", ""));
105 assert(taglib_read(linebuf, line_type, values, required));
111 parse_unigram(input, phrase_table, phrase_index);
115 parse_bigram(input, phrase_table, phrase_index, bigram);
120 } while (my_getline(input) != -1) ;
127 bool parse_unigram(FILE * input, PhraseLargeTable2 * phrase_table,
128 FacadePhraseIndex * phrase_index){
131 assert(taglib_add_tag(GRAM_1_ITEM_LINE, "\\item", 2, "count", ""));
134 assert(taglib_read(linebuf, line_type, values, required));
136 case GRAM_1_ITEM_LINE:{
137 /* handle \item in \1-gram */
138 TAGLIB_GET_TOKEN(token, 0);
139 TAGLIB_GET_PHRASE_STRING(word, 1);
140 assert(taglib_validate_token_with_string
141 (phrase_index, token, word));
143 TAGLIB_GET_TAGVALUE(glong, count, atol);
144 phrase_index->add_unigram_frequency(token, count);
154 } while (my_getline(input) != -1);
161 bool parse_bigram(FILE * input, PhraseLargeTable2 * phrase_table,
162 FacadePhraseIndex * phrase_index,
166 assert(taglib_add_tag(GRAM_2_ITEM_LINE, "\\item", 4, "count", ""));
168 phrase_token_t last_token = 0; SingleGram * last_single_gram = NULL;
170 assert(taglib_read(linebuf, line_type, values, required));
172 case GRAM_2_ITEM_LINE:{
173 /* handle \item in \2-gram */
175 TAGLIB_GET_TOKEN(token1, 0);
176 TAGLIB_GET_PHRASE_STRING(word1, 1);
177 assert(taglib_validate_token_with_string
178 (phrase_index, token1, word1));
180 TAGLIB_GET_TOKEN(token2, 2);
181 TAGLIB_GET_PHRASE_STRING(word2, 3);
182 assert(taglib_validate_token_with_string
183 (phrase_index, token2, word2));
185 TAGLIB_GET_TAGVALUE(glong, count, atol);
187 if ( last_token != token1 ) {
188 if ( last_token && last_single_gram ) {
189 bigram->store(last_token, last_single_gram);
190 delete last_single_gram;
193 last_single_gram = NULL;
195 SingleGram * single_gram = NULL;
196 bigram->load(token1, single_gram);
198 //create the new single gram
199 if ( single_gram == NULL )
200 single_gram = new SingleGram;
202 last_single_gram = single_gram;
205 guint32 total_freq = 0;
206 assert(last_single_gram->get_total_freq(total_freq));
207 assert(last_single_gram->insert_freq(token2, count));
209 assert(last_single_gram->set_total_freq(total_freq));
219 } while (my_getline(input) != -1);
222 if ( last_token && last_single_gram ) {
223 bigram->store(last_token, last_single_gram);
224 delete last_single_gram;
227 last_single_gram = NULL;
234 int main(int argc, char * argv[]){
235 FILE * input = stdin;
236 const char * bigram_filename = "bigram.db";
238 setlocale(LC_ALL, "");
240 GError * error = NULL;
241 GOptionContext * context;
243 context = g_option_context_new("- import interpolation model");
244 g_option_context_add_main_entries(context, entries, NULL);
245 if (!g_option_context_parse(context, &argc, &argv, &error)) {
246 g_print("option parsing failed:%s\n", error->message);
250 SystemTableInfo system_table_info;
252 gchar * filename = g_build_filename(table_dir, "table.conf", NULL);
253 bool retval = system_table_info.load(filename);
255 fprintf(stderr, "load table.conf failed.\n");
260 PhraseLargeTable2 phrase_table;
262 MemoryChunk * chunk = new MemoryChunk;
263 retval = chunk->load("phrase_index.bin");
265 fprintf(stderr, "open phrase_index.bin failed!\n");
268 phrase_table.load(chunk);
270 FacadePhraseIndex phrase_index;
272 const pinyin_table_info_t * phrase_files =
273 system_table_info.get_table_info();
275 if (!load_phrase_index(phrase_files, &phrase_index))
279 retval = bigram.attach(bigram_filename, ATTACH_CREATE|ATTACH_READWRITE);
281 fprintf(stderr, "open %s failed!\n", bigram_filename);
287 values = g_ptr_array_new();
288 required = g_hash_table_new(g_str_hash, g_str_equal);
290 /* read first line */
291 ssize_t result = my_getline(input);
292 if ( result == -1 ) {
293 fprintf(stderr, "empty file input.\n");
297 if (!parse_headline())
300 result = my_getline(input);
302 parse_body(input, &phrase_table, &phrase_index, &bigram);
306 if (!save_phrase_index(phrase_files, &phrase_index))