3

我正在试验 ruby​​log,它是 Ruby 的 Prolog。我的目标是在单词之间创建逻辑链接,以挖掘我可能不会注意到的文档之间的联系。我今天才开始,我正在玩同义词检测器。奇怪的是,似乎我已经使同义词规则几乎但不是完全对称和传递。

在这个例子中,我试图教 Prolog 'math'、'maths' 和 'mathematics' 都是同义词:

require 'rubylog'
include Rubylog::DSL::Constants

Symbol.rubylog_functor :synonym
Symbol.rubylog_functor :synonym_of
X.synonym(Y).if X.synonym_of(Y)
X.synonym(Y).if Y.synonym_of(X)
X.synonym(Z).if X.synonym_of(Y).and Y.synonym_of(Z)   ###########

:math.synonym_of! :mathematics
:mathematics.synonym_of! :maths
puts "Synonyms for math: #{:math.synonym(X).to_a}"
puts "Synonyms for maths: #{:maths.synonym(X).to_a}"
puts "Synonyms for mathematics: #{:mathematics.synonym(X).to_a}"

令我惊讶的是,结果是

Synonyms for math: [:mathematics, :maths]
Synonyms for maths: [:mathematics]
Synonyms for mathematics: [:maths, :math]

我认为问题可能是标记的行###########使用synonym_of而不是synonym,因此可能没有递归地描述规则。但是改变那条线X.synonym(Z).if X.synonym_of(Y).and Y.synonym(Z)给出了非常奇怪的输出

Synonyms for math: [:mathematics, :maths, :math, :mathematics]
Synonyms for maths: [:mathematics]
Synonyms for mathematics: [:maths, :math, :mathematics]

非常好奇!你怎么看?

4

1 回答 1

1

您正在对有向图上的传递闭包进行建模,然后您应该对传递属性使用递归规则,但要防止循环。

我可以建议的实用方法是明确地为同义词编写规则(对不起,我没有 ruby​​log,我正在 SWI-Prolog 中测试):

synonym(X, Z) :- synonyms(X, L), member(Z, L).

synonym_of(math, mathematics).
synonym_of(mathematics, maths).

synonyms(X, Ss) :- synonyms(X, [], Ss).

synonyms(X, Found, R) :-
    ( synonym_of(X, S) ; synonym_of(S, X) ),
    \+ member(S, Found),
    !, synonyms(S, [S|Found], R).
synonyms(_, Found, Found).
于 2012-09-22T08:25:59.820 回答