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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
/*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <locale.h>
#include "pinyin.h"
#include "k_mixture_model.h"
void print_help(){
printf("Usage: merge_k_mixture_model [--result-file <RESULT_FILENAME>]\n");
printf(" {<SOURCE_FILENAME>}+\n");
}
static bool merge_two_phrase_array( /* in */ FlexibleBigramPhraseArray first,
/* in */ FlexibleBigramPhraseArray second,
/* out */ FlexibleBigramPhraseArray & merged ){
merged = NULL;
/* avoid to do empty merge. */
assert( NULL != first && NULL != second && NULL != merged );
/* merge two arrays. */
guint first_index, second_index = first_index = 0;
KMixtureModelArrayItemWithToken * first_item,
* second_item = first_item = NULL;
while ( first_index < first->len && second_index < second->len ){
first_item = &g_array_index(first, KMixtureModelArrayItemWithToken,
first_index);
second_item = &g_array_index(second, KMixtureModelArrayItemWithToken,
second_index);
if ( first_item->m_token > second_item->m_token ) {
g_array_append_val(merged, *second_item);
second_index ++;
} else if ( first_item->m_token < second_item->m_token ) {
g_array_append_val(merged, *first_item);
first_index ++;
} else /* first_item->m_token == second_item->m_token */ {
KMixtureModelArrayItemWithToken merged_item;
memset(&merged_item, 0, sizeof(KMixtureModelArrayItemWithToken));
merged_item.m_token = first_item->m_token;/* same as second_item */
merged_item.m_item.m_WC = first_item->m_item.m_WC +
second_item->m_item.m_WC;
/* merged_item.m_item.m_T = first_item->m_item.m_T +
second_item->m_item.m_T; */
merged_item.m_item.m_N_n_0 = first_item->m_item.m_N_n_0 +
second_item->m_item.m_N_n_0;
merged_item.m_item.m_n_1 = first_item->m_item.m_n_1 +
second_item->m_item.m_n_1;
merged_item.m_item.m_Mr = std_lite::max(first_item->m_item.m_Mr,
second_item->m_item.m_Mr);
g_array_append_val(merged, merged_item);
first_index ++; second_index ++;
}
}
/* add remained items. */
while ( first_index < first->len ){
first_item = &g_array_index(first, KMixtureModelArrayItemWithToken,
first_index);
g_array_append_val(merged, *first_item);
first_index++;
}
while ( second_index < second->len ){
second_item = &g_array_index(second, KMixtureModelArrayItemWithToken,
second_index);
g_array_append_val(merged, *second_item);
second_index++;
}
return true;
}
static bool merge_magic_header( /* in & out */ KMixtureModelBigram * target,
/* in */ KMixtureModelBigram * new_one ){
KMixtureModelMagicHeader target_magic_header;
KMixtureModelMagicHeader new_magic_header;
KMixtureModelMagicHeader merged_magic_header;
memset(&merged_magic_header, 0, sizeof(KMixtureModelMagicHeader));
assert(target->get_magic_header(target_magic_header));
assert(new_one->get_magic_header(new_magic_header));
if ( target_magic_header.m_WC + new_magic_header.m_WC <
std_lite::max( target_magic_header.m_WC, new_magic_header.m_WC ) ){
fprintf(stderr, "the m_WC integer in magic header overflows.\n");
return false;
}
merged_magic_header.m_WC = target_magic_header.m_WC +
new_magic_header.m_WC;
merged_magic_header.m_N = target_magic_header.m_N +
new_magic_header.m_N;
assert(target->set_magic_header(merged_magic_header));
return true;
}
static bool merge_array_headers( /* in & out */ KMixtureModelBigram * target,
/* in */ KMixtureModelBigram * new_one){
GArray * new_items = g_array_new(FALSE, FALSE, sizeof(phrase_token_t));
new_one->get_all_items(new_items);
for ( size_t i = 0; i < new_items->len; ++i ){
phrase_token_t * token = &g_array_index(new_items, phrase_token_t, i);
KMixtureModelArrayHeader target_array_header;
KMixtureModelArrayHeader new_array_header;
KMixtureModelArrayHeader merged_array_header;
memset(&merged_array_header, 0, sizeof(KMixtureModelArrayHeader));
assert(new_one->get_array_header(*token, new_array_header));
bool exists_in_target = target->get_array_header(*token,
target_array_header);
if ( !exists_in_target ){
target->set_array_header(*token, new_array_header);
continue;
}
merged_array_header.m_WC = target_array_header.m_WC +
new_array_header.m_WC;
assert(target->set_array_header(*token, merged_array_header));
}
return true;
}
static bool merge_array_items( /* in & out */ KMixtureModelBigram * target,
/* in */ KMixtureModelBigram * new_one ){
GArray * new_items = g_array_new(FALSE, FALSE, sizeof(phrase_token_t));
new_one->get_all_items(new_items);
for ( size_t i = 0; i < new_items->len; ++i ){
phrase_token_t * token = &g_array_index(new_items, phrase_token_t, i);
KMixtureModelSingleGram * target_single_gram = NULL;
KMixtureModelSingleGram * new_single_gram = NULL;
assert(new_one->load(*token, new_single_gram));
bool exists_in_target = target->load(*token, target_single_gram);
if ( !exists_in_target ){
target->store(*token, new_single_gram);
delete new_single_gram;
continue;
}
assert(NULL != target_single_gram);
KMixtureModelSingleGram * merged_single_gram =
new KMixtureModelSingleGram;
FlexibleBigramPhraseArray target_array =
g_array_new(FALSE, FALSE, sizeof(KMixtureModelArrayItemWithToken));
target_single_gram->retrieve_all(target_array);
FlexibleBigramPhraseArray new_array =
g_array_new(FALSE, FALSE, sizeof(KMixtureModelArrayItemWithToken));
new_single_gram->retrieve_all(new_array);
FlexibleBigramPhraseArray merged_array =
g_array_new(FALSE, FALSE, sizeof(KMixtureModelArrayItemWithToken));
assert(merge_two_phrase_array(target_array, new_array, merged_array));
g_array_free(target_array, TRUE);
g_array_free(new_array, TRUE);
delete target_single_gram; delete new_single_gram;
for ( size_t m = 0; m < merged_array->len; ++m ){
KMixtureModelArrayItemWithToken * item =
&g_array_index(merged_array,
KMixtureModelArrayItemWithToken, m);
merged_single_gram->insert_array_item(item->m_token, item->m_item);
}
assert(target->store(*token, merged_single_gram));
delete merged_single_gram;
g_array_free(merged_array, TRUE);
}
g_array_free(new_items, TRUE);
return true;
}
bool merge_two_k_mixture_model( /* in & out */ KMixtureModelBigram * target,
/* in */ KMixtureModelBigram * new_one ){
assert(NULL != target);
assert(NULL != new_one);
return merge_array_items(target, new_one) &&
merge_array_headers(target, new_one) &&
merge_magic_header(target, new_one);
}
int main(int argc, char * argv[]){
int i = 1;
const char * result_filename = NULL;
setlocale(LC_ALL, "");
while (i < argc) {
if ( strcmp("--help", argv[i]) == 0 ){
print_help();
exit(0);
} else if ( strcmp("--result-file", argv[i]) == 0 ){
if ( ++i >= argc ){
print_help();
exit(EINVAL);
}
result_filename = argv[i];
} else {
break;
}
++i;
}
KMixtureModelBigram target(K_MIXTURE_MODEL_MAGIC_NUMBER);
target.attach(result_filename, ATTACH_READWRITE|ATTACH_CREATE);
while ( i < argc ){
const char * new_filename = argv[i];
KMixtureModelBigram new_one(K_MIXTURE_MODEL_MAGIC_NUMBER);
new_one.attach(new_filename, ATTACH_READONLY);
merge_two_k_mixture_model(&target, &new_one);
++i;
}
return 0;
}
|