1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
/*
* libpinyin
* Library to deal with pinyin.
*
* Copyright (C) 2011 Peng Wu <alexepico@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "pinyin_internal.h"
#include "k_mixture_model.h"
#include "utils_helper.h"
void print_help(){
printf("Usage: export_k_mixture_model [--k-mixture-model-file <FILENAME>]\n");
}
bool print_k_mixture_model_magic_header(FILE * output,
KMixtureModelBigram * bigram){
KMixtureModelMagicHeader magic_header;
if ( !bigram->get_magic_header(magic_header) ){
fprintf(stderr, "no magic header in k mixture model.\n");
exit(ENODATA);
}
fprintf(output, "\\data model \"k mixture model\" count %d N %d "
"total_freq %d\n", magic_header.m_WC, magic_header.m_N,
magic_header.m_total_freq);
return true;
}
bool print_k_mixture_model_array_headers(FILE * output,
KMixtureModelBigram * bigram,
FacadePhraseIndex * phrase_index){
fprintf(output, "\\1-gram\n");
GArray * items = g_array_new(FALSE, FALSE, sizeof(phrase_token_t));
bigram->get_all_items(items);
for (size_t i = 0; i < items->len; ++i) {
phrase_token_t * token = &g_array_index(items, phrase_token_t, i);
KMixtureModelArrayHeader array_header;
assert(bigram->get_array_header(*token, array_header));
char * phrase = taglib_token_to_string(phrase_index, *token);
if ( phrase )
fprintf(output, "\\item %s count %d freq %d\n",
phrase, array_header.m_WC, array_header.m_freq);
g_free(phrase);
}
return true;
}
bool print_k_mixture_model_array_items(FILE * output,
KMixtureModelBigram * bigram,
FacadePhraseIndex * phrase_index){
fprintf(output, "\\2-gram\n");
GArray * items = g_array_new(FALSE, FALSE, sizeof(phrase_token_t));
bigram->get_all_items(items);
for (size_t i = 0; i < items->len; ++i) {
phrase_token_t * token = &g_array_index(items, phrase_token_t, i);
KMixtureModelSingleGram * single_gram = NULL;
assert(bigram->load(*token, single_gram));
FlexibleBigramPhraseArray array = g_array_new
(FALSE, FALSE, sizeof(KMixtureModelArrayItemWithToken));
single_gram->retrieve_all(array);
for (size_t m = 0; m < array->len; ++m){
KMixtureModelArrayItemWithToken * item = &g_array_index(array, KMixtureModelArrayItemWithToken, m);
char * word1 = taglib_token_to_string(phrase_index, *token);
char * word2 = taglib_token_to_string(phrase_index, item->m_token);
if (word1 && word2)
fprintf(output, "\\item %s %s count %d T %d N_n_0 %d n_1 %d Mr %d\n",
word1, word2, item->m_item.m_WC, item->m_item.m_WC,
item->m_item.m_N_n_0, item->m_item.m_n_1,
item->m_item.m_Mr);
g_free(word1); g_free(word2);
}
g_array_free(array, TRUE);
delete single_gram;
}
g_array_free(items, TRUE);
return true;
}
bool end_data(FILE * output){
fprintf(output, "\\end\n");
return true;
}
int main(int argc, char * argv[]){
int i = 1;
const char * k_mixture_model_filename = NULL;
FILE * output = stdout;
while ( i < argc ){
if ( strcmp ("--help", argv[i]) == 0 ){
print_help();
exit(0);
} else if ( strcmp ("--k-mixture-model-file", argv[i]) == 0 ){
if ( ++i > argc ){
print_help();
exit(EINVAL);
}
k_mixture_model_filename = argv[i];
} else {
print_help();
exit(EINVAL);
}
++i;
}
FacadePhraseIndex phrase_index;
if (!load_phrase_index(&phrase_index))
exit(ENOENT);
KMixtureModelBigram bigram(K_MIXTURE_MODEL_MAGIC_NUMBER);
bigram.attach(k_mixture_model_filename, ATTACH_READONLY);
print_k_mixture_model_magic_header(output, &bigram);
print_k_mixture_model_array_headers(output, &bigram, &phrase_index);
print_k_mixture_model_array_items(output, &bigram, &phrase_index);
end_data(output);
return 0;
}
|