-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathegi_unihan.c
5918 lines (4944 loc) · 198 KB
/
egi_unihan.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
/*-----------------------------------------------------------------------------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
UNIHAN (Hanzi) 汉字
UNIHAN_SET 汉字集
UNIHANGROUP (Cizu) 词组
UNIHANGROUP_SET 词组集
PINYIN Chinese phoneticizing
Note:
1. See Unicode Character Database in http://www.unicode.org.
# Unicode Character Database
# © 2020 Unicode®, Inc.
# Unicode and the Unicode Logo are registered trademarks of Unicode, Inc. in the U.S. and other countries.
# For terms of use, see http://www.unicode.org/terms_of_use.html
# For documentation, see http://www.unicode.org/reports/tr38/
2. With reference to:
2.1 https://blog.csdn.net/firehood_/article/details/7648625
2.2 https://blog.csdn.net/FortuneWang/article/details/41575039
2.3 http://www.unicode.org/Public/UCD/latest/ucd/Unihan.zip (Unihan_Readings.txt)
2.3 http://www.unicode.org/reports/tr38
3. Procedure for generating UniHan data file for EGI_PINYIN IME:
3.1 Run test_pinyin3500.c to generate and save PINYIN3500 UinHan set.
fil=fopen(PINYIN3500_TXT_PATH,"r");
read line to uniset....
UniHan_save_set(uniset, PINYIN3500_DATA_PATH)
3.2 Load unisetMandarin from text MANDARIN_TXT_PATH. (test_pinyin.c)
unisetMandarin=UniHan_load_MandarinTxt(MANDARIN_TXT_PATH)
3.3 Merge unisetMandarin into uniset.
UniHan_merge_set(unisetMandarin, uniset)
PINYIN3500_TXT_PATH ----> uniset ------merge to ---> uniset
|
MANDARIN_TXT_PATH ----> unisetMandarin ---|
3.4 Sort and purify uniset.
UniHan_quickSort_set(uniset, UNIORDER_WCODE_TYPING_FREQ, 10)
UniHan_purify_set(uniset)
3.5 Generate frequency values for UniHans in uniset.
UniHan_poll_freq(uniset, "/mmc/xyj_all.txt") ....
3.6 Sort UniHan set.
UniHan_quickSort_set(uniset, UNIORDER_TYPING_FREQ, 10)
3.7 Save to UniHan data.
UniHan_save_set(uniset, UNIHANS_DATA_PATH)
3.8. MANDARIN_TXT lacks PINYIN/readings for some polyphonic UNIHANs, example: 单(chan). If PINYIN3500_TXT
also misses that PINYIN/reading, manually add it in PINYIN3500_TXT, and then run above procedure again.
4. Procedure for generating UniHanGroup data file for EGI_PINYIN IME:
4.1 Load UinHan data.
han_set=UniHan_load_set(UNIHANS_DATA_PATH);
4.2 Load UniHanGroup data from text. ( <----- OR text fed back from 4.7 -----
Text line format: Cizu+PINYIN.
Example: 词语 ci yu (PINYIN may be omitted, and leave it to UniHanGroup_assemble_typings() ).
group_set=UniHanGroup_load_CizuTxt(CIZU_TXT_PATH);
4.3 Assemble typings for UniHanGroup ( For text fed back from 4.7, MAY NOT necessary! )
UniHanGroup_assemble_typings(group_set, han_set)
4.4 Merge UniHan to UniHanGroup set. nch==1 unihan_groups!
UniHanGroup_load_uniset(group_set, han_set)
4.5 Sort UniHanGroup set.
UniHanGroup_quickSort_typings(group_set ...);
4.6 Save to UniHanGroup data. It also contains all UniHans(as nch=1 unihan_groups)!
UniHanGroup_save_set(group_set, UNIHANGROUPS_DATA_PATH).
4.7 Save UniHanGroup to a text. ( --------> for further manual PINYIN modification, then -------> Feed back to 4.2
UniHanGroup_saveto_CizuTxt(group_set, "/tmp/cizu+pinyin.txt"), ONLY Cizu(nch>1 groups) are saved to text!
Note: Since some assembled PINYINs are incorrect, we may modify them manually in the text, then reload
to group_set by calling UniHanGroup_load_CizuTxt().
!!! This text file will be improved iteratively !!!
x.x TODO: poll freq.
5. Expend UniHanGroup data by merging.
5.1 Collect new Words/Cizu/Phrases and put them into a text file.
Text line fromat: Cizu+PINYIN.
Example: 词语 ci yu (PINYIN may be omitted)
( TODO: Also you may add new polyphonic UniHan in the text file and merged into group set,
but it needs to export to unihan set later.)
5.2 Read above text into a UniHanGroup_Set:
group_expend=UniHanGroup_load_CizuTxt(PINYIN_NEW_WORDS_FPATH);
5.3 Merge group_expend into group_set and purify to clear redundancy:
UniHanGroup_merge_set(group_expend, group_set);
UniHanGroup_purify_set(group_set); Mem holes exists in uchar/typings after purification.
5.4 Follow 4.5, 4.6 and 4.7 to save to UniHanGroup data for EGI_PINYIN IME.
6. Call UniHanGroup_update_typings() to modify typings.
Example to correct Cizus containing '行':
1. Export group_test to a text file:
UniHanGroup_saveto_CizuTxt(group_set,"/mmc/group_test.txt").
2. Extract all Cizus/Phrases containing the same polyphoinc UNIHAN '行':
cat /mmc/group_test.txt | grep 行 > /mmc/update_words.txt
3. Modify all typings in update_words.txt manually.
4. Then reload update_words.txt into update_set by calling UniHanGroup_load_CizuTxt().
5. Replace typings in group_set by calling UniHanGroup_update_typings(group_set, update_set);
Journal:
2021-06-15:
1. Add polyphonic UNIHAN 盛(cheng) to unihans_pinyin.dat
Midas Zhou
[email protected](Not in use since 2022_03_01)
------------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <ctype.h>
#include <errno.h>
#include "egi_debug.h"
#include "egi_unihan.h"
#include "egi_cstring.h"
#include "egi_math.h"
#include "egi_utils.h"
//static inline int strstr_group_typings(const char *hold_typings, const char* try_typings);
static inline int compare_group_typings( const char *group_typing1, int nch1, const char *group_typing2, int nch2);
/*----------------------------------------------------------------------------
Parse an unbroken string into several groups of PINYIN typings.
A PINYIN typing typically consists of a Shengmu(initial consonant) and a
Yunmu(vowel/vowels). In some cases, a legitimat PINYIN MAY have NO Shengmu at
all (Zero Shengmu).
The result is in form of UNIHAN.typing, NOT UNIHAN.reading!
(See difinitions of 'typing' AND 'reading' in egi_unihan.h)
Note:
1. Zero Shengmu PINYINs: ( However, the function will treat all Yunmus as Zero Shengmu PINTYINs! )
a(啊) ai(爱) ao an ang
e(额)er(而) en
o(喔) ou
2. Shengmu(inital consonant) list(23):
(But 'y,w' here are treated as Shengmu)
b,p,m,f,d,t,n,l,g,k,h,j,q,x,zh,ch,sh,r,z,c,s,y,w
3. Yunmu(vowel/vowels) list:
a,o,e,
i,u,ü (v)
ai,ei,ui,
ao,ou,iu,
ie,üe,er (ve) [ yüe -> yue ]
an,en,in,un,ün (vn) [ yün -> yun ]
ang,eng,ing,ong
(other compounds)
ua, uo, iou, uei
iao, uai, uei, uen, ueng
ian, uan, üan, iang, uang, iong
Notice that 'iou','uei','uen' 'ueng' are to be written as 'iu','ui','un','ong' respectively when preceded by a Shengmu.
( --- Yunmus in Typings as Classified --- )
a,ai,ao,an,ang
o,ou,ong,
e,ei,er,en,eng,
i,iu,ie,in,ia, iao, ian, ing, iang, iong
u,ui,uo,ue,un,ua,uai,uan,uang
v,ve,vn, (van ?)
(as ü replaced by v in typings )
( All 26 alphabets will be the initial char of a Shenmu/Yunmu except 'v' )
4. If there is ambiguous for parsing an unbroken pingyin string, such as "fangan", use '(0x27) to separate.
"xian" is parsed to: xian
"xi'an" is parsed to: xi an
"haobangai" is parsed to: hao bang ai
"ha'o'ban'gai" is parsed to: ha o ban gai
TODO:
1. The function will treat all Yunmus as Zero Shengmu PINTYINs!
2. Some Shengmu and Yunmu are NOT compitable:
Example: xa#,xo#,xe#,xv#
be bia biu....
zhi zhing chin ching shin shing ---- OK.
@strp: An unbroken string of alphabet chars in lowercases,
terminated with a '\0'.
Note:
1. It SHOULD occupy UNIHAN_TYPING_MAXLEN*n bytes mem space,
as the function will try to read the content in above range
withou checking position of the string terminal NULL!
2. A terminal NULL '\0' will be written to strp as end of
dividing n groups!
@pinyin: To store parsed groups of PINYINs.
Each group takes UNIHAN_TYPING_MAXLEN bytes, including '\0's.
So it MUST hold at least UNIHAN_TYPING_MAXLEN*n bytes mem space.
@n: Max. groups of PINYINs expected.
Return:
>0 OK, total number of PINYIN typings as divided into.
<=0 Fails
----------------------------------------------------------------------------*/
int UniHan_divide_pinyin(char *strp, char *pinyin, int n)
{
int i;
int np; /* count total number of PINYIN typings */
const char *ps=NULL; /* Start of a complete PINYIN typing (Shengmu+Yunmu OR Yunmu) */
const char *pc=strp;
bool zero_shengmu; /* True if has NO Shengmu */
bool Shengmu_Is_ZCS=false; /* z,zh,c,ch,s,sh */
if( strp==NULL || pinyin ==NULL || n<1 )
return -1;
/* Clear data */
memset(pinyin, 0, n*UNIHAN_TYPING_MAXLEN);
np=0;
for( i=0; i<n+1; i++) {
while( *pc != '\0' ) { /* pc always points to the start of a new Shengmu or Yunmu */
/* If i==4: break. as a full end of 4 groups. See below explanation. */
if( i==n )
break;
ps=pc; /* mark start of a PINYIN typing */
zero_shengmu=false; /* Assume it has shengmu */
Shengmu_Is_ZCS=false; /* z,zh,c,ch,s,sh */
/* 1. Get Shengmu(initial consonant) */
switch(*pc)
{
/* b,p,m,f,d,t,n,l,g,k,h,j,q,x,zh,ch,sh,r,z,c,s,y,w */
case 'b': case 'p' : case 'm' : case 'f' : case 'd' : case 't' :
case 'n': case 'l' : case 'g' : case 'k' : case 'h' : case 'j' :
case 'q': case 'x' : case 'r' : case 'y' : case 'w' :
pc++;
break;
case 'z':
case 'c':
case 's':
Shengmu_Is_ZCS=true;
switch( *(pc+1) ) {
case 'h': /* zh, ch, sh */
pc +=2;
break;
default: /* z,c,s */
pc++;
break;
}
break;
/* Other case */
case 'v': /* A 'v' will never be an initial char of a Shengmu or Yunmu */
case 0x27: /* ' As separator */
pc++; /* get rid of it */
continue;
//break;
/* unrecognizable Shengmu, OR start of a Yunmu */
default:
/* Get rid of other chars ?? */
if( !isalpha(*pc) || !islower(*pc) ) {
pc++;
continue;
}
/* ELSE: a zero_Shengmu PINYIN, go on to parse Yunmu... */
zero_shengmu=true;
/* keep pc unchanged */
break;
}
/* 2. Get Yunmu(vowel/vowels) */
switch (*pc)
{
/* a, ai, ao, an, ang */
case 'a':
switch(*(pc+1)) {
case 'i': /* ai */
case 'o': /* ao */
pc+=2;
break;
case 'n':
if(*(pc+2)=='g') /* ang */
pc+=3;
else /* an */
pc+=2;
break;
default: /* a */
pc ++;
break;
}
break;
/* o,ou,ong */
case 'o':
if( *(pc+1)=='u' ) /* ou */
pc +=2;
else if( *(pc+1)=='n' && *(pc+2)=='g' ) /* ong */
pc +=3;
else if( *(pc+1)=='n') /* To appear on ! */
pc +=2;
else /* o */
pc++;
break;
/* e,ei,er,en,eng */
case 'e':
switch(*(pc+1)) {
case 'i': /* ei */
pc+=2;
break;
case 'r': /* er */
if(zero_shengmu)
pc+=2;
else
pc++;
break;
case 'n':
if(*(pc+2)=='g') /* eng */
pc+=3;
else /* en */
pc+=2;
break;
default: /* e */
pc++;
break;
}
break;
/* i,iu,ie,in,ing, ia, iao, ian, iang, iong */
case 'i':
switch(*(pc+1)) {
case 'u': /* iu */
case 'e': /* ie */
pc+=2;
break;
case 'n':
/* To avoid incompitable combinations:
* cin,cing,sin,sing,zin,zing; chin,ching,shin,shing,zhin,zhing
*/
if(Shengmu_Is_ZCS) {
pc +=1;
break;
}
if(*(pc+2)=='g') /* ing */
pc+=3;
else /* in */
pc+=2;
break;
case 'a':
switch(*(pc+2)) {
case 'o':
pc+=3; /* iao */
break;
case 'n':
if(*(pc+3)=='g') /* iang */
pc+=4;
else /* ian */
pc+=3;
break;
default: /* takes only 'i' */
pc+=2; /* ia */
break;
}
break;
case 'o':
if( *(pc+2)=='n' && *(pc+3)=='g' ) /* iong */
pc+=4;
else if( *(pc+2)=='n' )
pc+=3; /* Just to appear ion! */
else if( *(pc+2)=='n' ) /* To appear ion !! */
pc+=3;
else
pc+=2; /* To appear 'io' */ /* i */
break;
default:
pc++;
break;
}
break;
/* u,ui,uo,ue,un,ua,uai,uan,uang */
case 'u':
switch(*(pc+1)) {
case 'i': /* ui */
case 'o': /* ua */
case 'e': /* ue */
case 'n': /* un */
pc+=2;
break;
case 'a':
switch(*(pc+2)) {
case 'i':
pc+=3; /* uai */
break;
case 'n':
if(*(pc+3)=='g') /* uang */
pc+=4;
else /* uan */
pc+=3;
break;
default: /* ua */
pc+=2; /* taks only ua */
break;
}
break;
default:
pc++; /* u */
break;
}
break;
/* v,ve,vn*/
case 'v':
switch(*(pc+1)) {
case 'e': /* ve */
case 'n': /* vn */
pc+=2;
break;
default:
pc++; /* u */
break;
}
break;
/* Unrecognizable vowel/vowels, OR start of a Shengmu */
default:
/* keep pc unchanged */
break;
}
/* All 26 alphabets will be the initial char of a Shenmu or a Yunmu, except 'v'! */
/* 3. Copy a complete PINYIN typing(Shengmu+Yunmu or Yunmu) to pinyin[] */
strncpy(pinyin+i*UNIHAN_TYPING_MAXLEN, ps, pc-ps);
np++;
/* 4. For the next PINYIN */
break; // break while()
} /* while() */
} /* for() */
/* If i==n+1: full end of n groups. put terminal to pstr then!!
* Example: 'ling dian xing dong', 'jin yan jiao xiong'.
* Here 'ong' and 'iong' are special vowels, as 'on' and 'ion' are NOT vowels!
* Others 'iang'-'ia','ian', 'uang'-'ua','uan',... are all vowels!
*/
if(i==n+1)
*(char *)pc='\0'; /* ps as start pointer of redundant chars. */
return np;
}
/*-------------------------------------------------
Create an UNIHAN_HEAP with empty data.
@capacity: Max. number of EGI_UNIHANs to be
hold in the heap.
Return:
Pointer to an EGI_UNIHAN_HEAP Ok
NULL Fails
--------------------------------------------------*/
EGI_UNIHAN_HEAP* UniHan_create_heap(size_t capacity)
{
EGI_UNIHAN_HEAP *heap=NULL;
if(capacity==0)
return NULL;
/* Calloc heap */
heap = calloc(1, sizeof(EGI_UNIHAN_HEAP));
if(heap==NULL) {
printf("%s: Fail to calloc unihan heap.\n",__func__);
return NULL;
}
/* Calloc heap->unihans */
heap->unihans = calloc(capacity+1, sizeof(EGI_UNIHAN)); /* +1 more as for sentinel, as binary heap index starts from 1, NOT 0. */
if(heap->unihans==NULL) {
printf("%s: Fail to calloc heap->unihans.\n",__func__);
free(heap);
return NULL;
}
/* Assign memebers */
heap->capacity=capacity;
heap->size=0;
return heap;
}
/*---------------------------------------
Free an EGI_UNIHAN_HEAP.
----------------------------------------*/
void UniHan_free_heap( EGI_UNIHAN_HEAP **heap)
{
if(heap==NULL || *heap==NULL)
return
free( (*heap)->unihans );
free(*heap);
*heap=NULL;
}
/*------------------------------------------------
Create a set of unihan, with empty data.
@name: Short name for the set, MAX len 16-1.
If NULL, ignore.
@capacity: Capacity to total max. number of unihans in the set.
Return:
A pointer to EGI_UNIHAN_SET Ok
NULL Fail
------------------------------------------------*/
EGI_UNIHAN_SET* UniHan_create_set(const char *name, size_t capacity)
{
EGI_UNIHAN_SET *unihan_set=NULL;
if(capacity==0)
return NULL;
/* Calloc unihan set */
unihan_set = calloc(1, sizeof(EGI_UNIHAN_SET));
if(unihan_set==NULL) {
printf("%s: Fail to calloc unihan_set.\n",__func__);
return NULL;
}
/* Calloc unihan_set->unihans */
unihan_set->unihans = calloc(capacity+1, sizeof(EGI_UNIHAN)); /* +1 as for END token */
if(unihan_set->unihans==NULL) {
printf("%s: Fail to calloc unihan_set->unihans.\n",__func__);
free(unihan_set);
return NULL;
}
/* Assign memebers */
//printf("%s:sizeof(unihan_set->name)=%d\n", __func__, sizeof(unihan_set->name));
if(name != NULL)
strncpy(unihan_set->name, name, sizeof(unihan_set->name)-1);
unihan_set->capacity=capacity;
/* unihan_set->size=0 */
return unihan_set;
}
/*---------------------------------------
Free an EGI_UNIHAN_SET.
----------------------------------------*/
void UniHan_free_set( EGI_UNIHAN_SET **set)
{
if( set==NULL || *set==NULL )
return;
free( (*set)->unihans );
free(*set);
*set=NULL;
}
#if 0 ///////////////////////////////////////////////////////
/*----------------------------------------------------------------
Insert an unihan into the heap as per "Priority Binary Heap" Rule.
the inserted member is copied from given unihan.
@heap: Pointer to an EGI_UNIHAN_HEAP
@unihan: Pointer to an EGI_UNIHAN
Return:
0 OK
<0 Fails
----------------------------------------------------------------*/
int UniHan_heap_insert(EGI_UNIHAN_HEAP* heap, const EGI_UNIHAN* unihan)
{
int i;
if( heap==NULL || heap->unihans==NULL )
return -1;
if(heap->size >= heap->capacity) {
printf("%s: No more space for unihans. Please consider to increase capacity.\n",__func__);
return -2;
}
heap->size++;
/* If the first entry, index==1 */
if(size==1) {
heap->unihans[1]=*unihan; /* Direct assign */
return 0;
}
/* Bubble i up, as to find the right sequence for unihan.typing[] */
for( i=heap->size; UniHan_compare_typing(unihan, heap->unihans[i/2])<0; i /= 2 )
heap->unihans[i]=unihnas[i/2]; /* binary */
}
#endif //////////////////////////////
/*---------------------------------------------
Reset freq of all unihans in the uniset to 0.
---------------------------------------------*/
void UniHan_reset_freq( EGI_UNIHAN_SET *uniset )
{
int i;
/* Check input */
if(uniset==NULL || uniset->unihans==NULL || uniset->size==0) /* If empty */
return;
for(i=0; i < uniset->size; i++)
uniset->unihans[i].freq=0;
}
/*-------------------------------------------------------------------------
Compare typing+freq ORDER of two unihans and return an integer less than, equal to,
or greater than zero, if unha1 is found to be ahead of, at same order of,
or behind uhans2 in dictionary order respectively.
Note:
1. !!! CAUTION !!! Here typing refers to PINYIN, other type MAY NOT work.
2. All typing letters MUST be lowercase.
3. It first compares their dictionary order, if NOT in the same order, return
the result; if in the same order, then compare their frequency number to
decide the order as in 4.
4. If uhan1->freq is GREATER than uhan2->freq, it returns -1(IS_AHEAD);
else if uhan1->freq is LESS than uhan2->freq, then return 1(IS_AFTER);
if EQUAL, it returns 0(IS_SAME).
5. An empty UNIHAN(wcode==0) is always 'AFTER' a nonempty UNIHAN.
6. A NULL is always 'AFTER' a non_NULL UNIHAN pointer.
Return:
Relative Priority Sequence position:
CMPORDER_IS_AHEAD -1 (<0) unhan1 is 'less than' OR 'ahead of' unhan2
CMPORDER_IS_SAME 0 (=0) unhan1 and unhan2 are at the same order.
CMPORDER_IS_AFTER 1 (>0) unhan1 is 'greater than' OR 'behind' unhan2.
-------------------------------------------------------------------------*/
int UniHan_compare_typing(const EGI_UNIHAN *uhan1, const EGI_UNIHAN *uhan2)
{
int i;
/* 1. If uhan1 and/or uhan2 is NULL */
if( uhan1==NULL && uhan2==NULL )
return CMPORDER_IS_SAME;
else if( uhan1==NULL )
return CMPORDER_IS_AFTER;
else if( uhan2==NULL )
return CMPORDER_IS_AHEAD;
/* 2. Put the empty EGI_UNIHAN to the last, OR check typing[0] ? */
if(uhan1->wcode==0 && uhan2->wcode!=0)
return CMPORDER_IS_AFTER;
else if(uhan1->wcode!=0 && uhan2->wcode==0)
return CMPORDER_IS_AHEAD;
else if(uhan1->wcode==0 && uhan2->wcode==0)
return CMPORDER_IS_SAME;
/* 3. Compare dictionary oder of EGI_UNIHAN.typing[] */
for( i=0; i<UNIHAN_TYPING_MAXLEN; i++) {
if( uhan1->typing[i] > uhan2->typing[i] )
return CMPORDER_IS_AFTER;
else if ( uhan1->typing[i] < uhan2->typing[i])
return CMPORDER_IS_AHEAD;
/* End of both typings, EOF */
else if(uhan1->typing[i]==0 && uhan2->typing[i]==0)
break;
/* else: uhan1->typing[i] == uhan2->typing[i] != 0 */
}
/* NOW: CMPORDER_IS_SAME */
/* 4. Compare frequencey number: EGI_UNIHAN.freq */
if( uhan1->freq > uhan2->freq )
return CMPORDER_IS_AHEAD;
else if( uhan1->freq < uhan2->freq )
return CMPORDER_IS_AFTER;
else
return CMPORDER_IS_SAME;
}
/*-------------------------------------------------------------------------
Compare wcode+typing(+freq) ODER of two unihans and return an integer less than, equal to,
or greater than zero, if unha1 is found to be ahead of, at same order of,
or behind uhans2 in dictionary order respectively.
Note:
1. !!! CAUTION !!! Here typing refers to PINYIN, other type MAY NOT work.
2. All typing letters MUST be lowercase.
3. It first compares their wcode value, if NOT in the same order, return
the result; if in the same order, then compare their typing value to
decide the order.
4. An empty UNIHAN(wcode==0) is always 'AFTER' a nonempty UNIHAN !!!
5. A NULL is always 'AFTER' a non_NULL UNIHAN pointer.
Return:
Relative Priority Sequence position:
CMPORDER_IS_AHEAD -1 (<0) unhan1 is 'less than' OR 'ahead of' unhan2
CMPORDER_IS_SAME 0 (=0) unhan1 and unhan2 are at the same order.
CMPORDER_IS_AFTER 1 (>0) unhan1 is 'greater than' OR 'behind' unhan2.
-------------------------------------------------------------------------*/
int UniHan_compare_wcode(const EGI_UNIHAN *uhan1, const EGI_UNIHAN *uhan2)
{
/* 1. If uhan1 and/or uhan2 is NULL */
if( uhan1==NULL && uhan2==NULL )
return CMPORDER_IS_SAME;
else if( uhan1==NULL )
return CMPORDER_IS_AFTER;
else if( uhan2==NULL )
return CMPORDER_IS_AHEAD;
/* 2. Put the empty EGI_UNIHAN to the last */
if(uhan1->wcode==0 && uhan2->wcode!=0)
return CMPORDER_IS_AFTER;
else if(uhan1->wcode!=0 && uhan2->wcode==0)
return CMPORDER_IS_AHEAD;
else if(uhan1->wcode==0 && uhan2->wcode==0)
return CMPORDER_IS_SAME;
/* 3. Compare wcode value */
if(uhan1->wcode > uhan2->wcode)
return CMPORDER_IS_AFTER;
else if(uhan1->wcode < uhan2->wcode)
return CMPORDER_IS_AHEAD;
/* 4. If wcodes are the SAME, compare typing then */
return UniHan_compare_typing(uhan1, uhan2);
}
/*------------------------------------------------------------------------
Sort an EGI_UNIHAN array by Insertion_Sort algorithm, to rearrange unihans
in ascending order of typing+freq. (typing:in dictionary order)
Also ref. to mat_insert_sort() in egi_math.c.
Note:
1.The caller MUST ensure unihans array has at least n memebers!
2.Input 'n' MAY includes empty unihans with typing[0]==0, and they will
be rearranged to the end of the array.
@unihans: An array of EGI_UNIHANs.
@n: size of the array. ( NOT capacity )
------------------------------------------------------------------------*/
void UniHan_insertSort_typing( EGI_UNIHAN* unihans, int n )
{
int i; /* To tranverse elements in array, 1 --> n-1 */
int k; /* To decrease, k --> 1, */
EGI_UNIHAN tmp;
if(unihans==NULL)
return;
/* Start sorting ONLY when i>1 */
for( i=1; i<n; i++) {
tmp=unihans[i]; /* the inserting integer */
/* Slide the inseting integer left, until to the first smaller unihan */
for( k=i; k>0 && UniHan_compare_typing(unihans+k-1, &tmp)==CMPORDER_IS_AFTER; k--) {
unihans[k]=unihans[k-1]; /* swap */
}
/* Settle the inserting unihan at last swapped place */
unihans[k]=tmp;
}
}
/*------------------------------------------------------------------
Sort an EGI_UNIHAN array by Quick_Sort algorithm, to rearrange unihans
in ascending order of typing+freq.
To see UniHan_compare_typing() for the details of order comparing.
Refert to mat_quick_sort() in egi_math.h.
!!! WARNING !!! This is a recursive function.
Note:
1. The caller MUST ensure start/end are within the legal range.
@start: start index, as of unihans[start]
@End: end index, as of unihans[end]
@cutoff: cutoff value for switch to insert_sort.
Return:
0 Ok
<0 Fails
----------------------------------------------------------------------*/
int UniHan_quickSort_typing(EGI_UNIHAN* unihans, unsigned int start, unsigned int end, int cutoff)
{
int i=start;
int j=end;
int mid;
EGI_UNIHAN tmp;
EGI_UNIHAN pivot;
/* Check input */
if( unihans==NULL )
return -1;
/* End sorting */
if( start >= end )
return 0;
/* Limit cutoff */
if(cutoff<3)
cutoff=3;
/* 1. Implement quicksort */
if( end-start >= cutoff )
{
/* 1.1 Select pivot, by sorting array[start], array[mid], array[end] */
/* Get mid index */
mid=(start+end)/2;
/* Sort [start] and [mid] */
/* if( array[start] > array[mid] ) */
if( UniHan_compare_typing(unihans+start, unihans+mid)==CMPORDER_IS_AFTER ) {
tmp=unihans[start];
unihans[start]=unihans[mid];
unihans[mid]=tmp;
}
/* Now: [start]<=array[mid] */
/* IF: [mid] >= [start] > [end] */
/* if( array[start] > array[end] ) { */
if( UniHan_compare_typing(unihans+start, unihans+end)==CMPORDER_IS_AFTER ) {
tmp=unihans[start]; /* [start] is center */
unihans[start]=unihans[end];
unihans[end]=unihans[mid];
unihans[mid]=tmp;
}
/* ELSE: [start]<=[mid] AND [start]<=[end] */
/* else if( array[mid] > array[end] ) { */
if( UniHan_compare_typing(unihans+mid,unihans+end)==CMPORDER_IS_AFTER ) {
/* If: [start]<=[end]<[mid] */
tmp=unihans[end]; /* [end] is center */
unihans[end]=unihans[mid];
unihans[mid]=tmp;
/* Else: [start]<=[mid]<=[end] */
}
/* Now: array[start] <= array[mid] <= array[end] */
pivot=unihans[mid];
//printf("After_3_sort: %d, %d, %d\n", array[start], array[mid], array[end]);
/* Swap array[mid] and array[end-1], still keep array[start] <= array[mid] <= array[end]! */
tmp=unihans[end-1];
unihans[end-1]=unihans[mid]; /* !NOW, we set memeber [end-1] as pivot */
unihans[mid]=tmp;
/* 1.2 Quick sort: array[start] ... array[end-1], array[end] */
i=start;
j=end-1; /* As already sorted to: array[start]<=pivot<=array[end-1]<=array[end], see 1. above */
for(;;)
{
/* Stop at array[i]>=pivot: We preset array[end-1]==pivot as sentinel, so i will stop at [end-1] */
/* while( array[++i] < pivot ){ }; Acturally: array[++i] < array[end-1] which is the pivot memeber */
while( UniHan_compare_typing(unihans+(++i),&pivot)==CMPORDER_IS_AHEAD ) { };
/* Stop at array[j]<=pivot: We preset array[start]<=pivot as sentinel, so j will stop at [start] */
/* while( array[--j] > pivot ){ }; */
while( UniHan_compare_typing(unihans+(--j),&pivot)==CMPORDER_IS_AFTER ) { };
if( i<j ) {
/* Swap array[i] and array[j] */
tmp=unihans[i];
unihans[i]=unihans[j];
unihans[j]=tmp;
}
else {
break;
}
}
/* Swap pivot memeber array[end-1] with array[i], we left this step at last. */
unihans[end-1]=unihans[i];
unihans[i]=pivot; /* Same as array[i]=array[end-1] */
/* 1.3 Quick sort: recursive call for sorted parts. and leave array[i] as mid of the two parts */
UniHan_quickSort_typing(unihans, start, i-1, cutoff);
UniHan_quickSort_typing(unihans, i+1, end, cutoff);
}
/* 2. Implement insertsort */
else
UniHan_insertSort_typing( unihans+start, end-start+1);
return 0;
}
/*------------------------------------------------------------------------
Sort an EGI_UNIHAN array by Insertion_Sort algorithm, to rearrange unihans
with freq (as KEY) in ascending order.
Also ref. to mat_insert_sort() in egi_math.c.
Note:
1.The caller MUST ensure unihans array has at least n memebers!
@unihans: An array of EGI_UNIHANs.
@n: size of the array. ( NOT capacity )
-----------------------------------------------------------------------*/
void UniHan_insertSort_freq( EGI_UNIHAN* unihans, int n )
{
int i; /* To tranverse elements in array, 1 --> n-1 */
int k; /* To decrease, k --> 1, */
EGI_UNIHAN tmp;
if(unihans==NULL)
return;
/* Start sorting ONLY when i>1 */
for( i=1; i<n; i++) {
tmp=unihans[i];
/* Slide the inseting integer left, until to the first smaller unihan */
for( k=i; k>0 && unihans[k-1].freq > tmp.freq; k--)
unihans[k]=unihans[k-1]; /* swap */
/* Settle the inserting unihan at last swapped place */
unihans[k]=tmp;
}
}
/*--------------------------------------------------------------------
Sort an EGI_UNIHAN array by Quick_Sort algorithm, to rearrange unihans
with freq (as KEY) in ascending order.
Refert to mat_quick_sort() in egi_math.h.
!!! WARNING !!! This is a recursive function.
Note:
1. The caller MUST ensure start/end are within the legal range.
@start: start index, as of unihans[start]
@End: end index, as of unihans[end]
@cutoff: cutoff value for switch to insert_sort.
Return:
0 Ok
<0 Fails
----------------------------------------------------------------------*/
int UniHan_quickSort_freq(EGI_UNIHAN* unihans, unsigned int start, unsigned int end, int cutoff)
{
int i=start;
int j=end;
int mid;
EGI_UNIHAN tmp;
EGI_UNIHAN pivot;
/* Check input */
if( unihans==NULL )
return -1;
/* End sorting */
if( start >= end )
return 0;
/* Limit cutoff */
if(cutoff<3)
cutoff=3;
/* 1. Implement quicksort */
if( end-start >= cutoff )
{
/* 1.1 Select pivot, by sorting array[start], array[mid], array[end] */
/* Get mid index */
mid=(start+end)/2;
/* Sort [start] and [mid] */
/* if( array[start] > array[mid] ) */
if( unihans[start].freq > unihans[mid].freq ) {
tmp=unihans[start];
unihans[start]=unihans[mid];