I intend to create a gem containing everything needed to use libsvm through JRuby, including an implementation of libsvm. (In future, I expect to add some extensions to libsvm, such as further kernel types, or support for active learning.)
Creating a gem is not, on the face of it, too hard, although it takes a little while initially to get everything in place. The project code must be laid out in the correct format, and a gemspec file created to define all the components. The gem is built from the gemspec file, and then the gem can be distributed and installed. A useful guide is available on the rubygems website.
Project Files
My project files currently look as follows:
svm_toolkit/README.txt
svm_toolkit/svm_toolkit.gemspec
svm_toolkit/examples/example1.rb
svm_toolkit/lib/svm_toolkit.rb
svm_toolkit/lib/svm/libsvm.jar
The jar file contains my packed version of libsvm: later, I'll explore how to provide the java source files in the gem, so that the jar file can be compiled during installation.
The Gem Spec
The specification for a Gem includes information about the author, a summary and the version numbers. The list of files must contain the name of each file to be included within the Gem package. By setting platform to the current platform, the gem will also be labelled with the current Java settings.
Gem::Specification.new do |s|
s.name = "svm_toolkit"
s.platform = Gem::Platform::CURRENT
s.author = "Peter Lane"
s.version = "0.0.1"
s.email = "peter.lane@bcs.org.uk"
s.homepage = "http://rubyforscientificresearch.blogspot.com/"
s.summary = "A JRuby wrapper around an extended version of the libsvm library."
s.license = "GPL3"
s.description = <<-END
Support-vector machines are a popular tool in data mining.
This package supports common and extended techniques for working with the
Java implementation of the libsvm library, through JRuby. The package
includes an amended version of libsvm, to better support JRuby and
techniques such as active learning.
END
s.files = [
"README.txt",
"lib/svm_toolkit.rb",
"lib/svm/libsvm.jar",
"examples/example-1.rb"
]
s.require_path = "lib"
s.has_rdoc = true
end
svm_toolkit.rb
The gem currently does not do much. It simply provides the imports needed to write scripts using libsvm:
#
# svm_toolkit
#
require "java"
require "svm/libsvm"
import "libsvm.Parameter"
import "libsvm.Model"
import "libsvm.Problem"
import "libsvm.Node"
import "libsvm.Svm"
Building and Installing the Gem
Here, I had a little problem with JRuby (version 1.6.5) and its 1.8/1.9 compatibility. I have JRuby set to use --1.9 by default. But building a gem in this way led to problems with including the jar file, so:
$ jruby --1.8 -S gem build svm_toolkit.gemspec
which generates the file: "svm_toolkit-0.0.1-universal-java-1.6.gem"
The gem can then be installed with:
$ jruby -S gem install svm_toolkit-0.0.1-universal-java-1.6.gem
(Edit: this problem has now been fixed.)
Publishing the Gem
After pushing the gem, it is now available from RubyGems as the svm_toolkit. Let me know if it works!
0 comments:
Post a Comment