svmredlight 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +14 -0
- data/Gemfile.lock +35 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +44 -0
- data/Rakefile +46 -0
- data/VERSION +1 -0
- data/examples/example1/example.rb +76 -0
- data/examples/example1/test.dat +601 -0
- data/examples/example1/train.dat +2001 -0
- data/examples/example1/words +9947 -0
- data/ext/extconf.rb +6 -0
- data/ext/svmredlight.c +762 -0
- data/lib/svmredlight/document.rb +23 -0
- data/lib/svmredlight/model.rb +22 -0
- data/lib/svmredlight.rb +4 -0
- data/svmredlight.gemspec +73 -0
- data/test/assets/model +3888 -0
- data/test/helper.rb +19 -0
- data/test/test_document.rb +55 -0
- data/test/test_model.rb +114 -0
- metadata +134 -0
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "shoulda", ">= 0"
|
10
|
+
gem "bundler", "~> 1.0.0"
|
11
|
+
gem "jeweler", "~> 1.6.0"
|
12
|
+
gem "ZenTest"
|
13
|
+
gem "ruby-debug19"
|
14
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
ZenTest (4.5.0)
|
5
|
+
archive-tar-minitar (0.5.2)
|
6
|
+
columnize (0.3.4)
|
7
|
+
git (1.2.5)
|
8
|
+
jeweler (1.6.0)
|
9
|
+
bundler (~> 1.0.0)
|
10
|
+
git (>= 1.2.5)
|
11
|
+
rake
|
12
|
+
linecache19 (0.5.12)
|
13
|
+
ruby_core_source (>= 0.1.4)
|
14
|
+
rake (0.9.0)
|
15
|
+
ruby-debug-base19 (0.11.25)
|
16
|
+
columnize (>= 0.3.1)
|
17
|
+
linecache19 (>= 0.5.11)
|
18
|
+
ruby_core_source (>= 0.1.4)
|
19
|
+
ruby-debug19 (0.11.6)
|
20
|
+
columnize (>= 0.3.1)
|
21
|
+
linecache19 (>= 0.5.11)
|
22
|
+
ruby-debug-base19 (>= 0.11.19)
|
23
|
+
ruby_core_source (0.1.5)
|
24
|
+
archive-tar-minitar (>= 0.5.2)
|
25
|
+
shoulda (2.11.3)
|
26
|
+
|
27
|
+
PLATFORMS
|
28
|
+
ruby
|
29
|
+
|
30
|
+
DEPENDENCIES
|
31
|
+
ZenTest
|
32
|
+
bundler (~> 1.0.0)
|
33
|
+
jeweler (~> 1.6.0)
|
34
|
+
ruby-debug19
|
35
|
+
shoulda
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Camilo Lopez
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
== svmredlight
|
2
|
+
|
3
|
+
A partial interface to SVM-light [http://svmlight.joachims.org/] using it you can:
|
4
|
+
|
5
|
+
* Load an existent model (pre-created with svm_learn) from a file and using it for classification.
|
6
|
+
* Train a new classification SVM using linear kernels only (more kernels and SVM types to come) 100% form Ruby.
|
7
|
+
|
8
|
+
As of now it's know to work with SVM 6.02.
|
9
|
+
|
10
|
+
Make sure to build the libsvmlight.o version of svmlight by using
|
11
|
+
"make libsvmlight_hideo".
|
12
|
+
|
13
|
+
|
14
|
+
== Document
|
15
|
+
|
16
|
+
The Document class is a ruby representation of the DOC struct in svmlight, in order to
|
17
|
+
create a Document the feature vectors should be represented as a Hash (or array of arrays)
|
18
|
+
where the keys are the feature numbers and the values the feature weight.
|
19
|
+
|
20
|
+
Document.new({1 => 0.5, 100 => 0.7}, :docnum => 1, :costfactor => 0.3)
|
21
|
+
|
22
|
+
== Model
|
23
|
+
|
24
|
+
The Model class is a ruby representation of the MODEL struct in svmlight.
|
25
|
+
|
26
|
+
== Usage
|
27
|
+
|
28
|
+
Take a look at the examples directory for a quick usage overview.
|
29
|
+
|
30
|
+
== Contributing to svmredlight
|
31
|
+
|
32
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
33
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
34
|
+
* Fork the project
|
35
|
+
* Start a feature/bugfix branch
|
36
|
+
* Commit and push until you are happy with your contribution
|
37
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
38
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
39
|
+
|
40
|
+
== Copyright
|
41
|
+
|
42
|
+
Copyright (c) 2011 Camilo Lopez. See LICENSE.txt for
|
43
|
+
further details.
|
44
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.version = '0.1.0'
|
18
|
+
gem.name = "svmredlight"
|
19
|
+
gem.homepage = "http://github.com/camilo/svmredlight"
|
20
|
+
gem.license = "MIT"
|
21
|
+
gem.summary = %Q{Ruby interface to SVMLight}
|
22
|
+
gem.description = %Q{Ruby interface to SVMLight}
|
23
|
+
gem.email = "camilo@camilolopez.com"
|
24
|
+
gem.authors = ["Camilo Lopez"]
|
25
|
+
# dependencies defined in Gemfile
|
26
|
+
end
|
27
|
+
Jeweler::RubygemsDotOrgTasks.new
|
28
|
+
|
29
|
+
require 'rake/testtask'
|
30
|
+
Rake::TestTask.new(:test) do |test|
|
31
|
+
test.libs << 'lib' << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
|
36
|
+
task :default => :test
|
37
|
+
|
38
|
+
require 'rake/rdoctask'
|
39
|
+
Rake::RDocTask.new do |rdoc|
|
40
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
41
|
+
|
42
|
+
rdoc.rdoc_dir = 'rdoc'
|
43
|
+
rdoc.title = "svmredlight #{version}"
|
44
|
+
rdoc.rdoc_files.include('README*')
|
45
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
46
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require './lib/svmredlight'
|
2
|
+
include SVMLight
|
3
|
+
|
4
|
+
# This is the canonical example referenced in [http://svmlight.joachims.org/]. This ruby
|
5
|
+
# version will read the included train file, generate a set of labeled documents and
|
6
|
+
# train a new model based on it.
|
7
|
+
#
|
8
|
+
# Then it will read the test set, classify every document and report how many documents in
|
9
|
+
# the training set were correctly classified and missed.
|
10
|
+
|
11
|
+
# Turn a line of the example data into an useful features array
|
12
|
+
def line_to_ary(line, has_target = false)
|
13
|
+
records = line.split(' ')
|
14
|
+
target = records.shift if has_target
|
15
|
+
|
16
|
+
ary = []
|
17
|
+
|
18
|
+
records.each_with_index do |record, i|
|
19
|
+
pos, value = record.split(':')
|
20
|
+
ary << [pos.to_i, value.to_f]
|
21
|
+
end
|
22
|
+
|
23
|
+
[ary, target]
|
24
|
+
end
|
25
|
+
|
26
|
+
# Quick sign function
|
27
|
+
def sign(x)
|
28
|
+
x >= 0 ? 1 : -1
|
29
|
+
end
|
30
|
+
|
31
|
+
puts "Reading training data..."
|
32
|
+
training_documents_and_labels = []
|
33
|
+
|
34
|
+
File.open('./examples/example1/train.dat', 'r').each_with_index do |line, i|
|
35
|
+
next if line.chars.first == '#'
|
36
|
+
ary, target = line_to_ary(line, true)
|
37
|
+
training_documents_and_labels << [Document.create(i + 1, 1, 0, 1, ary), target.to_i]
|
38
|
+
end
|
39
|
+
|
40
|
+
# Train a model here, you can play with the available learn_params
|
41
|
+
m = Model.new(:classification, # type
|
42
|
+
training_documents_and_labels, # set of labeled documents
|
43
|
+
{'biased_hyperplane' => true, 'svm_c' => 1.5}, # learn_params
|
44
|
+
{}, # kernel_params
|
45
|
+
nil) # alpha values
|
46
|
+
|
47
|
+
puts "New model created, with #{m.support_vectors_count} support vectors"
|
48
|
+
|
49
|
+
hits = 0
|
50
|
+
misses = 0
|
51
|
+
total = 0
|
52
|
+
|
53
|
+
# Try the new model out against the test file.
|
54
|
+
puts "Classifiying test data, correct classitication : :) incorrect : :( .\n"
|
55
|
+
File.open('./examples/example1/test.dat', 'r').each_with_index do |line, i|
|
56
|
+
next if line.chars.first == '#'
|
57
|
+
ary, target = line_to_ary(line, true)
|
58
|
+
next if ary.empty?
|
59
|
+
|
60
|
+
# Classify an unknown document using the model
|
61
|
+
result = m.classify(Document.create(i + 1, 1, 0, 1, ary))
|
62
|
+
|
63
|
+
total += 1
|
64
|
+
|
65
|
+
if sign(result) == target.to_i
|
66
|
+
print ':) '
|
67
|
+
hits += 1
|
68
|
+
else
|
69
|
+
print ':( '
|
70
|
+
misses += 1
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
puts "\nResults"
|
76
|
+
puts "Correctly classified #{hits} of #{total} examples in the test set."
|