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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
|
#include <gu/map.h>
#include <gu/mem.h>
#include <gu/utf8.h>
#include <gu/file.h>
#include <gu/string.h>
#include <gu/choice.h>
#include <pgf/data.h>
#include <pgf/linearizer.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#if defined(__MINGW32__) || defined(_MSC_VER)
#include <malloc.h>
#endif
//#define PGF_LOOKUP_DEBUG
//#define PGF_LINEARIZER_DEBUG
typedef struct {
PgfAbsFun* fun;
size_t arg_idx;
} PgfAbsBottomUpEntry;
typedef struct {
PgfAbsFun* fun;
size_t count;
PgfMetaId args[0];
} PgfAbsProduction;
#ifdef PGF_LOOKUP_DEBUG
static void
pgf_print_abs_production(PgfMetaId id,
PgfAbsProduction* prod,
GuOut* out, GuExn* err)
{
gu_printf(out,err,"?%d = %s",id,prod->fun->name);
size_t n_hypos = gu_seq_length(prod->fun->type->hypos);
for (size_t i = 0; i < n_hypos; i++) {
gu_printf(out,err," ?%d", prod->args[i]);
}
gu_printf(out,err," <%d>\n",prod->count);
}
static void
pgf_print_abs_productions(GuBuf* prods,
GuOut* out, GuExn* err)
{
size_t n_prods = gu_buf_length(prods);
for (size_t id = 1; id < n_prods; id++) {
GuBuf* id_prods = gu_buf_get(prods, GuBuf*, id);
size_t n_id_prods = gu_buf_length(id_prods);
for (size_t i = 0; i < n_id_prods; i++) {
PgfAbsProduction* prod =
gu_buf_get(id_prods, PgfAbsProduction*, i);
pgf_print_abs_production(id, prod, out, err);
}
}
}
#endif
#ifdef PGF_LINEARIZER_DEBUG
PGF_INTERNAL_DECL void
pgf_print_cnc_tree(PgfCncTree ctree, GuOut* out, GuExn* err);
#endif
static void
pgf_lookup_index_syms(GuMap* lexicon_idx, PgfSymbols* syms, PgfAbsFun* absfun, GuPool* pool) {
size_t n_syms = gu_seq_length(syms);
for (size_t j = 0; j < n_syms; j++) {
PgfSymbol sym = gu_seq_get(syms, PgfSymbol, j);
GuVariantInfo i = gu_variant_open(sym);
switch (i.tag) {
case PGF_SYMBOL_KP: {
PgfSymbolKP* skp = (PgfSymbolKP*) i.data;
pgf_lookup_index_syms(lexicon_idx, skp->default_form, absfun, pool);
for (size_t k = 0; k < skp->n_forms; k++) {
pgf_lookup_index_syms(lexicon_idx, skp->forms[k].form, absfun, pool);
}
break;
}
case PGF_SYMBOL_KS: {
PgfSymbolKS* sks = (PgfSymbolKS*) i.data;
GuBuf* funs = gu_map_get(lexicon_idx, sks->token, GuBuf*);
if (funs == NULL) {
funs = gu_new_buf(PgfAbsFun*, pool);
gu_map_put(lexicon_idx, sks->token, GuBuf*, funs);
}
bool found = false;
size_t n_funs = gu_buf_length(funs);
for (size_t l = 0; l < n_funs; l++) {
PgfAbsFun* absfun1 = gu_buf_get(funs, PgfAbsFun*, l);
if (absfun1 == absfun) {
found = true;
break;
}
}
if (!found)
gu_buf_push(funs, PgfAbsFun*, absfun);
break;
}
}
}
}
typedef struct {
GuMap* function_idx;
GuMap* meta_ids;
GuBuf* spine;
GuPool* pool;
} PgfSpineBuilder;
typedef struct {
PgfToken token;
size_t n_funs;
PgfAbsFun** funs;
} PgfInputToken;
static PgfAbsProduction*
pgf_lookup_new_production(PgfAbsFun* fun, GuPool *pool)
{
size_t n_hypos = fun->type->hypos ? gu_seq_length(fun->type->hypos) : 0;
PgfAbsProduction* prod = gu_new_flex(pool, PgfAbsProduction, args, n_hypos);
prod->fun = fun;
prod->count = 0;
for (size_t i = 0; i < n_hypos; i++) {
prod->args[i] = 0;
}
return prod;
}
static void
pgf_lookup_add_production(PgfSpineBuilder* builder, PgfMetaId id, PgfAbsProduction* prod)
{
GuBuf* prods = gu_buf_get(builder->spine, GuBuf*, id);
gu_buf_push(prods, PgfAbsProduction*, prod);
}
static PgfMetaId
pgf_lookup_add_spine_nodes(PgfSpineBuilder* builder, PgfCId cat) {
PgfMetaId meta_id = gu_map_get(builder->meta_ids, cat, PgfMetaId);
if (meta_id != 0) {
return meta_id;
}
meta_id = gu_buf_length(builder->spine);
gu_buf_push(builder->spine, GuBuf*, gu_new_buf(PgfAbsProduction*, builder->pool));
gu_map_put(builder->meta_ids, cat, PgfMetaId, meta_id);
GuBuf* entries = gu_map_get(builder->function_idx, cat, GuBuf*);
if (entries != NULL) {
size_t n_entries = gu_buf_length(entries);
for (size_t i = 0; i < n_entries; i++) {
PgfAbsBottomUpEntry* entry = gu_buf_index(entries, PgfAbsBottomUpEntry, i);
PgfMetaId id = pgf_lookup_add_spine_nodes(builder, entry->fun->type->cid);
PgfAbsProduction* prod = pgf_lookup_new_production(entry->fun, builder->pool);
prod->args[entry->arg_idx] = meta_id;
pgf_lookup_add_production(builder, id, prod);
}
}
return meta_id;
}
static void
pgf_lookup_add_spine_leaf(PgfSpineBuilder* builder, PgfAbsFun *fun)
{
PgfMetaId id = pgf_lookup_add_spine_nodes(builder, fun->type->cid);
PgfAbsProduction* prod = pgf_lookup_new_production(fun, builder->pool);
prod->count = 1;
pgf_lookup_add_production(builder, id, prod);
}
static GuBuf*
pgf_lookup_build_spine(GuMap* function_idx,
PgfInputToken* tok, PgfType* typ, PgfMetaId* meta_id,
GuPool* pool)
{
PgfSpineBuilder builder;
builder.function_idx = function_idx;
builder.meta_ids = gu_new_string_map(PgfMetaId, &gu_null_struct, pool);
builder.spine = gu_new_buf(GuBuf*, pool);
builder.pool = pool;
gu_buf_push(builder.spine, GuBuf*, NULL);
for (size_t i = 0; i < tok->n_funs; i++) {
pgf_lookup_add_spine_leaf(&builder, tok->funs[i]);
}
*meta_id = gu_map_get(builder.meta_ids, typ->cid, PgfMetaId);
return builder.spine;
}
typedef PgfMetaId PgfPair[2];
static bool
pgf_pair_eq_fn(GuEquality* self, const void* p1, const void* p2)
{
(void) self;
const PgfMetaId* ip1 = p1;
const PgfMetaId* ip2 = p2;
return (ip1[0] == ip2[0] && ip1[1] == ip2[1]);
}
static GuHash
pgf_pair_hash_fn(GuHasher* self, const void* p)
{
(void) self;
const PgfMetaId* ip = p;
return (GuHash) (((ip[1] & 0xFFFF) << 16) | (ip[0] & 0xFFFF));
}
static GuHasher
pgf_pair_hasher[1] = {
{
{ pgf_pair_eq_fn },
pgf_pair_hash_fn
}
};
static PgfMetaId
pgf_lookup_merge_cats(GuBuf* spine, GuMap* pairs,
PgfMetaId meta_id1, GuBuf* spine1,
PgfMetaId meta_id2, GuBuf* spine2,
GuPool* pool)
{
if (meta_id1 == 0 && meta_id2 == 0)
return 0;
PgfPair pair;
pair[0] = meta_id1;
pair[1] = meta_id2;
PgfMetaId meta_id = gu_map_get(pairs, &pair, PgfMetaId);
if (meta_id != 0)
return meta_id;
meta_id = gu_buf_length(spine);
GuBuf* id_prods = gu_new_buf(PgfAbsProduction*, pool);
gu_buf_push(spine, GuBuf*, id_prods);
gu_map_put(pairs, &pair, PgfMetaId, meta_id);
GuBuf* id_prods1 = gu_buf_get(spine1, GuBuf*, meta_id1);
GuBuf* id_prods2 = gu_buf_get(spine2, GuBuf*, meta_id2);
size_t n_id_prods1 = (meta_id1 == 0) ? 0 : gu_buf_length(id_prods1);
size_t n_id_prods2 = (meta_id2 == 0) ? 0 : gu_buf_length(id_prods2);
if (meta_id1 != 0) {
for (size_t i = 0; i < n_id_prods1; i++) {
PgfAbsProduction* prod1 =
gu_buf_get(id_prods1, PgfAbsProduction*, i);
int count = 0;
for (size_t j = 0; j < n_id_prods2; j++) {
PgfAbsProduction* prod2 =
gu_buf_get(id_prods2, PgfAbsProduction*, j);
if (prod1->fun == prod2->fun) {
PgfAbsProduction* prod =
pgf_lookup_new_production(prod1->fun, pool);
prod->count = prod1->count+prod2->count;
size_t n_hypos = gu_seq_length(prod->fun->type->hypos);
for (size_t l = 0; l < n_hypos; l++) {
prod->args[l] =
pgf_lookup_merge_cats(spine, pairs,
prod1->args[l], spine1,
prod2->args[l], spine2,
pool);
}
gu_buf_push(id_prods, PgfAbsProduction*, prod);
count++;
}
}
if (count == 0) {
PgfAbsProduction* prod =
pgf_lookup_new_production(prod1->fun, pool);
prod->count = prod1->count;
size_t n_hypos = gu_seq_length(prod->fun->type->hypos);
for (size_t l = 0; l < n_hypos; l++) {
prod->args[l] =
pgf_lookup_merge_cats(spine, pairs,
prod1->args[l], spine1,
0, spine2,
pool);
}
gu_buf_push(id_prods, PgfAbsProduction*, prod);
}
}
}
if (meta_id2 != 0) {
for (size_t i = 0; i < n_id_prods2; i++) {
PgfAbsProduction* prod2 =
gu_buf_get(id_prods2, PgfAbsProduction*, i);
bool found = false;
for (size_t j = 0; j < n_id_prods1; j++) {
PgfAbsProduction* prod1 =
gu_buf_get(id_prods1, PgfAbsProduction*, j);
if (prod1->fun == prod2->fun) {
found = true;
break;
}
}
if (!found) {
PgfAbsProduction* prod =
pgf_lookup_new_production(prod2->fun, pool);
prod->count = prod2->count;
size_t n_hypos = gu_seq_length(prod->fun->type->hypos);
for (size_t l = 0; l < n_hypos; l++) {
prod->args[l] =
pgf_lookup_merge_cats(spine, pairs,
0, spine1,
prod2->args[l], spine2,
pool);
}
gu_buf_push(id_prods, PgfAbsProduction*, prod);
}
}
}
return meta_id;
}
static GuBuf*
pgf_lookup_merge(PgfMetaId meta_id1, GuBuf* spine1,
PgfMetaId meta_id2, GuBuf* spine2,
PgfMetaId* meta_id,
GuPool* pool, GuPool* out_pool)
{
GuBuf* spine = gu_new_buf(GuBuf*, out_pool);
gu_buf_push(spine, GuBuf*, NULL);
GuMap* pairs = gu_new_map(PgfPair, pgf_pair_hasher, PgfMetaId, &gu_null_struct, pool);
*meta_id =
pgf_lookup_merge_cats(spine, pairs,
meta_id1, spine1,
meta_id2, spine2,
out_pool);
return spine;
}
typedef struct {
PgfLinFuncs* funcs;
PgfConcr* concr;
GuBuf* join;
PgfMetaId start_id;
GuChoice* choice;
GuBuf* expr_tokens;
GuBuf* ctrees;
int max_fid;
PgfAbsFun** curr_absfun;
GuPool* pool;
} PgfLookupState;
struct PgfItemConts {
size_t count;
};
typedef struct {
GuEnum en;
double max;
size_t index;
GuBuf* ctrees;
GuPool* out_pool;
} PgfLookupEnum;
static void
gu_ccat_fini(GuFinalizer* fin)
{
PgfCCat* cat = gu_container(fin, PgfCCat, fin);
if (cat->prods != NULL)
gu_seq_free(cat->prods);
}
static PgfCCat*
pgf_lookup_new_ccat(PgfLookupState* st, PgfCCat* ccat)
{
PgfCCat* new_ccat = gu_new_flex(st->pool, PgfCCat, fin, 1);
new_ccat->cnccat = ccat->cnccat;
new_ccat->lindefs = ccat->lindefs;
new_ccat->linrefs = ccat->linrefs;
new_ccat->viterbi_prob = 0;
new_ccat->fid = st->max_fid++;
new_ccat->conts = gu_new(PgfItemConts, st->pool);
new_ccat->conts->count = 0;
new_ccat->answers = NULL;
new_ccat->prods = NULL;
new_ccat->n_synprods = 0;
new_ccat->fin[0].fn = gu_ccat_fini;
gu_pool_finally(st->pool, new_ccat->fin);
return new_ccat;
}
static PgfCCat*
pgf_lookup_concretize(PgfLookupState* st, GuMap* cache, PgfMetaId meta_id, PgfCCat *ccat);
static PgfCCat*
pgf_lookup_concretize_coercions(PgfLookupState* st, GuMap* cache,
PgfMetaId meta_id, PgfCCat* ccat,
GuBuf* coercions)
{
PgfPair pair;
pair[0] = meta_id;
pair[1] = ccat->fid;
PgfCCat** pnew_ccat = gu_map_find(cache, &pair);
if (pnew_ccat != NULL)
return *pnew_ccat;
PgfCCat* new_ccat = NULL;
size_t n_coercions = gu_buf_length(coercions);
for (size_t i = 0; i < n_coercions; i++) {
PgfProductionCoerce* pcoerce =
gu_buf_get(coercions, PgfProductionCoerce*, i);
PgfCCat* new_coerce =
pgf_lookup_concretize(st, cache, meta_id, pcoerce->coerce);
if (new_coerce == NULL)
continue;
if (new_ccat == NULL) {
new_ccat = pgf_lookup_new_ccat(st, ccat);
}
PgfProduction cnc_prod;
PgfProductionCoerce* new_pcoerce =
gu_new_variant(PGF_PRODUCTION_COERCE,
PgfProductionCoerce,
&cnc_prod, st->pool);
new_pcoerce->coerce = new_coerce;
size_t count = (new_coerce->conts == NULL) ? 0 : new_coerce->conts->count;
if (count > new_ccat->conts->count) {
new_ccat->conts->count = new_coerce->conts->count;
new_ccat->n_synprods = 0;
}
if (new_ccat->prods == NULL || new_ccat->n_synprods >= gu_seq_length(new_ccat->prods)) {
new_ccat->prods = gu_realloc_seq(new_ccat->prods, PgfProduction, new_ccat->n_synprods+(n_coercions-i));
}
if (count == new_ccat->conts->count) {
gu_seq_set(new_ccat->prods, PgfProduction, new_ccat->n_synprods++, cnc_prod);
}
#ifdef PGF_LOOKUP_DEBUG
{
GuPool* tmp_pool = gu_new_pool();
GuOut* out = gu_file_out(stderr, tmp_pool);
GuExn* err = gu_exn(tmp_pool);
gu_printf(out,err,"C%d -> _[C%d] <%d>\n",new_ccat->fid,new_coerce->fid,new_coerce->conts ? new_coerce->conts->count : 0);
gu_pool_free(tmp_pool);
}
#endif
}
gu_map_put(cache, &pair, PgfCCat*, new_ccat);
return new_ccat;
}
static PgfCCat*
pgf_lookup_concretize(PgfLookupState* st, GuMap* cache, PgfMetaId meta_id, PgfCCat *ccat)
{
if (meta_id == 0) {
// if there is no lindef for this ccat then we can't use it for
// linearization of a metavariable
if (ccat->lindefs == NULL || gu_seq_length(ccat->lindefs) == 0)
return NULL;
return ccat;
}
PgfPair pair;
pair[0] = meta_id;
pair[1] = ccat->fid;
PgfCCat** pnew_ccat = gu_map_find(cache, &pair);
if (pnew_ccat != NULL) {
// check for loops
if (*pnew_ccat == (PgfCCat*) &gu_null_struct)
return NULL;
return *pnew_ccat;
}
// put a marker to detect loops
gu_map_put(cache, &pair, PgfCCat*, (PgfCCat*) &gu_null_struct);
PgfCCat* new_ccat = NULL;
GuBuf* id_prods = gu_buf_get(st->join, GuBuf*, meta_id);
size_t n_id_prods = gu_buf_length(id_prods);
for (size_t i = 0; i < n_id_prods; i++) {
PgfAbsProduction* prod =
gu_buf_get(id_prods, PgfAbsProduction*, i);
PgfCncOverloadMap* overl_table =
gu_map_get(st->concr->fun_indices, prod->fun->name, PgfCncOverloadMap*);
if (overl_table == NULL)
continue;
GuBuf* buf =
gu_map_get(overl_table, ccat, GuBuf*);
if (buf == NULL)
continue;
size_t n_prods = gu_buf_length(buf);
for (size_t j = 0; j < n_prods; j++) {
PgfProductionApply* papply =
gu_buf_get(buf, PgfProductionApply*, j);
size_t count = prod->count;
size_t n_args = gu_seq_length(papply->args);
GuSeq* new_args = gu_new_seq(PgfPArg, n_args, st->pool);
for (size_t k = 0; k < n_args; k++) {
PgfPArg* parg = gu_seq_index(papply->args, PgfPArg, k);
PgfPArg* new_parg = gu_seq_index(new_args, PgfPArg, k);
new_parg->hypos = parg->hypos;
GuBuf* coercions =
gu_map_get(st->concr->coerce_idx, parg->ccat, GuBuf*);
if (coercions == NULL) {
new_parg->ccat =
pgf_lookup_concretize(st, cache, prod->args[k], parg->ccat);
} else {
new_parg->ccat =
pgf_lookup_concretize_coercions(st, cache, prod->args[k], parg->ccat, coercions);
}
if (new_parg->ccat == NULL)
goto skip;
if (new_parg->ccat->conts != NULL)
count += new_parg->ccat->conts->count;
}
if (new_ccat == NULL) {
new_ccat = pgf_lookup_new_ccat(st, ccat);
}
PgfProduction cnc_prod;
PgfProductionApply* new_papp =
gu_new_variant(PGF_PRODUCTION_APPLY,
PgfProductionApply,
&cnc_prod, st->pool);
new_papp->fun = papply->fun;
new_papp->args = new_args;
if (count > new_ccat->conts->count) {
new_ccat->conts->count = count;
new_ccat->n_synprods = 0;
}
if (new_ccat->prods == NULL || new_ccat->n_synprods >= gu_seq_length(new_ccat->prods)) {
new_ccat->prods = gu_realloc_seq(new_ccat->prods, PgfProduction, new_ccat->n_synprods+(n_prods-j));
}
if (count == new_ccat->conts->count) {
gu_seq_set(new_ccat->prods, PgfProduction, new_ccat->n_synprods++, cnc_prod);
}
#ifdef PGF_LOOKUP_DEBUG
{
GuPool* tmp_pool = gu_new_pool();
GuOut* out = gu_file_out(stderr, tmp_pool);
GuExn* err = gu_exn(tmp_pool);
gu_printf(out,err,"C%d -> F%d[",new_ccat->fid,new_papp->fun->funid);
size_t n_args = gu_seq_length(new_papp->args);
for (size_t l = 0; l < n_args; l++) {
if (l > 0)
gu_putc(',',out,err);
PgfPArg arg = gu_seq_get(new_papp->args, PgfPArg, l);
if (arg.hypos != NULL) {
size_t n_hypos = gu_seq_length(arg.hypos);
for (size_t r = 0; r < n_hypos; r++) {
if (r > 0)
gu_putc(' ',out,err);
PgfCCat *hypo = gu_seq_get(arg.hypos, PgfCCat*, r);
gu_printf(out,err,"C%d",hypo->fid);
}
}
gu_printf(out,err,"C%d",arg.ccat->fid);
}
gu_printf(out,err,"] <%d>\n", count);
gu_pool_free(tmp_pool);
}
#endif
skip:;
}
}
gu_map_put(cache, &pair, PgfCCat*, new_ccat);
return new_ccat;
}
static PgfCncTree
pgf_lookup_extract(PgfLookupState* st, PgfCCat* ccat)
{
PgfCncTree ret;
if (ccat->fid < st->concr->total_cats) {
int index =
gu_choice_next(st->choice, gu_seq_length(ccat->lindefs));
PgfCncTreeApp* capp =
gu_new_flex_variant(PGF_CNC_TREE_APP,
PgfCncTreeApp,
args, 1, &ret, st->pool);
capp->ccat = ccat;
capp->fun = gu_seq_get(ccat->lindefs, PgfCncFun*, index);
capp->fid = 0;
capp->n_vars = 0;
capp->context = NULL;
capp->n_args = 1;
PgfCncTreeChunks* chunks =
gu_new_flex_variant(PGF_CNC_TREE_CHUNKS,
PgfCncTreeChunks,
args, 0, &capp->args[0], st->pool);
chunks->id = 0;
chunks->n_vars = 0;
chunks->context = NULL;
chunks->n_args = 0;
} else {
int index =
gu_choice_next(st->choice, ccat->n_synprods);
PgfProduction prod =
gu_seq_get(ccat->prods, PgfProduction, index);
GuVariantInfo i = gu_variant_open(prod);
switch (i.tag) {
case PGF_PRODUCTION_APPLY: {
PgfProductionApply* papply = i.data;
size_t n_args = gu_seq_length(papply->args);
PgfCncTreeApp* capp =
gu_new_flex_variant(PGF_CNC_TREE_APP,
PgfCncTreeApp,
args, n_args, &ret, st->pool);
capp->ccat = ccat;
capp->fun = papply->fun;
capp->fid = 0;
capp->n_vars = 0;
capp->context = NULL;
capp->n_args = n_args;
for (size_t i = 0; i < n_args; i++) {
PgfPArg* arg = gu_seq_index(papply->args, PgfPArg, i);
capp->args[i] = pgf_lookup_extract(st, arg->ccat);
}
break;
}
case PGF_PRODUCTION_COERCE: {
PgfProductionCoerce* pcoerce = i.data;
ret = pgf_lookup_extract(st, pcoerce->coerce);
break;
}
default:
gu_impossible();
}
}
return ret;
}
static GuBuf*
pgf_lookup_tokenize(GuMap* lexicon_idx, GuString sentence, GuPool* pool)
{
GuBuf* tokens = gu_new_buf(PgfInputToken, pool);
GuUCS c = ' ';
const uint8_t* p = (const uint8_t*) sentence;
for (;;) {
while (gu_ucs_is_space(c)) {
c = gu_utf8_decode(&p);
}
if (c == 0)
break;
const uint8_t* start = p-1;
if (strchr(".!?,:",c) != NULL)
c = gu_utf8_decode(&p);
else {
while (c != 0 && strchr(".!?,:",c) == NULL && !gu_ucs_is_space(c)) {
c = gu_utf8_decode(&p);
}
}
const uint8_t* end = p-1;
PgfInputToken* tok = gu_buf_extend(tokens);
size_t len = end-start;
tok->token = gu_malloc(pool, len+1);
memcpy((uint8_t*) tok->token, start, len);
((uint8_t*) tok->token)[len] = 0;
GuBuf* funs = gu_map_get(lexicon_idx, tok->token, GuBuf*);
if (funs != NULL) {
tok->n_funs = gu_buf_length(funs);
tok->funs = gu_buf_data(funs);
} else {
tok->n_funs = 0;
tok->funs = NULL;
}
}
return tokens;
}
static double
pgf_lookup_compute_kernel_helper(GuBuf* sentence_tokens, GuBuf* expr_tokens,
double* matrix, size_t i, size_t j)
{
size_t dim = gu_buf_length(sentence_tokens)+1;
double score = matrix[i + dim*j];
if (score < 0) {
score = 0;
for (size_t l = 0; l < i; l++) {
matrix[l + dim*j] = score;
for (size_t k = j; k > 0; k--) {
PgfInputToken* sentence_token = gu_buf_index(sentence_tokens, PgfInputToken, l);
PgfInputToken* expr_token = gu_buf_index(expr_tokens, PgfInputToken, k-1);
if (sentence_token->token != NULL && expr_token->token != NULL &&
strcmp(sentence_token->token, expr_token->token) == 0) {
score += 1 + pgf_lookup_compute_kernel_helper(sentence_tokens, expr_tokens, matrix, l, k-1);
} else {
bool match = false;
for (size_t i = 0; i < sentence_token->n_funs; i++) {
for (size_t j = 0; j < expr_token->n_funs; j++) {
if (sentence_token->funs[i] == expr_token->funs[j]) {
match = true;
goto done;
}
}
}
done:
if (match) {
score += 0.5 + pgf_lookup_compute_kernel_helper(sentence_tokens, expr_tokens, matrix, l, k-1);
}
}
}
}
matrix[i + dim*j] = score;
}
return score;
}
static double
pgf_lookup_compute_kernel(GuBuf* sentence_tokens, GuBuf* expr_tokens)
{
size_t n_sentence_tokens = gu_buf_length(sentence_tokens);
size_t n_expr_tokens = gu_buf_length(expr_tokens);
size_t size = (n_sentence_tokens+1)*(n_expr_tokens+1);
double* matrix = alloca(size*sizeof(double));
for (size_t i = 0; i < size; i++) {
matrix[i] = -1;
}
return
pgf_lookup_compute_kernel_helper(sentence_tokens,expr_tokens,matrix,
n_sentence_tokens,n_expr_tokens);
}
typedef struct {
PgfCncTree ctree;
double score;
} PgfCncTreeScore;
static void
pgf_lookup_ctree_to_expr(PgfCncTree ctree, PgfExprProb* ep,
GuPool* out_pool)
{
size_t n_args = 0;
PgfCncTree* args = NULL;
GuVariantInfo cti = gu_variant_open(ctree);
switch (cti.tag) {
case PGF_CNC_TREE_APP: {
PgfCncTreeApp* fapp = cti.data;
*ep = fapp->fun->absfun->ep;
n_args = fapp->n_args;
args = fapp->args;
break;
}
case PGF_CNC_TREE_CHUNKS: {
PgfCncTreeChunks* fchunks = cti.data;
n_args = fchunks->n_args;
args = fchunks->args;
ep->expr = gu_new_variant_i(out_pool,
PGF_EXPR_META, PgfExprMeta,
.id = fchunks->id);
ep->prob = 0;
break;
}
/* case PGF_CNC_TREE_LIT: {
PgfCncTreeLit* flit = cti.data;
break;
}*/
default:
gu_impossible();
}
if (gu_variant_is_null(ep->expr)) {
gu_assert(n_args==1);
pgf_lookup_ctree_to_expr(args[0], ep, out_pool);
} else {
for (size_t i = 0; i < n_args; i++) {
PgfExprProb ep_arg;
pgf_lookup_ctree_to_expr(args[i], &ep_arg, out_pool);
ep->expr = gu_new_variant_i(out_pool,
PGF_EXPR_APP,
PgfExprApp,
ep->expr, ep_arg.expr);
ep->prob += ep_arg.prob;
}
}
}
static void
pgf_lookup_enum_next(GuEnum* self, void* to, GuPool* pool)
{
PgfLookupEnum* st = gu_container(self, PgfLookupEnum, en);
PgfCncTreeScore* cts = NULL;
while (st->index < gu_buf_length(st->ctrees)) {
cts = gu_buf_index(st->ctrees, PgfCncTreeScore, st->index);
st->index++;
if (cts->score == st->max) {
PgfExprProb* ep = gu_new(PgfExprProb, st->out_pool);
pgf_lookup_ctree_to_expr(cts->ctree, ep, st->out_pool);
*((PgfExprProb**) to) = ep;
return;
}
}
*((PgfExprProb**) to) = NULL;
}
static void
pgf_lookup_symbol_token(PgfLinFuncs** self, PgfToken token)
{
PgfLookupState* st = gu_container(self, PgfLookupState, funcs);
PgfInputToken* tok = gu_buf_extend(st->expr_tokens);
tok->token = token;
tok->n_funs = st->curr_absfun ? 1 : 0;
tok->funs = st->curr_absfun;
}
static void
pgf_lookup_begin_phrase(PgfLinFuncs** self, PgfCId cat, int fid, GuString ann, PgfCId funname)
{
PgfLookupState* st = gu_container(self, PgfLookupState, funcs);
PgfAbsFun* absfun = gu_seq_binsearch(st->concr->abstr->funs, pgf_absfun_order, PgfAbsFun, funname);
if (absfun != NULL) {
st->curr_absfun = gu_new(PgfAbsFun*, st->pool);
*st->curr_absfun = absfun;
} else {
st->curr_absfun = NULL;
}
}
static void
pgf_lookup_end_phrase(PgfLinFuncs** self, PgfCId cat, int fid, GuString ann, PgfCId fun)
{
PgfLookupState* st = gu_container(self, PgfLookupState, funcs);
st->curr_absfun = NULL;
}
static void
pgf_lookup_symbol_meta(PgfLinFuncs** self, PgfMetaId meta_id)
{
PgfLookupState* st = gu_container(self, PgfLookupState, funcs);
PgfInputToken* tok = gu_buf_extend(st->expr_tokens);
tok->token = NULL;
tok->n_funs = 0;
}
static PgfLinFuncs pgf_lookup_lin_funcs = {
.symbol_token = pgf_lookup_symbol_token,
.begin_phrase = pgf_lookup_begin_phrase,
.end_phrase = pgf_lookup_end_phrase,
.symbol_ne = NULL,
.symbol_bind = NULL,
.symbol_capit = NULL,
.symbol_meta = pgf_lookup_symbol_meta
};
PGF_API GuEnum*
pgf_lookup_sentence(PgfConcr* concr, PgfType* typ, GuString sentence, GuPool* pool, GuPool* out_pool)
{
//// building search indices //
GuMap* lexicon_idx = gu_new_string_map(GuBuf*, &gu_null_struct, pool);
size_t n_cncfuns = gu_seq_length(concr->cncfuns);
for (size_t i = 0; i < n_cncfuns; i++) {
PgfCncFun* cncfun = gu_seq_get(concr->cncfuns, PgfCncFun*, i);
for (size_t lin_idx = 0; lin_idx < cncfun->n_lins; lin_idx++) {
pgf_lookup_index_syms(lexicon_idx, cncfun->lins[lin_idx]->syms, cncfun->absfun, pool);
}
}
GuMap* function_idx = gu_new_string_map(GuBuf*, &gu_null_struct, pool);
size_t n_funs = gu_seq_length(concr->abstr->funs);
for (size_t i = 0; i < n_funs; i++) {
PgfAbsFun* fun = gu_seq_index(concr->abstr->funs, PgfAbsFun, i);
size_t n_hypos = gu_seq_length(fun->type->hypos);
for (size_t j = 0; j < n_hypos; j++) {
PgfHypo* hypo = gu_seq_index(fun->type->hypos, PgfHypo, j);
GuBuf* funs = gu_map_get(function_idx, hypo->type->cid, GuBuf*);
if (funs == NULL) {
funs = gu_new_buf(PgfAbsBottomUpEntry, pool);
gu_map_put(function_idx, hypo->type->cid, GuBuf*, funs);
}
PgfAbsBottomUpEntry* entry = gu_buf_extend(funs);
entry->fun = fun;
entry->arg_idx = j;
}
}
///////////////////////////////
GuPool *work_pool = gu_new_pool();
GuBuf* sentence_tokens =
pgf_lookup_tokenize(lexicon_idx, sentence, work_pool);
PgfMetaId meta_id1 = 0;
GuBuf* join = gu_new_buf(GuBuf*, pool);
gu_buf_push(join, GuBuf*, NULL);
size_t n_tokens = gu_buf_length(sentence_tokens);
for (size_t i = 0; i < n_tokens; i++) {
PgfInputToken* tok = gu_buf_index(sentence_tokens, PgfInputToken, i);
PgfMetaId meta_id2 = 0;
GuBuf* spine =
pgf_lookup_build_spine(function_idx,
tok, typ, &meta_id2,
work_pool);
join = pgf_lookup_merge(meta_id1, join, meta_id2, spine, &meta_id1, work_pool, pool);
}
#ifdef PGF_LOOKUP_DEBUG
GuPool* tmp_pool = gu_new_pool();
GuOut* out = gu_file_out(stderr, tmp_pool);
GuExn* err = gu_exn(tmp_pool);
pgf_print_abs_productions(join, out, err);
gu_putc('\n',out,err);
gu_pool_free(tmp_pool);
#endif
PgfLookupState st;
st.funcs = &pgf_lookup_lin_funcs;
st.concr = concr;
st.join = join;
st.start_id= meta_id1;
st.choice = gu_new_choice(work_pool);
st.expr_tokens=gu_new_buf(PgfInputToken, work_pool);
st.ctrees = gu_new_buf(PgfCncTreeScore, pool);
st.curr_absfun= NULL;
st.max_fid = concr->total_cats;
st.pool = pool;
GuMap* cache = gu_new_map(PgfPair, pgf_pair_hasher, PgfCCat*, &gu_null_struct, pool);
double sentence_value =
pgf_lookup_compute_kernel(sentence_tokens, sentence_tokens);
double max = 0;
PgfCncCat* cnccat =
gu_map_get(concr->cnccats, typ->cid, PgfCncCat*);
size_t n_ccats = (cnccat) ? gu_seq_length(cnccat->cats) : 0;
for (size_t i = 0; i < n_ccats; i++) {
PgfCCat* ccat = gu_seq_get(cnccat->cats, PgfCCat*, i);
PgfCCat* new_ccat = pgf_lookup_concretize(&st, cache, st.start_id, ccat);
if (new_ccat == NULL)
continue;
GuChoiceMark mark = gu_choice_mark(st.choice);
for (;;) {
PgfCncTreeScore* cts = gu_buf_extend(st.ctrees);
cts->ctree =
pgf_lookup_extract(&st, new_ccat);
cts->ctree = pgf_lzr_wrap_linref(cts->ctree, st.pool);
pgf_lzr_linearize(concr, cts->ctree, 0, &st.funcs, st.pool);
double value = pgf_lookup_compute_kernel(sentence_tokens, st.expr_tokens);
double expr_value = pgf_lookup_compute_kernel(st.expr_tokens, st.expr_tokens);
cts->score =
value / sqrt(sentence_value * expr_value);
gu_buf_flush(st.expr_tokens);
#ifdef PGF_LINEARIZER_DEBUG
{
GuPool* tmp_pool = gu_new_pool();
GuOut* out = gu_file_out(stderr, tmp_pool);
GuExn* err = gu_exn(tmp_pool);
pgf_lzr_linearize_simple(concr, cts->ctree, 0,
out, err, tmp_pool);
gu_putc('\n', out, err);
pgf_print_cnc_tree(cts->ctree, out, err);
gu_printf(out, err, " [%.1f/sqrt(%.1f*%.1f)=%f]\n\n", value, sentence_value, expr_value, cts->score);
gu_pool_free(tmp_pool);
}
#endif
if (cts->score > max) {
max = cts->score;
}
gu_choice_reset(st.choice, mark);
if (!gu_choice_advance(st.choice))
break;
}
}
gu_pool_free(work_pool);
PgfLookupEnum* lenum = gu_new(PgfLookupEnum, pool);
lenum->en.next = pgf_lookup_enum_next;
lenum->max = max;
lenum->index = 0;
lenum->ctrees = st.ctrees;
lenum->out_pool= out_pool;
return &lenum->en;
}
|