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.
I'm using SWI-prolog.
EDIT: 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:
From here, you can see where the recursion occurs and write predicates which align with the definitions above:
To execute:
The results are asserted, as can be seen if we list the
edge/2
facts: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)]
:And then assert them all at once from the resulting list with
maplist
: