编写一个 Prolog DCG 语法来处理句子的意思并构建它的解析树 [英] Write a Prolog DCG grammar that handle the mean of a sentence and also build its parse tree

查看:67
本文介绍了编写一个 Prolog DCG 语法来处理句子的意思并构建它的解析树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种 DCG 语法,可以理解并同意以下短语:[john,paints][john, likes, mary] 管理使用参数将语义直接转化为DCG语法

I have this DCG grammar that understand and agree phrases like: [john, paints] and [john, likes, mary] managing the semantic meaning directly into the DCG grammar by the use of parameters

sentence2(VP) --> noun_phrase2(Actor),
                  verb_phrase2(Actor, VP).

noun_phrase2(Name) --> properName(Name).

verb_phrase2(Actor, VP) --> intrans_verb(Actor, VP).

verb_phrase2(Somebody, VP) --> trans_verb(Somebody, Something, VP),
                               noun_phrase2(Something).

properName(john) --> [john].
properName(mary) --> [mary].

intrans_verb(Actor, paints(Actor)) --> [paints].

trans_verb(Somebody, Something, likes(Somebody, Something)) --> [likes].

例如,短语 [john,paints]mean 将是:paints(john),事实上这是查询:

For example the mean of the phrase [john, paints] will be: paints(john), infact this is the result of the query:

?- sentence2(Meaning, [john,paints],[]).
Meaning = paints(john) 

好的,这样不用构建解析树就可以处理含义.

Ok, so this handle the meaning without build a parse tree.

我有一个练习,要求我修改之前的语法,以从句子的句法树中获得均值.所以我的查询必须返回解析树,以及句子的含义

但我在做这件事时发现了一些问题......我正在尝试这样的事情,但不起作用:

But I am finding some problem doing it...I was trying something like this, but don't work:

/** Adding the meaning into the Parse Tree: */

sentence2(sentence(Name, VerbPhrase)) --> noun_phrase2(Name),
                                  verb_phrase2(VerbPhrase).

noun_phrase2(noun_phrase(Name)) --> properName2(Name).


verb_phrase2(verb_phrase(IntransVerb)) --> intrans_verb2(IntransVerb).

verb_phrase2(verb_phras(TransVerb, ProperName)) --> trans_verb2(TransVerb),
                                    noun_phrase2(ProperName).

/* properName, intrans_verb ant trans_verb are LEAVES: */
properName2(properName(john)) --> [john].

properName2(properName(mary)) --> [mary].

intrans_verb2(Actor, intrans_verb2(paints(Actor))) --> [paints].

trans_verb2(Somebody, Something, trans_verb2(likes(Somebody, Something))) --> [likes].

推荐答案

我认为你的问题是不恰当的.如果你说

I think your question is ill-posed. If you say

?- sentence2(Meaning, [john,paints], []).
Meaning = paints(john).

您清楚地表示解析的结果是意义"而不是解析树.我希望解析树看起来更像这样:

you're clearly saying that the result of the parse is a "meaning" and not a parse tree. I would expect a parse tree to look more like this:

Parse = s(np(properNoun(john)), vp(intrans(paints))).

您可以通过使用一些结构信息扩充 DCG 来轻松生成解析树:

You can easily produce parse trees by augmenting your DCG with some structural information:

sentence(s(NounPhrase, VerbPhrase)) --> 
    noun_phrase(NounPhrase), 
    verb_phrase(VerbPhrase).

noun_phrase(proper_noun(john))    --> [john].
verb_phrase(intransitive(paints)) --> [paints].

如果您想要一个或另一个,只需生成您想要的一个.如果您想要两者,您可能应该在使用树解析后收集含义.否则获得解析树没有多大意义,是吗?这是草图:

If you want one or the other, just generate the one you want. If you want both, you should probably collect the meaning after the parse using the tree. Otherwise there's not much point to getting a parse tree, is there? Here's a sketch:

meaning(s(proper_noun(Noun), intransitive(Verb)), Meaning) :-
  Meaning =.. [Verb, Noun].

这当然必须变得更健壮,但这就是它的使用方式:

This would of course have to be made more robust, but this is how it might be used:

?- phrase(sentence(S), [john, paints]), meaning(S, Meaning).
S = s(proper_noun(john), intransitive(paints)),
Meaning = paints(john).

现在探索,玩!制作一些将您的母语(意大利语,对吧?)解析为有意义"的结构的东西.将土耳其语的几句话解析为含义"并从中生成意大利语.您会惊讶地发现这一切是多么容易.

Now explore, play! Make something that parses some of your native language (Italian, right?) into "meaningful" structures. Parse a few sentences of Turkish into "meaning" and generate Italian from it. You'll be surprised how easily it all comes together.

这篇关于编写一个 Prolog DCG 语法来处理句子的意思并构建它的解析树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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