update utils/segment
[platform/upstream/libpinyin.git] / utils / storage / export_interpolation.cpp
1 /* 
2  *  libpinyin
3  *  Library to deal with pinyin.
4  *  
5  *  Copyright (C) 2010 Peng Wu
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 <assert.h>
25 #include <glib.h>
26 #include "pinyin_internal.h"
27 #include "utils_helper.h"
28
29 /* export interpolation model as textual format */
30
31 bool gen_unigram(FILE * output, FacadePhraseIndex * phrase_index);
32 bool gen_bigram(FILE * output, FacadePhraseIndex * phrase_index, Bigram * bigram);
33
34 bool begin_data(FILE * output){
35     fprintf(output, "\\data model interpolation\n");
36     return true;
37 }
38
39 bool end_data(FILE * output){
40     fprintf(output, "\\end\n");
41     return true;
42 }
43
44 int main(int argc, char * argv[]){
45     FILE * output = stdout;
46     const char * bigram_filename = SYSTEM_BIGRAM;
47
48     SystemTableInfo system_table_info;
49
50     bool retval = system_table_info.load(SYSTEM_TABLE_INFO);
51     if (!retval) {
52         fprintf(stderr, "load table.conf failed.\n");
53         exit(ENOENT);
54     }
55
56     FacadePhraseIndex phrase_index;
57
58     const pinyin_table_info_t * phrase_files =
59         system_table_info.get_table_info();
60
61     if (!load_phrase_index(phrase_files, &phrase_index))
62         exit(ENOENT);
63
64     Bigram bigram;
65     bigram.attach(bigram_filename, ATTACH_READONLY);
66
67     begin_data(output);
68
69     gen_unigram(output, &phrase_index);
70     gen_bigram(output, &phrase_index, &bigram);
71
72     end_data(output);
73     return 0;
74 }
75
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++) {
79
80         PhraseIndexRange range;
81         int result = phrase_index->get_range(i, range);
82         if (ERROR_OK != result )
83             continue;
84
85         PhraseItem item;
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);
89
90             if ( result == ERROR_NO_ITEM )
91                 continue;
92             assert( result == ERROR_OK);
93
94             size_t freq = item.get_unigram_frequency();
95             if ( 0 == freq )
96                 continue;
97             char * phrase = taglib_token_to_string(phrase_index, token);
98             if ( phrase )
99                 fprintf(output, "\\item %d %s count %ld\n", token, phrase, freq);
100
101             g_free(phrase);
102         }
103     }
104     return true;
105 }
106
107 bool gen_bigram(FILE * output, FacadePhraseIndex * phrase_index, Bigram * bigram){
108     fprintf(output, "\\2-gram\n");
109
110     /* Retrieve all user items. */
111     GArray * items = g_array_new(FALSE, FALSE, sizeof(phrase_token_t));
112
113     bigram->get_all_items(items);
114
115     PhraseItem item;
116
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);
121
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);
126
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;
130
131             if ( word1 && word2)
132                 fprintf(output, "\\item %d %s %d %s count %d\n",
133                         token, word1, item->m_token, word2, freq);
134
135             g_free(word1); g_free(word2);
136         }
137
138         g_array_free(array, TRUE);
139     }
140
141     g_array_free(items, TRUE);
142     return true;
143 }