Created
December 30, 2009 19:01
-
-
Save rjungemann/266288 to your computer and use it in GitHub Desktop.
How to use the Remember gem with Classifier
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# An example showing how to use Remember (http://github.com/jpignata/remember) | |
# to serialize a bayes classifier made with the "classifier" gem. This would | |
# be useful to share a Bayes classifier across server instances. | |
# | |
# This seems to be similar, but more rudimentary, to functionality found in | |
# Maglev and jruby-maglev, the ability to share globals across interpreters. | |
require 'rubygems' | |
require 'xattr' | |
require 'moneta' | |
require 'moneta/file' | |
require 'classifier' | |
require File.join(File.dirname(__FILE__), '../vendor/remember/lib/remember') | |
r = Remember.new(Moneta::File, :path => File.join(File.dirname(__FILE__), '../tmp')) | |
unless r["bayes"] | |
puts "Initializing r['bayes']" | |
r["bayes"] = Classifier::Bayes.new 'Interesting', 'Uninteresting' | |
bayes = r["bayes"] | |
bayes.train_interesting "here are some good words. I hope you love them" | |
bayes.train_uninteresting "here are some bad words, I hate you" | |
bayes.train_interesting "here are some good words. I hope you love them" | |
bayes.train_uninteresting "here are some bad words, I hate you" | |
r["bayes"] = bayes | |
end | |
bayes = r["bayes"] | |
puts "\nBayes says:\n\n" | |
puts 'bayes.classify "I love you"' | |
puts bayes.classify "I love you" | |
puts 'bayes.classify "I hate bad words and you"' | |
puts bayes.classify "I hate bad words and you" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'xattr' | |
require 'moneta' | |
require 'moneta/file' | |
require 'classifier' | |
require File.join(File.dirname(__FILE__), '../vendor/remember/lib/remember') | |
r = Remember.new(Moneta::File, :path => File.join(File.dirname(__FILE__), '../tmp')) | |
$strings = [ | |
["This text deals with dogs. Dogs.", :dog], | |
["This text involves dogs too. Dogs! ", :dog], | |
["This text revolves around cats. Cats.", :cat], | |
["This text also involves cats. Cats!", :cat], | |
["This text involves birds. Birds.",:bird ] | |
] | |
unless r["lsi"] | |
puts "Initializing r['lsi']" | |
r["lsi"] = Classifier::LSI.new | |
lsi = r["lsi"] | |
$strings.each { |x| lsi.add_item x.first, x.last } | |
r["lsi"] = lsi | |
end | |
lsi = r["lsi"] | |
puts "\nLSI says:\n\n" | |
puts 'lsi.search("dog", 3)' | |
puts lsi.search("dog", 3) | |
print "\n" | |
puts 'lsi.find_related($strings[2], 2)' | |
puts lsi.find_related($strings[2], 2) | |
print "\n" | |
puts 'lsi.classify "This text is also about dogs!"' | |
puts lsi.classify "This text is also about dogs!" | |
print "\n" | |
print "\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment