Reclassifier
Reclassifier is a gem that provides classification of strings.
Classification can be done via Naïve Bayes or Latent Semantic Indexing.
It is a fork of the original Classifier gem, which appears to be unmaintained as of a couple of years ago.
Installation
Add this line to your application's Gemfile:
gem 'reclassifier'
And then execute:
$ bundle
Or install it yourself as:
$ gem install reclassifier
Dependencies
Currently you need to install the GNU GSL library in order to use Reclassifier: http://www.gnu.org/software/gsl
Usage
Bayes
Bayesian Classifiers are accurate, fast, and have modest memory requirements.
Usage
require 'reclassifier'
b = Reclassifier::Bayes.new ['interesting', 'uninteresting']
b.train 'interesting', 'here are some good words. I hope you love them'
b.train 'uninteresting', 'here are some bad words, I hate you'
puts b.classify 'I hate bad words and you' # returns 'uninteresting'
m = SnapshotMadeleine.new("bayes_data") {
Reclassifier::Bayes.new ['interesting', 'uninteresting']
}
m.system.train 'interesting', 'here are some good words. I hope you love them'
m.system.train 'uninteresting', 'here are some bad words, I hate you'
m.take_snapshot
puts m.system.classify 'I love you' # returns 'interesting'
Using Madeleine, your application can persist the learned data over time. If you get a "Sanity check failed for file IO" error, you may need to update Madeleine to the latest version available from GitHub.
LSI
Latent Semantic Indexing engines are not as fast or as small as Bayesian classifiers, but are more flexible, providing fast search and clustering detection as well as semantic analysis of the text that theoretically simulates human learning.
Usage
require 'reclassifier'
lsi = Reclassifier::LSI.new
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 ]]
strings.each {|x| lsi.add_item x.first, x.last}
lsi.search("dog", 3)
# returns => ["This text deals with dogs. Dogs.", "This text involves dogs too. Dogs! ",
# "This text also involves cats. Cats!"]
lsi.find_related(strings[2], 2)
# returns => ["This text revolves around cats. Cats.", "This text also involves cats. Cats!"]
lsi.classify "This text is also about dogs!"
# returns => :dog
Please see the Reclassifier::LSI documentation for more information. It is possible to index, search and classify with more than just simple strings.
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
License
This library is released under the terms of the GNU LGPL. See LICENSE for more details.