我正在读佩雷拉和希伯的"Prolog和自然语言分析“(pdf格式)一书,我和他在问题2.7中遇到了一条评论,上面写着:
在语义网络表示中,我们经常想问.“福特和这类公司之间有什么关系?” 修改语义网络的表示,既允许这类新问题,也允许前面问题中的问题。提示:将语义网络关系视为Prolog个人。这是一种重要的Prolog编程技术,有时被称为哲学界的具体化。
我不熟悉这种reification技术。
好的,让我们假设这个事实和规则数据库:
isa('Ole Black', 'Mustangs').
isa('Lizzy', 'Automobiles').
isa('Ford','Companies').
isa('GM','Companies').
isa('1968','Dates').
ako('Model T', 'Automobiles').
ako('Mustangs', 'Automobiles').
ako('Companies', 'Legal Persons').
ako('Humans', 'Legal Persons').
ako('Humans', 'Physical Objects').
ako('Automobiles', 'Physical Objects').
ako('Legal Persons', 'Universal').
ako('Dates', 'Universal').
ako('Physical Objects', 'Universal').
have_mass('Physical Objects').
self_propelled('Automobiles').
company(X) :- isa(X,'Companies').
legal_persons(X) :- ako(X,'Legal Persons').如何编写一个查询,在上面的代码中发现'Ford'和'Companies'之间的关系是isa?当然,我总是可以写这样的东西
fact(isa, 'Ford','Companies').并查询?- fact(X, 'Ford','Companies').,但不知何故,我认为这不是正确的方法。
有人能解释我怎么做好吗?
发布于 2019-08-22 13:13:12
结合Paul和Carlo答案的各个方面,另一个可能的解决方案是引入元关系谓词。例如:
relation(isa/2).
relation(ako/2).
relation(have_mass/1).
...这避免了一些使用原始数据库的查询由于关系的具体化而失去了第一个参数索引的好处。在Carlo的解决方案中,它还避免了对current_predicate/2的调用,获取不应该考虑的辅助谓词。
通过上述relation/2谓词的定义,我们可以编写:
relation(Relation, Entity1, Entity2) :-
relation(Relation/2),
call(Relation, Entity1, Entity2).
relation(Relation, Entity) :-
relation(Relation/1),
call(Relation, Entity).然后询问:
?- relation(Relation, 'Ford', 'Companies').
Relation = isa.发布于 2019-08-26 16:42:32
我想在前面的答案中加入一些背景知识(这是非常有用的)。
据我所知,Prolog中没有公认的具体化定义。可以说,原始代码中的关系已经被部分具体化了。示例:
isa('Ford', 'Companies').
% ...is a reification of...
company('Ford').
ako('Companies', 'Legal Persons').
% ...is a reification of...
legal_person(X) :- company(X).
have_mass('Physical Objects').
% ...is a reification of...
has_mass(X) :- physical_object(X).
self_propelled('Automobiles').
% ...is a reification of...
self_propelled(X) :- automobile(X).有关详细信息,请参阅关于语义网和框架的注记 by 马修·亨特巴赫中的Reification部分。
发布于 2019-08-22 10:12:40
不确定这本书的意图是什么..。很多年前,我读过意大利语的翻译,不记得这个问题了。
在SWI-Prolog中,模块起着重要的作用,因此我将介绍:
:- module(so_relationships, [which_rel/3]).
isa('Ole Black', 'Mustangs').
isa('Lizzy', 'Automobiles').
isa('Ford','Companies').
isa('GM','Companies').
isa('1968','Dates').
ako('Model T', 'Automobiles').
ako('Mustangs', 'Automobiles').
ako('Companies', 'Legal Persons').
ako('Humans', 'Legal Persons').
ako('Humans', 'Physical Objects').
ako('Automobiles', 'Physical Objects').
ako('Legal Persons', 'Universal').
ako('Dates', 'Universal').
ako('Physical Objects', 'Universal').
have_mass('Physical Objects').
self_propelled('Automobiles').
company(X) :- isa(X,'Companies').
legal_persons(X) :- ako(X,'Legal Persons').
which_rel(A,B,R) :-
current_predicate(so_relationships:R/2),
C=..[R,A,B],
%catch(call(C),E,(writeln(E),fail)).
call(C).注意(注释掉) catch/3.是在对带有模块名称的谓词指示符进行限定之前需要的。
?- which_rel('Ford','Companies',R).
R = isa ;
false.https://stackoverflow.com/questions/57566271
复制相似问题