从序言结构中提取单词

发布于 2024-10-30 14:24:09 字数 1344 浏览 0 评论 0原文

我被安排了一个在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美人骨 2024-11-06 14:24:10

您可以

a) 在解析时构建列表

b) 对构建的语法树进行逐字访问

c) 使用(例如)=.. (univ) 和 findall 编写通用访问。

要遵循 a) 更改当前程序,在语法树后面添加列表。

b) 的草图(b 停留在无聊):

extract(sentence(NP, VP), L) :-
 extract(NP, A), extract(VP, B), append(A, B, L)
extract(noun_phrase(_, noun(N)), [N]).
etc etc...

一般访问:

extract(SynTree, O) :-
  SynTree =.. [F|As],
  findall(R, (member(M, As), extract(M, R)), Rs),
  ((F = noun ; F = verb) -> As = [K], L = [K|Rs] ; L = Rs),
  flatten(L, O).

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):

extract(sentence(NP, VP), L) :-
 extract(NP, A), extract(VP, B), append(A, B, L)
extract(noun_phrase(_, noun(N)), [N]).
etc etc...

The generic visit:

extract(SynTree, O) :-
  SynTree =.. [F|As],
  findall(R, (member(M, As), extract(M, R)), Rs),
  ((F = noun ; F = verb) -> As = [K], L = [K|Rs] ; L = Rs),
  flatten(L, O).
无远思近则忧 2024-11-06 14:24:09

考虑使用 format/2,而不是多次连续的 write/1 调用。然而,通常最好避免副作用,而是从关系的角度思考。要描述句子结构和列表之间的关系,请考虑使用 DCG 表示法:

sentence(sentence(NP, VP)) -->
    np(NP),
    vp(VP).

np(noun_phrase(det(D), np2(noun(N)))) --> [D, N].
... etc.

然后使用类似 ?-phrase(sentence(S), List). 的程序。然后,您可以在各个方向使用该程序,例如还可以检查给定列表是否对应于句子结构,如果您只是将输出写入屏幕,则无法轻松做到这一点。

Instead of multiple successive write/1 calls, consider using format/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:

sentence(sentence(NP, VP)) -->
    np(NP),
    vp(VP).

np(noun_phrase(det(D), np2(noun(N)))) --> [D, N].
... etc.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文