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.
26 #include "pinyin_internal.h"
27 #include "utils_helper.h"
29 /* export interpolation model as textual format */
31 bool gen_unigram(FILE * output, FacadePhraseIndex * phrase_index);
32 bool gen_bigram(FILE * output, FacadePhraseIndex * phrase_index, Bigram * bigram);
34 bool begin_data(FILE * output){
35 fprintf(output, "\\data model interpolation\n");
39 bool end_data(FILE * output){
40 fprintf(output, "\\end\n");
44 int main(int argc, char * argv[]){
45 FILE * output = stdout;
46 const char * bigram_filename = SYSTEM_BIGRAM;
48 SystemTableInfo system_table_info;
50 bool retval = system_table_info.load(SYSTEM_TABLE_INFO);
52 fprintf(stderr, "load table.conf failed.\n");
56 FacadePhraseIndex phrase_index;
58 const pinyin_table_info_t * phrase_files =
59 system_table_info.get_table_info();
61 if (!load_phrase_index(phrase_files, &phrase_index))
65 bigram.attach(bigram_filename, ATTACH_READONLY);
69 gen_unigram(output, &phrase_index);
70 gen_bigram(output, &phrase_index, &bigram);
76 bool gen_unigram(FILE * output, FacadePhraseIndex * phrase_index) {
77 fprintf(output, "\\1-gram\n");
78 for ( size_t i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; i++) {
80 PhraseIndexRange range;
81 int result = phrase_index->get_range(i, range);
82 if (ERROR_OK != result )
86 for (phrase_token_t token = range.m_range_begin;
87 token < range.m_range_end; token++) {
88 int result = phrase_index->get_phrase_item(token, item);
90 if ( result == ERROR_NO_ITEM )
92 assert( result == ERROR_OK);
94 size_t freq = item.get_unigram_frequency();
97 char * phrase = taglib_token_to_string(phrase_index, token);
99 fprintf(output, "\\item %d %s count %ld\n", token, phrase, freq);
107 bool gen_bigram(FILE * output, FacadePhraseIndex * phrase_index, Bigram * bigram){
108 fprintf(output, "\\2-gram\n");
110 /* Retrieve all user items. */
111 GArray * items = g_array_new(FALSE, FALSE, sizeof(phrase_token_t));
113 bigram->get_all_items(items);
117 for(size_t i = 0; i < items->len; i++){
118 phrase_token_t token = g_array_index(items, phrase_token_t, i);
119 SingleGram * single_gram = NULL;
120 bigram->load(token, single_gram);
122 BigramPhraseWithCountArray array = g_array_new(FALSE, FALSE, sizeof(BigramPhraseItemWithCount));
123 single_gram->retrieve_all(array);
124 for(size_t j = 0; j < array->len; j++) {
125 BigramPhraseItemWithCount * item = &g_array_index(array, BigramPhraseItemWithCount, j);
127 char * word1 = taglib_token_to_string(phrase_index, token);
128 char * word2 = taglib_token_to_string(phrase_index, item->m_token);
129 guint32 freq = item->m_count;
132 fprintf(output, "\\item %d %s %d %s count %d\n",
133 token, word1, item->m_token, word2, freq);
135 g_free(word1); g_free(word2);
138 g_array_free(array, TRUE);
141 g_array_free(items, TRUE);