forked from zhujian198/unispim
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathzi.c
1340 lines (1098 loc) · 57.6 KB
/
zi.c
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
/* 字相关函数。
* 处理:
* 1. 获得汉字候选
* 2. 对候选进行排序
* 3. 对候选汉字进行排重
*
* 对字频的处理:
* 1. 字频信息分为原始字频与用户汉字使用度量两种
* 2. 原始字频将不会再改变
* 3. 用户字使用度用于标识用户输入过的字的次数
* 4. 在比较字频的时候,首先进行用户字使用度的比较,
* 如果不同则,使用度最高的优先
* 5. 如果使用度相同,则进行原始字频的判断
* 6. 如果采用最近汉字优先方式,则比较Cache中的使用标识号
*
*/
#include <assert.h>
#include <tchar.h>
#include <kernel.h>
#include <config.h>
#include <zi.h>
#include <ci.h>
#include <utility.h>
#include <editor.h>
#include <fontcheck.h>
#include <gbk_map.h>
#include <share_segment.h>
#define DEFAULT_TOP_POS 100
static HZDATAHEADER *hz_data = 0;
static TCHAR *hz_data_share_name = TEXT("HYPIM_HZ_DATA_SHARED_NAME");
//#pragma data_seg(HYPIM_SHARED_SEGMENT)
//
//HZCACHE hz_cache = { ZICACHE_SIGNATURE, 1, {0}, {0}, }; //汉字Cache结构
//int zi_cache_loaded = 0; //是否已经装载了字Cache
//int zi_cache_modified = 0; //是否改变
//
//TOPZIITEM topzi_table[MAX_SYLLABLES] = { 0 }; //置顶字表
//int topzi_table_items = 0; //置顶字表项数目
//int topzi_loaded = 0; //是否已经装入到内存
//
//static int hz_data_loaded = 0; //汉字信息表是否已经装入
//#pragma data_seg()
/** 判断汉字能否插入词库
*/
int IsAllCanInLibrary(HZ *hz, int length)
{
int i;
for (i = 0; i < length; i++)
if (!_CanInLibrary(hz[i]))
return 0;
return 1;
}
#define IsGBHanzi(hz)
/** 判断是否为GB汉字
*/
int IsGB(HZ hz)
{
return (((hz) % 0x100) >= 0xb0 && ((hz) % 0x100) <= 0xf7 && ((hz) / 0x100) >= 0xa1 && ((hz) / 0x100) <= 0xfe);
}
/** 基于音节寻找置顶字
* 返回:
* 置顶字的数目
*/
int GetTopZi(SYLLABLE syllable, HZ *zi)
{
int i, j;
if (!(pim_config->hz_option & HZ_USE_FIX_TOP))
return 0;
zi[0] = 0;
//遍历数组寻找
for (i = 0; i < share_segment->topzi_table_items; i++)
{
if (*(short*)&syllable == *(short*)&share_segment->topzi_table[i].syllable) //找到
{
for (j = 0; j < MAX_TOPZI; j++)
{
if (!share_segment->topzi_table[i].top_zi[j]) //是字
break;
zi[j] = share_segment->topzi_table[i].top_zi[j];
}
if (j < MAX_TOPZI)
zi[j] = 0;
return j;
}
}
return 0;
}
/* 获得汉字项在汉字集合中的序号。
*/
static int GetHzItemIndex(HZITEM *item)
{
assert(item - hz_data->hz_item >= 0 && item - hz_data->hz_item < hz_data->hz_count);
return (int)item->hz_id;
}
/* 基于汉字以及字频进行排序,用于汉字候选的排重。
*/
int CompareHzCodeFreq(const CANDIDATE *cand1, const CANDIDATE *cand2)
{
assert(cand1 && cand2);
//按内码排序
if (cand1->hz.item->hz != cand2->hz.item->hz)
return cand1->hz.item->hz - cand2->hz.item->hz;
//按字频排序
return cand2->hz.item->freq - cand1->hz.item->freq;
}
/* 基于小音节词的汉字进行排序,用于小音节词候选的排重。
*/
int CompareSmallCiCode(const CANDIDATE *cand1, const CANDIDATE *cand2)
{
int ret;
assert(cand1 && cand2);
//先按长度排序
if (cand1->hz.word_item->ci_length != cand2->hz.word_item->ci_length)
return cand2->hz.word_item->ci_length - cand1->hz.word_item->ci_length;
//再按内码排序
ret = memcmp(
GetItemHZPtr(cand2->hz.word_item),
GetItemHZPtr(cand1->hz.word_item),
sizeof(HZ) * cand1->hz.word_item->ci_length);
return ret;
}
//去掉无法显示的汉字
int DeleteUnreadableZiCandidates(CANDIDATE *candidate_array, int count)
{
int i, new_count = 0;
for (i = 0; i < count; i++)
{
if ( pim_config->scope_gbk == HZ_SCOPE_UNICODE )
{
if ( pim_config->hide_black_window && !FontCanSupport( candidate_array[i].hz.item->hz ) )
continue;
}
else
{
//if it is not gbk,then continue
if(!IsGBK( candidate_array[i].hz.item->hz ))
continue;
}
candidate_array[new_count] = candidate_array[i];
new_count++;
}
return new_count;
}
/* 在候选数组中,排除掉重复的汉字,并且向前移动被排序的数组项
* 为了加快速度,使用bitmap的方式进行查重。Unicode字符将不能用
* 这种方法处理。
* 参数:
* candidate_array 候选数组
* count 候选数目
* 返回:
* 排重后的候选数目
*/
int UnifyZiCandidates(CANDIDATE *candidate_array, int count)
{
int i;
int new_count;
if (count < 2) //至少两个才能进行比较
return count;
//基于汉字的内码以及字频进行排序,这样相同的汉字将
//集中在一起存放,然后进行删除操作
qsort(candidate_array, count, sizeof(CANDIDATE), CompareHzCodeFreq);
new_count = 1;
for (i = 1; i < count; i++)
{
if (candidate_array[i].hz.item->hz == candidate_array[i - 1].hz.item->hz)
continue;
candidate_array[new_count] = candidate_array[i];
new_count++;
}
return new_count;
}
/* 在候选数组中,排除掉重复的小音节词
* 参数:
* candidate_array 候选数组
* count 候选数目
* 返回:
* 排重后的候选数目
*/
int UnifySmallCiCandidates(CANDIDATE *candidate_array, int count)
{
int i;
int new_count;
if (count < 2) //至少两个才能进行比较
return count;
//基于小音节词的内码进行排序,这样具有相同的汉字词将
//集中在一起存放,然后进行删除操作
qsort(candidate_array, count, sizeof(CANDIDATE), CompareSmallCiCode);
new_count = 1;
for (i = 1; i < count; i++)
{
if (candidate_array[i].hz.word_item->ci_length == candidate_array[i - 1].hz.word_item->ci_length &&
!memcmp(GetItemHZPtr(candidate_array[i].hz.word_item), GetItemHZPtr(candidate_array[i - 1].hz.word_item),
sizeof(HZ) * candidate_array[i].hz.word_item->ci_length))
continue;
candidate_array[new_count] = candidate_array[i];
new_count++;
}
return new_count;
}
/** 调整词频数据到字频数据,用于进行排序的比较。
* 字词都在同一个基准上进行。
*
* 词频计算方法:
* freq = freq_ori / 4;
* if (freq > 500000)
* freq = 500000 + freq / 2000;
* if (freq > (1 << 19) - 1)
* freq = (1 << 19) - 1;
*
* 字频计算方法:
* freq = freq_ori / 2;
* if (freq > 8000000)
* freq = 8000000 + freq / 2000;
* if (freq > (1 << 23) - 1)
* freq = (1 << 23) - 1;
*/
int AdjustWordFreqToHZFreq(int ci_freq)
{
int zi_freq;
//将词频转换到原始语料词频
if (ci_freq > BASE_CI_FREQ)
ci_freq = (ci_freq - BASE_CI_FREQ) * 2000;
ci_freq *= 4;
//将原始词频转换为字频
zi_freq = ci_freq / 2;
if (zi_freq > BASE_ZI_FREQ)
zi_freq = BASE_ZI_FREQ + zi_freq / 2000;
if (zi_freq > MAX_ZI_FREQ)
zi_freq = MAX_ZI_FREQ;
return zi_freq;
}
/** 将汉字字频转换为原始字频
*/
int ConvertToRealHZFreq(int zi_freq)
{
if (zi_freq > 8000000)
return (zi_freq - 8000000) * 2000 * 2;
return zi_freq * 2;
}
/** 将词频转换为原始词频
*/
int ConvertToRealCIFreq(int ci_freq)
{
if (ci_freq > 500000)
return (ci_freq - 500000) * 2000 * 4;
return ci_freq * 4;
}
/* 获得汉字的使用度
*/
int GetHzUsedCount(HZITEM *item)
{
return share_segment->hz_cache.used_count[GetHzItemIndex(item)];
}
//对汉字进行排序,简单比较字频以及固顶字以及是否在cache中。
/*
static int CompareZi(const CANDIDATE *cand1, const CANDIDATE *cand2)
{
int used_count1, used_count2;
int cache_pos1, cache_pos2;
int freq1, freq2;
//返回值大于0时cand2在cand1前,返回值小于0时cand1在cand2前
//排序方式:固顶字在最前(固顶字之间按ini文件里的顺序);若非固顶字,
//并设置了快速调整字频,若都是字,在Cache中序号大的(后进入Cache)在
//前,若不都是字,使用度高的在前;若设置了慢速调整字频,使用度高的
//在前;若没有设置字频调整,若都是字,字频高的在前,若不都是字,将
//是词的候选项的词频换算为字频再比较,字频高的在前
//特别注意CiCache和ZiCache结构区别很大,其cache_pos意义也不同,请
//认真阅读其定义
//xian->西安,xian->现
//在字、词混合排序的时候,用“使用度”进行;在没有使用度的情况下,用
//调整后的词频、字频进行判断
if (cand1->hz.is_word)
{
freq1 = cand1->hz.word_item->freq;
freq1 = AdjustWordFreqToHZFreq(freq1);
cache_pos1 = cand1->hz.cache_pos; //CiCache中的位置
if (cand1->hz.cache_pos != -1)
used_count1 = GetCiUsedCount(cache_pos1);
else
used_count1 = 0;
}
else
{
freq1 = cand1->hz.item->freq;
cache_pos1 = GetZiCachePos(cand1->hz.item); //ZiCache中的序号(这个pos不代表位置,详见HZCACHE中的注释)
used_count1 = GetHzUsedCount(cand1->hz.item);
//当前是全集模式 and 候选是繁体字不是简体字,减小字频
if (pim_config->hz_output_mode == HZ_OUTPUT_HANZI_ALL &&
cand1->hz.item->traditional && !cand1->hz.item->simplified)
freq1 = freq1 >> 10;
}
if (cand2->hz.is_word)
{
freq2 = cand2->hz.word_item->freq;
freq2 = AdjustWordFreqToHZFreq(freq2);
cache_pos2 = cand2->hz.cache_pos;
if (cand2->hz.cache_pos != -1)
used_count2 = GetCiUsedCount(cache_pos2);
else
used_count2 = 0;
}
else
{
freq2 = cand2->hz.item->freq;
cache_pos2 = GetZiCachePos(cand2->hz.item);
used_count2 = GetHzUsedCount(cand2->hz.item);
//当前是全集模式 and 候选是繁体字不是简体字,减小字频
if (pim_config->hz_output_mode == HZ_OUTPUT_HANZI_ALL &&
cand2->hz.item->traditional && !cand2->hz.item->simplified)
freq2 = freq2 > 10;
}
//判断固顶字。注意在输入繁体的时候失效!!!
if (!(pim_config->hz_output_mode & HZ_OUTPUT_TRADITIONAL) && (pim_config->hz_option & HZ_USE_FIX_TOP))
{
//词与字在这个问题上判断相同
if (cand1->hz.top_pos != cand2->hz.top_pos)
return cand1->hz.top_pos - cand2->hz.top_pos;
}
//快速调整字频
if (pim_config->hz_option & HZ_ADJUST_FREQ_FAST)
{
if (!cand1->hz.is_word && !cand2->hz.is_word)
{
if (cache_pos1 != cache_pos2)
return cache_pos2 - cache_pos1;
}
else
{
if (used_count1 != used_count2)
return used_count2 - used_count1;
}
}
//慢速调整字频
if (pim_config->hz_option & HZ_ADJUST_FREQ_SLOW)
{
if (used_count1 != used_count2)
return used_count2 - used_count1;
}
//比较字频
if (pim_config->hz_output_mode & HZ_OUTPUT_TRADITIONAL)
if (!cand1->hz.is_word && !cand2->hz.is_word)
return (cand2->hz.item->freq - cand1->hz.item->freq);
return freq2 - freq1;
}
*/
static int CompareZi(const CANDIDATE *cand1, const CANDIDATE *cand2)
{
int used_count1, used_count2;
int freq1, freq2;
if (cand1->hz.is_word)
{
freq1 = cand1->hz.word_item->freq;
freq1 = AdjustWordFreqToHZFreq(freq1);
if (cand1->hz.cache_pos != -1)
used_count1 = GetCiUsedCount(cand1->hz.cache_pos);
else
used_count1 = 0;
}
else
{
freq1 = cand1->hz.item->freq;
used_count1 = GetHzUsedCount(cand1->hz.item);
//当前是全集模式 and 候选是繁体字不是简体字,减小字频
if (pim_config->hz_output_mode == HZ_OUTPUT_HANZI_ALL &&
cand1->hz.item->traditional && !cand1->hz.item->simplified)
freq1 = freq1 >> 10;
}
if (cand2->hz.is_word)
{
freq2 = cand2->hz.word_item->freq;
freq2 = AdjustWordFreqToHZFreq(freq2);
if (cand2->hz.cache_pos != -1)
used_count2 = GetCiUsedCount(cand2->hz.cache_pos);
else
used_count2 = 0;
}
else
{
freq2 = cand2->hz.item->freq;
used_count2 = GetHzUsedCount(cand2->hz.item);
//当前是全集模式且候选是繁体字不是简体字,减小字频
if (pim_config->hz_output_mode == HZ_OUTPUT_HANZI_ALL &&
cand2->hz.item->traditional && !cand2->hz.item->simplified)
freq2 = freq2 > 10;
}
//判断固顶字。注意在输入繁体的时候失效!!!
if (!(pim_config->hz_output_mode & HZ_OUTPUT_TRADITIONAL) && (pim_config->hz_option & HZ_USE_FIX_TOP))
{
//词与字在这个问题上判断相同
if (cand1->hz.top_pos != cand2->hz.top_pos)
return cand1->hz.top_pos - cand2->hz.top_pos;
}
//快速或慢速调整字频
if (pim_config->hz_option & HZ_ADJUST_FREQ_FAST || pim_config->hz_option & HZ_ADJUST_FREQ_SLOW)
{
if (used_count1 != used_count2)
return used_count2 - used_count1;
}
//比较字频
if (pim_config->hz_output_mode & HZ_OUTPUT_TRADITIONAL)
if (!cand1->hz.is_word && !cand2->hz.is_word)
return (cand2->hz.item->freq - cand1->hz.item->freq);
return freq2 - freq1;
}
static void SortZiCandidatesInternal(CANDIDATE *candidate_array, int count)
{
int i, j, k;
CANDIDATE temp;
//繁体字不考虑固定字问题
if (!(pim_config->hz_output_mode & HZ_OUTPUT_TRADITIONAL) && (pim_config->hz_option & HZ_USE_FIX_TOP))
{
for (i = 0; i < count; i++)
{
if (candidate_array[i].hz.top_pos == DEFAULT_TOP_POS)
{
break;
}
}
//固定字不参与下面的排序
candidate_array += i;
count -= i;
}
for (i = 0; i < count; i++)
{
if (candidate_array[i].hz.is_word)
{
continue;
}
k = i;
for (j = i + 1; j < count; j++)
{
if (candidate_array[j].hz.is_word)
{
continue;
}
//注意hz.cache_pos里只存小音节词的cache位置
if (GetZiCachePos(candidate_array[j].hz.item) > GetZiCachePos(candidate_array[k].hz.item))
{
k = j;
}
}
if (k != i)
{
//移动而非交换(保证选择排序的无序区里是按使用度有序的)
temp = candidate_array[k];
memcpy(candidate_array + i + 1, candidate_array + i, (k - i) * sizeof(CANDIDATE));
candidate_array[i] = temp;
}
}
}
//对汉字候选进行排序
void SortZiCandidates(CANDIDATE *candidate_array, int count)
{
if (count < 2) //至少两个才能进行比较
return;
qsort(candidate_array, count, sizeof(CANDIDATE), CompareZi);
//若是快速字频调整,还要进一步处理,详见bugsys A00000022
if (pim_config->hz_option & HZ_ADJUST_FREQ_FAST)
{
SortZiCandidatesInternal(candidate_array, count);
}
}
/* 判断汉字是否包含特定的音调。
* 参数:
* hz 汉字
* syllable 汉字音节
* tone 被判断的音调
*
* 返回:
* 候选汉字数目。
*/
int ZiContainTone(HZ hz, SYLLABLE syllable, int tone)
{
int start = 0;
int end = hz_data->hz_count - 1;
int mid;
if (tone == TONE_0) //没有指定音调
return 1;
//用二分法查找到这个汉字
while(start <= end)
{
mid = (start + end) / 2;
//声母有区别
if (hz_data->hz_item[mid].syllable.con > syllable.con)
{
end = mid - 1;
continue;
}
else if (hz_data->hz_item[mid].syllable.con < syllable.con)
{
start = mid + 1;
continue;
}
//韵母有区别
if (hz_data->hz_item[mid].syllable.vow > syllable.vow)
{
end = mid - 1;
continue;
}
else if (hz_data->hz_item[mid].syllable.vow < syllable.vow)
{
start = mid + 1;
continue;
}
//字有区别
if (hz_data->hz_item[mid].hz > hz)
{
end = mid - 1;
continue;
}
if (hz_data->hz_item[mid].hz < hz)
{
start = mid + 1;
continue;
}
//找到!
break;
}
if (start > end) //没有找到
return 0;
return (hz_data->hz_item[mid].syllable.tone & tone) != 0;
}
/* 获得汉字候选。
* 参数:
* syllable 音节
* candidate_array 候选数组
* array_length 候选数组长度
* fuzzy_mode 模糊设置
* set_mode 汉字集合选项:常用(进化)、全集
* output_mode 字输出选项:繁体、简体
* 注:如果为繁体,则肯定是全集汉字集合。
* 返回:
* 候选汉字数目。
*/
int GetZiCandidates(SYLLABLE syllable, CANDIDATE *candidate_array, int array_length, int fuzzy_mode, int set_mode, int output_mode)
{
int i, j, count; //候选计数器
int topzi_count; //置顶字数目
HZ top_zi[MAX_TOPZI]; //置顶字
int check_top_zcs_fuzzy = 0; //置顶字zcs模糊
extern int LoadHZDataResource();
if (!array_length)
return 0;
if (!share_segment->hz_data_loaded)
LoadHZDataResource();
if (!hz_data)
{
hz_data = GetReadOnlySharedMemory(hz_data_share_name);
//可能存在其他进程已经装载了,但是退出后共享内存被释放的问题
if (!hz_data && share_segment->hz_data_loaded)
{
share_segment->hz_data_loaded = 0;
LoadHZDataResource();
}
}
if (!hz_data)
return 0;
topzi_count = GetTopZi(syllable, top_zi);
//由于s=是,c=出,z=中重 的设置,所以必须对zcs的模糊进行处理,找出
//带有固定字的候选
if (topzi_count && syllable.vow == VOW_NULL &&
(syllable.con == CON_Z || syllable.con == CON_C || syllable.con == CON_S))
check_top_zcs_fuzzy = 1;
count = 0;
//检索汉字
for (i = 0; i < hz_data->hz_count; i++)
{
//判断简繁体是否符合
if (!(
((output_mode & HZ_OUTPUT_HANZI_ALL)) || //输出全集汉字
((output_mode & HZ_OUTPUT_ICW_ZI) && hz_data->hz_item[i].icw_hz) || //输出ICW使用的汉字
((output_mode & HZ_OUTPUT_SIMPLIFIED) && hz_data->hz_item[i].simplified) || //输出简体字,字是简体
((output_mode & HZ_OUTPUT_TRADITIONAL) && (hz_data->hz_item[i].traditional || hz_data->hz_item[i].other)) //输出繁体字,字是繁体或该字未分类
))
continue;
if (check_top_zcs_fuzzy)
{
if (!ContainSyllableWithTone(syllable, hz_data->hz_item[i].syllable, fuzzy_mode | FUZZY_Z_ZH | FUZZY_C_CH | FUZZY_S_SH))
continue;
//判断是否为置顶字
for (j = 0; j < topzi_count; j++)
if (top_zi[j] == LOWORD(hz_data->hz_item[i].hz))
break;
//不是置顶字的话,需要重新进行音调的判断
if (j == topzi_count)
if (!ContainSyllableWithTone(syllable, hz_data->hz_item[i].syllable, fuzzy_mode))
continue; //拼音不相符
}
else if (!ContainSyllableWithTone(syllable, hz_data->hz_item[i].syllable, fuzzy_mode))
continue; //拼音不相符
candidate_array[count].type = CAND_TYPE_ZI;
candidate_array[count].hz.is_word = 0;
candidate_array[count].hz.item = &hz_data->hz_item[i];
candidate_array[count].hz.hz_type = ZI_TYPE_NORMAL;
//判断是否为置顶字
for (j = 0; j < topzi_count; j++)
if (top_zi[j] == LOWORD(hz_data->hz_item[i].hz))
break;
//设置置顶字序号,如果没有,则设为最大值
if (j == topzi_count)
j = DEFAULT_TOP_POS;
candidate_array[count].hz.top_pos = j;
count++;
if (count >= array_length) //数组过长
break;
}
return count;
}
HZITEM* GetSingleZiCandidate(TCHAR zi)
{
int i;
for (i = 0; i < hz_data->hz_count; i++)
{
if (zi == hz_data->hz_item[i].hz)
return &hz_data->hz_item[i];
}
return 0;
}
/* 完整处理音节到汉字候选。
* 处理过程:
* 1. 检索汉字候选
* 2. 汉字候选排重
* 3. 候选排序
* 本函数将依照系统配置与IC配置中的当前模糊音、检索结果集以及输出集合
* 设置进行处理。
*
* 参数:
* syllable 音节
* *candidate_array 候选数组
* array_length 候选数组长度
* 返回值:
* 返回候选数目
*/
int ProcessZiCandidates(SYLLABLE syllable, CANDIDATE *candidate_array, int array_length, int zi_level)
{
int zi_count = 0, normal_zi_count = 0, small_count = 0, small_ci_count = 0, small_zi_count = 0;
SYLLABLE small_syllables[2];
CANDIDATE small_word_candidates[0x10];
//寻找类似xian输入西安的候选,双拼不用考虑
if (/*pim_config->special_parse_pin_yin && */pim_config->pinyin_mode != PINYIN_SHUANGPIN && GetSmallSyllables(syllable, small_syllables))
{
int i;
small_ci_count = ProcessCiCandidate(small_syllables,
2,
0,
small_word_candidates,
_SizeOf(small_word_candidates),
1);
for (i = 0; i < small_ci_count && i < array_length; i++)
{
candidate_array[i].type = CAND_TYPE_ZI;
candidate_array[i].hz.is_word = 1;
candidate_array[i].hz.word_item = small_word_candidates[i].word.item;
candidate_array[i].hz.origin_syllable = syllable;
candidate_array[i].hz.top_pos = DEFAULT_TOP_POS;
candidate_array[i].hz.cache_pos =
GetCiCacheInfo(GetItemHZPtr(candidate_array[i].hz.word_item), candidate_array[i].hz.word_item->ci_length);
}
//检索小音节拆分字
small_zi_count = GetZiCandidates(small_syllables[0],
candidate_array + small_ci_count,
array_length - small_ci_count,
pim_config->use_fuzzy ? pim_config->fuzzy_mode : 0,
zi_level,
pim_config->hz_output_mode);
//如果没有找到汉字,如eng,则必须扩大汉字的范围进行查找
if (!small_zi_count)
small_zi_count = GetZiCandidates(small_syllables[0],
candidate_array + small_ci_count,
array_length - small_ci_count,
pim_config->use_fuzzy ? pim_config->fuzzy_mode : 0,
HZ_ALL_USED,
HZ_OUTPUT_HANZI_ALL);
for (i = small_ci_count; i < small_ci_count + small_zi_count; i++)
{
candidate_array[i].hz.hz_type = ZI_TYPE_OTHER;
}
small_count = small_ci_count + small_zi_count;
}
//检索普通字
normal_zi_count = GetZiCandidates(syllable,
candidate_array + small_count,
array_length - small_count,
pim_config->use_fuzzy ? pim_config->fuzzy_mode : 0,
zi_level,
pim_config->hz_output_mode);
//如果没有找到汉字,如eng,则必须扩大汉字的范围进行查找
if (!normal_zi_count)
normal_zi_count = GetZiCandidates(syllable,
candidate_array + small_count,
array_length - small_count,
pim_config->use_fuzzy ? pim_config->fuzzy_mode : 0,
HZ_ALL_USED,
HZ_OUTPUT_HANZI_ALL);
zi_count = small_zi_count + normal_zi_count;
//去掉无法显示的汉字
zi_count = DeleteUnreadableZiCandidates(candidate_array + small_ci_count, zi_count);
//排重(只针对字)
zi_count = UnifyZiCandidates(candidate_array + small_ci_count, zi_count);
//排序(字和小音节词都要排)
SortZiCandidates(candidate_array, small_ci_count + zi_count);
return small_ci_count + zi_count;
}
/* 向Cache中增加汉字项。
* 参数:
* item 汉字项
* 返回:无
*/
static void AddHzToCache(HZITEM *item)
{
int i;
//如果字为固定方式,则不需要重新写入
if (pim_config->hz_option & HZ_ADJUST_FREQ_NONE)
return;
share_segment->hz_cache.cache[GetHzItemIndex(item)] = share_segment->hz_cache.max_id; //将本汉字赋予最新标志
share_segment->hz_cache.max_id++; //累计当前最大的id
//达到最大值后,将所有的值都减去最大值即可
if (share_segment->hz_cache.max_id >= HZ_CACHE_MAX_ID)
{
for (i = 0; i < _SizeOf(share_segment->hz_cache.cache); i++)
share_segment->hz_cache.cache[i] -= HZ_CACHE_MAX_ID;
share_segment->hz_cache.max_id -= HZ_CACHE_MAX_ID;
}
share_segment->zi_cache_modified = 1;
}
/* 获得汉字项在Cache中的ID。
* 参数:
* item 汉字项指针
* 返回:
* 在Cache中的使用ID(越大越近使用)
*/
static int GetZiCachePos(HZITEM *item)
{
return share_segment->hz_cache.cache[GetHzItemIndex(item)];
}
/* 调整字的使用度
*/
static void AddHzToUsedCount(HZITEM *item)
{
//如果字为固定方式,则不需要重新写入
if (pim_config->hz_option & HZ_ADJUST_FREQ_NONE)
return;
share_segment->hz_cache.used_count[GetHzItemIndex(item)]++;
share_segment->zi_cache_modified = 1;
}
/* 处理汉字被选中后的更新事务。
* 调整字频,更新Cache。
* 参数:
* item 汉字项
* 返回:无
*/
void ProcessZiSelected(HZITEM *item)
{
//更新汉字使用度信息
AddHzToUsedCount(item);
//更新汉字Cache信息
AddHzToCache(item);
}
/** 处理以词定字的字的更新
* 找到与该字对应的HZITEM(最大字频),进行处理
*/
void ProcessZiSelectedByWord(HZ hz, SYLLABLE syllable)
{
int i, index = -1;
int max_freq = -1;
//todo:可能需要对音调进行处理
//检索汉字
for (i = 0; i < hz_data->hz_count; i++)
{
if (hz_data->hz_item[i].hz != hz)
continue;
if (!ContainSyllableWithTone(syllable, hz_data->hz_item[i].syllable, 0))
continue; //拼音不相符
if ((int)hz_data->hz_item[i].freq > max_freq)
{
index = i;
max_freq = hz_data->hz_item[i].freq;
}
}
if (index != -1) //找到
ProcessZiSelected(&hz_data->hz_item[index]);
}
/** 装载置顶字文件
* 返回:
* 成功:1
* 失败:0
*/
int LoadTopZiData(const TCHAR *name)
{
TCHAR *buffer;
TCHAR line[0x400];
int length, count, index;
TCHAR *syllable_string, *zi_string;
int syllable_length, zi_count, i;
SYLLABLE syllable;
if (share_segment->topzi_loaded)
return 1;
length = GetFileLength(name);
if (length <= 0)
return 0;
buffer = (TCHAR*)malloc(length);
if (!LoadFromFile(name, buffer, length))
{
free(buffer);
return 0;
}
index = 1;
length = length / sizeof(TCHAR);
share_segment->topzi_table_items = 0;
while(share_segment->topzi_table_items < MAX_SYLLABLES && index < length)
{
//获得一行数据
count = 0;
while(index < length && count < _SizeOf(line) - 1)
{
line[count++] = buffer[index++];
if (buffer[index - 1] == 0xa)
break;
}
line[count] = 0; //得到一行数据
if ('#' == line[0]) //注释
continue;
if (count && line[count - 1] == 0xa)
line[--count] = 0;
if (count && line[count - 1] == 0xd)
line[--count] = 0;
syllable_string = _tcstok(line, TEXT("="));
zi_string = _tcstok(0, TEXT("="));
//没有音、字或者音、字长度为0,忽略
if (!zi_string || zi_string[0] == 0 || !syllable_string || syllable_string[0] == 0)
continue;
//音节解析失败,忽略