-
Notifications
You must be signed in to change notification settings - Fork 0
/
regexp.cc
1452 lines (1285 loc) · 40.4 KB
/
regexp.cc
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 "common.h"
#include "knp_const.h"
#include "morpheme.h"
#include "phrase.h"
#include "grammar.h"
#include "cky.h"
#define MRPH_HINSHI 1
#define MRPH_BUNRUI 2
#define MRPH_KATA 3
#define MRPH_KEI 4
#define MRPH_GOI 5
using std::cerr;
using std::endl;
namespace Parser {
void *matched_ptr; /* memory of the matched morpheme or phrase */
const REGEXPMRPH RegexpMrphInitValue = {
MAT_FLG, (char) NULL,
/* Hinshi */
(char) NULL, {0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
/* Bunrui */
(char) NULL, {0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
/* Katuyou_Kata */
(char) NULL, {0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
/* Katuyou_Kei */
(char) NULL, {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
/* Goi */
(char) NULL, {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}};
const REGEXPMRPHS RegexpmrphsInitValue = {NULL, 0};
const REGEXPPHRASE RegexpPhraseInitValue = {
MAT_FLG, (char) NULL, (char) NULL
};
extern CLASS Class[CLASSIFY_NO + 1][CLASSIFY_NO + 1];
extern TYPE Type[TYPE_NO];
extern FORM Form[TYPE_NO][FORM_NO];
#define RENGO_ID "999"
std::vector<int> ANY_ID;
int get_hinsi_id(U_CHAR *x) {
int i;
if (x == NULL) {
cerr << "NULL string for hinsi." << endl;
return 0;
}
if (!strcmp((char *)x, "*")) {
return 0;
}
if (!strcmp((char *)x, "連語")) {
return(atoi(RENGO_ID));
}
for (i = 1; strcmp((char *)Class[i][0].id, (char *)x); ) {
if (!Class[++i][0].id) {
cerr << (char *)x << " is undefined in GRAMMARFILE.";
}
}
return i;
}
int get_bunrui_id(U_CHAR *x, int hinsi) {
int i;
if (!x ) {
cerr << "NULL string for bunrui." << endl;
return 0;
}
if (!strcmp((char *)x, "*")) {
return 0;
}
if (!Class[hinsi][1].id) {
cerr << (char *)Class[hinsi][0].id << " has no bunrui in GRAMMARFILE." << endl;
}
for (i = 1; strcmp((char *)Class[hinsi][i].id, (char *)x); ) {
if (!Class[hinsi][++i].id) {
cerr << (char *)Class[hinsi][0].id << " does not have bunrui " << (char *)x << " in GRAMMARFILE." << endl;
}
}
return i;
}
int get_type_id(U_CHAR *x) {
int i;
if (!x) {
cerr << "NULL string for type." << endl;
return 0;
}
if (!strcmp((char *)x, "*")) {
return 0;
}
for (i = 1; strcmp((char *)Type[i].name, (char *)x); ) {
if (!Type[++i].name) {
cerr << (char *)x << " is undefined in KATUYOUFILE." << endl;
}
}
return i;
}
/*==================================================================*/
/* Store */
/*==================================================================*/
/*==================================================================*/
REGEXPMRPH *regexpmrph_alloc(int size)
/*==================================================================*/
{
REGEXPMRPH *tmp;
int i;
if (!(tmp = (REGEXPMRPH *)malloc(size * sizeof(REGEXPMRPH)))) {
fprintf(stderr, "Can't allocate memory for REGEXPMRPH\n");
exit(-1);
}
for (i = 0; i < size; i++) {
*(tmp + i) = RegexpMrphInitValue;
}
return tmp;
}
/*==================================================================*/
REGEXPMRPHS *regexpmrphs_alloc()
/*==================================================================*/
{
REGEXPMRPHS *tmp;
if (!(tmp = (REGEXPMRPHS *)malloc(sizeof(REGEXPMRPHS)))) {
fprintf(stderr, "Can't allocate memory for REGEXPMRPHS\n");
exit(-1);
}
return tmp;
}
/*==================================================================*/
REGEXPPHRASE *regexpbnst_alloc(int size)
/*==================================================================*/
{
REGEXPPHRASE *tmp;
int i;
if (!(tmp = (REGEXPPHRASE *)malloc(size * sizeof(REGEXPPHRASE)))) {
fprintf(stderr, "Can't allocate memory for REGEXPPHRASE\n");
exit(-1);
}
for (i = 0; i < size; i++) {
*(tmp + i) = RegexpPhraseInitValue;
}
return tmp;
}
/*==================================================================*/
REGEXPPHRASES *regexpbnsts_alloc()
/*==================================================================*/
{
REGEXPPHRASES *tmp;
if (!(tmp = (REGEXPPHRASES *)malloc(sizeof(REGEXPPHRASES)))) {
fprintf(stderr, "Can't allocate memory for REGEXPPHRASES\n");
exit(-1);
}
return tmp;
}
/*==================================================================*/
void store_mrph_nflag(REGEXPMRPH *mp, int type)
/*==================================================================*/
{
switch (type) {
case MRPH_HINSHI: mp->Hinshi_not = NOT_FLG; break;
case MRPH_BUNRUI: mp->Bunrui_not = NOT_FLG; break;
case MRPH_KATA: mp->Kata_not = NOT_FLG; break;
case MRPH_KEI: mp->Kei_not = NOT_FLG; break;
case MRPH_GOI: mp->Goi_not = NOT_FLG; break;
default: break;
}
}
/*==================================================================*/
static CELL *store_mrph_item(REGEXPMRPH *mp, CELL *mcell, int type)
/*==================================================================*/
{
int nth;
CELL *list_cell;
char *tmp;
if (Null(car(mcell))) return NULL;
if (Atomp(car(mcell))) {
tmp = (char *)_Atom(car(mcell));
if (!strcmp(tmp, AST_STR)) { /* "*" */
return cdr(mcell);
}
else if (!strcmp(tmp, NOT_STR)) { /* "^" */
store_mrph_nflag(mp, type);
mcell = cdr(mcell);
if (Atomp(car(mcell)))
list_cell = cons(car(mcell), NULL);
else
list_cell = car(mcell);
}
else if (!strncmp(tmp, NOT_STR, strlen(NOT_STR))) { /* "^atom" */
store_mrph_nflag(mp, type);
_Atom(car(mcell)) += strlen(NOT_STR);
list_cell = cons(car(mcell), NULL);
}
else { /* "atom" */
list_cell = cons(car(mcell), NULL);
}
} else { /* "(...)" */
list_cell = car(mcell);
}
if (type == MRPH_BUNRUI && mp->Hinshi[1] != -1) {
fprintf(stderr, "Cannot restrict Bunrui for multiple Hinshis.\n");
error_in_lisp();
}
nth = 0;
while (!Null(car(list_cell))) {
switch (type) {
case MRPH_HINSHI:
mp->Hinshi[nth] = get_hinsi_id(_Atom(car(list_cell)));
break;
case MRPH_BUNRUI:
mp->Bunrui[nth] =
get_bunrui_id(_Atom(car(list_cell)), mp->Hinshi[0]);
break;
case MRPH_KATA:
mp->Katuyou_Kata[nth] = get_type_id(_Atom(car(list_cell)));
break;
case MRPH_KEI:
tmp = (char *)_Atom(car(list_cell));
mp->Katuyou_Kei[nth] =
(char *)malloc((strlen(tmp)+1) * sizeof(char));
strcpy(mp->Katuyou_Kei[nth], tmp);
break;
case MRPH_GOI:
tmp = (char *)_Atom(car(list_cell));
mp->Goi[nth] =
(char *)malloc((strlen(tmp)+1) * sizeof(char));
strcpy(mp->Goi[nth], tmp);
break;
default: break;
}
list_cell = cdr(list_cell);
nth++;
}
return cdr(mcell);
}
/*==================================================================*/
void make_ast_regexpmrph(REGEXPMRPH *mp)
/*==================================================================*/
{
mp->type_flag = QST_FLG;
mp->ast_flag = AST_FLG;
}
/*==================================================================*/
char store_regexpmrph(REGEXPMRPH *mp, CELL *mcell)
/*==================================================================*/
{
/* morpheme is specified by a special symbol */
if (Atomp(mcell)) {
if (!strcmp((char *)_Atom(mcell), NOT_STR)) { /* "^" */
mp->type_flag = NOT_FLG;
return NOT_FLG;
}
else if (!strcmp((char *)_Atom(mcell), QST_STR)) { /* "?" */
mp->type_flag = QST_FLG;
return QST_FLG;
}
else if (!strcmp((char *)_Atom(mcell), AST_STR)) { /* "*" */
(mp - 1)->ast_flag = AST_FLG;
return AST_FLG;
}
else if (!strcmp((char *)_Atom(mcell), QST_STR AST_STR)) { /* "?*" */
mp->type_flag = QST_FLG;
mp->ast_flag = AST_FLG;
return QST_FLG;
}
else {
fprintf(stderr, "Invalid string for meta mrph (%s).\n",
_Atom(mcell));
error_in_lisp();
return (char) NULL;
}
}
/* normal morphme */
else {
mp->f_pattern.fp[0] = NULL;
if ((mcell = store_mrph_item(mp, mcell, MRPH_HINSHI)) == NULL)
return MAT_FLG;
if ((mcell = store_mrph_item(mp, mcell, MRPH_BUNRUI)) == NULL)
return MAT_FLG;
if ((mcell = store_mrph_item(mp, mcell, MRPH_KATA)) == NULL)
return MAT_FLG;
if ((mcell = store_mrph_item(mp, mcell, MRPH_KEI)) == NULL)
return MAT_FLG;
if ((mcell = store_mrph_item(mp, mcell, MRPH_GOI)) == NULL)
return MAT_FLG;
list2feature_pattern(&(mp->f_pattern), car(mcell));
if (Null(cdr(mcell))) {
return MAT_FLG;
} else {
fprintf(stderr, "Invalid string for NOT_FLAG.\n");
error_in_lisp();
return (char) NULL;
}
}
}
/*==================================================================*/
void store_regexpmrphs(REGEXPMRPHS **mspp, CELL *cell)
/*==================================================================*/
{
int mrph_num = 0;
if (cell == NULL) {
*mspp = NULL;
return;
}
*mspp = regexpmrphs_alloc();
(*mspp)->mrph = regexpmrph_alloc(length(cell));
while (!Null(cell)) {
switch(store_regexpmrph(((*mspp)->mrph)+mrph_num, car(cell))) {
case MAT_FLG: case QST_FLG:
mrph_num++; break;
case NOT_FLG: case AST_FLG:
break;
default:
break;
}
cell = cdr(cell);
}
(*mspp)->mrphsize = mrph_num;
}
/*==================================================================*/
char store_regexpphrase(REGEXPPHRASE *bp, CELL *cell)
/*==================================================================*/
{
/* phrase is specified by a special symbol */
if (Atomp(cell)) {
if (!strcmp((char *)_Atom(cell), NOT_STR)) { /* "^" */
bp->type_flag = NOT_FLG;
return NOT_FLG;
}
else if (!strcmp((char *)_Atom(cell), QST_STR)) { /* "?" */
bp->type_flag = QST_FLG;
return QST_FLG;
}
else if (!strcmp((char *)_Atom(cell), AST_STR)) { /* "*" */
(bp - 1)->ast_flag = AST_FLG;
return AST_FLG;
}
else if (!strcmp((char *)_Atom(cell), QST_STR AST_STR)) { /* "?*" */
bp->type_flag = QST_FLG;
bp->ast_flag = AST_FLG;
return QST_FLG;
}
else {
fprintf(stderr, "Invalid string for meta bnst (%s).\n",
_Atom(cell));
error_in_lisp();
return (char) NULL;
}
}
/* normal phrase */
else {
store_regexpmrphs(&(bp->mrphs), car(cell));
if (!Null(cdr(cell)))
list2feature_pattern(&(bp->f_pattern), car(cdr(cell)));
else
bp->f_pattern.fp[0] = NULL;
return MAT_FLG;
}
}
/*==================================================================*/
void store_regexpphrases(REGEXPPHRASES **bspp, CELL *cell)
/*==================================================================*/
{
int bnst_num = 0;
if (cell == NULL) {
*bspp = NULL;
return;
}
*bspp = regexpbnsts_alloc();
(*bspp)->bnst = regexpbnst_alloc(length(cell));
while (!Null(cell)) {
switch(store_regexpphrase(((*bspp)->bnst)+bnst_num, car(cell))) {
case MAT_FLG: case QST_FLG:
bnst_num++; break;
case NOT_FLG: case AST_FLG:
break;
default:
break;
}
cell = cdr(cell);
}
(*bspp)->bnstsize = bnst_num;
}
/*==================================================================*/
/* Matching */
/*==================================================================*/
/*==================================================================*/
int rule_HBK_cmp(char flg, int r_data[], int data)
/*==================================================================*/
{
/* Matching of POS, fine-grained POS and inflection type */
int i, tmp_ret = FALSE;
if (r_data[0] == -1 || r_data[0] == 0)
return TRUE;
else {
for (i = 0; r_data[i]!=-1; i++)
if (r_data[i] == data) {
tmp_ret = TRUE;
break;
}
if ((flg == MAT_FLG && tmp_ret == TRUE) ||
(flg == NOT_FLG && tmp_ret == FALSE))
return TRUE;
else
return FALSE;
}
}
/*==================================================================*/
int rule_Kei_cmp(char flg, char *r_string[], int kata, int kei)
/*==================================================================*/
{
/* Matching of inflection form */
int i, tmp_ret = FALSE;
if (r_string[0] == NULL || !strcmp(r_string[0], AST_STR)) {
return TRUE;
}
else if (kata == 0 || kei == 0) {
return FALSE;
}
else {
// std::cerr << ";; rule_Kei_cmp is not implemented yet." << std::endl;
// return FALSE;
for (i = 0; r_string[i]; i++)
if (!strcmp(r_string[i], (char *)Form[kata][kei].name)) {
tmp_ret = TRUE;
break;
}
if ((flg == MAT_FLG && tmp_ret == TRUE) ||
(flg == NOT_FLG && tmp_ret == FALSE)) {
return TRUE;
}
else {
return FALSE;
}
}
}
/*==================================================================*/
int mrph_check_function(char *rule, char *data)
/*==================================================================*/
{
if (0) {
;
}
else {
fprintf(stderr, ";; Invalid Mrph-Feature-Function (%s)\n", rule);
return FALSE;
}
return FALSE;
}
/*==================================================================*/
int rule_Goi_cmp(char flg, char *r_string[], const char *d_string)
/*==================================================================*/
{
/* Matching of word string */
int i, tmp_ret = FALSE;
if (r_string[0] == NULL || !strcmp(r_string[0], AST_STR))
return TRUE;
else {
for (i = 0; r_string[i]; i++) {
/* function call
if (r_string[i][0] == '&')
if (mrph_check_function(r_string[i], d_string)) {
tmp_ret = TRUE;
break;
} */
if (!strcmp(r_string[i], d_string)) {
tmp_ret = TRUE;
break;
}
}
if ((flg == MAT_FLG && tmp_ret == TRUE) ||
(flg == NOT_FLG && tmp_ret == FALSE))
return TRUE;
else
return FALSE;
}
}
/*==================================================================*/
int regexpmrph_match(REGEXPMRPH *ptr1, Morpheme *ptr2)
/*==================================================================*/
{
/* Matching of morphemes */
int ret_mrph;
/* '?' */
if (ptr1->type_flag == QST_FLG) {
return TRUE;
}
else {
if (rule_HBK_cmp(ptr1->Hinshi_not, ptr1->Hinshi, ptr2->get_pos()) &&
rule_HBK_cmp(ptr1->Bunrui_not, ptr1->Bunrui, ptr2->get_spos()) &&
rule_HBK_cmp(ptr1->Kata_not, ptr1->Katuyou_Kata, ptr2->get_conj_type()) &&
rule_Kei_cmp(ptr1->Kei_not, ptr1->Katuyou_Kei, ptr2->get_conj_type(), ptr2->get_conj_form()) &&
rule_Goi_cmp(ptr1->Goi_not, ptr1->Goi, ptr2->get_genkei().c_str())) {
ret_mrph = TRUE;
}
else {
ret_mrph = FALSE;
}
if ((ptr1->type_flag == MAT_FLG && ret_mrph == TRUE) ||
(ptr1->type_flag == NOT_FLG && ret_mrph == FALSE)) {
return feature_pattern_match(&(ptr1->f_pattern), ptr2->get_f(), NULL, ptr2);
}
else {
return FALSE;
}
}
}
/*==================================================================*/
int regexpmrphs_match(REGEXPMRPH *r_ptr, int r_num,
std::vector<Morpheme *>::iterator d_ptr, int d_num,
int fw_or_bw,
int all_or_part,
int short_or_long)
/*==================================================================*/
{
int step, return_num;
(fw_or_bw == FW_MATCHING) ? (step = 1) : (step = -1);
if (r_num == 0) {
if (d_num == 0 || all_or_part == PART_MATCHING)
return d_num;
else
return -1;
} else {
if (r_ptr->ast_flag == AST_FLG) {
/*
if "condition*" is in the pattern, check the following possibilities
1. proceed only the pattern (skip "*" in the pattern)
2. proceed only the data (if condition matches the data)
do 1 first: SHORT_MATCHING, do 2 first: LONG_MATCHING
*/
if (short_or_long == SHORT_MATCHING) {
if ((return_num =
regexpmrphs_match(r_ptr + step, r_num - 1,
d_ptr, d_num,
fw_or_bw,
all_or_part,
short_or_long)) != -1)
return return_num;
else if (d_num &&
regexpmrph_match(r_ptr, *d_ptr) &&
(return_num =
regexpmrphs_match(r_ptr,r_num,
d_ptr + step, d_num - 1,
fw_or_bw,
all_or_part,
short_or_long)) != -1)
return return_num;
else
return -1;
} else {
if (d_num &&
regexpmrph_match(r_ptr, *d_ptr) &&
(return_num =
regexpmrphs_match(r_ptr, r_num,
d_ptr + step, d_num - 1,
fw_or_bw,
all_or_part,
short_or_long)) != -1)
return return_num;
else if ((return_num =
regexpmrphs_match(r_ptr + step, r_num - 1,
d_ptr, d_num,
fw_or_bw,
all_or_part,
short_or_long)) != -1)
return return_num;
else
return -1;
}
} else {
if (d_num &&
regexpmrph_match(r_ptr, *d_ptr) &&
(return_num =
regexpmrphs_match(r_ptr + step, r_num - 1,
d_ptr + step, d_num - 1,
fw_or_bw,
all_or_part,
short_or_long)) != -1)
return return_num;
else
return -1;
}
}
}
/*==================================================================*/
int regexpmrphs_match(REGEXPMRPH *r_ptr, int r_num,
Morpheme *d_ptr, int d_num,
int fw_or_bw,
int all_or_part,
int short_or_long)
/*==================================================================*/
{
std::vector<Morpheme *> morphemes; // Morpheme pointer -> std::vector<Morpheme *>
for (int i = 0; i < d_num; i++) {
morphemes.push_back(d_ptr + i);
}
return regexpmrphs_match(r_ptr, r_num, morphemes.begin(), morphemes.size(),
fw_or_bw, all_or_part, short_or_long);
}
/*==================================================================*/
int regexpmrphrule_match(MrphRule *r_ptr, std::vector<Morpheme *>::iterator d_ptr,
int bw_length, int fw_length)
/*==================================================================*/
{
/*
pre_pattern (shortest match)
self_pattern (longest match)
post_pattern (shortest match)
first, check pre_pattern, then from longest match of self_pattern,
after that post_pattern
*/
int match_length, match_rest;
matched_ptr = NULL; /* initialize the memory of matched morpheme or phrase */
/* first check pre_pattern */
if ((r_ptr->pre_pattern == NULL && /* differ! */
bw_length != 0) ||
(r_ptr->pre_pattern != NULL &&
regexpmrphs_match(r_ptr->pre_pattern->mrph +
r_ptr->pre_pattern->mrphsize - 1,
r_ptr->pre_pattern->mrphsize,
d_ptr - 1,
bw_length, /* differ! */
BW_MATCHING,
ALL_MATCHING,/* differ! */
SHORT_MATCHING) == -1))
return -1;
/* then, check from longest match of self_pattern, after that check post_pattern
match_length: the (possible) length of matching of self_pattern match */
match_length = fw_length; /* differ! */
while (match_length > 0) {
if (r_ptr->self_pattern == NULL) {
match_length = 1; /* if there is no self_pattern,
length is assumed to be 1 */
}
else if ((match_rest =
regexpmrphs_match(r_ptr->self_pattern->mrph,
r_ptr->self_pattern->mrphsize,
d_ptr,
match_length,
FW_MATCHING,
PART_MATCHING,
LONG_MATCHING)) != -1) {
match_length -= match_rest;
}
else {
return -1;
}
if (r_ptr->post_pattern == NULL ||
regexpmrphs_match(r_ptr->post_pattern->mrph,
r_ptr->post_pattern->mrphsize,
d_ptr + match_length,
fw_length - match_length, /* differ! */
FW_MATCHING,
ALL_MATCHING, /* differ! */
SHORT_MATCHING) != -1) {
return match_length;
}
match_length --;
}
return -1;
}
int regexpmrphs_match_for_lattice(CKY *cky, REGEXPMRPH *r_ptr,int r_num,
Morpheme *d_ptr, int d_pos, int d_num,
int fw_or_bw,
int all_or_part,
int short_or_long);
/*==================================================================*/
int regexpmrphs_lattice_match(CKY *cky, REGEXPMRPH *r_ptr, int r_num,
Morpheme *d_ptr, int d_pos, int d_num,
int fw_or_bw,
int all_or_part,
int short_or_long, vector<int> &valid_mrph_id)
/*==================================================================*/
{
int i, return_num;
/* to match ?* in rule, call at least once if there is no data */
if (d_num == 0) {
if ((return_num = regexpmrphs_match_for_lattice(cky, r_ptr, r_num,
NULL, d_pos, d_num,
fw_or_bw,
all_or_part,
short_or_long)) != -1) {
return return_num;
}
}
else {
/* if morpheme is specified (first call) */
if (d_ptr) {
if ((return_num = regexpmrphs_match_for_lattice(cky, r_ptr, r_num,
d_ptr, d_pos, d_num,
fw_or_bw,
all_or_part,
short_or_long)) != -1) {
return return_num;
}
}
/* if checking context, check all the possibilities of morpheme lengths */
else {
if (fw_or_bw == FW_MATCHING) {
/* check morpheme cells in CKY from (d_pos, d_pos) to (d_pos, Chr_num - 1) */
for (i = d_pos; i < cky->get_token_num(); i++) {
CKY_cell *cky_ptr = cky->get_base_cky_cell(d_pos, i);
while (cky_ptr && cky_ptr->exist()) {
if (cky_ptr->get_mrph_ptr() &&
cky_ptr->get_mrph_ptr()->check_id(valid_mrph_id) &&
(return_num = regexpmrphs_match_for_lattice(cky, r_ptr, r_num,
cky_ptr->get_mrph_ptr(), d_pos, d_num,
fw_or_bw,
all_or_part,
short_or_long)) != -1) {
return return_num;
}
cky_ptr = cky_ptr->get_next();
}
}
}
/* reverse direction */
else {
/* check morpheme cells in CKY from (d_pos, d_pos) to (0, d_pos) */
for (i = d_pos; i >= 0; i--) {
CKY_cell *cky_ptr = cky->get_base_cky_cell(i, d_pos);
while (cky_ptr && cky_ptr->exist()) {
if (cky_ptr->get_mrph_ptr() &&
cky_ptr->get_mrph_ptr()->check_id(valid_mrph_id) &&
(return_num = regexpmrphs_match_for_lattice(cky, r_ptr, r_num,
cky_ptr->get_mrph_ptr(), d_pos, d_num,
fw_or_bw,
all_or_part,
short_or_long)) != -1) {
return return_num;
}
cky_ptr = cky_ptr->get_next();
}
}
}
}
}
return -1;
}
/*==================================================================*/
int regexpmrphs_match_for_lattice(CKY *cky, REGEXPMRPH *r_ptr,int r_num,
Morpheme *d_ptr, int d_pos, int d_num,
int fw_or_bw,
int all_or_part,
int short_or_long)
/*==================================================================*/
{
int step, return_num;
(fw_or_bw == FW_MATCHING) ? (step = 1) : (step = -1);
if (r_num == 0) {
if (d_num == 0 || all_or_part == PART_MATCHING)
return d_num;
else
return -1;
} else {
if (r_ptr->ast_flag == AST_FLG) {
/*
if "condition*" is in the pattern, check the following possibilities
1. proceed only the pattern (skip "*" in the pattern)
2. proceed only the data (if condition matches the data)
do 1 first: SHORT_MATCHING, do 2 first: LONG_MATCHING
*/
if (short_or_long == SHORT_MATCHING) {
if ((return_num =
regexpmrphs_match_for_lattice(cky, r_ptr + step, r_num - 1,
d_ptr, d_pos, d_num,
fw_or_bw,
all_or_part,
short_or_long)) != -1)
return return_num;
else if (d_num &&
regexpmrph_match(r_ptr, d_ptr) &&
(return_num =
regexpmrphs_lattice_match(cky, r_ptr, r_num,
NULL, (fw_or_bw == FW_MATCHING) ? d_pos + d_ptr->get_token_num() : d_pos - d_ptr->get_token_num(), d_num - d_ptr->get_token_num(),
fw_or_bw,
all_or_part,
short_or_long,
(fw_or_bw == FW_MATCHING) ? d_ptr->get_forward_id() : d_ptr->get_backward_id())) != -1)
return return_num;
else
return -1;
} else {
if (d_num &&
regexpmrph_match(r_ptr, d_ptr) &&
(return_num =
regexpmrphs_lattice_match(cky, r_ptr, r_num,
NULL, (fw_or_bw == FW_MATCHING) ? d_pos + d_ptr->get_token_num() : d_pos - d_ptr->get_token_num(), d_num - d_ptr->get_token_num(),
fw_or_bw,
all_or_part,
short_or_long,
(fw_or_bw == FW_MATCHING) ? d_ptr->get_forward_id() : d_ptr->get_backward_id())) != -1)
return return_num;
else if ((return_num =
regexpmrphs_match_for_lattice(cky, r_ptr + step, r_num - 1,
d_ptr, d_pos, d_num,
fw_or_bw,
all_or_part,
short_or_long)) != -1)
return return_num;
else
return -1;
}
} else {
if (d_num &&
regexpmrph_match(r_ptr, d_ptr) &&
(return_num =
regexpmrphs_lattice_match(cky, r_ptr + step, r_num - 1,
NULL, (fw_or_bw == FW_MATCHING) ? d_pos + d_ptr->get_token_num() : d_pos - d_ptr->get_token_num(), d_num - d_ptr->get_token_num(),
fw_or_bw,
all_or_part,
short_or_long,
(fw_or_bw == FW_MATCHING) ? d_ptr->get_forward_id() : d_ptr->get_backward_id())) != -1)
return return_num;
else
return -1;
}
}
}
/*==================================================================*/
int regexpmrphrule_lattice_match(CKY *cky, MrphRule *r_ptr, Morpheme *d_ptr, int d_pos,
int bw_length, int fw_length)
/*==================================================================*/
{
/*
pre_pattern (shortest match)
self_pattern (longest match)
post_pattern (shortest match)
first, check pre_pattern, then from longest match of self_pattern,
after that post_pattern
*/
int match_length, match_rest;
matched_ptr = NULL; /* initialize the memory of matched morpheme or phrase */
/* first check pre_pattern */
if ((r_ptr->pre_pattern == NULL && /* differ! */
bw_length != 0) ||
(r_ptr->pre_pattern != NULL &&
regexpmrphs_lattice_match(cky,
r_ptr->pre_pattern->mrph + r_ptr->pre_pattern->mrphsize - 1,
r_ptr->pre_pattern->mrphsize,
NULL, d_pos - 1,
bw_length, /* differ! */
BW_MATCHING,
ALL_MATCHING,/* differ! */
SHORT_MATCHING, ANY_ID) == -1))
return -1;
/* then, check from longest match of self_pattern, after that check post_pattern
match_length: the (possible) length of matching of self_pattern match */
match_length = fw_length; /* differ! */
while (match_length > 0) {
if (r_ptr->self_pattern == NULL) {
match_length = 1; /* if there is no self_pattern,
length is assumed to be 1 */
}
else if ((match_rest =
regexpmrphs_lattice_match(cky,
r_ptr->self_pattern->mrph,
r_ptr->self_pattern->mrphsize,
d_ptr, d_pos,
match_length,
FW_MATCHING,
PART_MATCHING,
LONG_MATCHING, ANY_ID)) != -1) {
match_length -= match_rest;
}
else {
return -1;
}