/
pinyin.cpp
2411 lines (1908 loc) · 77.1 KB
/
pinyin.cpp
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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.h"
#include <stdio.h>
#include <unistd.h>
#include <glib/gstdio.h>
#include "pinyin_internal.h"
using namespace pinyin;
/* a glue layer for input method integration. */
typedef GArray * CandidateVector; /* GArray of lookup_candidate_t */
struct _pinyin_context_t{
pinyin_option_t m_options;
FullPinyinParser2 * m_full_pinyin_parser;
DoublePinyinParser2 * m_double_pinyin_parser;
ChewingParser2 * m_chewing_parser;
FacadeChewingTable * m_pinyin_table;
FacadePhraseTable2 * m_phrase_table;
FacadePhraseIndex * m_phrase_index;
Bigram * m_system_bigram;
Bigram * m_user_bigram;
PinyinLookup2 * m_pinyin_lookup;
PhraseLookup * m_phrase_lookup;
char * m_system_dir;
char * m_user_dir;
bool m_modified;
SystemTableInfo m_system_table_info;
};
struct _pinyin_instance_t{
pinyin_context_t * m_context;
gchar * m_raw_full_pinyin;
TokenVector m_prefixes;
ChewingKeyVector m_pinyin_keys;
ChewingKeyRestVector m_pinyin_key_rests;
size_t m_parsed_len;
CandidateConstraints m_constraints;
MatchResults m_match_results;
CandidateVector m_candidates;
};
struct _lookup_candidate_t{
lookup_candidate_type_t m_candidate_type;
gchar * m_phrase_string;
phrase_token_t m_token;
ChewingKeyRest m_orig_rest;
gchar * m_new_pinyins;
guint32 m_freq; /* the amplifed gfloat numerical value. */
public:
_lookup_candidate_t() {
m_candidate_type = NORMAL_CANDIDATE;
m_phrase_string = NULL;
m_token = null_token;
m_new_pinyins = NULL;
m_freq = 0;
}
};
struct _import_iterator_t{
pinyin_context_t * m_context;
guint8 m_phrase_index;
};
struct _export_iterator_t{
pinyin_context_t * m_context;
guint8 m_phrase_index;
/* null token means no next item. */
phrase_token_t m_next_token;
guint8 m_next_pronunciation;
};
static bool check_format(pinyin_context_t * context){
const char * userdir = context->m_user_dir;
UserTableInfo user_table_info;
gchar * filename = g_build_filename
(userdir, USER_TABLE_INFO, NULL);
user_table_info.load(filename);
g_free(filename);
bool exists = user_table_info.is_conform
(&context->m_system_table_info);
if (exists)
return exists;
const pinyin_table_info_t * phrase_files =
context->m_system_table_info.get_table_info();
/* clean up files, if version mis-matches. */
for (size_t i = 1; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) {
const pinyin_table_info_t * table_info = phrase_files + i;
if (NOT_USED == table_info->m_file_type)
continue;
if (NULL == table_info->m_user_filename)
continue;
const char * userfilename = table_info->m_user_filename;
/* remove dbin file. */
filename = g_build_filename(userdir, userfilename, NULL);
unlink(filename);
g_free(filename);
}
filename = g_build_filename
(userdir, USER_PINYIN_INDEX, NULL);
unlink(filename);
g_free(filename);
filename = g_build_filename
(userdir, USER_PHRASE_INDEX, NULL);
unlink(filename);
g_free(filename);
filename = g_build_filename
(userdir, USER_BIGRAM, NULL);
unlink(filename);
g_free(filename);
return exists;
}
static bool mark_version(pinyin_context_t * context){
const char * userdir = context->m_user_dir;
UserTableInfo user_table_info;
user_table_info.make_conform(&context->m_system_table_info);
gchar * filename = g_build_filename
(userdir, USER_TABLE_INFO, NULL);
bool retval = user_table_info.save(filename);
g_free(filename);
return retval;
}
pinyin_context_t * pinyin_init(const char * systemdir, const char * userdir){
pinyin_context_t * context = new pinyin_context_t;
context->m_options = USE_TONE;
context->m_system_dir = g_strdup(systemdir);
context->m_user_dir = g_strdup(userdir);
context->m_modified = false;
gchar * filename = g_build_filename
(context->m_system_dir, SYSTEM_TABLE_INFO, NULL);
if (!context->m_system_table_info.load(filename)) {
fprintf(stderr, "load %s failed!\n", filename);
return NULL;
}
g_free(filename);
check_format(context);
context->m_full_pinyin_parser = new FullPinyinParser2;
context->m_double_pinyin_parser = new DoublePinyinParser2;
context->m_chewing_parser = new ChewingParser2;
/* load chewing table. */
context->m_pinyin_table = new FacadeChewingTable;
/* load system chewing table. */
MemoryChunk * chunk = new MemoryChunk;
filename = g_build_filename
(context->m_system_dir, SYSTEM_PINYIN_INDEX, NULL);
if (!chunk->load(filename)) {
fprintf(stderr, "open %s failed!\n", filename);
return NULL;
}
g_free(filename);
/* load user chewing table */
MemoryChunk * userchunk = new MemoryChunk;
filename = g_build_filename
(context->m_user_dir, USER_PINYIN_INDEX, NULL);
if (!userchunk->load(filename)) {
/* hack here: use local Chewing Table to create empty memory chunk. */
ChewingLargeTable table(context->m_options);
table.store(userchunk);
}
g_free(filename);
context->m_pinyin_table->load(context->m_options, chunk, userchunk);
/* load phrase table */
context->m_phrase_table = new FacadePhraseTable2;
/* load system phrase table */
chunk = new MemoryChunk;
filename = g_build_filename
(context->m_system_dir, SYSTEM_PHRASE_INDEX, NULL);
if (!chunk->load(filename)) {
fprintf(stderr, "open %s failed!\n", filename);
return NULL;
}
g_free(filename);
/* load user phrase table */
userchunk = new MemoryChunk;
filename = g_build_filename
(context->m_user_dir, USER_PHRASE_INDEX, NULL);
if (!userchunk->load(filename)) {
/* hack here: use local Phrase Table to create empty memory chunk. */
PhraseLargeTable2 table;
table.store(userchunk);
}
g_free(filename);
context->m_phrase_table->load(chunk, userchunk);
context->m_phrase_index = new FacadePhraseIndex;
/* hack here: directly call load phrase library. */
pinyin_load_phrase_library(context, GB_DICTIONARY);
pinyin_load_phrase_library(context, MERGED_DICTIONARY);
context->m_system_bigram = new Bigram;
filename = g_build_filename(context->m_system_dir, SYSTEM_BIGRAM, NULL);
context->m_system_bigram->attach(filename, ATTACH_READONLY);
g_free(filename);
context->m_user_bigram = new Bigram;
filename = g_build_filename(context->m_user_dir, USER_BIGRAM, NULL);
context->m_user_bigram->load_db(filename);
g_free(filename);
gfloat lambda = context->m_system_table_info.get_lambda();
context->m_pinyin_lookup = new PinyinLookup2
( lambda, context->m_options,
context->m_pinyin_table, context->m_phrase_index,
context->m_system_bigram, context->m_user_bigram);
context->m_phrase_lookup = new PhraseLookup
(lambda,
context->m_phrase_table, context->m_phrase_index,
context->m_system_bigram, context->m_user_bigram);
return context;
}
bool pinyin_load_phrase_library(pinyin_context_t * context,
guint8 index){
if (!(index < PHRASE_INDEX_LIBRARY_COUNT))
return false;
/* check whether the sub phrase index is already loaded. */
PhraseIndexRange range;
int retval = context->m_phrase_index->get_range(index, range);
if (ERROR_OK == retval)
return false;
const pinyin_table_info_t * phrase_files =
context->m_system_table_info.get_table_info();
const pinyin_table_info_t * table_info = phrase_files + index;
if (SYSTEM_FILE == table_info->m_file_type ||
DICTIONARY == table_info->m_file_type) {
/* system phrase library */
MemoryChunk * chunk = new MemoryChunk;
const char * systemfilename = table_info->m_system_filename;
/* check bin file in system dir. */
gchar * chunkfilename = g_build_filename(context->m_system_dir,
systemfilename, NULL);
chunk->load(chunkfilename);
g_free(chunkfilename);
context->m_phrase_index->load(index, chunk);
const char * userfilename = table_info->m_user_filename;
chunkfilename = g_build_filename(context->m_user_dir,
userfilename, NULL);
MemoryChunk * log = new MemoryChunk;
log->load(chunkfilename);
g_free(chunkfilename);
/* merge the chunk log. */
context->m_phrase_index->merge(index, log);
return true;
}
if (USER_FILE == table_info->m_file_type) {
/* user phrase library */
MemoryChunk * chunk = new MemoryChunk;
const char * userfilename = table_info->m_user_filename;
gchar * chunkfilename = g_build_filename(context->m_user_dir,
userfilename, NULL);
/* check bin file exists. if not, create a new one. */
if (chunk->load(chunkfilename)) {
context->m_phrase_index->load(index, chunk);
} else {
delete chunk;
context->m_phrase_index->create_sub_phrase(index);
}
g_free(chunkfilename);
return true;
}
return false;
}
bool pinyin_unload_phrase_library(pinyin_context_t * context,
guint8 index){
/* gb_char.bin and merged.bin can't be unloaded. */
if (GB_DICTIONARY == index || MERGED_DICTIONARY == index)
return false;
assert(index < PHRASE_INDEX_LIBRARY_COUNT);
context->m_phrase_index->unload(index);
return true;
}
import_iterator_t * pinyin_begin_add_phrases(pinyin_context_t * context,
guint8 index){
import_iterator_t * iter = new import_iterator_t;
iter->m_context = context;
iter->m_phrase_index = index;
return iter;
}
bool pinyin_iterator_add_phrase(import_iterator_t * iter,
const char * phrase,
const char * pinyin,
gint count){
/* if -1 == count, use the default value. */
const gint default_count = 5;
const guint32 unigram_factor = 3;
if (-1 == count)
count = default_count;
pinyin_context_t * & context = iter->m_context;
FacadePhraseTable2 * & phrase_table = context->m_phrase_table;
FacadeChewingTable * & pinyin_table = context->m_pinyin_table;
FacadePhraseIndex * & phrase_index = context->m_phrase_index;
bool result = false;
if (NULL == phrase || NULL == pinyin)
return result;
/* check whether the phrase exists in phrase table */
glong len_phrase = 0;
ucs4_t * ucs4_phrase = g_utf8_to_ucs4(phrase, -1, NULL, &len_phrase, NULL);
pinyin_option_t options = PINYIN_CORRECT_ALL | USE_TONE;
FullPinyinParser2 parser;
ChewingKeyVector keys =
g_array_new(FALSE, FALSE, sizeof(ChewingKey));
ChewingKeyRestVector key_rests =
g_array_new(FALSE, FALSE, sizeof(ChewingKeyRest));
/* parse the pinyin. */
parser.parse(options, keys, key_rests, pinyin, strlen(pinyin));
if (len_phrase != keys->len)
return result;
if (0 == len_phrase || len_phrase >= MAX_PHRASE_LENGTH)
return result;
phrase_token_t token = null_token;
GArray * tokenarray = g_array_new(FALSE, FALSE, sizeof(phrase_token_t));
/* do phrase table search. */
PhraseTokens tokens;
memset(tokens, 0, sizeof(PhraseTokens));
phrase_index->prepare_tokens(tokens);
int retval = phrase_table->search(len_phrase, ucs4_phrase, tokens);
int num = reduce_tokens(tokens, tokenarray);
phrase_index->destroy_tokens(tokens);
/* find the best token candidate. */
for (size_t i = 0; i < tokenarray->len; ++i) {
phrase_token_t candidate = g_array_index(tokenarray, phrase_token_t, i);
if (null_token == token) {
token = candidate;
continue;
}
if (PHRASE_INDEX_LIBRARY_INDEX(candidate) == iter->m_phrase_index) {
/* only one phrase string per sub phrase index. */
assert(PHRASE_INDEX_LIBRARY_INDEX(token) != iter->m_phrase_index);
token = candidate;
continue;
}
}
g_array_free(tokenarray, TRUE);
PhraseItem item;
/* check whether it exists in the same sub phrase index; */
if (null_token != token &&
PHRASE_INDEX_LIBRARY_INDEX(token) == iter->m_phrase_index) {
/* if so, remove the phrase, add the pinyin for the phrase item,
then add it back;*/
phrase_index->get_phrase_item(token, item);
assert(len_phrase == item.get_phrase_length());
ucs4_t tmp_phrase[MAX_PHRASE_LENGTH];
item.get_phrase_string(tmp_phrase);
assert(0 == memcmp
(ucs4_phrase, tmp_phrase, sizeof(ucs4_t) * len_phrase));
PhraseItem * removed_item = NULL;
retval = phrase_index->remove_phrase_item(token, removed_item);
if (ERROR_OK == retval) {
/* maybe check whether there are duplicated pronunciations here. */
removed_item->add_pronunciation((ChewingKey *)keys->data,
count);
phrase_index->add_phrase_item(token, removed_item);
delete removed_item;
result = true;
}
} else {
/* if not exists in the same sub phrase index,
get the maximum token,
then add it directly with maximum token + 1; */
PhraseIndexRange range;
retval = phrase_index->get_range(iter->m_phrase_index, range);
if (ERROR_OK == retval) {
token = range.m_range_end;
if (0x00000000 == (token & PHRASE_MASK))
token++;
if (len_phrase == keys->len) { /* valid pinyin */
phrase_table->add_index(len_phrase, ucs4_phrase, token);
pinyin_table->add_index
(keys->len, (ChewingKey *)(keys->data), token);
item.set_phrase_string(len_phrase, ucs4_phrase);
item.add_pronunciation((ChewingKey *)(keys->data), count);
phrase_index->add_phrase_item(token, &item);
phrase_index->add_unigram_frequency(token,
count * unigram_factor);
result = true;
}
}
}
g_array_free(key_rests, TRUE);
g_array_free(keys, TRUE);
g_free(ucs4_phrase);
return result;
}
void pinyin_end_add_phrases(import_iterator_t * iter){
/* compact the content memory chunk of phrase index. */
iter->m_context->m_phrase_index->compact();
iter->m_context->m_modified = true;
delete iter;
}
export_iterator_t * pinyin_begin_get_phrases(pinyin_context_t * context,
guint index){
export_iterator_t * iter = new export_iterator_t;
iter->m_context = context;
iter->m_phrase_index = index;
iter->m_next_token = null_token;
iter->m_next_pronunciation = 0;
/* probe next token. */
PhraseIndexRange range;
int retval = iter->m_context->m_phrase_index->get_range
(iter->m_phrase_index, range);
if (retval != ERROR_OK)
return iter;
PhraseItem item;
phrase_token_t token = range.m_range_begin;
for (; token < range.m_range_end; ++token) {
retval = iter->m_context->m_phrase_index->get_phrase_item
(token, item);
if (ERROR_OK == retval && item.get_n_pronunciation() >= 1) {
iter->m_next_token = token;
break;
}
}
return iter;
}
bool pinyin_iterator_has_next_phrase(export_iterator_t * iter){
/* no next token. */
if (null_token == iter->m_next_token)
return false;
return true;
}
/* phrase, pinyin should be freed by g_free(). */
bool pinyin_iterator_get_next_phrase(export_iterator_t * iter,
gchar ** phrase,
gchar ** pinyin,
gint * count){
/* count "-1" means default count. */
*phrase = NULL; *pinyin = NULL; *count = -1;
PhraseItem item;
int retval = iter->m_context->m_phrase_index->get_phrase_item
(iter->m_next_token, item);
/* assume valid next token from previous call. */
assert(ERROR_OK == retval);
/* fill phrase and pronunciation pair. */
ucs4_t phrase_ucs4[MAX_PHRASE_LENGTH];
guint8 len = item.get_phrase_length();
assert(item.get_phrase_string(phrase_ucs4));
gchar * phrase_utf8 = g_ucs4_to_utf8
(phrase_ucs4, len, NULL, NULL, NULL);
guint8 nth_pronun = iter->m_next_pronunciation;
guint8 n_pronuns = item.get_n_pronunciation();
/* assume valid pronunciation from previous call. */
assert(nth_pronun < n_pronuns);
ChewingKey keys[MAX_PHRASE_LENGTH];
guint32 freq = 0;
assert(item.get_nth_pronunciation(nth_pronun, keys, freq));
GPtrArray * array = g_ptr_array_new();
for(size_t i = 0; i < len; ++i) {
g_ptr_array_add(array, keys[i].get_pinyin_string());
}
g_ptr_array_add(array, NULL);
gchar ** strings = (gchar **)g_ptr_array_free(array, FALSE);
gchar * pinyins = g_strjoinv("'", strings);
g_strfreev(strings);
/* use default value. */
*phrase = phrase_utf8; *pinyin = pinyins;
if (freq > 0)
*count = freq;
/* probe next pronunciation. */
nth_pronun ++;
if (nth_pronun < n_pronuns) {
iter->m_next_pronunciation = nth_pronun;
return true;
}
iter->m_next_pronunciation = 0;
/* probe next token. */
PhraseIndexRange range;
retval = iter->m_context->m_phrase_index->get_range
(iter->m_phrase_index, range);
if (retval != ERROR_OK) {
iter->m_next_token = null_token;
return true;
}
phrase_token_t token = iter->m_next_token + 1;
for (; token < range.m_range_end; ++token) {
retval = iter->m_context->m_phrase_index->get_phrase_item
(token, item);
if (ERROR_OK == retval && item.get_n_pronunciation() >= 1) {
iter->m_next_token = token;
break;
}
}
return true;
}
void pinyin_end_get_phrases(export_iterator_t * iter){
delete iter;
}
bool pinyin_save(pinyin_context_t * context){
if (!context->m_user_dir)
return false;
if (!context->m_modified)
return false;
context->m_phrase_index->compact();
const pinyin_table_info_t * phrase_files =
context->m_system_table_info.get_table_info();
/* skip the reserved zero phrase library. */
for (size_t i = 1; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) {
PhraseIndexRange range;
int retval = context->m_phrase_index->get_range(i, range);
if (ERROR_NO_SUB_PHRASE_INDEX == retval)
continue;
const pinyin_table_info_t * table_info = phrase_files + i;
if (NOT_USED == table_info->m_file_type)
continue;
const char * userfilename = table_info->m_user_filename;
if (NULL == userfilename)
continue;
if (SYSTEM_FILE == table_info->m_file_type ||
DICTIONARY == table_info->m_file_type) {
/* system phrase library */
MemoryChunk * chunk = new MemoryChunk;
MemoryChunk * log = new MemoryChunk;
const char * systemfilename = table_info->m_system_filename;
/* check bin file in system dir. */
gchar * chunkfilename = g_build_filename(context->m_system_dir,
systemfilename, NULL);
chunk->load(chunkfilename);
g_free(chunkfilename);
context->m_phrase_index->diff(i, chunk, log);
const char * userfilename = table_info->m_user_filename;
gchar * tmpfilename = g_strdup_printf("%s.tmp", userfilename);
gchar * tmppathname = g_build_filename(context->m_user_dir,
tmpfilename, NULL);
g_free(tmpfilename);
gchar * chunkpathname = g_build_filename(context->m_user_dir,
userfilename, NULL);
log->save(tmppathname);
int result = rename(tmppathname, chunkpathname);
if (0 != result)
fprintf(stderr, "rename %s to %s failed.\n",
tmppathname, chunkpathname);
g_free(chunkpathname);
g_free(tmppathname);
delete log;
}
if (USER_FILE == table_info->m_file_type) {
/* user phrase library */
MemoryChunk * chunk = new MemoryChunk;
context->m_phrase_index->store(i, chunk);
const char * userfilename = table_info->m_user_filename;
gchar * tmpfilename = g_strdup_printf("%s.tmp", userfilename);
gchar * tmppathname = g_build_filename(context->m_user_dir,
tmpfilename, NULL);
g_free(tmpfilename);
gchar * chunkpathname = g_build_filename(context->m_user_dir,
userfilename, NULL);
chunk->save(tmppathname);
int result = rename(tmppathname, chunkpathname);
if (0 != result)
fprintf(stderr, "rename %s to %s failed.\n",
tmppathname, chunkpathname);
g_free(chunkpathname);
g_free(tmppathname);
delete chunk;
}
}
/* save user pinyin table */
gchar * tmpfilename = g_build_filename
(context->m_user_dir, USER_PINYIN_INDEX ".tmp", NULL);
unlink(tmpfilename);
gchar * filename = g_build_filename
(context->m_user_dir, USER_PINYIN_INDEX, NULL);
MemoryChunk * chunk = new MemoryChunk;
context->m_pinyin_table->store(chunk);
chunk->save(tmpfilename);
delete chunk;
int result = rename(tmpfilename, filename);
if (0 != result)
fprintf(stderr, "rename %s to %s failed.\n",
tmpfilename, filename);
g_free(tmpfilename);
g_free(filename);
/* save user phrase table */
tmpfilename = g_build_filename
(context->m_user_dir, USER_PHRASE_INDEX ".tmp", NULL);
unlink(tmpfilename);
filename = g_build_filename
(context->m_user_dir, USER_PHRASE_INDEX, NULL);
chunk = new MemoryChunk;
context->m_phrase_table->store(chunk);
chunk->save(tmpfilename);
delete chunk;
result = rename(tmpfilename, filename);
if (0 != result)
fprintf(stderr, "rename %s to %s failed.\n",
tmpfilename, filename);
g_free(tmpfilename);
g_free(filename);
/* save user bi-gram */
tmpfilename = g_build_filename
(context->m_user_dir, USER_BIGRAM ".tmp", NULL);
unlink(tmpfilename);
filename = g_build_filename(context->m_user_dir, USER_BIGRAM, NULL);
context->m_user_bigram->save_db(tmpfilename);
result = rename(tmpfilename, filename);
if (0 != result)
fprintf(stderr, "rename %s to %s failed.\n",
tmpfilename, filename);
g_free(tmpfilename);
g_free(filename);
mark_version(context);
context->m_modified = false;
return true;
}
bool pinyin_set_double_pinyin_scheme(pinyin_context_t * context,
DoublePinyinScheme scheme){
context->m_double_pinyin_parser->set_scheme(scheme);
return true;
}
bool pinyin_set_chewing_scheme(pinyin_context_t * context,
ChewingScheme scheme){
context->m_chewing_parser->set_scheme(scheme);
return true;
}
void pinyin_fini(pinyin_context_t * context){
delete context->m_full_pinyin_parser;
delete context->m_double_pinyin_parser;
delete context->m_chewing_parser;
delete context->m_pinyin_table;
delete context->m_phrase_table;
delete context->m_phrase_index;
delete context->m_system_bigram;
delete context->m_user_bigram;
delete context->m_pinyin_lookup;
delete context->m_phrase_lookup;
g_free(context->m_system_dir);
g_free(context->m_user_dir);
context->m_modified = false;
delete context;
}
bool pinyin_mask_out(pinyin_context_t * context,
phrase_token_t mask,
phrase_token_t value) {
context->m_pinyin_table->mask_out(mask, value);
context->m_phrase_table->mask_out(mask, value);
context->m_user_bigram->mask_out(mask, value);
const pinyin_table_info_t * phrase_files =
context->m_system_table_info.get_table_info();
/* mask out the phrase index. */
for (size_t index = 1; index < PHRASE_INDEX_LIBRARY_COUNT; ++index) {
PhraseIndexRange range;
int retval = context->m_phrase_index->get_range(index, range);
if (ERROR_NO_SUB_PHRASE_INDEX == retval)
continue;
const pinyin_table_info_t * table_info = phrase_files + index;
if (NOT_USED == table_info->m_file_type)
continue;
const char * userfilename = table_info->m_user_filename;
if (NULL == userfilename)
continue;
if (SYSTEM_FILE == table_info->m_file_type ||
DICTIONARY == table_info->m_file_type) {
/* system phrase library */
MemoryChunk * chunk = new MemoryChunk;
const char * systemfilename = table_info->m_system_filename;
/* check bin file in system dir. */
gchar * chunkfilename = g_build_filename(context->m_system_dir,
systemfilename, NULL);
chunk->load(chunkfilename);
g_free(chunkfilename);
context->m_phrase_index->load(index, chunk);
const char * userfilename = table_info->m_user_filename;
chunkfilename = g_build_filename(context->m_user_dir,
userfilename, NULL);
MemoryChunk * log = new MemoryChunk;
log->load(chunkfilename);
g_free(chunkfilename);
/* merge the chunk log with mask. */
context->m_phrase_index->merge_with_mask(index, log, mask, value);
}
if (USER_FILE == table_info->m_file_type) {
/* user phrase library */
context->m_phrase_index->mask_out(index, mask, value);
}
}
context->m_phrase_index->compact();
return true;
}
/* copy from options to context->m_options. */
bool pinyin_set_options(pinyin_context_t * context,
pinyin_option_t options){
context->m_options = options;
context->m_pinyin_table->set_options(context->m_options);
context->m_pinyin_lookup->set_options(context->m_options);
return true;
}
pinyin_instance_t * pinyin_alloc_instance(pinyin_context_t * context){
pinyin_instance_t * instance = new pinyin_instance_t;
instance->m_context = context;
instance->m_raw_full_pinyin = NULL;
instance->m_prefixes = g_array_new(FALSE, FALSE, sizeof(phrase_token_t));
instance->m_pinyin_keys = g_array_new(FALSE, FALSE, sizeof(ChewingKey));
instance->m_pinyin_key_rests =
g_array_new(FALSE, FALSE, sizeof(ChewingKeyRest));
instance->m_parsed_len = 0;
instance->m_constraints = g_array_new
(TRUE, FALSE, sizeof(lookup_constraint_t));
instance->m_match_results =
g_array_new(FALSE, FALSE, sizeof(phrase_token_t));
instance->m_candidates =
g_array_new(FALSE, FALSE, sizeof(lookup_candidate_t));
return instance;
}
void pinyin_free_instance(pinyin_instance_t * instance){
g_free(instance->m_raw_full_pinyin);
g_array_free(instance->m_prefixes, TRUE);
g_array_free(instance->m_pinyin_keys, TRUE);
g_array_free(instance->m_pinyin_key_rests, TRUE);
g_array_free(instance->m_constraints, TRUE);
g_array_free(instance->m_match_results, TRUE);
g_array_free(instance->m_candidates, TRUE);
delete instance;
}
pinyin_context_t * pinyin_get_context (pinyin_instance_t * instance){
return instance->m_context;
}
static bool pinyin_update_constraints(pinyin_instance_t * instance){
pinyin_context_t * & context = instance->m_context;
ChewingKeyVector & pinyin_keys = instance->m_pinyin_keys;
CandidateConstraints & constraints = instance->m_constraints;
size_t key_len = constraints->len;
g_array_set_size(constraints, pinyin_keys->len);
for (size_t i = key_len; i < pinyin_keys->len; ++i ) {
lookup_constraint_t * constraint =
&g_array_index(constraints, lookup_constraint_t, i);
constraint->m_type = NO_CONSTRAINT;
}
context->m_pinyin_lookup->validate_constraint
(constraints, pinyin_keys);
return true;
}
bool pinyin_guess_sentence(pinyin_instance_t * instance){
pinyin_context_t * & context = instance->m_context;
g_array_set_size(instance->m_prefixes, 0);
g_array_append_val(instance->m_prefixes, sentence_start);
pinyin_update_constraints(instance);
bool retval = context->m_pinyin_lookup->get_best_match
(instance->m_prefixes,
instance->m_pinyin_keys,
instance->m_constraints,
instance->m_match_results);
return retval;
}
static void _compute_prefixes(pinyin_instance_t * instance,
const char * prefix){
pinyin_context_t * & context = instance->m_context;
FacadePhraseIndex * & phrase_index = context->m_phrase_index;
glong len_str = 0;
ucs4_t * ucs4_str = g_utf8_to_ucs4(prefix, -1, NULL, &len_str, NULL);
GArray * tokenarray = g_array_new(FALSE, FALSE, sizeof(phrase_token_t));
if (ucs4_str && len_str) {
/* add prefixes. */
for (ssize_t i = 1; i <= len_str; ++i) {
if (i > MAX_PHRASE_LENGTH)
break;
ucs4_t * start = ucs4_str + len_str - i;
PhraseTokens tokens;
memset(tokens, 0, sizeof(tokens));
phrase_index->prepare_tokens(tokens);
int result = context->m_phrase_table->search(i, start, tokens);
int num = reduce_tokens(tokens, tokenarray);
phrase_index->destroy_tokens(tokens);
if (result & SEARCH_OK)
g_array_append_vals(instance->m_prefixes,
tokenarray->data, tokenarray->len);
}
}
g_array_free(tokenarray, TRUE);
g_free(ucs4_str);
}
bool pinyin_guess_sentence_with_prefix(pinyin_instance_t * instance,
const char * prefix){
pinyin_context_t * & context = instance->m_context;
g_array_set_size(instance->m_prefixes, 0);
g_array_append_val(instance->m_prefixes, sentence_start);
_compute_prefixes(instance, prefix);
pinyin_update_constraints(instance);
bool retval = context->m_pinyin_lookup->get_best_match
(instance->m_prefixes,
instance->m_pinyin_keys,
instance->m_constraints,
instance->m_match_results);
return retval;
}
bool pinyin_phrase_segment(pinyin_instance_t * instance,
const char * sentence){
pinyin_context_t * & context = instance->m_context;
const glong num_of_chars = g_utf8_strlen(sentence, -1);
glong ucs4_len = 0;
ucs4_t * ucs4_str = g_utf8_to_ucs4(sentence, -1, NULL, &ucs4_len, NULL);