首页 文章

关于如何在Prolog中使用这种DCG语法作为自然语言子集的一些疑问

提问于
浏览
-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)

[john paints] 这是我的 final sentence ,我必须评估并说它是否属于我的语言 .

必须以下列方式形成一个句子:

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

(通过 noun phrase 后跟 verb phrase 的东西 .

noun phrase 以这种方式组成:

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

所以 noun phraseproper name

proper name 的含义很简单,因为通过这一行:

properName(john) --> [john].

我只是说 john is a proper name 和我正在为DCG语法添加一个指定其含义的参数 . 所以: the semantic meaning of the proper name john is john

所以, noun phrasemeaningproper namesame meaning (因为变量 Name 统一)

所以在前一种情况下, meaning 谓的 noun_phrase2 谓词是 john ,我原始句子的第一个评估步骤结束 .

现在我必须通过谓词评估第二部分是 verbal phraseverb_phrase2(Actor, VP)

在这种情况下, verbal phrase 可能是 intransitive verb

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)

因此,执行回溯到原始的 sentence2(VP) 谓词,我刚刚验证了 noun_phrase2(Actor) 谓词,其中 Actor = john

所以我有类似这样的情况:

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

所以决赛 VP is unified to paints(john)

这是我的推理是正确的还是我错过了什么?这是以Prolog方式推理的好方法吗?

1 回答

  • 2

    请问一个具体的简短问题 . 包含相关代码而不会有过多评论 .

    这是如何做 .


    鉴于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 | []]
    
    !-
    

相关问题