How to convert prolog parse tree back to a logical sentence

你说的曾经没有我的故事 提交于 2019-12-09 18:27:39

问题


I managed to build the parse tree for given sentence and here it is, for the sentence: "The man went home."

T = s(np(det(the), n(man)), vp(v(went), np(n(home))))

1) How to use phrase/2 on this?

How to translate a sentence in a logical language using prolog? - is similar to what I need, but it's solution doesn't work on me.

2)I want to map this with grammar pattern and get the words tag. Det=the, N(Subject)=man, V=went, N(Object)=home

Is there a way to map this tree with given set tree structures and identify the grammar. how can I use parse tree to identify Subject, verb, object, the grammar pattern and the generate the target language sentence.


Edited later.. I tried this code and it gives considerable answer. Any suggestions on this code.

sent("(s(np(n(man))) (vp(v(went)) (np(n(home)))))").

whitespace --> [X], { char_type(X, white) ; char_type(X, space) }, whitespace.
whitespace --> [].

char(C) --> [C], { char_type(C, graph), \+ memberchk(C, "()") }.

chars([C|Rest]) --> char(C), chars(Rest).
chars([C]) --> char(C).

term(T) --> chars(C), { atom_chars(T, C) }.
term(L) --> list(L).

list(T) --> "(", terms(T), ")".

terms([]) --> [].
terms([T|Terms]) --> term(T), whitespace, !, terms(Terms).

simplify([s,[np, [n,[Subject]]], [vp,[v,[Verb]],[np,[n,[Object]]]]],Result) :- Result = [Subject,Verb,Object].

Thanks Mathee


回答1:


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...



来源:https://stackoverflow.com/questions/17761508/how-to-convert-prolog-parse-tree-back-to-a-logical-sentence

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