2012-09-21 29 views
3

我正在嘗試rubylog,這是Ruby的Prolog。我的目標是創建單詞之間的邏輯鏈接,以挖掘我可能不會注意到的文檔之間的連接。我今天才開始,我正在玩同義詞檢測器。奇怪的是,我似乎已經使得同義詞規則幾乎不對稱和傳遞。Prolog中的簡單同義詞匹配器(well,rubylog)

在這個例子中,我試圖教Prolog的是「數學」,「數學」和「數學」都是同義詞:

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] 

非常好奇!你怎麼看?

回答

1

您正在對有向圖上的傳遞閉包進行建模之後,您應該對傳遞屬性使用遞歸規則,但是要防範循環。

的實際辦法,我可以建議是寫明確同義詞規則(對不起,我沒有rubylog,我在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).