我已經在Prolog中設置了自然語言分析任務。到目前爲止,我的程序在某種程度上工作。到目前爲止,如果我輸入[the,cat,sat,on,the,mat]
的列表,那麼它將打印sentence(noun_phrase(det(the), np2(noun(cat))), verb_phrase(verb(sat), pp(prep(on), noun_phrase(det(the), np2(noun(mat))))))
,這很好。從序言結構中提取單詞
下一個任務我必須做的是從句子中提取的關鍵字,即提取的名詞短語的名詞,在動詞短語動詞和動詞短語的名詞,所以我可以返回一個列表: [貓,飽和,墊。任何人都可以幫我開始,因爲我很堅持這一點。謝謝!
我當前的代碼是:
sentence(S,sentence((NP), (VP))):-
nl,
np(S, NP, R),
vp(R, VP, []),
write('sentence('), nl, write(' '), write((NP))
,nl,write(' '), write((VP)),nl,write(' ').
np([X | S], noun_phrase(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], verb_phrase(verb(X)), R) :- /* Changed here - added the third argument */
verb(X).
vp([X | S], verb_phrase(verb(X), PP), R) :-
verb(X),
pp(S, PP, R).
vp([X | S], verb_phrase(verb(X), NP), R) :-
verb(X),
np(S, NP, R).
det(the).
det(with).
noun(cat).
noun(mat).
verb(sat).
prep(on).
adj(big).
好的,感謝您的答覆。然而,我寧願不改變我已經有的標記,因爲時間是一個問題:( – MartinM 2011-04-05 13:20:11