diff --git a/README.markdown b/README.markdown index 6e99582..607c307 100644 --- a/README.markdown +++ b/README.markdown @@ -165,6 +165,23 @@ You can optionally use [`amatch`](http://flori.github.com/amatch/) by [Florian F Otherwise, pure ruby versions of the string similarity algorithms derived from the [answer to a StackOverflow question](http://stackoverflow.com/questions/653157/a-better-similarity-ranking-algorithm-for-variable-length-strings) and [the text gem](https://github.com/threedaymonk/text/blob/master/lib/text/levenshtein.rb) are used. Thanks [marzagao](http://stackoverflow.com/users/10997/marzagao) and [threedaymonk](https://github.com/threedaymonk)! +## JRuby usage + +In addition to amatch you can also use JRuby with a Java library. +In order to do that, you have to add jbundler to your Gemfile: + + platform :jruby do + gem 'jbundler' + end + +and you need a Jarfile containing + + jar 'info.debatty:java-string-similarity' + +The engine has to be set to jruby then: + + FuzzyMatch.engine = :jruby + ## Real-world usage

Brighter Planet logo

diff --git a/lib/fuzzy_match.rb b/lib/fuzzy_match.rb index 67c6c5b..e9eea99 100644 --- a/lib/fuzzy_match.rb +++ b/lib/fuzzy_match.rb @@ -19,6 +19,8 @@ def engine=(alt_engine) def score_class case engine + when :jruby + Score::JRuby when :pure_ruby Score::PureRuby when :amatch diff --git a/lib/fuzzy_match/score.rb b/lib/fuzzy_match/score.rb index e982464..060783c 100644 --- a/lib/fuzzy_match/score.rb +++ b/lib/fuzzy_match/score.rb @@ -1,5 +1,6 @@ require 'fuzzy_match/score/pure_ruby' require 'fuzzy_match/score/amatch' +require 'fuzzy_match/score/jruby' class FuzzyMatch class Score diff --git a/lib/fuzzy_match/score/jruby.rb b/lib/fuzzy_match/score/jruby.rb new file mode 100644 index 0000000..c950694 --- /dev/null +++ b/lib/fuzzy_match/score/jruby.rb @@ -0,0 +1,27 @@ +class FuzzyMatch + class Score + # be sure to run this with JRuby + class JRuby < Score + + def dices_coefficient_similar + @dices_coefficient_similar ||= if str1 == str2 + 1.0 + elsif str1.length == 1 and str2.length == 1 + 0.0 + else + java_import "info.debatty.java.stringsimilarity.SorensenDice" + Java::InfoDebattyJavaStringsimilarity::SorensenDice.new(2).similarity(str1, str2) + end + end + + def levenshtein_similar + @levenshtein_similar ||= levenshtein + end + + def levenshtein + java_import "info.debatty.java.stringsimilarity.Levenshtein" + 1 - Java::InfoDebattyJavaStringsimilarity::Levenshtein.new.distance(str1, str2) + end + end + end +end diff --git a/spec/amatch_spec.rb b/spec/amatch_spec.rb index 19cd465..01de8cb 100644 --- a/spec/amatch_spec.rb +++ b/spec/amatch_spec.rb @@ -1,7 +1,7 @@ unless RUBY_PLATFORM == 'java' require 'spec_helper' require 'amatch' - + describe FuzzyMatch do describe %{when using the :amatch string similarity engine} do before do