-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.pl
1396 lines (1215 loc) · 52 KB
/
generator.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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% This is the main file for the chart generator
%%
%% Author: Aurelien Giraud, [email protected]
%% Universität Bremen
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% PREREQUISITES
%%
%% o These can be given in theory.pl:
%% syntactic_object(<type being the mgsat of all syntactic objects>).
%%
%% ind_path([path,to,index,of,the,mrs]).
%%
%% cont_path([path,to,mrs]).
%% liszt_path([path,to,list,of,EPs]).
%% hcons_path([path,to,handle,constraints]).
%%
%% Here is what I use. There are chances that it is similar to that:
%%
%% syntactic_object(syntactic_object).
%%
%% ind_path([synsem,loc,cont,ind]).
%% cont_path([synsem,loc,cont]).
%% liszt_path([synsem,loc,cont,rels]).
%% hcons_path([synsem,loc,cont,h_cons]).
%%
%% LOADING
%%
%% o either launch trale with option:
%% -e "compile('/path/to/generator/directory/generator.pl')"
%% or call within trale:
%% compile('/path/to/generator/directory/generator.pl').
%%
%% USE
%%
%% o To parse a sentence and generate from its semantics:
%% p_and_g([sentence,as,list,of,words]).
%%
%% o To parse a phrase of description Desc and generate from its semantics:
%% p_and_g([sentence,as,list,of,words],Desc).
%%
%% o To generate from a description Desc (representing the whole sign, not only the
%% semantics), getting all the outputs in the list Words, and matching Desc to the
%% description Root:
%% g(Desc).
%% g(Desc,Words).
%% g_d(Desc,Words,Root).
%% g_d(Desc,Root).
%%
%% TEST SUITE HANDLING
%%
%% o The test suite handling for generation parses a sentence and tries then to
%% generate from its semantics.
%%
%% o Example lines to be added to test_items.pl for generation test suite handling:
%% tg(6,[pierre,donne,une,fée,à`,clochette],@root).
%% tg(6,"Pierre donne une fée à clochette.",@root).
%%
%% o To test the sentence numbered No in test_items.pl
%% testg(No).
%% To test all sentences in test_items.pl
%% testg(No).
%% o These can also be used to -write- the test results in file File
%% (if File exists, its content is replaced by the output)
%% testgw(No,File).
%% testgw(all,File).
%% o These can also be used to -append- the test results to file File
%% (if File exists, its content is not erased and the results are added at the
%% end of it)
%% testga(No,File).
%% testga(all,File).
%%
%%
%%
%% BUGS:
%%
%% There is a bug in trale: for some grammars the call to alec_rule/6 fails for some
%% grammars. In that case the generator cannot function.
%% 02/02/06 - Aurelien G.
%%
%% Indices get mixed sometimes in a way that subject and complements of a verb
%% get interchanged leading to more output than it should give.
%% 02/02/06 - Aurelien G.
%%
%% Does not work yet for difference-lists of EPs in liszt_path(...) and hcons_path(...).
%% 02/02/06 - Aurelien G.
:- use_module(library(system)).
:- dynamic agenda/1.
:- dynamic debug/1.
:- dynamic dbi/2.
:- dynamic stopg/0.
:- dynamic slg_chart/1.
:- dynamic slg_chart_copy/1.
:- dynamic allBits/1.
:- dynamic bitPos/2.
:- dynamic c_edge/6.
:- dynamic a_edge/6.
:- dynamic cc_edge/6.
:- dynamic u_no_st_mod/0.
:- dynamic u_st_mod/0.
:- dynamic mod_grRule/3.
:- dynamic non_mod_grRule/3.
:- dynamic u_modRules/1.
:- dynamic u_modCats/1.
:- dynamic gen_words/1.
:- dynamic u_syntactic_object/1.
%================================================================================
%Debugging predicates
%================================================================================
%--------------------------------------------------------------------------------
%To display the chart and the agenda at their current state with only the words
%and the BitString
%--------------------------------------------------------------------------------
dt:-call_residue(displayThings,_).
displayThings :-
write('Chart:'),nl,
dwc,
write('Agenda:'),nl,
dwa.
%--------------------------------------------------------------------------------
%To display the agenda at its current state with only the words
%and the BitString
%--------------------------------------------------------------------------------
dwa:-
( current_predicate(a_edge,a_edge(_,_,_,_,_,_)))
-> (dwa2 -> true ; true )
; true.
dwa2:-
a_edge(_,BS,Ws,_,_,_),
write(BS),
write(': '),
write(Ws),nl,
fail.
%--------------------------------------------------------------------------------
%To display the chart at its current state with only the words
%and the BitString
%--------------------------------------------------------------------------------
dwc:-
( current_predicate(c_edge,c_edge(_,_,_,_,_,_)))
-> (dwc2 -> true ; true )
; true.
dwc2:-
c_edge(_,BS,Ws,_,_,_),
write(BS),write(': '),write(Ws),nl,
fail.
%--------------------------------------------------------------------------------
%Used at the end of initAgenda to display its starting state with only the words
%and the BitString
%--------------------------------------------------------------------------------
displayWords([]):- nl.
displayWords([edge(_,BS,Ws,_,_,_)|L]):-
write(BS),write(': '),write(Ws),nl,
displayWords(L).
%================================================================================
%General Predicates
%================================================================================
%--------------------------------------------------------------------------------
%open(+L:<list>,-O:<openlist>)
%--------------------------------------------------------------------------------
%O is an open list whose non open part is L.
%--------------------------------------------------------------------------------
open([X],[X|_]).
open([X|Y],[X|Z]):-open(Y,Z).
%--------------------------------------------------------------------------------
%open_h(+L:<list>,-O:<openlist>,-H:<list>)
%--------------------------------------------------------------------------------
%like open/2 but with access to H, the difference between L and O
%--------------------------------------------------------------------------------
open_h([],L,L).
open_h([X|Y],[X|Z],L):-open_h(Y,Z,L).
%--------------------------------------------------------------------------------
%To write a %
%--------------------------------------------------------------------------------
writep(S):-
name(C,[37]),
write(C),write(S).
%--------------------------------------------------------------------------------
%To get an independent copy of a FS
%--------------------------------------------------------------------------------
copyFS(FS1,CopyFS1):- copy_term(FS1,CopyFS1).
%================================================================================
%Predicates for handling the list of generated sentences
%================================================================================
%--------------------------------------------------------------------------------
%Initialisation of the list of generated sentences
%--------------------------------------------------------------------------------
initGenWords:-
abolish(gen_words,1),
assert(gen_words([])).
%--------------------------------------------------------------------------------
%To add a sentence to the list of generated sentences
%--------------------------------------------------------------------------------
add_words(L):-
gen_words(L1),
abolish(gen_words,1),
append(L1,[L],L2),
assert(gen_words(L2)).
%================================================================================
%Predicates useful for manipulating bitstring bags of EPs
%================================================================================
%--------------------------------------------------------------------------------
%twoPower(+N:<int>, -V:<int>)
%--------------------------------------------------------------------------------
%2^N
%--------------------------------------------------------------------------------
twoPower(0,1).
twoPower(N,V) :- N>0, NMinusOne is N-1, twoPower(NMinusOne,W), V is W*2.
%--------------------------------------------------------------------------------
%getAllBits(+L:<list>, -BitString<int>)
%--------------------------------------------------------------------------------
%get (as an integer) a bitstring AllBits of 1s from a list L
%Allbits and L having the same "length"
%--------------------------------------------------------------------------------
getAllBits(L,AllBits) :-
getAllBits(L,_,AllBits).
getAllBits([],0,0).
getAllBits([_X|L],N2,V2) :-
getAllBits(L,N1,V1),
N2 is N1+1,
twoPower(N1,W),
V2 is (V1+W).
%--------------------------------------------------------------------------------
%makeAllBits(+L:<list>, -AllBits:<int>, -EPsBits:<list>)
%--------------------------------------------------------------------------------
%Same as getAllBits plus keeps records of the list elements'bit position
%--------------------------------------------------------------------------------
makeAllBits(L,AllBits,EPsBits) :-
makeAllBits(L,_,AllBits,EPsBits).
makeAllBits([],0,0,[]).
makeAllBits([X|L],N2,V2,[bitPos(X,W)|M]) :-
makeAllBits(L,N1,V1,M),
N2 is N1+1,
twoPower(N1,W),
V2 is (V1+W).
%--------------------------------------------------------------------------------
%getBitString(+EPsBits:<list>,+EPs:<list>,-N:<int>)
%--------------------------------------------------------------------------------
%It maps a bag of EPs to a bitstring integer.
%It fails if some EPs are present more than once
%and if an EP was not given in the input bag (bitPos/2 would fail).
%--------------------------------------------------------------------------------
getBitString(_EPsBits,[],0).
getBitString(EPsBits,[EP1|EPs],BS) :-
getBitString(EPsBits,EPs,BS2),
member(bitPos(EP1,BS1),EPsBits),
bs_disjoint(BS1,BS2),
bs_union(BS1,BS2,BS).
%--------------------------------------------------------------------------------
%getMaxBitString(+EPsBits:<list>,-N:<int>)
%--------------------------------------------------------------------------------
%EPsBits is a list of bitpos(EP,BS)
%getMaxBitString/2 gets the bitstring value of a bag which would contain all the
%EPs found in EPsBits
%--------------------------------------------------------------------------------
getMaxBitString(L,EPs,BS) :-
getMaxBitString(L,EPs,0,BS).
getMaxBitString([],[],BS,BS).
getMaxBitString([bitPos(EP1,BS1)|L],[EP1|K],BStemp,BS) :-
bs_disjoint(BS1,BStemp),
bs_union(BS1,BStemp,BStemp2),
getMaxBitString(L,K,BStemp2,BS).
%--------------------------------------------------------------------------------
%retrieveEPsBitString(+EPsBits:<list>,-EPs:<list>,-N:<int>)
%--------------------------------------------------------------------------------
%retrieves from a set EPsBits of bitPos(A,B) structures, via backtracking, all
%the possible bags of EPs (As) together with the corresponding bitString value
%of the bag
%--------------------------------------------------------------------------------
retrieveEPsBitString(EPsBits,EPs,BS) :-
retrieveEPsBitString2(_List1,EPsBits,EPs,0,BS).
retrieveEPsBitString2([],[],[],BS,BS).
retrieveEPsBitString2([bitPos(EP1,BS1)|EPBits1],[bitPos(EP1,BS1)|EPBits],[EP1|L],BStemp,BS) :-
bs_disjoint(BS1,BStemp),
bs_union(BS1,BStemp,BStemp2),
retrieveEPsBitString2(EPBits1,EPBits,L,BStemp2,BS).
retrieveEPsBitString2(L1,[_|EPBits],L,BStemp,BS) :-
retrieveEPsBitString2(L1,EPBits,L,BStemp,BS).
%--------------------------------------------------------------------------------
%sameBags(+N:<int>,+M:<int>)
%--------------------------------------------------------------------------------
%(bitstring) bag equality
%--------------------------------------------------------------------------------
sameBags(N,N).
%--------------------------------------------------------------------------------
%bs_disjoint(+BitS1:<int>,+BitS2:<int>)
%--------------------------------------------------------------------------------
%(bitstring) bag disjunction
%--------------------------------------------------------------------------------
bs_disjoint(BitS1,BitS2) :-
X is BitS1/\BitS2, X=0.
%--------------------------------------------------------------------------------
%bs_union(+BitS1:<int>,+BitS2:<int>,-NewBitS:<int>)
%--------------------------------------------------------------------------------
%(bitstring) bag union
%--------------------------------------------------------------------------------
bs_union(BitS1,BitS2,NewBitS) :-
NewBitS is BitS1\/BitS2.
%================================================================================
%Predicates for manipulating edges, chart and agenda
%================================================================================
%--------------------------------------------------------------------------------
%actEdge(+Edge:<edge>) inactEdge(+Edge:<edge>)
%--------------------------------------------------------------------------------
%Definition/test of active and inactive edges
%--------------------------------------------------------------------------------
actEdge(edge(_Vertex,_Bag_EPs,_Words,_Cat,_Found,[_|_])).
inactEdge(edge(_Vertex,_Bag_EPs,_Words,_Cat,_Found,[])).
%--------------------------------------------------------------------------------
%getEdge(-Edge:<edge>)
%--------------------------------------------------------------------------------
%retrieves an edge from the agenda
%--------------------------------------------------------------------------------
getFromAgenda(edge(A,B,C,D,E,F)) :-
retract(a_edge(A,B,C,D,E,F)).
%--------------------------------------------------------------------------------
%getFromChart(-Edge:<edge>)
%--------------------------------------------------------------------------------
%retrieves an edge from the chart
%--------------------------------------------------------------------------------
getFromChart(edge(A,B,C,D,E,F)) :-
retract(c_edge(A,B,C,D,E,F)).
getFromChartCopy(edge(A,B,C,D,E,F)) :-
retract(cc_edge(A,B,C,D,E,F)).
%--------------------------------------------------------------------------------
%addToChart(+Edge:<edge>)
%--------------------------------------------------------------------------------
%adds the edge Edge to the chart
%--------------------------------------------------------------------------------
addToChart(edge(A,B,C,D,E,F)) :-
asserta(c_edge(A,B,C,D,E,F)).
%--------------------------------------------------------------------------------
%addToChartCopy(+Edge:<edge>)
%--------------------------------------------------------------------------------
%adds the edge Edge to the chart copy
%--------------------------------------------------------------------------------
addToChartCopy(edge(A,B,C,D,E,F)) :-
asserta(cc_edge(A,B,C,D,E,F)).
%--------------------------------------------------------------------------------
%addToAgenda(+Edge:<edge>)
%--------------------------------------------------------------------------------
%adds the edge Edge to the Agenda
%--------------------------------------------------------------------------------
addToAgenda(edge(A,B,C,D,E,F)) :-
(
asserta(a_edge(A,B,C,D,E,F)),
( F=[]
-> (dotMovementCheck(edge(A,B,C,D,E,F));true)
; true) )
.
%--------------------------------------------------------------------------------
%initChart
%--------------------------------------------------------------------------------
%Chart Initialisation
%--------------------------------------------------------------------------------
initChart :-
clearChart.
%--------------------------------------------------------------------------------
%Chart deletion
%--------------------------------------------------------------------------------
clearChart :- abolish(c_edge,6).
%--------------------------------------------------------------------------------
%Agenda deletion
%--------------------------------------------------------------------------------
clearAgenda :- abolish(a_edge,6).
%--------------------------------------------------------------------------------
%initAgenda(+EPsBits:<list>)
%--------------------------------------------------------------------------------
%Agenda Initialisation
%For empty rels, specialEdgesToAgenda is used
%--------------------------------------------------------------------------------
initAgenda(EPsBits) :-
clearAgenda,
( (
initAgenda2(EPsBits))
-> true
; true).
initAgenda2(EPsBits):-
isAWordSRels(Word,Ref-SVs,W_Bag1,Vert),
retrieveEPsBitString(EPsBits,W_Bag2,W_BitStr),
udListReOrder(W_Bag1,W_Bag2),
fully_deref(Ref,SVs,RefOut,SVsOut),
asserta(a_edge(Vert,W_BitStr,[Word],RefOut-SVsOut,[],[])),
fail.
%--------------------------------------------------------------------------------
%ignore special edges (i_se) or use them (u_se)
%--------------------------------------------------------------------------------
i_se:-abolish(seta,1),assert(seta(no)).
u_se:-abolish(seta,1),assert(seta(yes)).
seta(yes).
%--------------------------------------------------------------------------------
%specialEdgesToAgenda
%--------------------------------------------------------------------------------
%Special Edges Agenda Initialisation
%-For empty rels-
%--------------------------------------------------------------------------------
specialEdgesToAgenda :-
write('Adding special edges to the agenda...'),nl,
( (
specialEdgesToAgenda2)
-> true
; true).
specialEdgesToAgenda2:-
isAWordSRels(Word,Ref-SVs,[],Vert),
fully_deref(Ref,SVs,RefOut,SVsOut),
asserta(a_edge(Vert,0,[Word],RefOut-SVsOut,[],[])),
fail.
%--------------------------------------------------------------------------------
%udList(+A:<list>,+B:<list>)
%--------------------------------------------------------------------------------
%unifies the element of two lists from left to right
%--------------------------------------------------------------------------------
udList([A],[B]) :- ud(A,B).
udList([A|L],[B|M]) :-
ud(A,B),
udList(L,M).
%--------------------------------------------------------------------------------
%udListReOrder(+A:<list>,+B:<list>,-C:<list>)
%--------------------------------------------------------------------------------
%it does kind of the same job as would be done by calling sameOrderRelsList and
%udList one after the other. Namely it unifies two lists, element by element,
%but in any possible order.
%--------------------------------------------------------------------------------
udListReOrder([FS1],[FS2]):- ud(FS1,FS2).
udListReOrder([FS1|L],L2) :-
select(FS2,L2,Rest2),
ud(FS1,FS2),
udListReOrder(L,Rest2).
%--------------------------------------------------------------------------------
%isAWordSRels(-Word:<list>,-Cat:<FS>,-W_Bag:<FS>,-Vert:<FS>) ?
%isAWordSRels(-Word:<list>,-Cat:<svs>,-W_Bag:<svs>,-Vert:<svs>) ?
%--------------------------------------------------------------------------------
%Like isAWord/4 but fails if the Word has not a specified 'rels' value
%--------------------------------------------------------------------------------
isAWordSRels(Word,Tag-SVs,Word_Bag,TagOut2-Vert) :-
lex(Word,Tag-SVs),
check_syntactic_object(Tag-SVs),
liszt_path(L),
( (
pathval(L,Tag,SVs,_TagOut,list))
-> fail
; (
pathval(L,Tag,SVs,TagOut1,SVsOut1),
getListFromFS(TagOut1,SVsOut1,Word_Bag),
ind_path(M),
pathval(M,Tag,SVs,TagOut2,Vert))).
%--------------------------------------------------------------------------------
%check_syntactic_object(+Cat:<FS>)
%--------------------------------------------------------------------------------
%checks if the word found is not a stem...
%--------------------------------------------------------------------------------
check_syntactic_object(_Tag-SVs):-
functor(SVs,Type,_),
u_syntactic_object(SO),
unify_type(Type,SO,_Res).
%--------------------------------------------------------------------------------
%syntactic_object(+Cat:<FS>)
%--------------------------------------------------------------------------------
%This is made modifyable by the user, if the user does not use this type...
%--------------------------------------------------------------------------------
u_syntactic_object(syntactic_object).
syntactic_object_act:-
current_predicate(syntactic_object,syntactic_object(_))
-> syntactic_object(L),
abolish(u_syntactic_object,1),
assert(u_syntactic_object(L))
; true.
%--------------------------------------------------------------------------------
%getEPsFromRef(+Tag:<ref>,+SVs:<svs>,-Bag:<list>)
%--------------------------------------------------------------------------------
%Bag is the list of EPs corresponding to the ale-list of EPs found in the
%semantics of the sign Tag-SVs
%--------------------------------------------------------------------------------
getEPsFromRef(Tag,SVs,Bag) :-
getRelsFromRef(Tag,SVs,Ref2,SVs2),
getListFromFS(Ref2,SVs2,Bag).
%--------------------------------------------------------------------------------
%getRelsFromRef(+Tag:<ref>,-SVs:<svs>,-Ref2:<ref>,-SVs2:<svs>)
%--------------------------------------------------------------------------------
%Ref2-SVs2 is the liszt part of the sign Tag-SVs
%--------------------------------------------------------------------------------
getRelsFromRef(Tag,SVs,Ref2,SVs2) :-
liszt_path(L),
pathval(L,TagOut,SVsOut,Ref2,SVs2),
deref(Tag,SVs,TagOut,SVsOut).
%--------------------------------------------------------------------------------
%getListFromRef(+Tag:<ref>,+SVs:<svs>,-List:<list>)
%--------------------------------------------------------------------------------
%List is the prolog list corresponding to the ale-list Tag-SVs
%--------------------------------------------------------------------------------
getListFromFS(_A-e_list,list,[]).
getListFromFS(_A,e_list,[]).
getListFromFS(Tag,SVs,[X-SVs1|L]) :-
pathval([hd],TagOut,SVsOut,X,SVs1),
pathval([tl],TagOut,SVsOut,Tag2,SVs2),
deref(Tag,SVs,TagOut,SVsOut),
getListFromFS(Tag2,SVs2,L).
%--------------------------------------------------------------------------------
%to rebuild the chart from the chart copy
%--------------------------------------------------------------------------------
rebuild_chart:-
( rebuild_chart2
-> true
; true).
rebuild_chart2:-
getFromChartCopy(edge(A,B,C,D,E,F)),
addToChart(edge(A,B,C,D,E,F)),
fail.
%================================================================================
%Predicates for grammar rules access and special treatment of modification
%================================================================================
%--------------------------------------------------------------------------------
%grRule(-RuleName:<atom>,-FS:<fs>,-DtrsList:<list>) :-
%--------------------------------------------------------------------------------
%To retrieve grammar rules from the grammar
%--------------------------------------------------------------------------------
grRule(Name,TagMoth-bot,DtrsList) :-
clause(alec_rule(Name,DtrsDesc,_,Moth,_,_),true),satisfy_dtrs(DtrsDesc,_DtrCats,[],DtrsList,gdone),
add_to(Moth,TagMoth,bot).
%-------------------------------------------------------------------------------
%u_modRules(+L:<list>)
%-------------------------------------------------------------------------------
%predicate which may be re-defined by the user via mod_rules/1.
%It says to the generator that no grammar rule perform modification
%--------------------------------------------------------------------------------
u_modRules([]).
%-------------------------------------------------------------------------------
%mod_rules_act(+L:<list>)
%-------------------------------------------------------------------------------
%allows the user to specify the rules performing modification.
%(L is a list of grammar rule names)
%It changes the asserted value of u_modRules/1
%-------------------------------------------------------------------------------
mod_rules_act:-
current_predicate(mod_rules,mod_rules(_))
-> mod_rules(L),
abolish(u_modRules,1),
assert(u_modRules(L))
; true.
%--------------------------------------------------------------------------------
%isARule(-Moth:<fs>,-DtrsList:<fs>,-RuleName:<atom>)
%--------------------------------------------------------------------------------
%It succeeds if the grammar contains a rule building the FS Moth from the list
%of daughteres DtrsList.
%It only succeeds for non-modification rules when modifiers edges have not
%come back to the agenda. And it only succeeds for modification rules when
%modifiers edges have come back to the agenda.
%--------------------------------------------------------------------------------
isARule(Moth,DtrsList,Name) :-
( u_no_st_mod
-> ( non_mod_grRule(Name,Moth,DtrsList) ; (current_predicate(mod_grRule,mod_grRule(_,_,_)),mod_grRule(Name,Moth,DtrsList)) )
;
(
( toggleModification(yes)
-> ( (current_predicate(mod_grRule,mod_grRule(_,_,_)),mod_grRule(Name,Moth,DtrsList)) ; non_mod_grRule(Name,Moth,DtrsList) )
; non_mod_grRule(Name,Moth,DtrsList)
)
)
).
%--------------------------------------------------------------------------------
%modRule(-RuleName:<atom>)
%--------------------------------------------------------------------------------
%Used to check if a rule is said as performing modification
%--------------------------------------------------------------------------------
modRule(RuleName):-
u_modRules(L),
member(RuleName,L).
%--------------------------------------------------------------------------------
%stores the modification and non-modification rules separately as
%[non_]mod_grRule/3 clauses.
%--------------------------------------------------------------------------------
sortRules :-
abolish(mod_grRule,3),
abolish(non_mod_grRule,3),
sortRules1.
sortRules1:-
( sortRules2 -> true ; true ).
sortRules2 :-
grRule(Name,Moth,DtrsList),
( (
modRule(Name))
->
(
assert(mod_grRule(Name,Moth,DtrsList)) )
;
(
assert(non_mod_grRule(Name,Moth,DtrsList)) )
),
fail.
%--------------------------------------------------------------------------------
%initialises the modification mechanism:
%stores the modification and non-modification rules separately
%determines the modifiers categories
%--------------------------------------------------------------------------------
initMod :-
abolish(toggleModification,1),
assert(toggleModification(no)),
mod_cats_act,
mod_rules_act,
sortRules.
%--------------------------------------------------------------------------------
%specifies the modifiers descriptions
%it would enable to only bring back to the agenda the edges that correspond to a
%potential modifier
%--------------------------------------------------------------------------------
modCat(Tag-SVs):-
u_modCats(L),
member(Type,L),
pos_path(M),
pathval(M,TagOut,SVsOut,_Ref2,SVs2),
deref(Tag,SVs,TagOut,SVsOut),
functor(SVs2,Type2,_),
unify_type(Type2,Type,_Res).
mod_cats_act:-
current_predicate(mod_cats,mod_cats(_))
-> mod_cats(L),
abolish(u_modCats,1),
assert(u_modCats(L))
; true.
u_modCats([]).
%--------------------------------------------------------------------------------
%To switch on or off the special treatment of modification
%--------------------------------------------------------------------------------
u_st_mod(no).
st_mod_act:-
current_predicate(st_mod,st_mod(_))
-> st_mod(K),
abolish(u_st_mod,1),
assert(u_st_mod(K))
; true.
st_mod:-
abolish(u_st_mod,1),
assert(u_st_mod(yes)).
no_st_mod:-
abolish(u_st_mod,1),
assert(u_st_mod(no)).
%--------------------------------------------------------------------------------
%When all edges have been processed excluding modification, then
%toggleModification/0 puts modifiers edges back into the agenda and toggles
%modification rules.
%--------------------------------------------------------------------------------
toggleModification :-
( u_st_mod(yes)
-> ( u_modCats([_|_])
-> (write('Bringing back modification edges to the agenda'),nl,
toggleModification1,
rebuild_chart))),
abolish(toggleModification,1),
assert(toggleModification(yes)).
toggleModification1 :-
( toggleModification2
-> true
; true).
toggleModification2 :-
getFromChart(edge(Vert,W_BitStr,Word,Cat,Found,[])),
( modCat(Cat)
-> addToAgenda(edge(Vert,W_BitStr,Word,Cat,Found,[]))
; addToChartCopy(edge(Vert,W_BitStr,Word,Cat,Found,[])) ),
fail.
%================================================================================
%Generation predicates
%================================================================================
%--------------------------------------------------------------------------------
%start Symbol
%--------------------------------------------------------------------------------
startSymbol(Tag-SVs):-
u_gen_root(Root),
add_to(Root,TagRoot,bot),
ud(TagRoot-bot,Tag-SVs).
%--------------------------------------------------------------------------------
%to enable the user to specify the categories of the generated FS
%--------------------------------------------------------------------------------
u_gen_root(R):-root_symbol(R).
gen_root_act:-
current_predicate(gen_root,gen_root(_))
-> gen_root(Desc), %gen_root might be put in theory.pl by the user to change from default
abolish(u_gen_root,1),
assert(u_gen_root(Desc))
; true.
%--------------------------------------------------------------------------------
%g(+Cat:<desc>,-Words:<list>,+Root:<desc>,+Query:<atom>)
%--------------------------------------------------------------------------------
%Words is a list representing the sentence generated from Cat, corresponding to
%the description Root, with Query enabling to switch query_proceed on and off
%--------------------------------------------------------------------------------
g(Cat,Words,Root,Query):-
call_residue((add_to(Cat,Tag,bot),
frozen_term(Tag,Frozen),
((current_predicate(portray_cat,portray_cat(_,_,_,_,_)),
portray_cat(_,Cat,Tag,bot,Frozen)) -> true
; nl, write('INITIAL CATEGORY: '), nl, ttyflush,
pp_fs_res(Tag,bot,Frozen), nl
),
slgGen(Tag,SVs,Words,Root,Query)),Residue),
((current_predicate(portray_cat,portray_cat(_,_,_,_,_)),
portray_cat(Words,Cat,Tag,SVs,Residue)) -> true
; nl, write('STRING: '),
nl, write_list(Words),
\+ \+ (nl, write('FINAL CATEGORY: '),nl, ttyflush,
pp_fs_res(Tag,SVs,Residue)), nl
).
%--------------------------------------------------------------------------------
%Other versions of g(Cat,Words,Root,Query)
%--------------------------------------------------------------------------------
g(Cat):-
u_gen_root(Root),
g(Cat,_Words,Root,query).
g_d(Cat,Root):-
g(Cat,_Words,Root,query).
g_d(Cat,Words,Root):-
g(Cat,Words,Root,query).
g(Cat,Words):-
u_gen_root(Root),
g(Cat,Words,Root,query).
%--------------------------------------------------------------------------------
%use_root(+R:<desc>)
%--------------------------------------------------------------------------------
%to select to what kind of description the generation output has to correspond to
%--------------------------------------------------------------------------------
use_root(R):-
abolish(u_gen_root,1),
assert(u_gen_root(R)).
%--------------------------------------------------------------------------------
%to switch on and off the query_proceed during generation
%(useful for test suites)
%--------------------------------------------------------------------------------
output_query(yes).
no_query_act:-
abolish(output_query,1),
assert(output_query(no)).
query_act:-
abolish(output_query,1),
assert(output_query(yes)).
%--------------------------------------------------------------------------------
%slgGen(+Tag:<ref>,+SVs:<svs>,-Words:<list>,+Root:<desc>)
%--------------------------------------------------------------------------------
%Words is a phrase generated from Tag-SVs, and corresponding to description Root
%Query enables to switch proceed_query on and off
%--------------------------------------------------------------------------------
slgGen(Tag,SVs,Words,Root,Query) :-
( (var(Query) ; Query=query) -> query_act ; no_query_act),
u_gen_root(R),
(var(Root) -> true ; use_root(Root)),
slgGen1(Tag,SVs,Words),
use_root(R).
%--------------------------------------------------------------------------------
%Other versions of slgGen(Tag,SVs,Words,Root,Query)
%--------------------------------------------------------------------------------
slgGen(Tag,SVs,Words,Root):-
slgGen(Tag,SVs,Words,Root,query).
slgGen(Tag,SVs,Words) :-
query_act,
gen_root_act,
slgGen1(Tag,SVs,Words).
%--------------------------------------------------------------------------------
%slgGen1(+Tag:<ref>,+SVs:<svs>,-Words:<list>)
%--------------------------------------------------------------------------------
%Words is a sentence or phrase generated from Tag-SVs
%This predicate is called by slgGen predicates (they do some initialisation before)
%--------------------------------------------------------------------------------
slgGen1(Tag,SVs,Words) :-
(current_predicate(stopg,stopg) -> abolish(stopg) ; true),
initGenWords,
syntactic_object_act,
st_mod_act,
write('Initialising the bitstrings...'),nl,
getEPsFromRef(Tag,SVs,Bag),
makeAllBits(Bag,AllBits,EPsBits),
write('Initialising the modification treatment...'),nl,
initMod,
write('Initialising the chart...'),nl,
initChart,
write('Building the agenda...'),nl,
initAgenda(EPsBits),
(seta(yes)->specialEdgesToAgenda;true),
write('Generating new edges...'),nl,
( slgGenProc(Words,AllBits,EPsBits,Tag,SVs) ; gen_words(Words)).
%--------------------------------------------------------------------------------
%slgGenProc(-S:<list>,+AllBits:<int>,+EPsBits:<list>,+Ref:<ref>,+SVs:<svs>)
%--------------------------------------------------------------------------------
%Processes one by one the edges in the agenda
%--------------------------------------------------------------------------------
slgGenProc(S,AllBits,EPsBits,Ref,SVs) :-
(current_predicate(stopg,stopg) -> fail ; true),
( (getFromAgenda(Edge),
Edge=edge(_,_W_B,_Words,_,_,_)
)
->
( addToChart(Edge),
(
slgGenEdgeProc(Edge,AllBits,S,Ref,SVs)
;
slgGenProc(S,AllBits,EPsBits,Ref,SVs)
)
)
;
( toggleModification(no)
->
( toggleModification,
slgGenProc(S,AllBits,EPsBits,Ref,SVs)
)
)
).
%--------------------------------------------------------------------------------
%slgGenEdgeProc(Edge:<edge>,-S:<list>,+AllBits:<int>,+Words:<list>,+Ref:<ref>,+SVs:<svs>)
%--------------------------------------------------------------------------------
%Generation succeeds if:
%- the edge spans the entire Bag
%- its Cat is a start symbol
%--------------------------------------------------------------------------------
slgGenEdgeProc(edge(_,W_Bag,Words,Tag-SVs,_,[]),AllBits,Words,_Ref,_SVs2) :-
sameBags(W_Bag,AllBits),
startSymbol(Tag-SVs),
add_words(Words),
frozen_term([Tag|SVs],Frozen),
((current_predicate(portray_cat,portray_cat(_,_,_,_,_)),
portray_cat(Words,bot,Tag,SVs,Frozen)) -> true
; nl, ttyflush,
pp_fs_res(Tag,SVs,Frozen), nl
),
write(Words),nl,
( output_query(yes)
-> query_proceed, assert(stopg)),
fail.
%--------------------------------------------------------------------------------
%Invocate Grammar Rules on the inactive edge
%--------------------------------------------------------------------------------
slgGenEdgeProc(edge(V,W_B,Words,Cat,Found,[]),_AllBits,_S,_,_) :-
ruleInvocation(edge(V,W_B,Words,Cat,Found,[])).
%--------------------------------------------------------------------------------
%Perform Dot Movement on the active edge
%--------------------------------------------------------------------------------
slgGenEdgeProc(edge(V,W_B,Words,Cat,Found,[X|Cats]),_AllBits,_S,_,_) :-
dotMovement(edge(V,W_B,Words,Cat,Found,[X|Cats])).
%--------------------------------------------------------------------------------
%removeGoal(-L1:<list>,-L2:<list>).
%--------------------------------------------------------------------------------
%L1 is a list of elements like: cat>_, cats>_, sem_head>_, goal>_
%L2 is the same list, with the goals removed
%--------------------------------------------------------------------------------
removeGoal([],[]).
removeGoal([cat>Cat|Rest],[cat>Cat|Rest2]):-
removeGoal(Rest,Rest2).
removeGoal([sem_head>Cat|Rest],[sem_head>Cat|Rest2]):-
removeGoal(Rest,Rest2).
removeGoal([cats>Cat|Rest],[cats>Cat|Rest2]):-
removeGoal(Rest,Rest2).
removeGoal([goal>_Cat|Rest],Rest2):-
removeGoal(Rest,Rest2).
%--------------------------------------------------------------------------------
%getVertex(+FS1:<fs>,-FS2:<fs>)
%--------------------------------------------------------------------------------
%FS2 it the vertex of FS1
%--------------------------------------------------------------------------------
getVertex(Tag-SVs,Tag2-SVs2):-
ind_path(L),
pathval(L,TagOut,SVsOut,Tag2,SVs2),
deref(Tag,SVs,TagOut,SVsOut).
%--------------------------------------------------------------------------------
%instanciate_dtrs(+N:<int>,+MRef:<ref>,+MSVs:<svs>,+Cat:<fs>)
%--------------------------------------------------------------------------------
%it unifies the Nth daughter of MRef-MSVs with Cat
%--------------------------------------------------------------------------------
instanciate_dtrs(N,MRef,MSVs,Cat) :-
dtrs_feat_(Feat),
pathval([Feat],TagOut,SVsOut,DRef,DSVs),
deref(MRef,MSVs,TagOut,SVsOut),
P is N-1,
length(L,P),
open_h(L,K,[Cat|_]),