Reputation: 1
i have a college assignment to make a prolog program parse natural language, ie it should output
P = np(det(the), np2(noun(cat))), vp(verb(sat), pp(prep(on), np(det(the), np2(noun(mat)))))
L = []
when i input np([the, cat, sat, on, the, mat],P,L).
instead, the verb and noun phrases are being split and coming out seperatly, like:
P = np(det(the), np2(noun(cat))),
L = [sat, on, the, mat]
i was wondering if anyone could point out where i am going wrong, it would be most appreciated, my code is:
sentence(Sentence,sentence(np(Noun_Phrase)), (vp(Verb_Phrase))):-
/* so take a sentence (first arg) and parse it into a noun phrase and a verb phase */
np(Sentence,Noun_Phrase, Rem),
vp(Rem, Verb_Phrase).
np([X|T],np(det(X),NP2),Rem):-
det(X),
np2(T,NP2,Rem).
np(Sentence,Parse,Rem):- np2(Sentence,Parse,Rem).
np(Sentence,np(NP,PP),Rem):-
np(Sentence,NP,Rem1),
pp(Rem1,PP,Rem).
np2([H|T],np2(noun(H)),T):- noun(H).
np2([H|T],np2(adj(H),Rest),Rem):- adj(H),np2(T,Rest,Rem).
pp([H|T],pp(prep(H),Parse),Rem):-
prep(H),
np(T,Parse,Rem).
vp([H|[]],vp(verb(H))):- verb(H).
vp([H|Rest],vp(verb(H),RestParsed),Rem):- verb(H), pp(Rest, RestParsed, Rem).
vp([H|Rest],vp(verb(H),RestParsed),Rem):- verb(H), np(Rest, RestParsed, Rem).
det(the).
det(with).
noun(cat).
noun(mat).
noun(mouse).
noun(rabbit).
noun(moon).
verb(sat).
verb(ate).
verb(ran_away).
prep(on).
adj(big).
adj(fat).
adj(comfy).
adj(yellow).
adj(silvery).
adj(orange).
Upvotes: 0
Views: 2057
Reputation: 9644
After carefully looking through your code, I've noticed at least two errors.
First and most importantly, you need to be more careful how you define your 'vp' predicate, I noticed you've defined two different versions which don't do the same things. You can tell they're different because one is only arity 2 while the other is arity 3.
I suggest defining all the important and functional 'vp' predicate definitions as arity 3, preserving the "Tail" parameter, then using a single arity 2 predicate that looks like this:
vp(T, Parse) :- vp(T, Parse, []).
Second, I think you need to check how you've nested your brackets in your definition of the 'sentence' predicate. I think you've mixed up a couple of them.
Upvotes: 1
Reputation: 9091
Well, are you really sure you want to get NP for "the cat sat on the mat". This isn't an NP, it's a full sentence, isn't?
Secondly I haven't written in Prolog for quite a some time. I think the code should be something along these lines:
sentence(S, sentence(np(NP), vp(VP))) :-
np(S, NP, R),
vp(R, VP, []). /* Changed here - added the third argument */
np([X | S], np(det(X), NP2), R) :-
det(X),
np2(S, NP2, R).
np(S, NP, R) :-
np2(S, NP, R).
np(S, np(NP, PP), R) :-
append(X, Y, S), /* Changed here - otherwise possible endless recursion */
pp(Y, PP, R),
np(X, NP, []).
np2([X | R], np2(noun(X)), R) :-
noun(X).
np2([X | S], np2(adj(X), NP), R) :-
adj(X),
np2(S, NP, R).
pp([X | S], pp(prep(X), NP), R):-
prep(X),
np(S, NP, R).
vp([X | R], vp(verb(X)), R) :- /* Changed here - added the third argument */
verb(X).
vp([X | S], vp(verb(X), PP), R) :-
verb(X),
pp(S, PP, R).
vp([X | S], vp(verb(X), NP), R) :-
verb(X),
np(S, NP, R).
det(the).
det(with).
...
Sorry for the changed names. I wasn't able to work with the code the way it was.
Upvotes: 1