% Prolog half of the Interlisp-D/Prolog parser demo % Herb Jellinek, 13-Feb-86. This is a fixed-up version of Vince Pecora's % program of the same name. % ---------------------------------------------------------------------- % Lisp functions :- lisp_predicate('CREATE.WINDOWS',create_windows). :- lisp_predicate('SET.STEP.TYPE',set_step_type(+)). :- lisp_predicate('GRAPH.STRUCTURES',graph_structures(+,+)). :- lisp_predicate('WAIT.FOR.STEP',wait_for_step). :- lisp_predicate('DISPLAY.SENTENCE',display_sentence(+)). :- lisp_predicate('GET.SENTENCE', get_sentence_list([-])). % ---------------------------------------------------------------------- % To run the example, use run or run_all_eg run :- create_windows, set_step_type([]), get_sentence_list(Sen), display_sentence(Sen), sentence(sentence(_X,_Y),Sen,['.']). run_all_eg :- create_windows, set_step_type('one.second'), eg(N,Sen), do_parse(Sen), fail. run_all_eg :- halt. do_parse(Sen) :- display_sentence(Sen), sentence(sentence(_X,_Y),Sen,['.']),!. run_eg(N) :- create_windows, set_step_type('one.second'), eg(N,Sen), display_sentence(Sen), sentence(sentence(_X,_Y),Sen,['.']). run_eg_step(N) :- create_windows, set_step_type('mouse.button'), eg(N,Sen), display_sentence(Sen), sentence(sentence(_X,_Y),Sen,['.']). run_step :- create_windows, set_step_type('mouse.button'), read_in(Sen), display_sentence(Sen), sentence(sentence(_X,_Y),Sen,['.']). % ------------------------------------------------------------------------ sentence(sentence(A,B),C,D) :- S = 'Sentence'('Noun-phrase','Verb-phrase'), show_cumulative_structure(S,[],Cs), noun_phrase(A,C,E,Cs,CsOut), S1 = 'Sentence'(A,'Verb-phrase'), show_cumulative_structure(CsOut,S1,Cs1), verb_phrase(B,E,D,Cs1,Cs2), S2 = sentence(A,B), show_cumulative_structure(Cs2,S2,_). noun_phrase('noun-phrase'(A,B),C,D,CsIn,CsOut) :- S = 'Noun-phrase'('Det','Noun'), show_cumulative_structure(CsIn,S,Cs1), determiner(A,C,E), S1 = 'Noun-phrase'(A,'Noun'), show_cumulative_structure(Cs1,S1,Cs2), noun(B,E,D), S2 = 'noun-phrase'(A,B), show_cumulative_structure(Cs2,S2,CsOut). noun_phrase('noun-phrase'(A),B,C,CsIn,CsOut) :- S = 'Noun-phrase'('Noun'), show_cumulative_structure(CsIn,S,Cs1), noun(A,B,C), S1 = 'noun-phrase'(A), show_cumulative_structure(Cs1,S1,CsOut). noun_phrase('noun-phrase'(A,B),C,D,CsIn,CsOut) :- S = 'Noun-phrase'('Adj','Noun'), show_cumulative_structure(CsIn,S,Cs1), adjective(A,C,E), S1 = 'Noun-phrase'(A,'Noun'), show_cumulative_structure(Cs1,S1,Cs2), noun(B,E,D), S2 = 'noun-phrase'(A,B), show_cumulative_structure(Cs2,S2,CsOut). noun_phrase('noun-phrase'(A,B),C,D,CsIn,CsOut) :- S = 'Noun-phrase'('Pronoun','Noun'), show_cumulative_structure(CsIn,S,Cs1), pronoun(A,C,E), S1 = 'Noun-phrase'(A,'Noun'), show_cumulative_structure(Cs1,S1,Cs2), noun(B,E,D), S2 = 'noun-phrase'(A,B), show_cumulative_structure(Cs2,S2,CsOut). noun_phrase('noun-phrase'(A,B,C),D,H,CsIn,CsOut) :- S = 'Noun-phrase'('Det','Adj','Noun'), show_cumulative_structure(CsIn,S,Cs1), determiner(A,D,F), S1 = 'Noun-phrase'(A,'Adj','Noun'), show_cumulative_structure(Cs1,S1,Cs2), adjective(B,F,G), S2 = 'Noun-phrase'(A,B,'Noun'), show_cumulative_structure(Cs2,S2,Cs3), noun(C,G,H), S3 = 'noun-phrase'(A,B,C), show_cumulative_structure(Cs3,S3,CsOut). noun_phrase('noun-phrase'(A),B,C,CsIn,CsOut) :- S = 'Noun-phrase'('Pronoun'), show_cumulative_structure(CsIn,S,Cs1), pronoun(A,B,C), S1 = 'noun-phrase'(A), show_cumulative_structure(Cs1,S1,CsOut). verb_phrase('verb-phrase'(A),B,C,CsIn,CsOut) :- S = 'Verb-phrase'('Verb'), show_cumulative_structure(CsIn,S,Cs1), verb(A,B,C), S1 = 'verb-phrase'(A), show_cumulative_structure(Cs1,S1,CsOut). verb_phrase('verb-phrase'(A,B),C,D,CsIn,CsOut) :- S = 'Verb-phrase'('Verb','Noun-phrase'), show_cumulative_structure(CsIn,S,Cs1), verb(A,C,E), S1 = 'Verb-phrase'(A,'Noun-phrase'), show_cumulative_structure(Cs1,S1,Cs2), noun_phrase(B,E,D,Cs2,Cs3), S2 = 'verb-phrase'(A,B), show_cumulative_structure(Cs3,S2,CsOut). determiner(det(Word),A,B) :- 'C'(A,Word,B), determiner(Word). noun(noun(Word),A,B) :- 'C'(A,Word,B), noun(Word). verb(verb(Word),A,B) :- 'C'(A,Word,B), verb(Word). adjective(adj(Word),A,B) :- 'C'(A,Word,B), adjective(Word). pronoun(pronoun(Word),A,B) :- 'C'(A,Word,B), pronoun(Word). determiner(the). determiner(a). noun(manufacturer). noun(workstations). noun(xerox). noun(prolog). noun(factory). noun(customer). noun(client). noun(steel). noun(man). noun(apple). noun(dog). verb(produce). verb(produces). verb(purchase). verb(purchases). verb(process). verb(buy). verb(buys). verb(require). verb(requires). verb(eats). verb(run). verb(walks). adjective(sheet). adjective(hardened). adjective(rolled). adjective(intelligent). adjective(quintus). adjective(xerox). adjective(fat). adjective(small). adjective(large). pronoun(he). pronoun(she). pronoun(they). pronoun(their). % 'C'([C|R],C,R). This is the internal definition of 'C'/3 % ---------------------------------------------------------------------- eg(1,[the,intelligent,customer,requires,intelligent,workstations,'.']). eg(2,[xerox,produces,intelligent,workstations,'.']). eg(3,[xerox,workstations,run,quintus,prolog,'.']). % eg(4,[the,manufacturer,purchases,rolled,steel,'.']). % eg(5,[the,customer,requires,hardened,steel,'.']). % eg(6,[the,factory,produces,sheet,steel,'.']). % eg(7,[the,customer,buys,sheet,steel,'.']). % eg(8,[they,process,the,sheet,steel,'.']). % eg(9,[their,factory,produces,hardened,steel,'.']). % ---------------------------------------------------------------------- show_cumulative_structure(Old,[],Cumulative) :- convert_structure(Old,Old1), expand_list(Old1,[],Cumulative), graph_structures(Old1,Cumulative), wait_for_step, !. show_cumulative_structure(Old,New,Cumulative) :- convert_structure(New,New1), expand_list(Old,New1,Cumulative), graph_structures(New1,Cumulative), wait_for_step, !. convert_structure(X,[X]) :- var(X). convert_structure(S,[H,H1]) :- S =.. [H|[H1|[]]], atom(H), not_structure(H1). convert_structure(S,[H|L1]) :- S =.. [H|L], convert_structure1(L,L1). not_structure(S) :- var(S). not_structure(S) :- atomic(S). convert_structure1([],[]). convert_structure1([H|L],[H1|L1]) :- convert_structure(H,H1), convert_structure1(L,L1). % ---------------------------------------------------------------------- % expand_list(Current,Addition,Final). % expand_list([sen,[noun_phrase],[verb_phrase]], % [noun_phrase,[determiner],[noun]], % [sen,[noun_phrase,[determiner],[noun]],[verb_phrase]]). % expand_list([sen,[np],[vp]],[np,[det],[noun]],X). expand_list([],_,[]). expand_list([[X|L]|L1],L2,L5) :- expand_list([X|L],L2,L3), expand_list(L1,L2,L4), append1([L3],L4,L5). expand_list([X|_],[X1|L1],[X1|L1]) :- matches(X,X1), !. expand_list([X|L],In,Out) :- expand(X,In,Out1), expand_list(L,In,Out2), append1([Out1],Out2,Out),!. expand([],X,[]). expand([X|[]],[X1|L1],[X1|L1]) :- matches(X,X1),!. expand(X,_,X) :- !. matches('Sentence',sentence). matches('Sentence','Sentence'). matches('Noun-phrase','Noun-phrase'). matches('Noun-phrase','noun-phrase'). matches('Verb-phrase','Verb-phrase'). matches('Verb-phrase','verb-phrase'). matches('Determiner',det). matches('Noun',noun). matches('Verb',verb). append1([],L,L). append1([X|L1],L2,[X|L3]) :- append1(L1,L2,L3). % ---------------------------------------------------------------------- /* | ?- run. [Sentence,[Noun-phrase],[Verb-phrase]] **[Sentence,[Noun-phrase],[Verb-phrase]] [Noun-phrase,[Determiner],[Noun]] **[Sentence,[Noun-phrase,[Determiner],[Noun]],[Verb-phrase]] [Noun-phrase,[det,the],[Noun]] **[Sentence,[Noun-phrase,[det,the],[Noun]],[Verb-phrase]] [noun-phrase,[det,the],[noun,man]] **[Sentence,[noun-phrase,[det,the],[noun,man]],[Verb-phrase]] [Sentence,[noun-phrase,[det,the],[noun,man]],[Verb-phrase]] **[Sentence,[noun-phrase,[det,the],[noun,man]],[Verb-phrase]] [Verb-phrase,Verb] **[Sentence,[noun-phrase,[det,the],[noun,man]],[Verb-phrase,Verb]] [Verb-phrase,[Verb],[Noun-phrase]] **[Sentence,[noun-phrase,[det,the],[noun,man]],[Verb-phrase,[Verb],[Noun-phrase ]]] [Verb-phrase,[verb,eats],[Noun-phrase]] **[Sentence,[noun-phrase,[det,the],[noun,man]],[Verb-phrase,[verb,eats],[Noun-p hrase]]] [Noun-phrase,[Determiner],[Noun]] **[Sentence,[noun-phrase,[det,the],[noun,man]],[Verb-phrase,[verb,eats],[Noun-p hrase,[Determiner],[Noun]]]] [Noun-phrase,[det,the],[Noun]] **[Sentence,[noun-phrase,[det,the],[noun,man]],[Verb-phrase,[verb,eats],[Noun-p hrase,[det,the],[Noun]]]] [noun-phrase,[det,the],[noun,apple]] **[Sentence,[noun-phrase,[det,the],[noun,man]],[Verb-phrase,[verb,eats],[noun-p hrase,[det,the],[noun,apple]]]] [verb-phrase,[verb,eats],[noun-phrase,[det,the],[noun,apple]]] **[Sentence,[noun-phrase,[det,the],[noun,man]],[verb-phrase,[verb,eats],[noun-p hrase,[det,the],[noun,apple]]]] [sentence,[noun-phrase,[det,the],[noun,man]],[verb-phrase,[verb,eats],[noun-phr ase,[det,the],[noun,apple]]]] **[sentence,[noun-phrase,[det,the],[noun,man]],[verb-phrase,[verb,eats],[noun-p hrase,[det,the],[noun,apple]]]] */ % ----------------------------------------------------------------------