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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
|
/*
* libpinyin
* Library to deal with pinyin.
*
* Copyright (C) 2006-2007 Peng Wu
*
* 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.
*/
#ifndef PHRASE_INDEX_H
#define PHRASE_INDEX_H
#include <stdio.h>
#include <glib.h>
#include "novel_types.h"
#include "chewing_key.h"
#include "pinyin_parser2.h"
#include "pinyin_phrase2.h"
#include "memory_chunk.h"
#include "phrase_index_logger.h"
/**
* Phrase Index File Format
*
* Indirect Index: Index by Token
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* + Phrase Offset + Phrase Offset + Phrase Offset + ...... +
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* Phrase Content:
* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* + Phrase Length + number of Pronunciations + Uni-gram Frequency+
* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* + Phrase String(UCS2) + n Pronunciations with Frequency +
* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/
namespace pinyin{
class PinyinLookup;
/* Store delta info by phrase index logger in user home directory.
*/
const size_t phrase_item_header = sizeof(guint8) + sizeof(guint8) + sizeof(guint32);
/**
* PhraseItem:
*
* The PhraseItem to access the items in phrase index.
*
*/
class PhraseItem{
friend class SubPhraseIndex;
private:
MemoryChunk m_chunk;
bool set_n_pronunciation(guint8 n_prouns);
public:
/**
* PhraseItem::PhraseItem:
*
* The constructor of the PhraseItem.
*
*/
PhraseItem(){
m_chunk.set_size(phrase_item_header);
memset(m_chunk.begin(), 0, m_chunk.size());
}
#if 0
PhraseItem(MemoryChunk & chunk){
m_chunk.set_content(0, chunk->begin(), chunk->size());
assert ( m_chunk.size() >= phrase_item_header);
}
#endif
/**
* PhraseItem::get_phrase_length:
* @returns: the length of this phrase item.
*
* Get the length of this phrase item.
*
*/
guint8 get_phrase_length(){
char * buf_begin = (char *)m_chunk.begin();
return (*(guint8 *)buf_begin);
}
/**
* PhraseItem::get_n_pronunciation:
* @returns: the number of the pronunciations.
*
* Get the number of the pronunciations.
*
*/
guint8 get_n_pronunciation(){
char * buf_begin = ( char *) m_chunk.begin();
return (*(guint8 *)(buf_begin + sizeof(guint8)));
}
/**
* PhraseItem::get_unigram_frequency:
* @returns: the uni-gram frequency of this phrase item.
*
* Get the uni-gram frequency of this phrase item.
*
*/
guint32 get_unigram_frequency(){
char * buf_begin = (char *)m_chunk.begin();
return (*(guint32 *)(buf_begin + sizeof(guint8) + sizeof(guint8)));
}
/**
* PhraseItem::get_pronunciation_possibility:
* @options: the pinyin options.
* @keys: the pronunciation keys.
* @returns: the possibility of this phrase item pronounces the pinyin.
*
* Get the possibility of this phrase item pronounces the pinyin.
*
*/
gfloat get_pronunciation_possibility(pinyin_option_t options,
ChewingKey * keys){
guint8 phrase_length = get_phrase_length();
guint8 npron = get_n_pronunciation();
size_t offset = phrase_item_header + phrase_length * sizeof (ucs4_t);
char * buf_begin = (char *)m_chunk.begin();
guint32 matched = 0, total_freq =0;
for ( int i = 0 ; i < npron ; ++i){
char * chewing_begin = buf_begin + offset +
i * (phrase_length * sizeof(ChewingKey) + sizeof(guint32));
guint32 * freq = (guint32 *)(chewing_begin +
phrase_length * sizeof(ChewingKey));
total_freq += *freq;
if ( 0 == pinyin_compare_with_ambiguities2
(options, keys,
(ChewingKey *)chewing_begin,phrase_length) ){
matched += *freq;
}
}
// use preprocessor to avoid zero freq, in gen_pinyin_table.
/*
if ( 0 == total_freq )
return 0.1;
*/
gfloat retval = matched / (gfloat) total_freq;
/*
if ( 0 == retval )
return 0.03;
*/
return retval;
}
/**
* PhraseItem::increase_pronunciation_possibility:
* @options: the pinyin options.
* @keys: the pronunciation keys.
* @delta: the delta to be added to the pronunciation keys.
*
* Add the delta to the pronunciation of the pronunciation keys.
*
*/
void increase_pronunciation_possibility(pinyin_option_t options,
ChewingKey * keys,
gint32 delta);
/**
* PhraseItem::get_phrase_string:
* @phrase: the ucs4 character buffer.
* @returns: whether the get operation is successful.
*
* Get the ucs4 characters of this phrase item.
*
*/
bool get_phrase_string(ucs4_t * phrase);
/**
* PhraseItem::set_phrase_string:
* @phrase_length: the ucs4 character length of this phrase item.
* @phrase: the ucs4 character buffer.
* @returns: whether the set operation is successful.
*
* Set the length and ucs4 characters of this phrase item.
*
*/
bool set_phrase_string(guint8 phrase_length, ucs4_t * phrase);
/**
* PhraseItem::get_nth_pronunciation:
* @index: the pronunciation index.
* @keys: the pronunciation keys.
* @freq: the frequency of the pronunciation.
* @returns: whether the get operation is successful.
*
* Get the nth pronunciation of this phrase item.
*
*/
bool get_nth_pronunciation(size_t index,
/* out */ ChewingKey * keys,
/* out */ guint32 & freq);
/**
* PhraseItem::append_pronunciation:
* @keys: the pronunciation keys.
* @freq: the frequency of the pronunciation.
*
* Append one pronunciation.
*
*/
void append_pronunciation(ChewingKey * keys, guint32 freq);
/**
* PhraseItem::remove_nth_pronunciation:
* @index: the pronunciation index.
*
* Remove the nth pronunciation.
*
* Note: Normally don't change the first pronunciation,
* which decides the token number.
*
*/
void remove_nth_pronunciation(size_t index);
bool operator == (const PhraseItem & rhs) const{
if (m_chunk.size() != rhs.m_chunk.size())
return false;
return memcmp(m_chunk.begin(), rhs.m_chunk.begin(),
m_chunk.size()) == 0;
}
bool operator != (const PhraseItem & rhs) const{
return ! (*this == rhs);
}
};
/*
* In Sub Phrase Index, token == (token & PHRASE_MASK).
*/
/**
* SubPhraseIndex:
*
* The SubPhraseIndex class for internal usage.
*
*/
class SubPhraseIndex{
private:
guint32 m_total_freq;
MemoryChunk m_phrase_index;
MemoryChunk m_phrase_content;
MemoryChunk * m_chunk;
void reset(){
m_total_freq = 0;
m_phrase_index.set_size(0);
m_phrase_content.set_size(0);
if ( m_chunk ){
delete m_chunk;
m_chunk = NULL;
}
}
public:
/**
* SubPhraseIndex::SubPhraseIndex:
*
* The constructor of the SubPhraseIndex.
*
*/
SubPhraseIndex():m_total_freq(0){
m_chunk = NULL;
}
/**
* SubPhraseIndex::~SubPhraseIndex:
*
* The destructor of the SubPhraseIndex.
*
*/
~SubPhraseIndex(){
reset();
}
/**
* SubPhraseIndex::load:
* @chunk: the memory chunk of the binary sub phrase index.
* @offset: the begin of binary data in the memory chunk.
* @end: the end of binary data in the memory chunk.
* @returns: whether the load operation is successful.
*
* Load the sub phrase index from the memory chunk.
*
*/
bool load(MemoryChunk * chunk,
table_offset_t offset, table_offset_t end);
/**
* SubPhraseIndex::store:
* @new_chunk: the new memory chunk to store this sub phrase index.
* @offset: the begin of binary data in the memory chunk.
* @end: the end of stored binary data in the memory chunk.
* @returns: whether the store operation is successful.
*
* Store the sub phrase index to the new memory chunk.
*
*/
bool store(MemoryChunk * new_chunk,
table_offset_t offset, table_offset_t & end);
/**
* SubPhraseIndex::diff:
* @oldone: the original content of sub phrase index.
* @logger: the delta information of user self-learning data.
* @returns: whether the diff operation is successful.
*
* Compare this sub phrase index with the original content of the system
* sub phrase index to generate the logger of difference.
*
* Note: Switch to logger format to reduce user space storage.
*
*/
bool diff(SubPhraseIndex * oldone, PhraseIndexLogger * logger);
/**
* SubPhraseIndex::merge:
* @logger: the logger of difference in user home directory.
* @returns: whether the merge operation is successful.
*
* Merge the user logger of difference with this sub phrase index.
*
*/
bool merge(PhraseIndexLogger * logger);
/**
* SubPhraseIndex::get_range:
* @range: the token range.
* @returns: whether the get operation is successful.
*
* Get the token range in this sub phrase index.
*
*/
int get_range(/* out */ PhraseIndexRange & range);
/**
* SubPhraseIndex::get_phrase_index_total_freq:
* @returns: the total frequency of this sub phrase index.
*
* Get the total frequency of this sub phrase index.
*
* Note: maybe call it "Zero-gram".
*
*/
guint32 get_phrase_index_total_freq();
/**
* SubPhraseIndex::add_unigram_frequency:
* @token: the phrase token.
* @delta: the delta value of the phrase token.
* @returns: the status of the add operation.
*
* Add delta value to the phrase of the token.
*
* Note: this method is a fast path to add delta value.
* Maybe use the get_phrase_item method instead in future.
*
*/
int add_unigram_frequency(phrase_token_t token, guint32 delta);
/**
* SubPhraseIndex::get_phrase_item:
* @token: the phrase token.
* @item: the phrase item of the token.
* @returns: the status of the get operation.
*
* Get the phrase item from this sub phrase index.
*
* Note:get_phrase_item function can't modify the phrase item size,
* but can increment the freq of the special pronunciation,
* or change the content without size increasing.
*
*/
int get_phrase_item(phrase_token_t token, PhraseItem & item);
/**
* SubPhraseIndex::add_phrase_item:
* @token: the phrase token.
* @item: the phrase item of the token.
* @returns: the status of the add operation.
*
* Add the phrase item to this sub phrase index.
*
*/
int add_phrase_item(phrase_token_t token, PhraseItem * item);
/**
* SubPhraseIndex::remove_phrase_item:
* @token: the phrase token.
* @item: the removed phrase item of the token.
* @returns: the status of the remove operation.
*
* Remove the phrase item of the token.
*
* Note: this remove_phrase_item method will substract the unigram
* frequency of the removed item from m_total_freq.
*
*/
int remove_phrase_item(phrase_token_t token, /* out */ PhraseItem * & item);
};
/**
* FacadePhraseIndex:
*
* The facade class of phrase index.
*
*/
class FacadePhraseIndex{
private:
guint32 m_total_freq;
SubPhraseIndex * m_sub_phrase_indices[PHRASE_INDEX_LIBRARY_COUNT];
public:
/**
* FacadePhraseIndex::FacadePhraseIndex:
*
* The constructor of the FacadePhraseIndex.
*
*/
FacadePhraseIndex(){
m_total_freq = 0;
memset(m_sub_phrase_indices, 0, sizeof(m_sub_phrase_indices));
}
/**
* FacadePhraseIndex::~FacadePhraseIndex:
*
* The destructor of the FacadePhraseIndex.
*
*/
~FacadePhraseIndex(){
for ( size_t i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; ++i){
if ( m_sub_phrase_indices[i] ){
delete m_sub_phrase_indices[i];
m_sub_phrase_indices[i] = NULL;
}
}
}
/**
* FacadePhraseIndex::load_text:
* @phrase_index: the index of sub phrase index to be loaded.
* @infile: the textual format file of the phrase table.
* @returns: whether the load operation is successful.
*
* Load one sub phrase index from the textual format file.
* Note: load sub phrase index according to the config in future.
*
*/
bool load_text(guint8 phrase_index, FILE * infile);
/**
* FacadePhraseIndex::load:
* @phrase_index: the index of sub phrase index to be loaded.
* @chunk: the memory chunk of sub phrase index to be loaded.
* @returns: whether the load operation is successful.
*
* Load one sub phrase index from the memory chunk.
*
*/
bool load(guint8 phrase_index, MemoryChunk * chunk);
/**
* FacadePhraseIndex::store:
* @phrase_index: the index of sub phrase index to be stored.
* @new_chunk: the memory chunk of sub phrase index to be stored.
* @returns: whether the store operation is successful.
*
* Store one sub phrase index to the memory chunk.
*
*/
bool store(guint8 phrase_index, MemoryChunk * new_chunk);
/**
* FacadePhraseIndex::unload:
* @phrase_index: the index of sub phrase index to be unloaded.
* @returns: whether the unload operation is successful.
*
* Unload one sub phrase index.
*
*/
bool unload(guint8 phrase_index);
/**
* FacadePhraseIndex::diff:
* @phrase_index: the index of sub phrase index to be differed.
* @oldchunk: the original content of sub phrase index.
* @newlog: the delta information of user self-learning data.
* @returns: whether the diff operation is successful.
*
* Store user delta information in the logger format.
*
* Note: the ownership of oldchunk is transfered here.
*
*/
bool diff(guint8 phrase_index, MemoryChunk * oldchunk,
MemoryChunk * newlog);
/**
* FacadePhraseIndex::merge:
* @phrase_index: the index of sub phrase index to be merged.
* @log: the logger of difference in user home directory.
* @returns: whether the merge operation is successful.
*
* Merge the user logger of difference with the sub phrase index.
*
* Note: the ownership of log is transfered here.
*
*/
bool merge(guint8 phrase_index, MemoryChunk * log);
/**
* FacadePhraseIndex::compact:
* @returns: whether the compact operation is successful.
*
* Compat all sub phrase index memory usage.
*
*/
bool compact();
/**
* FacadePhraseIndex::get_sub_phrase_range:
* @min_index: the minimal sub phrase index.
* @max_index: the maximal sub phrase index.
* @returns: the status of the get operation.
*
* Get the minimum and maximum of the sub phrase index.
*
*/
int get_sub_phrase_range(guint8 & min_index, guint8 & max_index);
/**
* FacadePhraseIndex::get_range:
* @phrase_index: the index of sub phrase index.
* @range: the token range of the sub phrase index.
* @returns: the status of the get operation.
*
* Get the token range of the sub phrase index.
*
*/
int get_range(guint8 phrase_index, /* out */ PhraseIndexRange & range);
/**
* FacadePhraseIndex::get_phrase_index_total_freq:
* @returns: the total freq of the facade phrase index.
*
* Get the total freq of the facade phrase index.
*
* Note: maybe call it "Zero-gram".
*
*/
guint32 get_phrase_index_total_freq(){
return m_total_freq;
}
/**
* FacadePhraseIndex::add_unigram_frequency:
* @token: the phrase token.
* @delta: the delta value of the phrase token.
* @returns: the status of the add operation.
*
* Add delta value to the phrase of the token.
*
*/
int add_unigram_frequency(phrase_token_t token, guint32 delta){
guint8 index = PHRASE_INDEX_LIBRARY_INDEX(token);
SubPhraseIndex * sub_phrase = m_sub_phrase_indices[index];
if ( !sub_phrase )
return ERROR_NO_SUB_PHRASE_INDEX;
m_total_freq += delta;
return sub_phrase->add_unigram_frequency(token, delta);
}
/**
* FacadePhraseIndex::get_phrase_item:
* @token: the phrase token.
* @item: the phrase item of the token.
* @returns: the status of the get operation.
*
* Get the phrase item from the facade phrase index.
*
*/
int get_phrase_item(phrase_token_t token, PhraseItem & item){
guint8 index = PHRASE_INDEX_LIBRARY_INDEX(token);
SubPhraseIndex * sub_phrase = m_sub_phrase_indices[index];
if ( !sub_phrase )
return ERROR_NO_SUB_PHRASE_INDEX;
return sub_phrase->get_phrase_item(token, item);
}
/**
* FacadePhraseIndex::add_phrase_item:
* @token: the phrase token.
* @item: the phrase item of the token.
* @returns: the status of the add operation.
*
* Add the phrase item to the facade phrase index.
*
*/
int add_phrase_item(phrase_token_t token, PhraseItem * item){
guint8 index = PHRASE_INDEX_LIBRARY_INDEX(token);
SubPhraseIndex * & sub_phrase = m_sub_phrase_indices[index];
if ( !sub_phrase ){
sub_phrase = new SubPhraseIndex;
}
m_total_freq += item->get_unigram_frequency();
return sub_phrase->add_phrase_item(token, item);
}
/**
* FacadePhraseIndex::remove_phrase_item:
* @token: the phrase token.
* @item: the removed phrase item of the token.
* @returns: the status of the remove operation.
*
* Remove the phrase item of the token.
*
*/
int remove_phrase_item(phrase_token_t token, PhraseItem * & item){
guint8 index = PHRASE_INDEX_LIBRARY_INDEX(token);
SubPhraseIndex * & sub_phrase = m_sub_phrase_indices[index];
if ( !sub_phrase ){
return ERROR_NO_SUB_PHRASE_INDEX;
}
int result = sub_phrase->remove_phrase_item(token, item);
if ( result )
return result;
m_total_freq -= item->get_unigram_frequency();
return result;
}
/**
* FacadePhraseIndex::prepare_ranges:
* @ranges: the ranges to be prepared.
* @returns: whether the prepare operation is successful.
*
* Prepare the ranges.
*
*/
bool prepare_ranges(PhraseIndexRanges ranges) {
/* assume memset(ranges, 0, sizeof(ranges)); */
for (size_t i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) {
GArray * & range = ranges[i];
assert(NULL == range);
SubPhraseIndex * sub_phrase = m_sub_phrase_indices[i];
if (sub_phrase) {
range = g_array_new(FALSE, FALSE, sizeof(PhraseIndexRange));
}
}
return true;
}
/**
* FacadePhraseIndex::clear_ranges:
* @ranges: the ranges to be cleared.
* @returns: whether the clear operation is successful.
*
* Clear the ranges.
*
*/
bool clear_ranges(PhraseIndexRanges ranges) {
for (size_t i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) {
GArray * range = ranges[i];
if (range) {
g_array_set_size(range, 0);
}
}
return true;
}
/**
* FacadePhraseIndex::destroy_ranges:
* @ranges: the ranges to be destroyed.
* @returns: whether the destroy operation is successful.
*
* Destroy the ranges.
*
*/
bool destroy_ranges(PhraseIndexRanges ranges) {
for (size_t i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) {
GArray * & range = ranges[i];
if (range) {
g_array_free(range, TRUE);
range = NULL;
}
}
return true;
}
/**
* FacadePhraseIndex::create_sub_phrase:
* @index: the phrase index to be created.
* @returns: the result of the create operation.
*
* Create the sub phrase index.
*
*/
int create_sub_phrase(guint8 index) {
SubPhraseIndex * & sub_phrase = m_sub_phrase_indices[index];
if (sub_phrase) {
return ERROR_ALREADY_EXISTS;
}
sub_phrase = new SubPhraseIndex;
return ERROR_OK;
}
};
typedef enum {
NOT_USED, /* not used. */
SYSTEM_FILE, /* system phrase file. */
USER_FILE, /* user only phrase file. */
} PHRASE_FILE_TYPE;
typedef struct {
const char * m_table_filename;
const char * m_system_filename;
const char * m_user_filename;
PHRASE_FILE_TYPE m_file_type;
} pinyin_table_info_t;
extern const pinyin_table_info_t pinyin_phrase_files[PHRASE_INDEX_LIBRARY_COUNT];
};
#endif
|