我有一個大學分配做出Prolog程序分析自然語言,也就是說,它應該輸出序言自然語言解析
P = np(det(the), np2(noun(cat))), vp(verb(sat), pp(prep(on), np(det(the), np2(noun(mat)))))
L = []
當我輸入NP([中,貓,坐,上,中,墊] ,P,L)。
相反,動詞和名詞短語被拆分並走出seperatly,如:
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).
這是一個相當奇怪的解析結構,你確定這是它應該出來的嗎? – Raceimaztion 2011-04-02 08:25:33
你爲什麼不使用DCG? – Kaarel 2011-04-03 08:06:54