How to convert prolog parse tree back to a logical sentence

天大地大妈咪最大 提交于 2019-12-04 07:24:35

the simpler way to do is by means a visit of the tree, 'hardcoded' on the symbols you are interested.

Here is a more generic utility, that uses (=..)/2 to capture a named part of the tree:

part_of(T, S, R) :- T =.. [F|As],
    (   F = S,
        R = T
    ;   member(N, As),
        part_of(N, S, R)
    ).

?- part_of(s(np(det(the), n(man)), vp(v(went), np(n(home)))),np,P).
P = np(det(the), n(man)) ;
P = np(n(home)) ;
false.

It's a kind of member/2, just for trees. BTW I don't understand the first part of your question: why do you want to use phrase/2 on a syntax tree ? Usually a grammar (the first argument to phrase/2) is meant to build a syntax tree from 'raw' characters stream...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!