-1

我正在使用 Prolog 研究用于自然语言处理的 DCG 语法,我对我是否理解正确或是否遗漏了一些东西有一些疑问。

这是我的 DCG 语法:

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

/* A noun phrase is a proper name of a person (that is unified in the Name variable) */
noun_phrase2(Name) --> properName(Name).

/* A verb_phrase can be an intransitive verb */
verb_phrase2(Actor, VP) --> intrans_verb(Actor, VP).

/* A verb_phrase can be a transitive verb followed by an object complement
verb_phrase2(Somebody, VP) --> trans_verb(Somebody, Something, VP),
                               noun_phrase2(Something).

/* The meaning of a proper name john is john
   The meaning of a proper name mary is mary */
properName(john) --> [john].
properName(mary) --> [mary].

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

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

所以这个语法可以接受像这样的短语:[john,paints]具有以下含义:paints(john)

我会看看我关于如何达到这个意思的想法是否正确。

所以我认为当我执行以下查询时会发生这种情况:

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

[约翰绘画]这是我必须评估并说出它是否属于我的语言的最后一句话。

句子必须按以下方式组成:

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

(由名词短语后面跟着动词短语的东西。

名词短语是这样组成的:

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

所以名词短语专有名称

专有名称的含义很简单,因为通过这一行:

properName(john) --> [john].

我只是说john 是一个专有名称,我正在向 DCG 语法添加一个参数来指定其含义。所以:专名john的语义是john

因此,作为名词短语含义专有名称的含义相同(因为变量名称统一)

所以在前面的例子中,noun_phrase2 谓词的含义john 原句的第一个评估步骤是结束。

现在我必须评估第二部分是谓词的口头短语verb_phrase2(Actor, VP)

在这种情况下,口头短语可以是不及物动词

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

不及物动词是这样定义的:

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

所以paints这个词是一个不及物动词,它的意思是paints(Actor),其中Actor是一个依赖于上下文的变量(在这种情况下,Actor rappresent who do the action, who paints

因此,它会回溯到verb_phrase2(Actor, VP)以验证verb_phrase2(Actor, VP)

现在 Actor 仍然是一个尚未统一的变量,其含义是VP = paints(Actor)

所以,验证paints是一个不及物动词,它的意思是paints(Actor)

所以执行回溯到我刚刚验证了noun_phrase2(Actor)谓词和Actor = john的原始sentence2(VP)谓词

所以我有这样的情况:

sentence2(VP) --> noun_phrase2(john),
                  verb_phrase2(john, paints(john)).

所以最终的VP统一到paints(john)

是我的推理正确还是我遗漏了什么?以 Prolog 方式进行推理是一种好方法吗?

4

1 回答 1

3

请提出一个具体的简短问题。包含相关代码,不要过多注释。

就是这样。


给定 DCG 规则

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

以下如何达到其结果?

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

回答:

上述规则等价于

sentence2(VP, L, Z):- noun_phrase2(Actor, L, L2), 
                      verb_phrase2(Actor, VP, L2, Z).
noun_phrase2(Name, L, Z):- properName(Name, L, Z).    
verb_phrase2(Actor, VP, L, Z):- intrans_verb(Actor, VP, L, Z). 
verb_phrase2(Somebody, VP, L, Z):- trans_verb(Somebody, Something, VP, L, L2),
                                   noun_phrase2(Something, L2, Z).    
properName(john, L, Z):- 
    L = [john | Z].         %// 'john' is present in the input stream
properName(mary, L, Z):- 
    L = [mary | Z].         %// 'mary' is present in the input stream
/* an alternative definition 
properName(X) --> [X], { member(X, [john, mary]) }.
   %% would be translated as
properName(X, L, Z):- L = [X | Z], member(X, [john, mary]). 
*/    
intrans_verb(Actor, paints(Actor), L, Z):- 
    L = [paints | Z].       %// 'paints' is present in the input stream    
trans_verb(Somebody, Something, likes(Somebody, Something), L, Z):-
    L = [likes | Z].        %// 'likes' is present in the input stream

尤其是,

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

?- noun_phrase2(Actor, [john, paints], L2),                    
   verb_phrase2(Actor, Meaning, L2, []).

  ?- noun_phrase2(Actor, [john, paints], L2).
  ?- properName(Actor, [john, paints], L2).   
  ?- properName(john, [john, paints], L2).            { Actor=john }
  !- [john, paints] = [john | [paints]]               { L2=[paints] }

  ?- verb_phrase2(john, Meaning, [paints], []).
  ?- intrans_verb(john, Meaning, [paints], []).
  ?- intrans_verb(john, paints(john), [paints], []).  { Meaning=paints(john) }
  !- [paints] = [paints | []]

!- 
于 2013-05-19T17:10:41.890 回答