write phrase index logger
[platform/upstream/libpinyin.git] / src / storage / phrase_index.cpp
1 /* 
2  *  libpinyin
3  *  Library to deal with pinyin.
4  *  
5  *  Copyright (C) 2006-2007 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */
21
22 #include "phrase_index.h"
23
24 using namespace pinyin;
25
26 bool PhraseItem::set_n_pronunciation(guint8 n_prouns){
27     m_chunk.set_content(sizeof(guint8), &n_prouns, sizeof(guint8));
28     return true;
29 }
30
31 bool PhraseItem::get_nth_pronunciation(size_t index, PinyinKey * pinyin, guint32 & freq){
32     guint8 phrase_length = get_phrase_length();
33     table_offset_t offset = phrase_item_header + phrase_length * sizeof( utf16_t) + index * ( phrase_length * sizeof (PinyinKey) + sizeof(guint32));
34     bool retval = m_chunk.get_content(offset, pinyin, phrase_length * sizeof(PinyinKey));
35     if ( !retval )
36         return retval;
37     return m_chunk.get_content(offset + phrase_length * sizeof(PinyinKey), &freq , sizeof(guint32));
38 }
39
40 void PhraseItem::append_pronunciation(PinyinKey * pinyin, guint32 freq){
41     guint8 phrase_length = get_phrase_length();
42     set_n_pronunciation(get_n_pronunciation() + 1);
43     m_chunk.set_content(m_chunk.size(), pinyin, phrase_length * sizeof(PinyinKey));
44     m_chunk.set_content(m_chunk.size(), &freq, sizeof(guint32));
45 }
46
47 void PhraseItem::remove_nth_pronunciation(size_t index){
48     guint8 phrase_length = get_phrase_length();
49     set_n_pronunciation(get_n_pronunciation() - 1);
50     size_t offset = phrase_item_header + phrase_length * sizeof ( utf16_t ) + index * (phrase_length * sizeof (PinyinKey) + sizeof(guint32));
51     m_chunk.remove_content(offset, phrase_length * sizeof(PinyinKey) + sizeof(guint32));
52 }
53
54 bool PhraseItem::get_phrase_string(utf16_t * phrase){
55     guint8 phrase_length = get_phrase_length();
56     return m_chunk.get_content(phrase_item_header, phrase, phrase_length * sizeof(utf16_t));
57 }
58
59 bool PhraseItem::set_phrase_string(guint8 phrase_length, utf16_t * phrase){
60     m_chunk.set_content(0, &phrase_length, sizeof(guint8));
61     m_chunk.set_content(phrase_item_header, phrase, phrase_length * sizeof(utf16_t));
62     return true;
63 }
64
65 void PhraseItem::increase_pinyin_possibility(PinyinCustomSettings & custom,
66                                              PinyinKey * pinyin_keys,
67                                              gint32 delta){
68     guint8 phrase_length = get_phrase_length();
69     guint8 npron = get_n_pronunciation();
70     size_t offset = phrase_item_header + phrase_length * sizeof ( utf16_t );
71     char * buf_begin = (char *) m_chunk.begin();
72     guint32 total_freq = 0;
73     for ( int i = 0 ; i < npron ; ++i){
74         char * pinyin_begin = buf_begin + offset +
75             i * ( phrase_length * sizeof(PinyinKey) + sizeof(guint32) );
76         guint32 * freq = (guint32 *)(pinyin_begin + phrase_length * sizeof(PinyinKey));
77         total_freq += *freq;
78         if ( 0 == pinyin_compare_with_ambiguities(custom,
79                                                   (PinyinKey *)pinyin_begin,
80                                                   pinyin_keys,
81                                                   phrase_length)){
82             //protect against total_freq overflow.
83             if ( delta > 0 && total_freq > total_freq + delta )
84                 return;
85             *freq += delta;
86             total_freq += delta;
87         }
88     }
89 }
90
91
92 guint32 SubPhraseIndex::get_phrase_index_total_freq(){
93     return m_total_freq;
94 }
95
96 int SubPhraseIndex::add_unigram_frequency(phrase_token_t token, guint32 delta){
97     table_offset_t offset;
98     guint32 freq;
99     bool result = m_phrase_index.get_content
100         ((token & PHRASE_MASK) 
101          * sizeof(table_offset_t), &offset, sizeof(table_offset_t));
102
103     if ( !result )
104         return ERROR_OUT_OF_RANGE;
105
106     if ( 0 == offset )
107     return ERROR_NO_ITEM;
108
109     result = m_phrase_content.get_content
110         (offset + sizeof(guint8) + sizeof(guint8), &freq, sizeof(guint32));
111
112     if ( !result )
113     return ERROR_FILE_CORRUPTION;
114
115     //protect total_freq overflow
116     if ( delta > 0 && m_total_freq > m_total_freq + delta )
117         return ERROR_INTEGER_OVERFLOW;
118
119     freq += delta;
120     m_total_freq += delta;
121     m_phrase_content.set_content(offset + sizeof(guint8) + sizeof(guint8), &freq, sizeof(guint32));
122
123     return ERROR_OK;
124 }
125
126 int SubPhraseIndex::get_phrase_item(phrase_token_t token, PhraseItem & item){
127     table_offset_t offset;
128     guint8 phrase_length;
129     guint8 n_prons;
130     
131     bool result = m_phrase_index.get_content
132         ((token & PHRASE_MASK) 
133          * sizeof(table_offset_t), &offset, sizeof(table_offset_t));
134
135     if ( !result )
136         return ERROR_OUT_OF_RANGE;
137
138     if ( 0 == offset )
139     return ERROR_NO_ITEM;
140
141     result = m_phrase_content.get_content(offset, &phrase_length, sizeof(guint8));
142     if ( !result ) 
143     return ERROR_FILE_CORRUPTION;
144     
145     result = m_phrase_content.get_content(offset+sizeof(guint8), &n_prons, sizeof(guint8));
146     if ( !result ) 
147         return ERROR_FILE_CORRUPTION;
148
149     size_t length = phrase_item_header + phrase_length * sizeof ( utf16_t ) + n_prons * ( phrase_length * sizeof (PinyinKey) + sizeof(guint32) );
150     item.m_chunk.set_chunk((char *)m_phrase_content.begin() + offset, length, NULL);
151     return ERROR_OK;
152 }
153
154 int SubPhraseIndex::add_phrase_item(phrase_token_t token, PhraseItem * item){
155     table_offset_t offset = m_phrase_content.size();
156     if ( 0 == offset )
157         offset = 8;
158     m_phrase_content.set_content(offset, item->m_chunk.begin(), item->m_chunk.size());
159     m_phrase_index.set_content((token & PHRASE_MASK) 
160                                * sizeof(table_offset_t), &offset, sizeof(table_offset_t));
161     m_total_freq += item->get_unigram_frequency();
162     return ERROR_OK;
163 }
164
165 int SubPhraseIndex::remove_phrase_item(phrase_token_t token, PhraseItem * & item){
166     PhraseItem old_item;
167
168     int result = get_phrase_item(token, old_item);
169     if (result != ERROR_OK)
170     return result;
171
172     item = new PhraseItem;
173     //implictly copy data from m_chunk_content.
174     item->m_chunk.set_content(0, (char *) old_item.m_chunk.begin() , old_item.m_chunk.size());
175
176     const table_offset_t zero_const = 0;
177     m_phrase_index.set_content((token & PHRASE_MASK)
178                                * sizeof(table_offset_t), &zero_const, sizeof(table_offset_t));
179     m_total_freq -= item->get_unigram_frequency();
180     return ERROR_OK;
181 }
182
183 bool FacadePhraseIndex::load(guint8 phrase_index, MemoryChunk * chunk){
184     SubPhraseIndex * & sub_phrases = m_sub_phrase_indices[phrase_index];
185     if ( !sub_phrases ){
186         sub_phrases = new SubPhraseIndex;
187     }
188     
189     bool retval = sub_phrases->load(chunk, 0, chunk->size());
190     if ( !retval )
191         return retval;
192     m_total_freq += sub_phrases->get_phrase_index_total_freq();
193     return retval;
194 }
195
196 bool FacadePhraseIndex::store(guint8 phrase_index, MemoryChunk * new_chunk){
197     table_offset_t end;
198     SubPhraseIndex * & sub_phrases = m_sub_phrase_indices[phrase_index];
199     if ( !sub_phrases )
200         return false;
201     
202     sub_phrases->store(new_chunk, 0, end);
203     return true;
204 }
205
206 bool FacadePhraseIndex::unload(guint8 phrase_index){
207     SubPhraseIndex * & sub_phrases = m_sub_phrase_indices[phrase_index];
208     if ( !sub_phrases )
209         return false;
210     m_total_freq -= sub_phrases->get_phrase_index_total_freq();
211     delete sub_phrases;
212     sub_phrases = NULL;
213     return true;
214 }
215
216 bool FacadePhraseIndex::diff(guint8 phrase_index, MemoryChunk * oldchunk,
217                              MemoryChunk * newlog){
218     SubPhraseIndex * & sub_phrases = m_sub_phrase_indices[phrase_index];
219     if ( !sub_phrases )
220         return false;
221
222     SubPhraseIndex old_sub_phrases;
223     old_sub_phrases.load(oldchunk, 0, oldchunk->size());
224     PhraseIndexLogger logger;
225
226     bool retval = sub_phrases->diff(&old_sub_phrases, &logger);
227     logger.store(newlog);
228     return retval;
229 }
230
231 bool FacadePhraseIndex::merge(guint8 phrase_index, MemoryChunk * log){
232     SubPhraseIndex * & sub_phrases = m_sub_phrase_indices[phrase_index];
233     if ( !sub_phrases )
234         return false;
235
236     PhraseIndexLogger logger;
237     logger.load(log);
238
239     return sub_phrases->merge(&logger);
240 }
241
242 bool SubPhraseIndex::load(MemoryChunk * chunk, 
243                           table_offset_t offset, table_offset_t end){
244     //save the memory chunk
245     if ( m_chunk ){
246         delete m_chunk;
247         m_chunk = NULL;
248     }
249     m_chunk = chunk;
250     
251     char * buf_begin = (char *)chunk->begin();
252     chunk->get_content(offset, &m_total_freq, sizeof(guint32));
253     offset += sizeof(guint32);
254     table_offset_t index_one, index_two, index_three;
255     chunk->get_content(offset, &index_one, sizeof(table_offset_t));
256     offset += sizeof(table_offset_t);
257     chunk->get_content(offset, &index_two, sizeof(table_offset_t));
258     offset += sizeof(table_offset_t);
259     chunk->get_content(offset, &index_three, sizeof(table_offset_t));
260     offset += sizeof(table_offset_t);
261     g_return_val_if_fail(*(buf_begin + offset) == c_separate, FALSE);
262     g_return_val_if_fail(*(buf_begin + index_two - 1) == c_separate, FALSE);
263     g_return_val_if_fail(*(buf_begin + index_three - 1) == c_separate, FALSE);
264     m_phrase_index.set_chunk(buf_begin + index_one, 
265                              index_two - 1 - index_one, NULL);
266     m_phrase_content.set_chunk(buf_begin + index_two, 
267                                  index_three - 1 - index_two, NULL);
268     g_return_val_if_fail( index_three <= end, FALSE);
269     return true;
270 }
271
272 bool SubPhraseIndex::store(MemoryChunk * new_chunk, 
273                            table_offset_t offset, table_offset_t& end){
274     new_chunk->set_content(offset, &m_total_freq, sizeof(guint32));
275     table_offset_t index = offset + sizeof(guint32);
276         
277     offset = index + sizeof(table_offset_t) * 3 ;
278     new_chunk->set_content(offset, &c_separate, sizeof(char));
279     offset += sizeof(char);
280     
281     new_chunk->set_content(index, &offset, sizeof(table_offset_t));
282     index += sizeof(table_offset_t);
283     new_chunk->set_content(offset, m_phrase_index.begin(), m_phrase_index.size());
284     offset += m_phrase_index.size();
285     new_chunk->set_content(offset, &c_separate, sizeof(char));
286     offset += sizeof(char);
287
288     new_chunk->set_content(index, &offset, sizeof(table_offset_t));
289     index += sizeof(table_offset_t);
290     
291     new_chunk->set_content(offset, m_phrase_content.begin(), m_phrase_content.size());
292     offset += m_phrase_content.size();
293     new_chunk->set_content(offset, &c_separate, sizeof(char));
294     offset += sizeof(char);
295     new_chunk->set_content(index, &offset, sizeof(table_offset_t));
296     return true;
297 }
298
299 bool SubPhraseIndex::diff(SubPhraseIndex * oldone, PhraseIndexLogger * logger){
300     PhraseIndexRange oldrange, currange, range;
301     oldone->get_range(oldrange); get_range(currange);
302     range.m_range_begin = std_lite::min(oldrange.m_range_begin,
303                                         currange.m_range_begin);
304     range.m_range_end = std_lite::max(oldrange.m_range_end,
305                                      currange.m_range_end);
306     PhraseItem olditem, newitem;
307
308     for (phrase_token_t token = range.m_range_begin;
309          token < range.m_range_end; ++token ){
310         bool oldretval = ERROR_OK == oldone->get_phrase_item(token, olditem);
311         bool newretval = ERROR_OK == get_phrase_item(token, newitem);
312
313         if ( oldretval ){
314             if ( newretval ) { /* compare phrase item. */
315                 if ( olditem == newitem )
316                     continue;
317                 logger->append_record(LOG_MODIFY_RECORD, token,
318                                       &(olditem.m_chunk), &(newitem.m_chunk));
319             } else { /* remove phrase item. */
320                 logger->append_record(LOG_REMOVE_RECORD, token,
321                                       &(olditem.m_chunk), NULL);
322             }
323         } else {
324             if ( newretval ){ /* add phrase item. */
325                 logger->append_record(LOG_ADD_RECORD, token,
326                                       NULL, &(newitem.m_chunk));
327             } else { /* both empty. */
328                     /* do nothing. */
329             }
330         }
331     }
332
333     return true;
334 }
335
336 bool SubPhraseIndex::merge(PhraseIndexLogger * logger){
337     LOG_TYPE log_type; phrase_token_t token;
338     MemoryChunk oldchunk, newchunk;
339     PhraseItem olditem, newitem, item, * tmpitem;
340
341     while(logger->has_next_record()){
342         logger->next_record(log_type, token, &oldchunk, &newchunk);
343
344         switch(log_type){
345         case LOG_ADD_RECORD:{
346             assert( 0 == oldchunk.size() );
347             newitem.m_chunk.set_chunk(newchunk.begin(), newchunk.size(),
348                                       NULL);
349             add_phrase_item(token, &newitem);
350             break;
351         }
352         case LOG_REMOVE_RECORD:{
353             assert( 0 == newchunk.size() );
354             tmpitem = NULL;
355             remove_phrase_item(token, tmpitem);
356
357             olditem.m_chunk.set_chunk(oldchunk.begin(), oldchunk.size(),
358                                    NULL);
359             if (olditem != *tmpitem)
360                 return false;
361             delete tmpitem;
362
363             break;
364         }
365         case LOG_MODIFY_RECORD:{
366             get_phrase_item(token, item);
367             olditem.m_chunk.set_chunk(oldchunk.begin(), oldchunk.size(),
368                                       NULL);
369             newitem.m_chunk.set_chunk(newchunk.begin(), newchunk.size(),
370                                       NULL);
371             if (item != olditem)
372                 return false;
373
374             if (newchunk.size() > item.m_chunk.size() ){ /* increase size. */
375                 tmpitem = NULL;
376                 remove_phrase_item(token, tmpitem);
377                 assert(olditem == *tmpitem);
378                 add_phrase_item(token, &newitem);
379                 delete tmpitem;
380             } else { /* in place editing. */
381                 /* newchunk.size() <= item.m_chunk.size() */
382                 /* Hack here: we assume the behaviour of get_phrase_item
383                  * point to the actual data positon, so changes to item
384                  * will be saved in SubPhraseIndex immediately.
385                  */
386                 memmove(item.m_chunk.begin(), newchunk.begin(),
387                         newchunk.size());
388             }
389             break;
390         }
391         default:
392             assert(false);
393         }
394     }
395 }
396
397 bool FacadePhraseIndex::load_text(guint8 phrase_index, FILE * infile){
398     SubPhraseIndex * & sub_phrases = m_sub_phrase_indices[phrase_index];
399     if ( !sub_phrases ){
400         sub_phrases = new SubPhraseIndex;
401     }
402
403     char pinyin[256];
404     char phrase[256];
405     phrase_token_t token;
406     size_t freq;
407     PhraseItem * item_ptr = new PhraseItem;
408     phrase_token_t cur_token = 0;
409     while ( !feof(infile)){
410         fscanf(infile, "%s", pinyin);
411         fscanf(infile, "%s", phrase);
412         fscanf(infile, "%u", &token);
413         fscanf(infile, "%ld", &freq);
414         if ( feof(infile) )
415             break;
416
417         assert(PHRASE_INDEX_LIBRARY_INDEX(token) == phrase_index );
418
419         glong written;
420         utf16_t * phrase_utf16 = g_utf8_to_utf16(phrase, -1, NULL, 
421                                                &written, NULL);
422         
423         if ( 0 == cur_token ){
424             cur_token = token;
425             item_ptr->set_phrase_string(written, phrase_utf16);
426         }
427
428         if ( cur_token != token ){
429             add_phrase_item( cur_token, item_ptr);
430             delete item_ptr;
431             item_ptr = new PhraseItem;
432             cur_token = token;
433             item_ptr->set_phrase_string(written, phrase_utf16);
434         }
435
436         PinyinDefaultParser parser;
437         NullPinyinValidator validator;
438         PinyinKeyVector keys;
439         PinyinKeyPosVector poses;
440         
441         keys = g_array_new(FALSE, FALSE, sizeof( PinyinKey));
442         poses = g_array_new(FALSE, FALSE, sizeof( PinyinKeyPos));
443         parser.parse(validator, keys, poses, pinyin);
444         
445         assert ( item_ptr->get_phrase_length() == keys->len );
446         item_ptr->append_pronunciation((PinyinKey *)keys->data, freq);
447
448         g_array_free(keys, TRUE);
449         g_array_free(poses, TRUE);
450         g_free(phrase_utf16);
451     }
452
453     add_phrase_item( cur_token, item_ptr);
454     delete item_ptr;
455     m_total_freq += m_sub_phrase_indices[phrase_index]->get_phrase_index_total_freq();
456     return true;
457 }
458
459 int FacadePhraseIndex::get_range(guint8 phrase_index, /* out */ PhraseIndexRange & range){
460     SubPhraseIndex * sub_phrase = m_sub_phrase_indices[phrase_index];
461     if ( !sub_phrase )
462         return ERROR_NO_SUB_PHRASE_INDEX;
463
464     int result = sub_phrase->get_range(range);
465     if ( result )
466         return result;
467
468     range.m_range_begin = PHRASE_INDEX_MAKE_TOKEN(phrase_index, range.m_range_begin);
469     range.m_range_end = PHRASE_INDEX_MAKE_TOKEN(phrase_index, range.m_range_end);
470     return ERROR_OK;
471 }
472
473 int SubPhraseIndex::get_range(/* out */ PhraseIndexRange & range){
474     const table_offset_t * begin = (const table_offset_t *)m_phrase_index.begin();
475     const table_offset_t * end = (const table_offset_t *)m_phrase_index.end();
476
477     range.m_range_begin = 1; /* token starts with 1 in gen_pinyin_table. */
478     range.m_range_end = end - begin;
479
480     return ERROR_OK;
481 }
482
483 bool FacadePhraseIndex::compat(){
484     for ( size_t index = 0; index < PHRASE_INDEX_LIBRARY_COUNT; ++index) {
485         SubPhraseIndex * sub_phrase = m_sub_phrase_indices[index];
486         if ( !sub_phrase )
487             continue;
488
489         SubPhraseIndex * new_sub_phrase =  new SubPhraseIndex;
490         PhraseIndexRange range;
491         int result = sub_phrase->get_range(range);
492         if ( result != ERROR_OK ) {
493             delete new_sub_phrase;
494             continue;
495         }
496
497         PhraseItem item;
498         for ( phrase_token_t token = range.m_range_begin;
499               token < range.m_range_end;
500               ++token ) {
501             result = sub_phrase->get_phrase_item(token, item);
502             if ( result != ERROR_OK )
503                 continue;
504             new_sub_phrase->add_phrase_item(token, &item);
505         }
506
507         delete sub_phrase;
508         m_sub_phrase_indices[index] = new_sub_phrase;
509     }
510     return true;
511 }