基于模式匹配进行多个断言 [英] make multiple assert basing on pattern matching

查看:29
本文介绍了基于模式匹配进行多个断言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个字符串的解析树,我的目标是更新我的知识库.

Given a parse tree of a string my goal is to update my knowledge base.

来自一个长度可变的句子,如下所示:

From a sentence with variable length like the following:

"node 1 is near node 2 that is near node 3 that is near node 4 that..."

在我的表示中变成了句子的解析树表示,例如:

in my representation becomes a parse tree representation of the sentence, for example:

 s(desc(np(noun(node),id(1)),vp(verb(is),prep(near),np(noun(node),id(2),rel_clause(rel(that)...

我想从中提取和断言以下信息:

from which I'd like to extract and assert the following informations:

 edge(1,2),edge(2,3),edge(3,4).

我怎样才能实现这个目标?

How can I achieve this goal?

我试图用类似的东西来管理一些案例

I've tried to manage some case with something like

 :- dynamic edge/2.

   extract(T):- T= s(desc(np(noun(node),id(A)),vp(verb(is),prep(near),np(noun(node),id(B)))),
   assert(edge(A,B)).

   extract(T):- T= s(desc(np(noun(node),id(A)),vp(verb(is),prep(near),np(noun(node),id(B),rel_clause(rel(that)...

等但我想管理潜在的无限句子.

etc but I'd like to manage potential infinite sentences.

我正在使用 SWI-prolog.

I'm using SWI-prolog.

我在输入中得到的解析树的完整示例:

complete example of a parse tree I get in input:

 desc(np(noun(node), id(1)), vp(verb(is), prep(near), np(noun(node), id(2), 
     rel_clause(rel(that), vp(verb(is), prep(near), np(noun(node), id(3),  
     rel_clause(rel(that), vp(verb(is), prep(near), np(noun(node), id(4))))))))) 

推荐答案

首先要做的是对数据进行更有用的描述.一种方法是像这样分解它:

The first thing to do is come up with a more useable description of your data. One way is to break it down like this:

description = desc(subject, verb_part)
subject = np(noun(node), id(A))
verb_part = vp(verb(is), prep(near), object_part)
object_part = np(noun(node), id(B))
object_part = np(noun(node), id(B), rel_part)
rel_part = relcl(rel(that), verb_part)

从这里,您可以看到递归发生的位置并编写符合上述定义的谓词:

From here, you can see where the recursion occurs and write predicates which align with the definitions above:

% description = desc(subject, verb_part)
% subject = np(noun(node), id(A))
%
extract(desc(np(noun(node), id(A)), VerbPart)) :-
    select_edge(A, VerbPart).

% verb_part = vp(verb(is), prep(near), object_part)
%
select_edge(A, vp(verb(is), prep(near), ObjectPart)) :-
    connect_node(A, ObjectPart).

% object_part = np(noun(node), id(B))
%
connect_node(A, np(noun(node), id(B))) :-
    assertz(edge(A, B)).

% object_part = np(noun(node), id(B), rel_part)
% rel_part = relcl(rel(that), verb_part)
%
connect_node(A, np(noun(node), id(B), relcl(rel(that), VerbPart))) :-
    assertz(edge(A, B)),
    select_edge(B, VerbPart).

执行:

| ?- extract(desc(np(noun(node), id(1)), vp(verb(is), prep(near), np(noun(node), id(2),
     relcl(rel(that), vp(verb(is), prep(near), np(noun(node), id(3),
     relcl(rel(that), vp(verb(is), prep(near), np(noun(node), id(4))))))))))).

true ? ;

no

结果被断言,如果我们列出 edge/2 事实可以看出:

The results are asserted, as can be seen if we list the edge/2 facts:

| ?- listing(edge).

% file: user_input

edge(1, 2).
edge(2, 3).
edge(3, 4).

yes

您也可以在列表中收集边而不是断言它们,并且查询的结果是,[edge(1,2), edge(2,3), edge(3,4)]:

You can also collect the edges in a list instead of asserting them, and have a result of the query be, [edge(1,2), edge(2,3), edge(3,4)]:

extract(desc(np(noun(node), id(A)), VerbPart), Edges) :-
    select_edge(A, VerbPart, Edges).

select_edge(A, vp(verb(is), prep(near), ObjectPart), Edges) :-
    connect_node(A, ObjectPart, Edges).

connect_node(A, np(noun(node), id(B)), [edge(A,B)]).
connect_node(A, np(noun(node), id(B), relcl(rel(that), VerbPart)), [edge(A,B)|Edges]) :-
    select_edge(B, VerbPart, Edges).

然后使用 maplist 从结果列表中一次性断言它们:

And then assert them all at once from the resulting list with maplist:

extract(Description, Edges), maplist(assertz, Edges).

这篇关于基于模式匹配进行多个断言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆