我有一个大学作业,让prolog程序解析自然语言,即它应该输出
P = np(det(the), np2(noun(cat))), vp(verb(sat), pp(prep(on), np(det(the), np2(noun(mat)))))
L = []
当我输入np([the,cat,sat,on,the,mat],P,L)时。
相反,动词和名词短语被分开并单独出现,如:P = np(det(the), np2(noun(cat))),
L = [sat, on, the, mat]
我想知道是否有人能指出我哪里出错了,我将非常感激,我的代码是:
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).
答案 0 :(得分:2)
嗯,你真的确定你想要“猫坐在垫子上”的NP。这不是NP,它是一个完整的句子,不是吗?
其次我没有在Prolog写过很长一段时间。我认为代码应该是这样的:
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).
...
抱歉更改了名称。我无法按原样处理代码。
答案 1 :(得分:2)
仔细查看代码后,我发现至少有两个错误。
首先也是最重要的是,你需要更加谨慎地定义你的'vp'谓词,我注意到你已经定义了两个不同版本的东西。你可以说它们是不同的,因为一个只有arity 2而另一个是arity 3。
我建议将所有重要的和功能性'vp'谓词定义定义为arity 3,保留“Tail”参数,然后使用单个arity 2谓词,如下所示:
vp(T, Parse) :- vp(T, Parse, []).
其次,我认为你需要检查你在“句子”谓词的定义中如何嵌套括号。我想你已经混淆了其中几个。