forked from zhujian198/unispim
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathci.c
1421 lines (1223 loc) · 69.5 KB
/
ci.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
/* 词汇处理模块
*/
#include <assert.h>
#include <tchar.h>
#include <kernel.h>
#include <syllable.h>
#include <wordlib.h>
#include <config.h>
#include <utility.h>
#include <zi.h>
#include <ci.h>
#include <share_segment.h>
//#pragma data_seg(HYPIM_SHARED_SEGMENT)
//CICACHE ci_cache = {CI_CACHE_V66_SIGNATURE, 0, 0, 0};
//int ci_cache_loaded = 0; //是否已经装入
//int ci_cache_modified = 0; //是否改变
//
//NEWCI new_ci = {0, {0},};
//int new_ci_loaded = 0; //是否装入
//int new_ci_modified = 0; //是否已经修改
//#pragma data_seg()
/* 保存词Cache数据。
* 参数:
* ci_cache_file_name 词Cache文件名称
* 返回:
* 成功:1
* 失败:0
*/
int SaveCiCacheData(const TCHAR *ci_cache_file_name)
{
if (!share_segment->ci_cache_modified)
return 1;
share_segment->ci_cache_modified = 0;
if (!SaveToFile(ci_cache_file_name, &share_segment->ci_cache, sizeof(share_segment->ci_cache)))
{
Log(LOG_ID, L"保存词Cache数据失败,file:%s", ci_cache_file_name);
return 0;
}
return 1;
}
void CheckAndUpgradeCiCache()
{
if (0 == share_segment->ci_cache.length || CI_CACHE_V66_SIGNATURE == share_segment->ci_cache.signature)
return;
if (CI_CACHE_SIGNATURE == share_segment->ci_cache.signature)
{
int pos = 0;
int item_length = (char)share_segment->ci_cache.cache[pos] * sizeof(HZ);
share_segment->ci_cache.signature = CI_CACHE_V66_SIGNATURE;
//遍历词的cache
while(pos + item_length <= share_segment->ci_cache.length) //需要判定最后一个词条不越界
{
char ci_ansi[MAX_WORD_LENGTH * sizeof(HZ)] = {0};
TCHAR ci_uc[MAX_WORD_LENGTH] = {0};
memcpy_s(ci_ansi, MAX_WORD_LENGTH * sizeof(HZ), &share_segment->ci_cache.cache[pos + WORDLIB_FEATURE_LENGTH], item_length);
AnsiToUtf16(ci_ansi, ci_uc, MAX_WORD_LENGTH);
memcpy_s(&share_segment->ci_cache.cache[pos + WORDLIB_FEATURE_LENGTH], item_length, ci_uc, item_length);
//下一个词
pos += WORDLIB_FEATURE_LENGTH + share_segment->ci_cache.cache[pos] * sizeof(HZ);
item_length = (char)share_segment->ci_cache.cache[pos] * sizeof(HZ);
}
}
else
{
share_segment->ci_cache.signature = CI_CACHE_V66_SIGNATURE;
share_segment->ci_cache.length = 0;
share_segment->ci_cache.max_used_id = 0;
}
return;
}
/* 装载词Cache数据。
* 参数:
* ci_cache_file_name 词Cache文件名字
* 返回:
* 成功:1
* 失败:0
*/
int LoadCiCacheData(const TCHAR *ci_cache_file_name)
{
int length;
assert(ci_cache_file_name);
//初始化数据,避免文件装载失败造成程序崩溃
share_segment->ci_cache.length = share_segment->ci_cache.max_used_id = 0;
length = LoadFromFile(ci_cache_file_name, (char*)&share_segment->ci_cache, sizeof(share_segment->ci_cache));
if (length < 0)
{
share_segment->ci_cache.length = share_segment->ci_cache.max_used_id = 0;
Log(LOG_ID, L"读取词Cache文件出错,file:%s, length=%d", ci_cache_file_name, length);
return 0;
}
CheckAndUpgradeCiCache();
share_segment->ci_cache_loaded = 1;
return 1;
}
/** 释放词Cache数据
*/
int FreeCiCacheData()
{
share_segment->ci_cache_loaded = 0;
return 1;
}
/* 在词Cache中找出词的位置。
* 参数:
* hz 词
* length 长度
* 返回:
* -1:没有找到
* >0 该词在Cache中的位置(越小越新)
*/
int GetCiCacheInfo(HZ *hz, int length)
{
int pos = 0;
int item_length = WORDLIB_FEATURE_LENGTH + sizeof(HZ) * length;
//遍历词的cache
while(pos + item_length <= share_segment->ci_cache.length) //需要判定最后一个词条不越界
{
if ((char)share_segment->ci_cache.cache[pos] == (char)length && //长度相同
!memcmp(&share_segment->ci_cache.cache[pos + WORDLIB_FEATURE_LENGTH], hz, length * sizeof(HZ))) //汉字相同
return pos;
//下一个词
pos += WORDLIB_FEATURE_LENGTH + share_segment->ci_cache.cache[pos] * sizeof(HZ);
}
//没有找到
return -1;
}
/* 词Cache的使用度到达临界值,需要将所有的词的使用度减少。
* 参数:无
* 返回:无
*/
void ReduceCiCacheUsedCount()
{
int pos = 0;
//遍历词条
while(pos < share_segment->ci_cache.length)
{
int used_count;
int length = share_segment->ci_cache.cache[pos];
if ((int)(pos + WORDLIB_FEATURE_LENGTH + sizeof(HZ) * length) >= share_segment->ci_cache.length) //越界
break;
used_count = *(int*)&share_segment->ci_cache.cache[pos] >> 8;
used_count -= CI_MAX_USED_COUNT / 2; //减少一半的值
*(int*)&share_segment->ci_cache.cache[pos] = (used_count << 8) + length;
pos += WORDLIB_FEATURE_LENGTH + sizeof(HZ) * length;
}
}
/* 向词的cache中插入新的词。
* 参数:
* hz 词
* length 长度
* 返回:无
*/
void InsertCiToCache(HZ *hz, int length, int syllable_length, int set_fix_top)
{
int pos; //Cache中的位置
int item_length; //本词条的长度
int used_count; //词汇使用度
int i;
char item_save[WORDLIB_FEATURE_LENGTH + sizeof(HZ) * MAX_WORD_LENGTH]; //临时保存区
//如果以当前的位置确定词的顺序,则不能向词频中增加内容
if (pim_config->ci_option & CI_ADJUST_FREQ_NONE)
return;
if (length > MAX_WORD_LENGTH)
return;
//UCS4的词不加入Cache
if (length != syllable_length)
return;
item_length = length * sizeof(HZ) + WORDLIB_FEATURE_LENGTH;
//以下这个词是否出现在Cache中?
pos = GetCiCacheInfo(hz, length);
if (pos != -1) //找到
{
used_count = *(int*)&share_segment->ci_cache.cache[pos] >> 8; //找出词的使用度
used_count++;
}
else //没有找到的话,当作插入到cache末尾处
{
used_count = 1;
pos = share_segment->ci_cache.length;
share_segment->ci_cache.length = pos + item_length;
share_segment->ci_cache.cache[pos] = (char)length;
for (i = 0; i < (int)(length * sizeof(HZ)); i++)
share_segment->ci_cache.cache[pos + i + WORDLIB_FEATURE_LENGTH] = *((char*)hz + i); //将汉字拷贝到Cache
}
if (set_fix_top)
used_count = CI_TOP_USED_COUNT;
else if (CI_TOP_USED_COUNT == used_count)
used_count++;
*(int*)&share_segment->ci_cache.cache[pos] = (used_count << 8) + share_segment->ci_cache.cache[pos]; //更新使用度
//调整顺序
//(0)(1)....(pos-1)(pos)(pos+1)...(cache_length-1) ====> (pos)(0)(1)...(pos-1)(pos+1)...(cache_length-1)
//保存当前词条
for (i = 0; i < item_length; i++)
item_save[i] = share_segment->ci_cache.cache[pos + i];
for (i = pos - 1; i >= 0; i--)
share_segment->ci_cache.cache[i + item_length] = share_segment->ci_cache.cache[i];
for (i = 0; i < item_length; i++)
share_segment->ci_cache.cache[i] = item_save[i];
//将越界的词Cache长度减少回来
if (share_segment->ci_cache.length > CI_CACHE_SIZE)
share_segment->ci_cache.length = CI_CACHE_SIZE;
//如果使用度越界,则需要将所有词项的使用度减少
if (used_count > CI_MAX_USED_COUNT)
ReduceCiCacheUsedCount();
share_segment->ci_cache_modified = 1;
return;
}
/** 返回词的使用度
*/
int GetCiUsedCount(int cache_pos)
{
if (cache_pos < 0)
return 0;
return *(int*)&share_segment->ci_cache.cache[cache_pos] >> 8;
}
/* 比较词中的汉字以及词频,用于处理词的候选顺序。
* 比较过程:
* 1. 比较两个词在Cache中的位置
* 2. 比较两个词的词频
* 参数:
* cand1, cand2 用于比较的两个候选指针
* 返回:
* 第一个比第二个小 <0 表示cand1排在cand2前
* 第一个等于第二个 =0
* 第一个大于第二个 >0 表示cand2排在cand1前
*/
static int CompareCi(const CANDIDATE *cand1, const CANDIDATE *cand2)
{
int cache_pos1, cache_pos2;
int used_count1, used_count2;
//排序方式:置顶词排在非置顶词前;若都是置顶词或者都不是置顶词,看是否
//设置了快速调整词频,若设置,在Cache中位置靠前的词排在前面(新近使用并
//加入Cache的词),位置靠后的词排在后面,不在Cache中的词排在最后;若未设
//置,看是否设置了慢速词频调整,若设置,使用度高的词排在前面,使用度低
//的词排在后面,不在Cache中的词排在最后;若未设置,词频高的排在前面,词
//频低的排在后面
//特别注意CiCache和ZiCache结构区别很大,其cache_pos意义也不同,请
//认真阅读其定义
cache_pos1 = cand1->word.cache_pos;
cache_pos2 = cand2->word.cache_pos;
used_count1 = GetCiUsedCount(cache_pos1);
used_count2 = GetCiUsedCount(cache_pos2);
if (CI_TOP_USED_COUNT == used_count1 && CI_TOP_USED_COUNT != used_count2)
return -1;
else if (CI_TOP_USED_COUNT != used_count1 && CI_TOP_USED_COUNT == used_count2)
return 1;
//快速调整词频
if (pim_config->ci_option & CI_ADJUST_FREQ_FAST)
{
//不是同一个词
if (cache_pos1 != cache_pos2)
{
//cand1不在Cache中(cand2不可能同时不在Cache中,
//否则不满足上面的if条件),cand2在Cache中,cand1
//排在后面
if (cache_pos1 == -1)
return 1;
//cand1在Cache中,cand2不在Cache中,cand1排在前面
//(返回-1)
if (cache_pos2 == -1)
return -1;
//在Cache中位置靠前的排在前面
return cache_pos1 - cache_pos2;
}
}
//慢速词频调整
if (pim_config->ci_option & CI_ADJUST_FREQ_SLOW)
{
if (cache_pos1 != cache_pos2)
{
if (cache_pos1 == -1)
return 1;
if (cache_pos2 == -1)
return -1;
if (used_count1 != used_count2)
return used_count2 - used_count1;
}
}
//比较词频
return cand2->word.item->freq - cand1->word.item->freq;
}
/* 比较词中的汉字以及词频,用于处理词的排重
* 参数:
* cand1, cand2 用于比较的两个候选指针
* 返回:
* 第一个比第二个小 <0
* 第一个等于第二个 =0
* 第一个大于第二个 >0
*/
static int CompareCiCodeFreq(const CANDIDATE *cand1, const CANDIDATE *cand2)
{
int ret;
assert(cand1 && cand2);
//长度不同
if (cand1->word.item->ci_length != cand2->word.item->ci_length)
return cand2->word.item->ci_length - cand1->word.item->ci_length;
//如果词的字不同则返回两个词的内码差
ret = memcmp(cand2->word.hz, cand1->word.hz, sizeof(HZ) * cand1->word.item->ci_length);
if (ret)
return ret;
//如果两个词中有一个是被删除的,则排在前面
if (cand1->word.item->effective && !cand2->word.item->effective)
return 1;
if (cand2->word.item->effective && !cand1->word.item->effective)
return -1;
//比较字频
return cand2->word.item->freq - cand1->word.item->freq;
}
/* 对词进行排序。
* 参数:
* candidates 词候选
* count 候选个数
* 返回:无
*/
void SortCiCandidates(CANDIDATE *candidates, int count)
{
if (count < 2)
return;
qsort(candidates, count, sizeof(CANDIDATE), CompareCi);
}
/* 词候选排重。
* 参数:
* candidates 词候选数组
* count 数组长度
* 返回:
* 排重后的候选数目
*/
int UnifyCiCandidates(CANDIDATE *candidates, int count)
{
int new_count, i;
if (!count) //两个以下词不用排重
return count;
//以hz内码顺序进行排序,两个相同的词汇将处于一起的位置
qsort(candidates, count, sizeof(CANDIDATE), CompareCiCodeFreq);
//排重
for (new_count = 1, i = 1; i < count; i++)
{
if (candidates[i].word.item->ci_length == candidates[i - 1].word.item->ci_length &&
!memcmp(candidates[i].word.hz, candidates[i - 1].word.hz, sizeof(HZ) * candidates[i].word.item->ci_length))
continue;
candidates[new_count] = candidates[i];
new_count++;
}
count = new_count;
//删除已经删除的词汇
for (new_count = 0, i = 0; i < count; i++)
{
if (!candidates[i].word.item->effective) //找到被删除的词条
continue;
candidates[new_count++] = candidates[i];
}
return new_count;
}
/* 获得下一条词的指针。
* 参数:
* item 当前词指针
* 返回:
* 下一条词的指针
*/
WORDLIBITEM *GetNextCiItem(WORDLIBITEM *item)
{
//feature + syllable + hz
return (WORDLIBITEM*)((char*)item + sizeof(int) + sizeof(SYLLABLE) * item->syllable_length + sizeof(HZ) * item->ci_length);
}
/* V6B1词库,获得下一条词的指针。
* 参数:
* item 当前词指针
* 返回:
* 下一条词的指针
*/
WORDLIBITEMV6B1 *GetNextCiItemV6B1(WORDLIBITEMV6B1 *item)
{
//feature + syllable + hz
return (WORDLIBITEMV6B1*)((char*)item + sizeof(int) + sizeof(SYLLABLE) * item->length + sizeof(HZ) * item->length);
}
/** 获得词汇的汉字指针
*/
HZ *GetItemHZPtr(WORDLIBITEM *item)
{
return (HZ*)((char*)item + sizeof(int) + sizeof(SYLLABLE) * item->syllable_length);
}
/** 获得词汇的音节指针
*/
SYLLABLE *GetItemSyllablePtr(WORDLIBITEM *item)
{
return (SYLLABLE*)((char*)item + sizeof(int));
}
/** 基于词长以及音节长度确定词项的长度
*/
int GetItemLength(int hz_length, int syllable_length)
{
return sizeof(HZ) * hz_length + sizeof(SYLLABLE) * syllable_length + WORDLIB_FEATURE_LENGTH;
}
/* 获得页中的词候选(没有通配符)。
* 参数:
* page 页指针
* syllable_array 音节序列
* syllable_count 音节长度
* candidate_array 候选数组
* candidate_length 候选数组长度
* fuzzy_mode 模糊音选项
* 返回:
* 候选词汇数目
*/
int GetCiInPage(PAGE *page, SYLLABLE *syllable_array, int syllable_count, CANDIDATE *candidate_array, int candidate_length, int fuzzy_mode)
{
WORDLIBITEM *item; //词项
int count = 0; //候选计数
if (!candidate_length)
return 0;
//如果页中没有包含这个长度的词,则直接返回(为提高检索效率)
if (!(page->length_flag & (1 << syllable_count)))
return 0;
//遍历页表
for (item = (WORDLIBITEM*) page->data; (char*)item < (char*) &page->data + page->data_length; item = GetNextCiItem(item))
{
if (!(fuzzy_mode & FUZZY_CI_SYLLABLE_LENGTH))
if (item->ci_length != item->syllable_length)
continue;
//判断长度是否符合
if (item->syllable_length != syllable_count)
continue;
//判断音节是否符合
if (!CompareSyllables(syllable_array, item->syllable, syllable_count, fuzzy_mode))
continue;
//找到!
candidate_array[count].word.type = CI_TYPE_NORMAL;
candidate_array[count].word.item = item;
candidate_array[count].word.cache_pos = 0;
candidate_array[count].word.syllable = (SYLLABLE*)((char*)item + sizeof(int));
candidate_array[count].word.hz = GetItemHZPtr(item);
count++;
if (count >= candidate_length)
break;
}
return count;
}
/* 获得页中的词候选(使用通配符)。
* 参数:
* page 页指针
* syllable_array 音节序列
* syllable_count 音节长度
* candidate_array 候选数组
* candidate_length 候选数组长度
* fuzzy_mode 模糊音选项
* 返回:
* 候选词汇数目
*/
int GetCiInPageWild(PAGE *page, SYLLABLE *syllable_array, int syllable_count, CANDIDATE *candidate_array, int candidate_length, int fuzzy_mode)
{
WORDLIBITEM *item; //词项
int count = 0; //候选计数
if (!(pim_config->ci_option & CI_WILDCARD))
return 0;
if (!candidate_length)
return 0;
//如果页中没有包含这个长度的词,则直接返回(为提高检索效率)
if ((unsigned int)page->length_flag < (unsigned int)(1 << syllable_count))
{
if (page->length_flag != 1) //1 << 32 = 1, 所以必须加这个判断
return 0;
}
//遍历页表
for (item = (WORDLIBITEM*) page->data; (char*)item < (char*) &page->data + page->data_length; item = GetNextCiItem(item))
{
if (!(fuzzy_mode & FUZZY_CI_SYLLABLE_LENGTH))
if (item->ci_length != item->syllable_length)
continue;
//判断长度是否符合
if ((int)item->syllable_length < syllable_count)
continue;
//判断音节是否符合
if (!WildCompareSyllables(syllable_array, syllable_count, item->syllable, item->syllable_length, fuzzy_mode))
continue;
//找到!
candidate_array[count].word.type = CI_TYPE_NORMAL;
candidate_array[count].word.item = item;
candidate_array[count].word.cache_pos = 0;
candidate_array[count].word.syllable = (SYLLABLE*)((char*)item + sizeof(int));
candidate_array[count].word.hz = GetItemHZPtr(item);
count++;
if (count >= candidate_length)
break;
}
return count;
}
/* 使用首字母,获得页中的词候选(没有通配符)。
* 参数:
* page 页指针
* letters 音节序列
* letter_count 音节长度
* candidate_array 候选数组
* candidate_length 候选数组长度
* fuzzy_mode 模糊音选项
* 返回:
* 候选词汇数目
*/
int GetCiInPageByLetter(PAGE *page, const TCHAR *letters, int letter_count, CANDIDATE *candidate_array, int candidate_length)
{
WORDLIBITEM *item; //词项
int count = 0; //候选计数
if (!candidate_length || !letters)
return 0;
//如果页中没有包含这个长度的词,则直接返回(为提高检索效率)
if (!(page->length_flag & (1 << letter_count)))
return 0;
//遍历页表
for (item = (WORDLIBITEM*) page->data; (char*)item < (char*) &page->data + page->data_length; item = GetNextCiItem(item))
{
//判断长度是否符合
if (letter_count != item->syllable_length)
continue;
//判断音节是否符合
if (!CompareSyllablesAndLetters(letters, item->syllable, letter_count))
continue;
//找到!
candidate_array[count].word.type = CI_TYPE_LETTER;
candidate_array[count].word.item = item;
candidate_array[count].word.cache_pos = 0;
candidate_array[count].word.syllable = GetItemSyllablePtr(item);
candidate_array[count].word.hz = GetItemHZPtr(item);
count++;
if (count >= candidate_length)
break;
}
return count;
}
/* 使用首字母,获得页中的词候选(使用通配符)。
* 参数:
* page 页指针
* letters 音节序列
* letter_count 音节长度
* candidate_array 候选数组
* candidate_length 候选数组长度
* 返回:
* 候选词汇数目
*/
int GetCiInPageByWildLetter(PAGE *page, const TCHAR *letters, int letter_count, CANDIDATE *candidate_array, int candidate_length)
{
WORDLIBITEM *item; //词项
int count = 0; //候选计数
if (!candidate_length || !letters)
return 0;
//如果页中没有大于等于这首字母个数的词,则直接返回(为提高检索效率)
if ((unsigned int)page->length_flag < (unsigned int)(1 << letter_count))
{
if (page->length_flag != 1)
return 0;
}
//遍历页表
for (item = (WORDLIBITEM*) page->data; (char*)item < (char*) &page->data + page->data_length; item = GetNextCiItem(item))
{
//判断长度是否符合
if (letter_count < pim_config->first_letter_input_min_hz)
{
if (item->syllable_length != letter_count)
continue;
}
else if ((int)item->syllable_length < pim_config->first_letter_input_min_hz)
continue;
//判断音节是否符合
if (!WildCompareSyllablesAndLetters(letters, (int)_tcslen(letters), item->syllable, item->syllable_length))
continue;
//找到!
candidate_array[count].word.type = CI_TYPE_LETTER;
candidate_array[count].word.item = item;
candidate_array[count].word.cache_pos = 0;
candidate_array[count].word.syllable = GetItemSyllablePtr(item);
candidate_array[count].word.hz = GetItemHZPtr(item);
count++;
if (count >= candidate_length)
break;
}
return count;
}
/* 获得词汇候选。
* 参数:
* wordlib_id 词库标识
* syllable_array 音节序列
* syllable_count 音节长度
* candidate_array 候选数组
* candidate_length 候选数组长度
* fuzzy_mode 模糊音选项
* 返回:
* 候选词汇数目
*/
int GetCiCandidates(int wordlib_id, SYLLABLE *syllable_array, int syllable_count, CANDIDATE *candidate_array, int candidate_length, int fuzzy_mode)
{
WORDLIB *wordlib; //词库指针
int page_no; //页号
int count = 0; //候选计数
int i, j;
SYLLABLE syllable_tmp1, syllable_tmp2;
int wild_compare = 0; //是否使用通配比较
if (!candidate_length || wordlib_id < 0)
return 0;
//音节过少或者过大
if (syllable_count < 2 || syllable_count > MAX_WORD_LENGTH)
return 0;
wordlib = GetWordLibrary(wordlib_id);
if (!wordlib) //没有这个词库
return 0;
//查看是否需要进行通配比较
for (i = 0; i < syllable_count; i++)
if (syllable_array[i].con == CON_ANY) //带有通配符
break;
wild_compare = i != syllable_count;
//用于比较的临时音节初始化
syllable_tmp1.con = syllable_tmp2.con = CON_NULL;
syllable_tmp1.vow = syllable_tmp2.vow = VOW_NULL;
syllable_tmp1.tone = syllable_tmp2.tone = TONE_0;
//由于有模糊音的存在,所以必须进行声母的遍历才能找出全部正确的声母。
for (i = CON_NULL; i < CON_END; i++)
{
syllable_tmp1.con = i;
if (!ContainCon(syllable_array[0], syllable_tmp1, fuzzy_mode))
continue;
//找到第一个相同的音节
for (j = CON_NULL; j < CON_END; j++)
{
syllable_tmp2.con = j;
if (!ContainCon(syllable_array[1], syllable_tmp2, fuzzy_mode))
continue;
//音节序列的词库页索引
page_no = wordlib->header.index[i][j];
//遍历页表找出词候选
while(page_no != PAGE_END)
{
count += wild_compare
?
GetCiInPageWild( //使用通配符
&wordlib->pages[page_no],
syllable_array,
syllable_count,
candidate_array + count,
candidate_length - count,
fuzzy_mode)
:
GetCiInPage( //不使用通配符
&wordlib->pages[page_no],
syllable_array,
syllable_count,
candidate_array + count,
candidate_length - count,
fuzzy_mode);
if (count >= candidate_length)
break;
page_no = wordlib->pages[page_no].next_page_no;
}
if (count >= candidate_length)
break;
}
}
//设置词库标识
for (i = 0; i < count; i++)
{
candidate_array[i].word.source = wordlib_id;
candidate_array[i].type = CAND_TYPE_CI;
}
return count;
}
/* 获得词汇候选。
* 参数:
* wordlib_id 词库标识
* letters 首字母串
* candidate_array 候选数组
* candidate_length 候选数组长度
* 返回:
* 候选词汇数目
*/
int GetCiCandidatesByLetter(int wordlib_id, const TCHAR *letters, CANDIDATE *candidate_array, int candidate_length)
{
WORDLIB *wordlib; //词库指针
SYLLABLE syllable_tmp1, syllable_tmp2; //临时音节
int page_no; //页号
int count = 0; //候选计数
int length = 0; //首字母数目
int wild_compare = 0; //是否使用通配比较
int i, j, k, l;
assert(letters);
//检查首字母合法性
while(letters[length])
{
if (letters[length] == SYLLABLE_ANY_CHAR)
wild_compare = 1;
else if (letters[length] < 'a' || letters[length] > 'z')
return 0;
else if (letters[length] == 'i' || letters[length] == 'v' || letters[length] == 'u')
return 0;
length++;
};
//音节过小、过大
if (length < 2 || length > MAX_WORD_LENGTH)
return 0;
wordlib = GetWordLibrary(wordlib_id);
if (!wordlib) //没有这个词库
return 0;
//进行声母的遍历,找出全部正确的声母。
for (i = CON_NULL; i < CON_END; i++)
{
for (j = VOW_NULL; j < VOW_END; j++)
{
syllable_tmp1.con = i;
syllable_tmp1.vow = j;
if (!SyllableStartWithLetter(letters[0], syllable_tmp1))
continue;
j = VOW_END; //一旦找到,就不进行内层循环
//找到第一个相同的音节
for (k = CON_NULL; k < CON_END; k++)
{
for (l = VOW_NULL; l < VOW_END; l++)
{
syllable_tmp2.con = k;
syllable_tmp2.vow = l;
if (!SyllableStartWithLetter(letters[1], syllable_tmp2))
continue;
l = VOW_END; //一旦找到,就不进行内层循环
//音节序列的词库页索引
page_no = wordlib->header.index[i][k];
//遍历页表找出词候选
while(page_no != PAGE_END)
{
count += wild_compare
? GetCiInPageByWildLetter( //使用通配符
&wordlib->pages[page_no],
letters,
length,
candidate_array + count,
candidate_length - count)
: GetCiInPageByLetter( //不使用通配符
&wordlib->pages[page_no],
letters,
length,
candidate_array + count,
candidate_length - count);
page_no = wordlib->pages[page_no].next_page_no;
}
}
}
}
}
//设置词库标识
for (i = 0; i < count; i++)
{
candidate_array[i].word.source = wordlib_id;
candidate_array[i].type = CAND_TYPE_CI;
}
return count;
}
/* 处理词汇被用户选中后的操作:插入到Cache中,增加用户使用度。
* 参数:
* ci_cand 词候选
* 返回:无
*/
void ProcessCiSelected(SYLLABLE *syllable, int syllable_length, HZ *hz, int hz_length)
{
/*
//加入到用户词库中,并且更新词条信息
if (pim_config->insert_used_word_to_user_wl)
AddCi(syllable, syllable_length, hz, hz_length);
*/
//插入到词cache中
InsertCiToCache(hz, hz_length, syllable_length, 0);
}
/* 基于音调删除不正确的词。
* 一般词库中的词都不附带音调,需要进行字的反查。
* 如:z4f 能够捡出的词包括:征服,但“征”字并没有四声,因此必须除掉
*
* 参数:
* syllable_array 音节数组
* syllable_count 音节计数
* candidate_array 候选数组
* candidate_count 候选数目
* 返回:
* 剩余候选数目
*/
int DeleteCiCandidateByTone(SYLLABLE *syllable_array, int syllable_count, CANDIDATE *candidate_array, int candidate_count)
{
int i, j;
int new_count = 0;
int has_tone = 0, has_wild = 0;
//检查音节是否带有音调
for (i = 0; i < syllable_count; i++)
{
if (syllable_array[i].tone != TONE_0)
has_tone = 1;
if (syllable_array[i].con == CON_ANY)
has_wild = 1;
}
if (!has_tone) //没有带有音调的音或者包含通配符(已经排除),直接返回
return candidate_count;
if (has_wild)
{
for (i = 0; i < candidate_count; i++)
{
if (!WildCompareSyllablesWithCi(syllable_array, syllable_count, candidate_array[i].word.syllable, candidate_array[i].word.hz, candidate_array[i].word.item->syllable_length, 0))
{
candidate_array[i] = candidate_array[candidate_count - 1];
candidate_count--;
i--;
continue;
}
}
return candidate_count;
}
for (i = 0; i < candidate_count; i++)
{
for (j = 0; j < syllable_count; j++)
{
if (syllable_array[j].tone != TONE_0 && !ZiContainTone(candidate_array[i].word.hz[j], candidate_array[i].word.syllable[j], syllable_array[j].tone))
break;
}
if (j != syllable_count) //不符合音调要求
{
candidate_array[i] = candidate_array[candidate_count - 1];
candidate_count--;
i--;
continue;
}
}
return candidate_count;
}
/* 基于音节处理词的候选。
* 1. 获得候选(音节候选以及单字母候选)
* 2. 排重
* 3. 排序
* 参数:
* syllable_array 音节数组
* syllable_count 音节长度
* letters 用户输入串,为0时为不操作
* candidate_array 候选数组
* candidate_length 候选数组长度
* same_ci_syllable_length 是否需要词与音节的长度相同
* 返回:
* 候选数目
*/
int ProcessCiCandidate(SYLLABLE *syllable_array, int syllable_count, const TCHAR *letters, CANDIDATE *candidate_array, int candidate_length, int same_ci_syllable_length)
{
int count = 0; //候选数目
int fuzzy_mode = pim_config->use_fuzzy ? pim_config->fuzzy_mode : 0; //模糊方式