-
Notifications
You must be signed in to change notification settings - Fork 0
/
chart.pl
3596 lines (3162 loc) · 108 KB
/
chart.pl
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
% -*- Mode: Prolog -*-
#!/Applications/SWI-Prolog.app/Contents/MacOS//swipl -q -t main -f
:- encoding(utf8).
:- set_prolog_flag(encoding, utf8).
:- use_module(transform_proof, [transform_proof/2]).
:- use_module(lexicon, [macro_expand/2,get_item_semantics/5]).
:- use_module(heap, [empty_heap/1,add_to_heap/4,get_from_heap/4]).
:- use_module(prob_lex, [list_atom_term/2,list_atom_term/3,remove_brackets/2]).
:- use_module(sem_utils, [substitute_sem/3,reduce_sem/2,replace_sem/4,melt_bound_variables/2,subterm/2,subterm_with_unify/2,renumbervars/1,try_unify_semantics/2,is_closed/1]).
:- use_module(latex, [latex_proof/2,latex_header/1,latex_header/2,latex_tail/1,latex_drs_semantics/2]).
:- use_module(options, [create_options/0,get_option/2,option_true/1]).
:- use_module(print_proof, [print_proof/3,xml_proof/3]).
:- use_module(ordset, [ord_subtract/3, ord_member/2, ord_insert/3, ord_subset/2, ord_key_insert/4, ord_key_insert_unify/4, ord_select/3, ord_delete/3]).
:- use_module(list_utils, [strip_keys/2,insert_nth0/4]).
:- use_module(library(pce)).
:- use_module(library(pce_util)).
:- dynamic sentence_length/1, total_formulas/1, unparsed/3, parsed/2.
:- dynamic word/3, word/4, word/5, stored/6, max_queue_size/1, justify/2.
:- dynamic crosses/3, crosses/4, constituent/3, constituent/4, current_sentence/1.
:- dynamic translate_form/2, translate_sem/2, state/2.
:- dynamic active_rule/1, vp_left/1, let_right/1, '$PROOFAXIOMS'/1.
:- compile('big_french_drt.pl').
:- dynamic '$SOLUTION'/1.
:- dynamic verbose/0, interactive/0.
:- dynamic key_index/2.
:- create_options.
quote_mode(1, 1).
display_unreduced_semantics(no).
default_depth_limit(10000).
%default_depth_limit(25000).
%output_proofs(nd).
output_proofs(chart).
% = function combining the weight of items given a rule application.
%LP% = combine two log-probabilities using sum
%LPcombine_probability(Prob0, Prob1, J, K, _R, Prob) :-
%LP /* assign penalty based on crosses tree branches */
%LP /* experimental, needs to be properly evaluated */
%LP crosses(J, K, Cross),
%LP CrossProb is log(1/(Cross+1)),
%LP Prob is Prob0 + Prob1 + CrossProb.
%LP
%LP% = take log of initial probability
%LPcompute_weight(Prob, Weight) :-
%LP Weight is log(Prob).
%CR% = count the crossing links (subtract the crossing links since we are maximizing)
%CRcombine_probability(Prob0, Prob1, J, K, _R, Prob) :-
%CR crosses(J, K, Cross),
%CR (
%CR constituent(_, J, K)
%CR ->
%CR Const = 2
%CR ;
%CR Const = 0
%CR ),
%CR Prob is Prob0 + Prob1 - Cross + Const.
%CR% Prob is Prob0 + Prob1 + Const.
%CR
%CR% = initialize to zero for crossing links
%CRcompute_weight(_, 0).
% = Chart parse library.
%
% This library contains code adapted from the following paper:
%
% Stuart M. Shieber, Yves Schabes and Fernando C. N. Pereira (1995)
% `Principles and Implementation of Deductive Parsing', Journal of
% Logic Programming 24(1-2):3-36
%
% though with some notable additions and modifications.
% Chart items are of the form
%
% item(Formula, Left, Right, Data)
%
% where Formula is a multimodal formula, Left and Right are
% string positions representing the leftmost and the
% rightmost part of the string which was recognized as
% being of type Formula.
%
% Item data is a structure containing the following information.
%
% data(Pros, Sem, Prob, H, A, B, C, D)
%
% where Pros is the prosodic structure (representing the
% words of the string in tree form), Sem is the semantics
% of the Formula, Prob is its probabiblity, H is the head
% word. In addition, there are four stacks (two for
% infixation, two for extraction). The first is for
% parenthetical infixation, the second for the (more
% constrained) adverbs and verb-modifying pps. The
% extraction stacks are for left extraction (fairly
% rare) and right extraction.
% = main
%
% handles the command line invocation of this script; all command line arguments are treated
% as file names to be compiled and parsed (using chart_parse_all).
main :-
current_prolog_flag(os_argv, Argv),
append(_, [A|Av], Argv),
file_base_name(A, 'chart.pl'),
!,
main(Av).
main([]).
main([from,A0,to,B0,F|Fs]) :-
!,
get_integer(A0, A),
get_integer(B0, B),
compile(F),
chart_parse_from_to(A,B),
main(Fs).
main([from,A0,F|Fs]) :-
!,
get_integer(A0, A),
compile(F),
chart_parse_all(A),
main(Fs).
main([to,B0,F|Fs]) :-
!,
get_integer(B0, B),
compile(F),
chart_parse_until(B),
main(Fs).
main([F|Fs]) :-
compile(F),
chart_parse_all,
main(Fs).
get_integer(N0, N) :-
integer(N0),
!,
N = N0.
get_integer(N0, N) :-
atom(N0),
!,
atom_number(N0, N).
% = parse all sentences.
chart_parse_all :-
default_depth_limit(DLimit),
chart_parse_all(1, max, DLimit).
% = parse all sentences with a given depth limit DL
chart_parse_all_dl(DL) :-
chart_parse_all(1, max, DL).
% = parse all sentences starting with SentNo
chart_parse_all(SentNo) :-
default_depth_limit(DLimit),
chart_parse_all(SentNo, max, DLimit).
% = parse all sentences until Max
chart_parse_until(Max) :-
default_depth_limit(DLimit),
chart_parse_all(1, Max, DLimit).
chart_parse_until(Max, DLimit) :-
chart_parse_all(1, Max, DLimit).
chart_parse_from_to(Start, End) :-
default_depth_limit(DLimit),
chart_parse_all(Start, End, DLimit).
chart_parse_from_to(Start, End, DLimit) :-
chart_parse_all(Start, End, DLimit).
chart_parse_all(SentNo, Max, DL) :-
retractall(unparsed(_,_,_)),
new_output_file(grail_log, log),
new_output_file(unparsed, unparsed),
new_output_file(parse_logs, plog),
new_output_file('proof.tex', proof),
new_output_file('proofs.pl', pl_proof),
set_global_counter('$CHART_CURRENT', 0),
set_global_counter('$CHART_FAIL', 0),
set_global_counter('$CHART_LIMIT', 0),
set_global_counter('$CHART_ALL', 0),
print_grail_semantics_header,
chart_parse_all0(SentNo, Max, DL),
/* cleanup after failure-driven loop has parsed all sentences */
'$CHART_ALL'(ALL),
(
ALL > 0
->
'$CHART_FAIL'(FAIL),
'$CHART_LIMIT'(LIMIT),
retractall('$CHART_ALL'(_)),
retractall('$CHART_FAIL'(_)),
retractall('$CHART_LIMIT'(_)),
Success is (ALL - FAIL) - LIMIT,
SPercentage is (100*Success)/ALL,
FPercentage is (100*FAIL)/ALL,
LPercentage is (100*LIMIT)/ALL,
format('~nFinished parsing~n~w total sentences~n~w sentences succeeded (~w %)~n~w sentences failed (~w %)~n~w resource limits (~w %)~n', [ALL,Success,SPercentage,FAIL,FPercentage,LIMIT,LPercentage])
;
format('No sentences~n', [])
),
print_grail_semantics_tail,
close(unparsed),
close(proof),
close(pl_proof),
close(plog),
close(log).
chart_parse_all0(N, Max, DL) :-
clause(user:sent(N0,_),_),
(
/* fail for sentence numbers smaller than N or bigger than Max */
N0 >= N, N0 @=< Max
->
increase_global_counter('$CHART_ALL'),
set_global_counter('$CHART_CURRENT', N0)
),
format('~nStarting: ~w~n', [N0]),
update_crossing(N0),
statistics(process_cputime, CPU0),
statistics(inferences, Inferences0),
parse_with_depth_limit(N0, Result, DL, DepthLimit),
(
DepthLimit = depth_limit_exceeded
->
try_recover_chart_semantics(N0, DL, Inferences0, CPU0)
;
Result = fail
->
print_statistics('F', N0, DepthLimit, Inferences0, CPU0),
sentence_length(Length),
assert('unparsed'(N0,Length,fail)),
portray_clause(unparsed, unparsed(N0,Length,fail)),
increase_global_counter('$CHART_FAIL'),
format('~nFailure: ~w (~w)~n', [N0,DepthLimit])
;
print_statistics('S', N0, DepthLimit, Inferences0, CPU0),
print_grail_semantics(N0, Result),
assert(parsed(N0, Result)),
format('~nSuccess: ~w (~w)~n', [N0,DepthLimit])
),
fail.
chart_parse_all0(_, _, _).
% = parse_with_depth_limit(+SentNo, -Result, +MaxDepth, -FinalDepth)
%
% parse SentNo (with semantics Result) given depth limit MaxDepth, on exit
% FinalDepth will be the maximum depth used; in case the depth limit is
% exceeded FinalDepth will be depth_limit_exceeded.
parse_with_depth_limit(N, Result, DL, DepthLimit) :-
call_with_depth_limit(parse_sentence(N, Result), DL, DepthLimit0),
(
Result = fail,
DepthLimit0 >= DL
->
DepthLimit = depth_limit_exceeded
;
DepthLimit = DepthLimit0
).
% = parse_id(+SentNo, -Result, +MaxDepth, -FinalDepth)
%
% as parse_with_depth_limit/4, but performs iterative deepening: if the
% depth limit is exceeded (while the search space is not yet exhausted)
% then the sentence is retried with a higher depth limit (1000 higher).
parse_id(N, Result, DL0, DepthLimit) :-
update_crossing(N),
parse_with_depth_limit(N, Result0, DL0, DepthLimit0),
(
DepthLimit0 = depth_limit_exceeded
->
DL is DL0 + 1000,
format('~nFailed~nNew Depth Limit: ~w~n', [DL]),
parse_id(N, Result, DL, DepthLimit)
;
Result = Result0,
DepthLimit = DepthLimit0
).
% = parse_with_time_limit(+SentNo, -Result, +Time)
%
% parses SentNo (with semantics Result) given the time limit Time, in case the time limit
% is exceeded, Result with be depth_limit_exceeded (for reasons of uniformity, no distinction
% is made between an exceeded depth or time limit).
parse_with_time_limit(N, Result, T) :-
catch(call_with_time_limit(T, parse_sentence(N, Result)), _, Result=depth_limit_exceeded).
% = parse_sentence(+SentNo, -Semantics)
%
% computes the first Semantics corresponding to sentence SentNo, returning Semantics=fail in case the
% parse is unsuccessful.
%
% In combination with call_with_depth_limit, this will return Semantics=fail in case the depth limit
% is exceeded; "fail" with values of less than the depth limit give an indication of the depth of
% the search space.
parse_sentence(N, Result) :-
(
user:sent(N, Result)
->
true
;
Result = fail
).
sentence(S, Sem) :-
update_crossing(S),
user:sent(S, Sem).
% = update_crossing(+SentNo)
%
% asserts basic facts in preparation for parsing SentNo; this includes, for each pair of string
% positions, the number of constituent boundaries it crosses.
update_crossing(S) :-
retractall(current_sentence(_)),
assert(current_sentence(S)),
retractall(crosses(_,_,_)),
retractall(constituent(_,_,_)),
% set three-argument crosses/constituent to current sentence
assert((crosses(X,Y,Z) :- crosses(S,X,Y,Z), !)),
assert((constituent(Y,Z) :- constituent(S,_,Y,Z), !)),
% default to no crosses
assertz(crosses(_,_,0)),
retractall(word(_,_,_)),
retractall(vp_left(_)),
retractall(let_right(_)),
assert((word(A,B,C) :- word(S,A,B,C))).
% = print_statistics
%
% output parse statistics to the log file
print_statistics(State, N0, DepthLimit, Inferences0, CPU0) :-
statistics(inferences, Inferences1),
statistics(process_cputime, CPU1),
CPU is CPU1 - CPU0,
Inferences is Inferences1 - Inferences0,
sentence_length(Length),
total_formulas(TotalForms),
robust_max_queue_size(MaxQ),
format(plog, '~w\t~w\t~w\t~w\t~w\t~2f\t~w\t~w\t', [N0, State, Length, TotalForms, DepthLimit, CPU, Inferences, MaxQ]),
output_rule_statistics.
% = statistics_header
%
% add header with column names to the parse logs file
statistics_header :-
check_plog_stream,
rule_counts_init(Counts),
format(plog, 'Sent\tState\tLength\tForms\tDepth\tCPU\tInfs\tQueue', []),
print_header(Counts).
% print the rule names
print_header([]) :-
nl(plog).
print_header([R-_|Rest]) :-
format(plog, '~w\t', [R]),
print_header(Rest).
% = try_recover_chart_semantics
%
% once the DepthLimit has been exceeded, check if the chart contains a solution
% and if so, recover its semantics (and count the parse as a success); if not
% the parse is added as a failure (for the current depth_limit) and noted as such.
try_recover_chart_semantics(N0, DL, Inferences, CPU) :-
final_item(Goal, Index, Sem),
item_in_chart(Goal, Index),
compute_proof(Index),
!,
increase_global_counter('$SOLUTION'),
assert(parsed(N0, Sem)),
print_grail_semantics(N0, Sem),
format('~nSuccess: ~w (MAX)~n', [N0]),
print_statistics('S', N0, DL, Inferences, CPU).
try_recover_chart_semantics(N0, DL, Inferences, CPU) :-
increase_global_counter('$CHART_LIMIT'),
sentence_length(Length),
assert('unparsed'(N0,Length,depth_limit)),
portray_clause(unparsed, unparsed(N0,Length,depth_limit)),
format('~nDepth limit exceeded: ~w~n', [N0]),
print_statistics('L', N0, DL, Inferences, CPU).
% = startsymbol(+Start, +Semantics)
%
% true if Start is a valid category for spanning the entire chart.
% Semantics is the semantic term for existantial closure and other "final" operations to produce proper DRSs
startsymbol(lit(txt), lambda(X,X)).
startsymbol(lit(s), lambda(S,merge(drs([event(E)],[]),appl(S,E)))).
startsymbol(lit(s(_)), lambda(S,merge(drs([event(E)],[]),appl(S,E)))).
startsymbol(lit(np(_,_,_)), lambda(P,appl(P,lambda(_V,drs([],[]))))).
startsymbol(lit(n), lambda(N,merge(drs([variable(X)],[]),appl(N,X)))).
startsymbol(dl(0,lit(np(_,_,_)),lit(s(_))), lambda(VP,merge(drs([event(E),variable(X)],[appl(generic,X)]),appl(appl(VP,lambda(P,appl(P,X))),E)))).
startsymbol(dl(0,lit(n),lit(n)), lambda(ADJ,merge(drs([variable(X)],[]),appl(appl(ADJ,lambda(_,drs([],[]))),X)))).
startsymbol(dr(0,lit(s),lit(s)), lambda(ADV,merge(drs([event(E)],[bool(E,=,'event?')]),appl(appl(ADV,lambda(_,drs([],[]))),E)))).
startsymbol(dr(0,lit(s(_)),lit(s(_))), lambda(ADV,merge(drs([event(E)],[bool(E,=,'event?')]),appl(appl(ADV,lambda(_,drs([],[]))),E)))).
startsymbol(lit(let), lambda(_,drs([],[]))).
chart_semantics(SemInfo0, Semantics0, Semantics) :-
(
'$PROOFAXIOMS'(PFs),
update_seminfo(SemInfo0, PFs, SemInfo)
->
true
;
/* proceed normally if no match is found */
SemInfo0 = SemInfo
),
compute_semantics(SemInfo, Subst),
substitute_sem(Subst, Semantics0, Semantics).
% = update_seminfo(+InitialEntries, +ProofAxioms, -MergedEntries)
%
% try to unify the initial lexical formulas with the axioms of the proof; this may further instantiate some
% underspecified lexical formulas.
% done when we have matched all proof axioms
update_seminfo(_, [], []) :-
!.
update_seminfo([IN-t(W,PosTT,Lemma,F)|Rest], [X-(W0-F0)|WFs], Update0) :-
(
W0 = W,
F = F0
->
Update0 = [IN-t(W,PosTT,Lemma,F)|Update],
update_seminfo(Rest, WFs, Update)
;
/* ignore axioms which don't match the given word-formula pair */
Update = Update0,
update_seminfo(Rest, [X-(W0-F0)|WFs], Update)
).
compute_semantics([], []).
compute_semantics([IN-t(W,PosTT,Lemma,F)|Rest0], [IN-Sem|Rest]) :-
get_item_semantics(W, PosTT, Lemma, F, Sem),
compute_semantics(Rest0, Rest).
print_grail_semantics_header :-
open_semantics_files,
get_option(paper_size, PaperSize),
latex_header(sem, PaperSize).
print_grail_semantics(SentN0, Sem) :-
renumbervars(Sem),
reduce_sem(Sem, RSem),
format('~nSemantics : ~p~n', [Sem]),
format('Reduced Sem : ~p~n', [RSem]),
format(log, '~n% = Semantics~2n ~W~2n', [Sem,[numbervars(true),quoted(true)]]),
format(log, '% = Reduced Semantics~2n~W~2n', [RSem,[numbervars(true),quoted(true)]]),
format(sem_pl, '~n% = Semantics~2nsemantics(~d, unreduced, ~W).~2n', [SentN0,Sem,[numbervars(true),quoted(true)]]),
format(sem_pl, '% = Reduced Semantics~2nsemantics(~d, reduced, ~W).~2n', [SentN0,RSem,[numbervars(true),quoted(true)]]),
format(sem, '~n\\begin{multline}~n', []),
(
display_unreduced_semantics(yes)
->
latex_drs_semantics(Sem, sem),
format(sem, '\\rightarrow_{\\beta}\\\\ ', [])
;
true
),
latex_drs_semantics(RSem, sem),
format(sem, '~n\\end{multline}~2n', []).
print_grail_semantics_tail :-
latex_tail(sem),
close(sem),
pdflatex_semantics.
% = prob_parse(+ListOfAxioms, -Result)
prob_parse(List, Result) :-
check_log_stream,
init_chart,
empty_heap(Heap),
list_to_chart(List, 0, Heap, Chart, [], 0, _V, SemInfo, []),
(
interactive
->
interactive_parse(Chart, Result0)
;
chart_parse(Chart, Result0)
),
chart_semantics(SemInfo, Result0, Result).
verify_word(W, N0, N) :-
word(W1, N0, N),
!,
(
W = W1
->
true
;
format('Alignment error: ~w-~w (~w-~w)~n', [W, W1, N0, N]),
format(log, 'Alignment error: ~w-~w (~w-~w)~n', [W, W1, N0, N])
).
verify_word(_, _ , _).
lemma_sequence([], N, N).
lemma_sequence([L|Ls], N0, N) :-
word(_, _, L, N0, N1),
lemma_sequence(Ls, N1, N).
% = list_to_chart
%
% convert a list of (weighted) lexical entries to an agenda.
% add the best item of each word to the agenda, while constructing a heap with
% all of the alternatives.
%
% The result is an agenda with the best items for each word from left to right
% followed by the alternatives in order of decreasing weight.
list_to_chart([], N, H, As0, As, V, V, S, S) :-
retractall(sentence_length(_)),
assert(sentence_length(N)),
add_heap_to_chart(H, As0, As).
% skip final punctuation if its formula is "boring"
%LPlist_to_chart([si(_, PUN, _, FP)], N, H, As0, As, V, V, S, S) :-
%LP is_punct(PUN),
%LP boring(FP) ,
%LP retractall(sentence_length(_)),
%LP assert(sentence_length(N)),
%LP !,
%LP add_heap_to_chart(H, As0, As).
list_to_chart([ex_si(_,_,_,_)|_], _N0, _H0, _As0, _As, _V0, _V, _S0, _S) :-
format('~N{Error: unlemmatized sentence!}~n', []),
fail.
list_to_chart([si(W,Pos,Lemma,FPs)|Ws], N0, H0, As0, As, V0, V, S0, S) :-
N1 is N0 + 1,
assert(word(W, Pos, Lemma, N0, N1)),
assert_if_verb(Pos, N0),
assert_if_let(Pos, N1),
append_item_and_update_heap(FPs, W, Pos, Lemma, N0, N1, V0, V1, S0, S1, H0, H, As0, As1),
list_to_chart(Ws, N1, H, As1, As, V1, V, S1, S).
% = add_heap_to_chart
%
% add the alternative items of the agenda by decreasing weight.
add_heap_to_chart(H0) -->
{get_from_heap(H0, _Key, Datum, H)},
!,
[Datum],
add_heap_to_chart(H).
add_heap_to_chart(_) -->
[].
% = append_item_and_update_heap
%
% adds best item to the list and all alternatives to the heap.
append_item_and_update_heap([], _, _, _, _, _, _, S, S, H, H) -->
[].
append_item_and_update_heap([F0-P|FPs], W, Pos, Lemma, N0, N1, IN0, IN, S0, S, H0, H) -->
!,
{create_item(F0, P, W, Pos, Lemma, N0, N1, IN0, S0, S1, Item),
IN1 is IN0 + 1},
[Item],
{update_heap(FPs, P, W, Pos, Lemma, N0, N1, IN1, IN, S1, S, H0, H)}.
append_item_and_update_heap([F0,P|FPs], W, Pos, Lemma, N0, N1, IN0, IN, S0, S, H0, H) -->
{create_item(F0, P, W, Pos, Lemma, N0, N1, IN0, S0, S1, Item),
IN1 is IN0 + 1},
[Item],
{update_heap(FPs, P, W, Pos, Lemma, N0, N1, IN1, IN, S1, S, H0, H)}.
% = update_heap
%
% adds items to the heap
update_heap([], _, _, _, _, _, _, IN, IN, S, S, H, H).
update_heap([F0-P|FPs], PMax, W, Pos, Lemma, N0, N1, IN0, IN, S0, S, H0, H) :-
!,
create_item(F0, P, W, Pos, Lemma, N0, N1, IN0, S0, S1, Item),
IN1 is IN0 + 1,
Key is PMax/P,
add_to_heap(H0, Key, Item, H1),
update_heap(FPs, PMax, W, Pos, Lemma, N0, N1, IN1, IN, S1, S, H1, H).
update_heap([F0,P|FPs], PMax, W, Pos, Lemma, N0, N1, IN0, IN, S0, S, H0, H) :-
create_item(F0, P, W, Pos, Lemma, N0, N1, IN0, S0, S1, Item),
IN1 is IN0 + 1,
Key is PMax/P,
add_to_heap(H0, Key, Item, H1),
update_heap(FPs, PMax, W, Pos, Lemma, N0, N1, IN1, IN, S1, S, H1, H).
% = create_item(+Formula, +Probability, +Word, +POStag, +Lemma, +Left, +Right, +ItemNo, ListIn, ListOut, -Item)
%
% construct Item based on all available information
create_item(F0, P, W, Pos, Lemma, N0, N1, IN, [word(IN)-t(W,PosTT,Lemma,F)|Ss], Ss, item(F, N0, N1, Data)) :-
macro_expand(F0, F1),
get_pos_tt(Pos, PosTT),
enrich_formula(Lemma, PosTT, F1),
correct_formula(Lemma, PosTT, F1, F),
create_data(W, F, Lemma, word(IN), P, N0, N1, Data).
% = get_semantics(+ChartItem, ?Semantics)
%
% true if ChartItem has meaning Semantics
get_semantics(item(_, _, _, Data), Sem) :-
get_data_semantics(Data, Sem).
get_data_semantics(data(_, Sem, _, _, _, _, _, _), Sem).
% = get_weight(+ChartItem, ?Weight)
%
% true if ChartItem has Weight
get_weight(item(_, _, _, Data), Weight) :-
get_data_weight(Data, Weight).
get_data_weight(data(_, _, Weight, _, _, _, _, _), Weight).
% = robust_max_queue_size(+QueueSize)
%
% computes the size of the queue; normally this is stored as a dynamic predicate max_queue_size/1.
% However, if it is not, this predicate will scan the chart to find the chart item with the
% highest value.
robust_max_queue_size(MaxQ) :-
(
max_queue_size(MaxQ)
->
true
;
robust_max_queue_size1(1000, 1000, MaxQ)
).
robust_max_queue_size1(Current, Step, MaxQ) :-
(
stored(Current, _, _, _, _, _)
->
New is Current + Step,
robust_max_queue_size1(New, Step, MaxQ)
;
NewStep is Step/10,
New is Current - Step + NewStep,
robust_max_queue_size2(NewStep, New, MaxQ)
).
robust_max_queue_size2(1, Current, MaxQ) :-
!,
(
stored(Current, _, _, _, _, _)
->
New is Current + 1,
robust_max_queue_size2(1, New, MaxQ)
;
MaxQ is Current - 1
).
robust_max_queue_size2(Step, Current, MaxQ) :-
robust_max_queue_size1(Current, Step, MaxQ).
% = is_punct(+POS)
%
% true if POS tag is an interpunction symbol
is_punct(ponct).
is_punct(pun).
is_punct(ponct-pun).
% = boring(+Entry)
%
% true if all solutions formulas assigned to a word are "boring"
boring([]).
boring([Item-_|FPs]) :-
boring_item(Item),
!,
boring(FPs).
boring([Item,_|FPs]) :-
boring_item(Item),
boring(FPs).
boring_item(let).
boring_item(dl(0,_,txt)).
boring_item(dl(0,_,lit(txt))).
% = enrich_formula(+Word, +POStag, ?Formula)
%
% given a Word-POStag pair, tries to unify Formula with a formula containing more detailed information;
% for example, Word with formula dr(0,pp(_),np) is instantiated to dr(0,pp(Word),np)
enrich_formula(à, _, dr(0,dl(0,lit(np(_,_,_)),lit(s(inf(à)))),dl(0,lit(np(_,_,_)),lit(s(inf(base)))))) :-
!.
enrich_formula(à, _, dr(0,dl(0,lit(np(_,_,_)),lit(s(inf(à)))),dl(0,lit(np(_,_,_)),lit(s(inf(base)))))) :-
!.
enrich_formula(à, _, dr(0,lit(pp(à)),lit(np(acc,_,_)))) :-
!.
enrich_formula(de, _, dr(0,lit(pp(de)),lit(n))) :-
!.
enrich_formula(de, _, dr(0,dl(0,lit(np(_,_,_)),lit(s(inf(de)))),dl(0,lit(np(_,_,_)),lit(s(inf(base)))))) :-
!.
enrich_formula('d\'', _, dr(0,dl(0,lit(np(_,_,_)),lit(s(inf(de)))),dl(0,lit(np(_,_,_)),lit(s(inf(base)))))) :-
!.
enrich_formula(de, _, dr(0,dl(0,lit(np(_,_,_)),lit(s(inf(de)))),dl(0,lit(np(_,_,_)),lit(s(inf(base)))))) :-
!.
enrich_formula(de, _, dr(0,lit(pp(de)),lit(np(acc,_,_)))) :-
!.
enrich_formula(de, _, dr(0,lit(pp(de)),lit(n))) :-
!.
enrich_formula(par, _, dr(0,dl(0,lit(np(_,_,_)),lit(s(inf(par)))),dl(0,lit(np(_,_,_)),lit(s(inf(base)))))) :-
!.
enrich_formula(par, _, dr(0,lit(pp(par)),lit(np(acc,_,_)))) :-
!.
enrich_formula(par, _, dr(0,lit(pp(par)),lit(n))) :-
!.
enrich_formula(pour, _, dr(0,dl(0,lit(np(_,_,_)),lit(s(inf(pour)))),dl(0,lit(np(_,_,_)),lit(s(inf(base)))))) :-
!.
enrich_formula(pour, _, dr(0,lit(pp(pour)),lit(np(acc,_,_)))) :-
!.
enrich_formula(pour, _, dr(0,lit(pp(pour)),lit(n))) :-
!.
enrich_formula(pour, _, dr(0,lit(pp(pour)),lit(s(q)))) :-
!.
enrich_formula(contre, _, dr(0,lit(pp(contre)),lit(np(acc,_,_)))) :-
!.
enrich_formula(contre, _, dr(0,lit(pp(contre)),lit(n))) :-
!.
enrich_formula(sous, _, dr(0,lit(pp(sous)),lit(np(acc,_,_)))) :-
!.
enrich_formula(sous, _, dr(0,lit(pp(sous)),lit(n))) :-
!.
enrich_formula(sur, _, dr(0,lit(pp(sur)),lit(np(acc,_,_)))) :-
!.
enrich_formula(sur, _, dr(0,lit(pp(sur)),lit(n))) :-
!.
enrich_formula(en, _, dr(0,lit(pp(en)),lit(np(acc,_,_)))) :-
!.
enrich_formula(en, _, dr(0,lit(pp(en)),lit(n))) :-
!.
enrich_formula(avant, _, dr(0,lit(pp(avant)),lit(np(acc,_,_)))) :-
!.
enrich_formula(avant, _, dr(0,lit(pp(avant)),lit(n))) :-
!.
enrich_formula(après, _, dr(0,lit(pp(après)),lit(np(acc,_,_)))) :-
!.
enrich_formula(après, _, dr(0,lit(pp(après)),lit(n))) :-
!.
enrich_formula(L, _, dr(0,lit(pp(L)),lit(np(acc,_,_)))) :-
!.
enrich_formula(L, _, dr(0,lit(pp(L)),lit(n))) :-
!.
% preposed prepositions
enrich_formula(L, _, dr(0, dr(0, lit(s(S)), dr(0, lit(s(S)), dia(1, box(1, lit(pp(L)))))), lit(np(acc,_,_)))) :-
!.
enrich_formula(L, _, dr(0, dr(0, lit(s(S)), dr(0, lit(s(S)), dia(1, box(1, lit(pp(L)))))), lit(n))) :-
!.
% = quoted speech (note that only sentence-modifier, as used here, works correctly for the entire corpus)
enrich_formula(_, ver:futu, dr(0, dl(1, lit(s(S)), lit(s(S))), lit(np(nom,_,_)))) :-
!.
enrich_formula(_, ver:pres, dr(0, dl(1, lit(s(S)), lit(s(S))), lit(np(nom,_,_)))) :-
!.
enrich_formula(_, ver:impf, dr(0, dl(1, lit(s(S)), lit(s(S))), lit(np(nom,_,_)))) :-
!.
enrich_formula(_, ver:impe, lit(s(impe))) :-
!.
enrich_formula(_, ver:_, lit(s(main))) :-
!.
enrich_formula(_, int, lit(s(main))) :-
!.
enrich_formula(_, adv, lit(s(main))) :-
!.
enrich_formula(_, ver:impe, dr(0, lit(s(impe)), lit(s(q)))) :-
!.
enrich_formula(_, ver:_, dr(0, lit(s(main)), lit(s(q)))) :-
!.
enrich_formula(_, ver:impe, dr(0, lit(s(impe)), lit(cl_r))) :-
!.
enrich_formula(_, ver:impe, dr(0,lit(s(impe)),dl(0,lit(np(nom,_,_)),lit(s(inf(_)))))) :-
!.
enrich_formula(_, ver:_, dr(0,lit(s(main)),dl(0,lit(np(nom,_,_)),lit(s(inf(_)))))) :-
!.
enrich_formula(_, prp, dr(0, dl(1, lit(s(S)), lit(s(S))), lit(np(acc,_,_)))) :-
!.
enrich_formula(_, prp, dr(0, dr(0, lit(s(S)), lit(s(S))), lit(np(acc,_,_)))) :-
!.
enrich_formula(_, pun, dr(0, dl(0, lit(np(_,_,_)), lit(s(S))), lit(s(S)))) :-
!.
enrich_formula(_, pun, dl(0, dl(0, dl(0, lit(np(_,_,_)), lit(s(_))), lit(s(S))), lit(s(S)))) :-
!.
enrich_formula(_, kon, dl(0, dl(0, lit(np(_,_,_)), lit(s(S))), lit(s(S)))) :-
!.
enrich_formula(_, ver:_, dr(0,dr(0,lit(s(main)),dl(1,lit(s(S)),lit(s(S)))),lit(np(nom,_,_)))) :-
!.
enrich_formula(le, pro:per, dr(0, _, dr(0, _, dia(1, box(1, lit(np(acc,_,3-s))))))) :-
!.
enrich_formula('l\'', pro:per, dr(0, _, dr(0, _, dia(1, box(1, lit(np(acc,_,3-s))))))) :-
!.
enrich_formula(la, pro:per, dr(0, _, dr(0, _, dia(1, box(1, lit(np(acc,_,3-s))))))) :-
!.
enrich_formula(les, pro:per, dr(0, _, dr(0, _, dia(1, box(1, lit(np(acc,_,3-p))))))) :-
!.
enrich_formula(il, pro:per, lit(np(nom,il,3-s))) :-
!.
enrich_formula('Il', pro:per, lit(np(nom,il,3-s))) :-
!.
enrich_formula('-il', pro:per, lit(np(nom,il,3-s))) :-
!.
enrich_formula('-t-il', pro:per, lit(np(nom,il,3-s))) :-
!.
enrich_formula(ce, pro:per, lit(np(nom,ce,3-s))) :-
!.
enrich_formula('c\'', pro:per, lit(np(nom,ce,3-s))) :-
!.
enrich_formula('-ce', pro:per, lit(np(nom,ce,3-s))) :-
!.
enrich_formula('Ce', pro:per, lit(np(nom,ce,3-s))) :-
!.
enrich_formula('C\'', pro:per, lit(np(nom,ce,3-s))) :-
!.
enrich_formula(_, _, _).
% = correct_formula(+Lemma, +TTPostag, +Formula, -CorrectedFormula)
%
% sometimes, the correct formula translation is decided in part by the POS-tag (eg. for an imperative, the argument np is an
% accusative instead of a nominative); this predicate corrects the formula based on the POS-tag information; defaults to
% leaving the formula as is
correct_formula(_, pro:rel, dl(0, dr(0, dl(0, lit(np(A,B,C)), lit(s(main))), lit(np(D,E,F))), lit(s(whq))),
dl(0, dr(0, dl(0, lit(np(A,B,C)), lit(s(_))), lit(np(D,E,F))), lit(s(whq)))) :-
!.
correct_formula(_, adv, dr(0, dl(0, lit(np(nom, A, B)), lit(s(main))), lit(s(q))), dr(0, dl(0,lit(np(nom,A,B)), lit(s(_))), lit(s(q)))) :-
!.
correct_formula(_, ver:impe, dr(0, lit(s(_)),lit(np(nom,A,B))), dr(0, lit(s(impe)), lit(np(acc,A,B)))) :-
!.
correct_formula(_, ver:impe, dr(0, dr(0, lit(s(_)),lit(np(nom,A,B))), lit(pp(P))), dr(0, dr(0, lit(s(impe)), lit(np(acc,A,B))), lit(pp(P)))) :-
!.
correct_formula(_, ver:impe, dr(0, dr(0, lit(s(_)), lit(pp(P))),lit(np(nom,A,B))), dr(0, dr(0, lit(s(impe)), lit(pp(P))), lit(np(acc,A,B)))) :-
!.
correct_formula(_, ver:impe, dr(0, dr(0, lit(s(_)), lit(s(Q))), lit(np(nom,A,B))), dr(0, dr(0, lit(s(impe)), lit(s(Q))), lit(np(acc,A,B)))) :-
!.
correct_formula(_, ver:impe, dr(0, dr(0, lit(s(_)), dl(0, lit(n), lit(n))), lit(np(nom,A,B))), dr(0, dr(0, lit(s(_)), dl(0, lit(n), lit(n))), lit(np(acc,A,B)))) :-
!.
correct_formula(_, ver:impe, dr(0, dr(0, lit(s(_)), dl(0,lit(np(nom,A,B)), lit(s(inf(C))))), lit(cl_r)),
dr(0, dr(0, lit(s(impe)), dl(0,lit(np(nom,A,B)), lit(s(inf(C))))), lit(cl_r))) :-
!.
correct_formula(_, ver:infi, dr(0, dl(0, lit(np(nom,A,B)), lit(s(inf(_)))), dl(0,lit(np(nom,A,B)), lit(s(inf(C))))),
dr(0, dl(0, lit(np(nom,A,B)), lit(s(inf(base)))), dl(0,lit(np(nom,A,B)), lit(s(inf(C)))))) :-
!.
% "faire echo/allusion/partie"
correct_formula(faire, ver:_, dr(0,dr(0,dr(0,lit(s(A)),lit(np(acc,B,C))),lit(pp(D))),lit(np(nom,E,F))),
dr(0,dr(0,dr(0,lit(s(A)),lit(np(nom,B,C))),lit(pp(D))),lit(np(acc,E,F)))).
% "donner lieu"
correct_formula(donner, ver:_, dr(0,dr(0,dr(0,lit(s(A)),lit(np(acc,B,C))),lit(pp(D))),lit(np(nom,E,F))),
dr(0,dr(0,dr(0,lit(s(A)),lit(np(nom,B,C))),lit(pp(D))),lit(np(acc,E,F)))).
%correct_formula(ver:_, dr(0, dl(1, lit(s(_)), lit(s(S2))), lit(np(_,A,B))), dr(0, dl(1, lit(s(S1)), lit(s(S2))), lit(np(nom,A,B)))
correct_formula(_, _, F, F).
get_pos_tt(Pos, PosTT) :-
(
Pos = _Melt0-Pos0:Sub
->
PosTT = Pos0:Sub
;
Pos = _Melt1-PosTT
->
true
;
PosTT = Pos
).
% =
chart_parse(Axioms, Sem) :-
init_agenda(Axioms, Agenda),
active_rules(Axioms),
(
/* succeed for empty sentences (eg. interpunction only) */
Agenda = queue(0,0)
->
Sem = drs([],[])
;
exhaust(Agenda),
check_solution(Sem)
).
% = active_rules(+Axioms)
%
% check all formulas used in the axioms and activate only the chart inferences
% which are required by these rules (eg. the gapping rules and products rules
% are useful only when there is a gap formula or a product formula in the
% axioms.
%
% TODO: it is worth thinking about a smarter version of this type of
% rule restriction by constraining the rules which can apply given a
% certain span of the active formulas.
active_rules(Axioms) :-
all_rules_triggers(Axioms, List, []),
sort(List, Set),
retractall(active_rule(_)),
% "dr" and "dl" are always active
assert(active_rule(dr)),
assert(active_rule(dl)),
assert_active_rules(Set).
all_rules_triggers([]) -->
[].
all_rules_triggers([item(F,_,_,_)|As]) -->
rules_trigger(F),
all_rules_triggers(As).
assert_active_rules([]).
assert_active_rules([R|Rs]) :-
assert(active_rule(R)),
assert_active_rules(Rs).
all_active_rule_statistics :-
clause(sent(_Number, Sem), prob_parse(List, Sem)),
all_active_rule_statistics(List),
fail.
all_active_rule_statistics.
all_active_rule_statistics(List) :-
init_chart,
empty_heap(Heap),
list_to_chart(List, 0, Heap, Axioms, [], 0, _V, _SemInfo, []),
all_active_rule_statistics1(Axioms).
% TODO: complete!
all_active_rule_statistics1([]).
all_active_rule_statistics1([item(F,_,_,_)|As]) :-
rules_trigger(F, L, []),
sort(L, Set),
do_something_with(Set),
all_active_rule_statistics1(As).
% = rules_trigger(+Formula, -ListOfInferences)
%
% given a formula, provide a list (with possible repetitions) of all special chart inference which we (may) need
% to apply (besides dl and dr) in order to complete the chart.
rules_trigger(lit(let)) -->
!,
[let],
[wr].
rules_trigger(dl(0, lit(cl_r), dl(I, lit(s(_)), dr(0, lit(s(_)), lit(np(_,_,_)))))) -->
{I > 0},
[wr],
[wr_a],
[wpop],
[e_endd],
[dit_np],
[se_dit].
rules_trigger(dl(I,_,dl(0,lit(np(_,_,_)),lit(s(_))))) -->
{I > 0},
!,
[wr],
[wr_a],
[wpop],
[a_dit],
[e_endd],
[dit_np],
[se_dit].
rules_trigger(dl(I,_,dl(0,lit(cl_r),dl(0,lit(np(_,_,_)),lit(s(_)))))) -->
{I > 0},
!,
[wr],
[wr_a],
[wpop],
[e_endd],
[a_dit],
[a_dit_se],
[dit_np],
[se_dit].
rules_trigger(dl(I,_,_)) -->
{I > 0},
!,