从序言结构中提取单词
我被安排了一个在Prolog中进行自然语言解析的任务。到目前为止,我的程序已经在一定程度上发挥了作用。到目前为止,它将打印 sentence(noun_phrase(det(the), np2(noun(cat))), verb_phrase(verb(sat), pp(prep(on), noun_phrase(det(the)), np2(noun) (mat))))))
如果我输入 [the,cat,sat,on,the,mat]
列表,这很好。
我要做的下一个任务是从句子中提取关键字,即提取名词短语中的名词、动词短语中的动词和动词短语中的名词,这样我就可以返回一个列表:[cat,sat ,垫]。有人可以帮我开始吗,因为我对此非常坚持。谢谢!
我当前的代码是:
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).
i have been set a task of natural language parsing in Prolog. so far i have the program working to an extent. So far it will print sentence(noun_phrase(det(the), np2(noun(cat))), verb_phrase(verb(sat), pp(prep(on), noun_phrase(det(the), np2(noun(mat))))))
if i input a list of [the,cat,sat,on,the,mat]
, which is fine.
the next task i have to do is to extract the keywords from the sentence, ie extract the noun in the noun phrase, the verb in the verb phrase and the noun in the verb phrase, so i could return a list: [cat,sat,mat]. Could anybody give me a hand getting started because im very stuck with this. thanks!
my current code is:
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以
a) 在解析时构建列表
b) 对构建的语法树进行逐字访问
c) 使用(例如)=.. (univ) 和 findall 编写通用访问。
要遵循 a) 更改当前程序,在语法树后面添加列表。
b) 的草图(b 停留在无聊):
一般访问:
You could
a) build the list while parsing
b) take a verbatim visit of the syntax tree built
c) write a generic visit using (for instance) =.. (univ) and findall.
To follow a) change your current program, adding the list after the syntax tree.
A sketch of b) (b stay for boring):
The generic visit:
考虑使用
format/2
,而不是多次连续的write/1
调用。然而,通常最好避免副作用,而是从关系的角度思考。要描述句子结构和列表之间的关系,请考虑使用 DCG 表示法:然后使用类似
?-phrase(sentence(S), List).
的程序。然后,您可以在各个方向使用该程序,例如还可以检查给定列表是否对应于句子结构,如果您只是将输出写入屏幕,则无法轻松做到这一点。Instead of multiple successive
write/1
calls, consider usingformat/2
. However, it is usually best to avoid side-effects, and instead to think in terms of relations. To describe the relation between your sentence structures and lists, consider using DCG notation:and then using the program like
?- phrase(sentence(S), List).
. You can then use the program in all directions and for example also check whether a given list corresponds to a sentence structure, which you cannot do so easily if you just write output to the screen.