我正在研究使用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] 具有以下含义: paint(john)
我会看到我对如何达到这个含义的想法是正确的。
所以我认为这是我执行以下查询时发生的事情:
?- sentence2(Meaning, [john, paints], []).
Meaning = paints(john)
[john paints] 这是我的最后一句,我必须评估并说明它是否属于我的语言。
必须以下列方式形成一句话:
sentence2(VP) --> noun_phrase2(Actor),
verb_phrase2(Actor, VP).
(通过名词短语之后的内容,后跟动词短语。
名词短语以这种方式组成:
noun_phrase2(Name) --> properName(Name).
所以名词短语是专有名称
专有名称的含义很简单,因为通过以下行:
properName(john) --> [john].
我只是说 john是一个正确的名称,我在DCG语法中添加了一个指定其含义的参数。所以:正确名称john的语义是john
因此,名词短语的含义是专有名称的相同含义(因为变量名称统一)
所以在前一种情况下,含义 noun_phrase2 谓词是 john ,我原句的第一个评估步骤结束。< / p>
现在我必须通过谓词评估第二部分是语言短语: verb_phrase2(演员,VP)
口头短语可以是不及物动词:
verb_phrase2(Actor, VP) --> intrans_verb(Actor, VP).
以这种方式定义不及物动词:
intrans_verb(Actor, paints(Actor)) --> [paints].
所以 paint 这个词是一个不及物动词,它的含义是 paint(Actor),其中 Actor 是一个依赖于上下文的变量(在这种情况下演员 rappresent 谁执行操作,谁画)
因此,它回溯到 verb_phrase2(演员,VP)以验证 verb_phrase2(演员,VP)
现在,Actor仍然是一个尚未统一的变量,其含义为 VP = paint(Actor)
因此,验证 paint 是一个不及物动词,其含义是 paint(Actor)
所以执行回溯到原来的 sentence2(VP)谓词,我刚刚验证了 noun_phrase2(Actor)谓词,其中 Actor = john
所以我有类似这样的情况:
sentence2(VP) --> noun_phrase2(john),
verb_phrase2(john, paints(john)).
所以最终的 VP统一到paint(john)
我的推理是正确还是我错过了什么?这是用Prolog方式推理的好方法吗?
答案 0 :(得分: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 | []]
!-