-
Notifications
You must be signed in to change notification settings - Fork 90
/
tree.c
1006 lines (894 loc) · 24.7 KB
/
tree.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
/**
* tree.c
*
* Copyright (c) 1999, 2000, 2001
* Lu-chuan Kung and Kang-pen Chen.
* All rights reserved.
*
* Copyright (c) 2004, 2005, 2006, 2008, 2011
* libchewing Core Team. See ChangeLog for details.
*
* See the file "COPYING" for information on usage and redistribution
* of this file.
*/
/**
* @file tree.c
* @brief API for accessing the phrase tree.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "chewing-utf8-util.h"
#include "chewing-definition.h"
#include "userphrase-private.h"
#include "global.h"
#include "global-private.h"
#include "dict-private.h"
#include "char-private.h"
#include "tree-private.h"
#include "private.h"
#include "plat_mmap.h"
#define INTERVAL_SIZE ( ( MAX_PHONE_SEQ_LEN + 1 ) * MAX_PHONE_SEQ_LEN / 2 )
typedef struct {
int from, to, pho_id, source;
Phrase *p_phr;
} PhraseIntervalType;
typedef struct tagRecordNode {
int *arrIndex; /* the index array of the things in "interval" */
int nInter, score;
struct tagRecordNode *next;
int nMatchCnnct; /* match how many Cnnct. */
} RecordNode;
typedef struct {
int leftmost[ MAX_PHONE_SEQ_LEN + 1 ] ;
char graph[ MAX_PHONE_SEQ_LEN + 1 ][ MAX_PHONE_SEQ_LEN + 1 ];
PhraseIntervalType interval[ MAX_INTERVAL ];
int nInterval;
RecordNode *phList;
int nPhListLen;
} TreeDataType;
static int IsContain( IntervalType in1, IntervalType in2 )
{
return ( in1.from <= in2.from && in1.to >= in2.to );
}
int IsIntersect( IntervalType in1, IntervalType in2 )
{
return ( max( in1.from, in2.from ) < min( in1.to, in2.to ) );
}
static int PhraseIntervalContain(PhraseIntervalType in1, PhraseIntervalType in2)
{
return ( in1.from <= in2.from && in1.to >= in2.to );
}
static int PhraseIntervalIntersect(PhraseIntervalType in1, PhraseIntervalType in2)
{
return ( max( in1.from, in2.from ) < min( in1.to, in2.to ) );
}
#if 0
/** @brief check for intersection of two intervals and return it */
static int GetIntersection( IntervalType in1, IntervalType in2, IntervalType *in3 )
{
in3->from = max( in1.from, in2.from );
in3->to = min( in1.to, in2.to );
if ( in3->from < in3->to )
return 1;
return 0;
}
#endif
void TerminateTree( ChewingData *pgdata )
{
#ifdef USE_BINARY_DATA
pgdata->static_data.tree = NULL;
plat_mmap_close( &pgdata->static_data.tree_mmap );
#else
free( pgdata->static_data.tree );
pgdata->static_data.tree = NULL;
#endif
}
int InitTree( ChewingData *pgdata, const char * prefix )
{
#ifdef USE_BINARY_DATA
char filename[ PATH_MAX ];
size_t len;
size_t offset;
len = snprintf( filename, sizeof( filename ), "%s" PLAT_SEPARATOR "%s", prefix, PHONE_TREE_FILE );
if ( len + 1 > sizeof( filename ) )
return -1;
plat_mmap_set_invalid( &pgdata->static_data.tree_mmap );
pgdata->static_data.tree_size = plat_mmap_create( &pgdata->static_data.tree_mmap, filename, FLAG_ATTRIBUTE_READ );
if ( pgdata->static_data.tree_size <= 0 )
return -1;
offset = 0;
pgdata->static_data.tree = (TreeType *) plat_mmap_set_view( &pgdata->static_data.tree_mmap, &offset, &pgdata->static_data.tree_size );
if ( !pgdata->static_data.tree )
return -1;
return 0;
#else
char filename[ PATH_MAX ];
int len;
FILE *infile = NULL;
int i;
len = snprintf( filename, sizeof( filename ), "%s" PLAT_SEPARATOR "%s", prefix, PHONE_TREE_FILE );
if ( len + 1 > sizeof( filename ) )
return -1;
infile = fopen( filename, "r" );
if ( !infile )
return -1;
pgdata->tree = ALC( TreeType, TREE_SIZE );
if ( !pgdata->tree ) {
fclose( infile );
return -1;
}
/* XXX: What happen if infile contains more than TREE_SIZE data? */
for ( i = 0; i < TREE_SIZE; i++ ) {
if ( fscanf( infile, "%hu%d%d%d",
&pgdata->tree[ i ].phone_id,
&pgdata->tree[ i ].phrase_id,
&pgdata->tree[ i ].child_begin,
&pgdata->tree[ i ].child_end ) != 4 )
break;
}
fclose( infile );
return 0;
#endif
}
static int CheckBreakpoint( int from, int to, int bArrBrkpt[] )
{
int i;
for ( i = from + 1; i < to; i++ )
if ( bArrBrkpt[ i ] )
return 0;
return 1;
}
static int CheckUserChoose(
ChewingData *pgdata,
uint16_t *new_phoneSeq, int from , int to,
Phrase **pp_phr,
char selectStr[][ MAX_PHONE_SEQ_LEN * MAX_UTF8_SIZE + 1 ],
IntervalType selectInterval[], int nSelect )
{
IntervalType inte, c;
int chno, len;
int user_alloc;
UserPhraseData *pUserPhraseData;
Phrase *p_phr = ALC( Phrase, 1 );
assert( p_phr );
inte.from = from;
inte.to = to;
*pp_phr = NULL;
/* pass 1
* if these exist one selected interval which is not contained by inte
* but has intersection with inte, then inte is an unacceptable interval
*/
for ( chno = 0; chno < nSelect; chno++ ) {
c = selectInterval[ chno ];
if ( IsIntersect( inte, c ) && ! IsContain( inte, c ) ) {
free( p_phr );
return 0;
}
}
/* pass 2
* if there exist one phrase satisfied all selectStr then return 1, else return 0.
* also store the phrase with highest freq
*/
pUserPhraseData = UserGetPhraseFirst( pgdata, new_phoneSeq );
p_phr->freq = -1;
do {
for ( chno = 0; chno < nSelect; chno++ ) {
c = selectInterval[ chno ];
if ( IsContain( inte, c ) ) {
/*
* find a phrase of ph_id where the text contains
* 'selectStr[chno]' test if not ok then return 0,
* if ok then continue to test. */
len = c.to - c.from;
if ( memcmp(
ueStrSeek( pUserPhraseData->wordSeq, c.from - from ),
selectStr[ chno ],
ueStrNBytes( selectStr[ chno ], len ) ) )
break;
}
}
if ( chno == nSelect ) {
/* save phrase data to "pp_phr" */
if ( pUserPhraseData->userfreq > p_phr->freq ) {
if ( ( user_alloc = ( to - from ) ) > 0 ) {
ueStrNCpy( p_phr->phrase,
pUserPhraseData->wordSeq,
user_alloc, 1);
}
p_phr->freq = pUserPhraseData->userfreq;
*pp_phr = p_phr;
}
}
} while ( ( pUserPhraseData = UserGetPhraseNext( pgdata, new_phoneSeq ) ) != NULL );
if ( p_phr->freq != -1 )
return 1;
free( p_phr );
return 0;
}
/*
* phrase is said to satisfy a choose interval if
* their intersections are the same */
static int CheckChoose(
ChewingData *pgdata,
int ph_id, int from, int to, Phrase **pp_phr,
char selectStr[][ MAX_PHONE_SEQ_LEN * MAX_UTF8_SIZE + 1 ],
IntervalType selectInterval[], int nSelect )
{
IntervalType inte, c;
int chno, len;
Phrase *phrase = ALC( Phrase, 1 );
assert( phrase );
inte.from = from;
inte.to = to;
*pp_phr = NULL;
/* if there exist one phrase satisfied all selectStr then return 1, else return 0. */
GetPhraseFirst( pgdata, phrase, ph_id );
do {
for ( chno = 0; chno < nSelect; chno++ ) {
c = selectInterval[ chno ];
if ( IsContain( inte, c ) ) {
/* find a phrase of ph_id where the text contains
* 'selectStr[chno]' test if not ok then return 0, if ok
* then continue to test
*/
len = c.to - c.from;
if ( memcmp(
ueStrSeek( phrase->phrase, c.from - from ),
selectStr[ chno ],
ueStrNBytes( selectStr[ chno ], len ) ) )
break;
}
else if ( IsIntersect( inte, selectInterval[ chno ] ) ) {
free( phrase );
return 0;
}
}
if ( chno == nSelect ) {
*pp_phr = phrase;
return 1;
}
} while ( GetPhraseNext( pgdata, phrase ) );
free( phrase );
return 0;
}
/** @brief search for the phrases have the same pronunciation.*/
/* if phoneSeq[a] ~ phoneSeq[b] is a phrase, then add an interval
* from (a) to (b+1) */
int TreeFindPhrase( ChewingData *pgdata, int begin, int end, const uint16_t *phoneSeq )
{
int child, tree_p, i;
tree_p = 0;
for ( i = begin; i <= end; i++ ) {
for (
child = pgdata->static_data.tree[ tree_p ].child_begin;
child != -1 && child <= pgdata->static_data.tree[ tree_p ].child_end;
child++ ) {
#ifdef USE_BINARY_DATA
assert(0 <= child && child * sizeof(TreeType) < pgdata->static_data.tree_size);
#endif
if ( pgdata->static_data.tree[ child ].phone_id == phoneSeq[ i ] )
break;
}
/* if not found any word then fail. */
if ( child == -1 || child > pgdata->static_data.tree[ tree_p ].child_end )
return -1;
else {
tree_p = child;
}
}
return pgdata->static_data.tree[ tree_p ].phrase_id;
}
static void AddInterval(
TreeDataType *ptd, int begin , int end,
int p_id, Phrase *p_phrase, int dict_or_user )
{
ptd->interval[ ptd->nInterval ].from = begin;
ptd->interval[ ptd->nInterval ].to = end + 1;
ptd->interval[ ptd->nInterval ].pho_id = p_id;
ptd->interval[ ptd->nInterval ].p_phr = p_phrase;
ptd->interval[ ptd->nInterval ].source = dict_or_user;
ptd->nInterval++;
}
/* Item which inserts to interval array */
typedef enum {
USED_PHRASE_NONE, /**< none of items used */
USED_PHRASE_USER, /**< User phrase */
USED_PHRASE_DICT /**< Dict phrase */
} UsedPhraseMode;
static void internal_release_Phrase( UsedPhraseMode mode, Phrase *pUser, Phrase *pDict )
{
/* we must free unused phrase entry to avoid memory leak. */
switch ( mode ) {
case USED_PHRASE_USER:
if ( pDict != NULL )
free( pDict );
break;
case USED_PHRASE_DICT:
if ( pUser != NULL )
free( pUser );
break;
default: /* In fact, it is alwyas 0 */
if ( pDict != NULL )
free( pDict );
if ( pUser != NULL )
free( pUser );
break;
}
}
static void FindInterval( ChewingData *pgdata, TreeDataType *ptd )
{
int end, begin, pho_id;
Phrase *p_phrase, *puserphrase, *pdictphrase;
UsedPhraseMode i_used_phrase;
uint16_t new_phoneSeq[ MAX_PHONE_SEQ_LEN ];
for ( begin = 0; begin < pgdata->nPhoneSeq; begin++ ) {
for ( end = begin; end < pgdata->nPhoneSeq; end++ ) {
if ( ! CheckBreakpoint( begin, end + 1, pgdata->bArrBrkpt ) )
continue;
/* set new_phoneSeq */
memcpy(
new_phoneSeq,
&pgdata->phoneSeq[ begin ],
sizeof( uint16_t ) * ( end - begin + 1 ) );
new_phoneSeq[ end - begin + 1 ] = 0;
puserphrase = pdictphrase = NULL;
i_used_phrase = USED_PHRASE_NONE;
/* check user phrase */
if ( UserGetPhraseFirst( pgdata, new_phoneSeq ) &&
CheckUserChoose( pgdata, new_phoneSeq, begin, end + 1,
&p_phrase, pgdata->selectStr, pgdata->selectInterval, pgdata->nSelect ) ) {
puserphrase = p_phrase;
}
/* check dict phrase */
pho_id = TreeFindPhrase( pgdata, begin, end, pgdata->phoneSeq );
if (
( pho_id != -1 ) &&
CheckChoose(
pgdata,
pho_id, begin, end + 1,
&p_phrase, pgdata->selectStr,
pgdata->selectInterval, pgdata->nSelect ) ) {
pdictphrase = p_phrase;
}
/* add only one interval, which has the largest freqency
* but when the phrase is the same, the user phrase overrides
* static dict
*/
if ( puserphrase != NULL && pdictphrase == NULL ) {
i_used_phrase = USED_PHRASE_USER;
}
else if ( puserphrase == NULL && pdictphrase != NULL ) {
i_used_phrase = USED_PHRASE_DICT;
}
else if ( puserphrase != NULL && pdictphrase != NULL ) {
/* the same phrase, userphrase overrides */
if ( ! strcmp(
puserphrase->phrase,
pdictphrase->phrase ) ) {
i_used_phrase = USED_PHRASE_USER;
}
else {
if ( puserphrase->freq > pdictphrase->freq ) {
i_used_phrase = USED_PHRASE_USER;
}
else {
i_used_phrase = USED_PHRASE_DICT;
}
}
}
switch ( i_used_phrase ) {
case USED_PHRASE_USER:
AddInterval( ptd, begin, end, -1, puserphrase,
IS_USER_PHRASE );
break;
case USED_PHRASE_DICT:
AddInterval( ptd, begin, end, pho_id, pdictphrase,
IS_DICT_PHRASE );
break;
case USED_PHRASE_NONE:
default:
break;
}
internal_release_Phrase(
i_used_phrase,
puserphrase,
pdictphrase );
}
}
}
static void SetInfo( int len, TreeDataType *ptd )
{
int i, a;
for ( i = 0; i <= len; i++ )
ptd->leftmost[ i ] = i;
for ( i = 0; i < ptd->nInterval; i++ ) {
ptd->graph[ ptd->interval[ i ].from ][ ptd->interval[ i ].to ] = 1;
ptd->graph[ ptd->interval[ i ].to ][ ptd->interval[ i ].from ] = 1;
}
/* set leftmost */
for ( a = 0; a <= len; a++ ) {
for ( i = 0; i <= len; i++ ) {
if ( ! ( ptd->graph[ a ][ i ] ) )
continue;
if ( ptd->leftmost[ i ] < ptd->leftmost[ a ] )
ptd->leftmost[ a ] = ptd->leftmost[ i ];
}
}
}
#if 0
static int CompLen( IntervalType *pa, IntervalType *pb )
{
return ( ( pa->to - pa->from ) - ( pb->to - pb->from ) );
}
static int CompLenDescend( IntervalType *pa, IntervalType *pb )
{
return ( ( pb->to - pb->from ) - ( pa->to - pa->from ) );
}
static int CompFrom( IntervalType *pa, IntervalType *pb )
{
int cmp = pa->from - pb->from;
if ( cmp )
return cmp;
return ( pa->to - pb->to );
}
#endif
/*
* First we compare the 'nMatchCnnct'.
* If the values are the same, we will compare the 'score'
*/
static int CompRecord( const RecordNode **pa, const RecordNode **pb )
{
int diff = (*pb)->nMatchCnnct - (*pa)->nMatchCnnct;
if ( diff )
return diff;
return ( (*pb)->score - (*pa)->score );
}
/*
* Remove the interval containing in another interval.
*
* Example:
* 國民大會 has three interval: 國民, 大會, 國民大會. This function removes
* 國名, 大會 becasue 國民大會 contains 國民 and 大會.
*/
static void Discard1( TreeDataType *ptd )
{
int a, b;
char failflag[ INTERVAL_SIZE ];
int nInterval2;
memset( failflag, 0, sizeof( failflag ) );
for ( a = 0; a < ptd->nInterval; a++ ) {
if ( failflag[ a ] )
continue;
for ( b = 0; b < ptd->nInterval; b++ ) {
if ( a == b || failflag[ b ] )
continue ;
if ( ptd->interval[ b ].from >= ptd->interval[ a ].from &&
ptd->interval[ b ].to <= ptd->interval[ a ].to )
continue;
if ( ptd->interval[ b ].from <= ptd->interval[ a ].from &&
ptd->interval[ b ].to <= ptd->interval[ a ].from )
continue;
if ( ptd->interval[ b ].from >= ptd->interval[ a ].to &&
ptd->interval[ b ].to >= ptd->interval[ a ].to )
continue;
break;
}
/* if any other interval b is inside or leftside or rightside the
* interval a */
if ( b >= ptd->nInterval ) {
/* then kill all the intervals inside the interval a */
int i;
for ( i = 0; i < ptd->nInterval; i++ ) {
if (
! failflag[ i ] && i != a &&
ptd->interval[ i ].from >=
ptd->interval[ a ].from &&
ptd->interval[ i ].to <= ptd->interval[ a ].to ) {
failflag[ i ] = 1;
}
}
}
}
/* discard all the intervals whose failflag[a] = 1 */
nInterval2 = 0;
for ( a = 0; a < ptd->nInterval; a++ ) {
if ( ! failflag[ a ] ) {
ptd->interval[ nInterval2++ ] = ptd->interval[ a ];
}
else {
if ( ptd->interval[ a ].p_phr != NULL ) {
free( ptd->interval[ a ].p_phr );
}
}
}
ptd->nInterval = nInterval2;
}
/*
* Remove the interval that cannot connect to head or tail by other intervals.
*
* Example:
* The input string length is 5
* The available intervals are [1,1], [1,2], [2,3], [2,4], [5,5], [3,5].
*
* The possible connection from head to tail are [1,2][3,5], and
* [1,1][2,4][5,5]. Since [2,3] cannot connect to head or tail, it is removed
* by this function.
*/
static void Discard2( TreeDataType *ptd )
{
int i, j;
char overwrite[ MAX_PHONE_SEQ_LEN ], failflag[ MAX_PHONE_SEQ_LEN ];
int nInterval2;
memset( failflag, 0, sizeof( failflag ) );
for ( i = 0; i < ptd->nInterval; i++ ) {
if ( ptd->leftmost[ ptd->interval[ i ].from ] == 0 )
continue;
/* test if interval i is overwrited by other intervals */
memset( overwrite, 0, sizeof( overwrite ) );
for ( j = 0; j < ptd->nInterval; j++ ) {
if ( j == i )
continue;
memset(
&overwrite[ ptd->interval[ j ].from ],
1,
ptd->interval[ j ].to - ptd->interval[ j ].from );
}
if ( memchr(
&overwrite[ ptd->interval[ i ].from ],
1,
ptd->interval[ i ].to - ptd->interval[ i ].from ) )
failflag[ i ] = 1;
}
/* discard all the intervals whose failflag[a] = 1 */
nInterval2 = 0;
for ( i = 0; i < ptd->nInterval; i++ )
if ( ! failflag[ i ] )
ptd->interval[ nInterval2++ ] = ptd->interval[ i ];
ptd->nInterval = nInterval2;
}
static void LoadChar( ChewingData *pgdata, char *buf, int buf_len, uint16_t phoneSeq[], int nPhoneSeq )
{
int i;
Word word;
memset(buf, 0, buf_len);
for ( i = 0; i < nPhoneSeq; i++ ) {
GetCharFirst( pgdata, &word, phoneSeq[ i ] );
strncat(buf, word.word, buf_len - strlen(buf) - 1);
}
buf[ buf_len - 1 ] = '\0';
}
/* kpchen said, record is the index array of interval */
static void OutputRecordStr(
ChewingData *pgdata,
char *out_buf, int out_buf_len,
int *record, int nRecord,
uint16_t phoneSeq[], int nPhoneSeq,
char selectStr[][ MAX_PHONE_SEQ_LEN * MAX_UTF8_SIZE + 1 ],
IntervalType selectInterval[],
int nSelect, TreeDataType *ptd )
{
PhraseIntervalType inter;
int i;
LoadChar( pgdata, out_buf, out_buf_len, phoneSeq, nPhoneSeq );
for ( i = 0; i < nRecord; i++ ) {
inter = ptd->interval[ record[ i ] ];
ueStrNCpy(
ueStrSeek( out_buf, inter.from ),
( inter.p_phr )->phrase,
( inter.to - inter.from ), -1);
}
for ( i = 0; i < nSelect; i++ ) {
inter.from = selectInterval[ i ].from;
inter.to = selectInterval[ i ].to ;
ueStrNCpy(
ueStrSeek( out_buf, inter.from ),
selectStr[ i ], ( inter.to - inter.from ), -1);
}
}
static int rule_largest_sum( int *record, int nRecord, TreeDataType *ptd )
{
int i, score = 0;
PhraseIntervalType inter;
for ( i = 0; i < nRecord; i++ ) {
inter = ptd->interval[ record[ i ] ];
assert( inter.p_phr );
score += inter.to - inter.from;
}
return score;
}
static int rule_largest_avgwordlen( int *record, int nRecord, TreeDataType *ptd )
{
/* constant factor 6=1*2*3, to keep value as integer */
return 6 * rule_largest_sum( record, nRecord, ptd ) / nRecord;
}
static int rule_smallest_lenvariance( int *record, int nRecord, TreeDataType *ptd )
{
int i, j, score = 0;
PhraseIntervalType inter1, inter2;
/* kcwu: heuristic? why variance no square function? */
for ( i = 0; i < nRecord; i++ ) {
for ( j = i + 1; j < nRecord; j++ ) {
inter1 = ptd->interval[ record[ i ] ];
inter2 = ptd->interval[ record[ j ] ];
assert( inter1.p_phr && inter2.p_phr );
score += abs((inter1.to - inter1.from) - (inter2.to - inter2.from));
}
}
return -score;
}
static int rule_largest_freqsum( int *record, int nRecord, TreeDataType *ptd )
{
int i, score = 0;
PhraseIntervalType inter;
for ( i = 0; i < nRecord; i++ ) {
inter = ptd->interval[ record[ i ] ];
assert( inter.p_phr );
/* We adjust the 'freq' of One-word Phrase */
score += ( inter.to - inter.from == 1 ) ?
( inter.p_phr->freq / 512 ) :
inter.p_phr->freq;
}
return score;
}
static int LoadPhraseAndCountScore( int *record, int nRecord, TreeDataType *ptd )
{
int total_score = 0;
/* NOTE: the balance factor is tuneable */
if (nRecord) {
total_score += 1000*rule_largest_sum( record, nRecord, ptd );
total_score += 1000*rule_largest_avgwordlen( record, nRecord, ptd );
total_score += 100*rule_smallest_lenvariance( record, nRecord, ptd );
total_score += rule_largest_freqsum( record, nRecord, ptd );
}
return total_score;
}
static int IsRecContain( int *intA, int nA, int *intB, int nB, TreeDataType *ptd )
{
int big, sml;
for ( big = 0, sml = 0; sml < nB; sml++ ) {
while (
( big < nA ) &&
ptd->interval[ intA[ big ] ].from <
ptd->interval[ intB[ sml ] ].to ) {
if ( PhraseIntervalContain(
ptd->interval[ intA[ big ] ],
ptd->interval[ intB[ sml ] ] ) )
break;
big++;
}
if (
( big >= nA ) ||
ptd->interval[ intA[ big ] ].from >=
ptd->interval[ intB[ sml ] ].to )
return 0;
}
return 1;
}
static void SortListByScore( TreeDataType *ptd )
{
int i, listLen;
RecordNode *p, **arr;
for (
listLen = 0, p = ptd->phList;
p;
listLen++, p = p->next )
;
ptd->nPhListLen = listLen;
arr = ALC( RecordNode *, listLen );
assert( arr );
for (
i = 0, p = ptd->phList;
i < listLen;
p = p->next, i++ ) {
arr[ i ] = p;
p->score = LoadPhraseAndCountScore(
p->arrIndex,
p->nInter,
ptd );
}
qsort( arr, listLen, sizeof( RecordNode * ), (CompFuncType) CompRecord );
ptd->phList = arr[ 0 ];
for ( i = 1; i < listLen; i++ ) {
arr[ i - 1 ]->next = arr[ i ];
}
arr[ listLen - 1 ]->next = NULL;
free( arr );
}
/* when record==NULL then output the "link list" */
static void SaveRecord( int *record, int nInter, TreeDataType *ptd )
{
RecordNode *now, *p, *pre;
pre = NULL;
for ( p = ptd->phList; p; ) {
/* if 'p' contains 'record', then discard 'record'. */
if ( IsRecContain( p->arrIndex, p->nInter, record, nInter, ptd ) )
return;
/* if 'record' contains 'p', then discard 'p'
* -- We must deal with the linked list. */
if ( IsRecContain( record, nInter, p->arrIndex, p->nInter, ptd ) ) {
RecordNode *tp = p;
if ( pre )
pre->next = p->next;
else
ptd->phList = ptd->phList->next;
p = p->next;
free( tp->arrIndex );
free( tp );
}
else
pre = p, p = p->next;
}
now = ALC( RecordNode, 1 );
assert( now );
now->next = ptd->phList;
now->arrIndex = ALC( int, nInter );
assert( now->arrIndex );
now->nInter = nInter;
memcpy( now->arrIndex, record, nInter * sizeof( int ) );
ptd->phList = now;
}
static void RecursiveSave( int depth, int to, int *record, TreeDataType *ptd )
{
int first, i;
/* to find first interval */
for (
first = record[ depth - 1 ] + 1;
ptd->interval[ first ].from < to && first < ptd->nInterval;
first++ )
;
if ( first == ptd->nInterval ) {
SaveRecord( record + 1, depth - 1, ptd );
return;
}
record[ depth ] = first;
RecursiveSave( depth + 1, ptd->interval[ first ].to, record, ptd );
/* for each interval which intersects first */
for (
i= first + 1;
PhraseIntervalIntersect(
ptd->interval[ first ],
ptd->interval[ i ] ) && i < ptd->nInterval;
i++ ) {
record[ depth ] = i;
RecursiveSave( depth + 1, ptd->interval[ i ].to,record, ptd );
}
}
static void SaveList( TreeDataType *ptd )
{
int record[ MAX_PHONE_SEQ_LEN + 1 ] = { -1 };
RecursiveSave( 1, 0, record, ptd );
}
static void InitPhrasing( TreeDataType *ptd )
{
memset( ptd, 0, sizeof( TreeDataType ) );
}
static void SaveDispInterval( PhrasingOutput *ppo, TreeDataType *ptd )
{
int i;
for ( i = 0; i < ptd->phList->nInter; i++ ) {
ppo->dispInterval[ i ].from =
ptd->interval[ ptd->phList->arrIndex[ i ] ].from;
ppo->dispInterval[ i ].to =
ptd->interval[ ptd->phList->arrIndex[ i ] ].to;
}
ppo->nDispInterval = ptd->phList->nInter;
}
static void CleanUpMem( TreeDataType *ptd )
{
int i;
RecordNode *pNode;
for ( i = 0; i < ptd->nInterval; i++ ) {
if ( ptd->interval[ i ].p_phr ) {
free( ptd->interval[ i ].p_phr );
ptd->interval[ i ].p_phr = NULL;
}
}
while ( ptd->phList != NULL ) {
pNode = ptd->phList;
ptd->phList = pNode->next;
free( pNode->arrIndex );
free( pNode );
}
}
static void CountMatchCnnct( TreeDataType *ptd, int *bUserArrCnnct, int nPhoneSeq )
{
RecordNode *p;
int i, k, sum;
for ( p = ptd->phList; p; p = p->next ) {
/* for each record, count its 'nMatchCnnct' */
for ( sum = 0, i = 1; i < nPhoneSeq; i++ ) {
if ( !bUserArrCnnct[ i ] )
continue;
/* check if matching 'cnnct' */
for ( k = 0; k < p->nInter; k++ ) {
if (
ptd->interval[ p->arrIndex[ k ] ].from < i &&
ptd->interval[ p->arrIndex[ k ] ].to > i ) {
sum++;
break;
}
}
}
p->nMatchCnnct = sum;
}
}
#ifdef ENABLE_DEBUG
static void ShowList( TreeDataType *ptd )
{
RecordNode *p;
int i;
DEBUG_OUT( "After SaveList :\n" );
for ( p = ptd->phList; p; p = p->next ) {
DEBUG_OUT( " interval : " );
for ( i = 0; i < p->nInter; i++ ) {
DEBUG_OUT(
"[%d %d] ",
ptd->interval[ p->arrIndex[ i ] ].from,
ptd->interval[ p->arrIndex[ i ] ].to );
}
DEBUG_OUT(
"\n"
" score : %d , nMatchCnnct : %d\n",
p->score,
p->nMatchCnnct );
}
DEBUG_OUT( "\n" );
}
#endif
static RecordNode* NextCut( TreeDataType *tdt, PhrasingOutput *ppo )
{
/* pop nNumCut-th candidate to first */
int i;
RecordNode *former;
RecordNode *want;
if ( ppo->nNumCut >= tdt->nPhListLen )
ppo->nNumCut = 0;
if (ppo->nNumCut == 0)
return tdt->phList;
/* find the former of our candidate */
former = tdt->phList;
for ( i = 0; i < ppo->nNumCut - 1; i++ ) {
former = former->next;
assert( former );
}
/* take the candidate out of the listed list */
want = former->next;
assert( want );
former->next = former->next->next;
/* prepend to front of list */
want->next = tdt->phList;
tdt->phList = want;
return tdt->phList;
}
int Phrasing( ChewingData *pgdata )
{
TreeDataType treeData;
InitPhrasing( &treeData );
FindInterval( pgdata, &treeData );
SetInfo( pgdata->nPhoneSeq, &treeData );
Discard1( &treeData );
Discard2( &treeData );
SaveList( &treeData );
CountMatchCnnct( &treeData, pgdata->bUserArrCnnct, pgdata->nPhoneSeq );
SortListByScore( &treeData );
NextCut( &treeData, &pgdata->phrOut );
#ifdef ENABLE_DEBUG
ShowList( &treeData );
DEBUG_FLUSH;
#endif
/* set phrasing output */
OutputRecordStr(
pgdata,
pgdata->phrOut.chiBuf, sizeof(pgdata->phrOut.chiBuf),
( treeData.phList )->arrIndex,
( treeData.phList )->nInter,
pgdata->phoneSeq,
pgdata->nPhoneSeq,
pgdata->selectStr, pgdata->selectInterval, pgdata->nSelect, &treeData );