如何将Prolog解析树转换回逻辑语句 [英] How to convert prolog parse tree back to a logical sentence

查看:115
本文介绍了如何将Prolog解析树转换回逻辑语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法为给定的句子构建了一个解析树,在这里,句子的意思是:那个人回家了."

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)如何在此上使用词组/2?

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)我想用语法模式将其映射并获取单词标签. Det=theN(Subject)=manV=wentN(Object)=home

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

感谢Mathee

推荐答案

最简单的方法是通过对树进行访问,对您感兴趣的符号进行硬编码".

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.

这是一种member/2,仅适用于树木.顺便说一句,我不明白您问题的第一部分:为什么要在语法树上使用词组/2?通常,语法(短语/2的第一个参数)用于从原始"字符流中构建树...

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

这篇关于如何将Prolog解析树转换回逻辑语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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