How to convert prolog parse tree back to a logical

2020-07-16 13:03发布

问题:

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