Text classifier in Ruby that uses Hadoop/HBase, Mongo, or Cassandra for storage. New location for http://github.com/livingsocial/ankusa
Text classifier in Ruby that uses Hadoop/HBase, Mongo, or Cassandra for storage. New location for http://github.com/livingsocial/ankusa
= ankusa {}[https://travis-ci.org/bmuller/ankusa] {}[https://gemnasium.com/bmuller/ankusa]
Ankusa is a text classifier in Ruby that can use either Hadoop's HBase, Mongo, or Cassandra for storage. Because it uses HBase/Mongo/Cassandra as a backend, the training corpus can be many terabytes in size (though additional memory and single file storage abilities also exist for smaller corpora).
Ankusa currently provides both a Naive Bayes and Kullback-Leibler divergence classifier. It ignores common words (a.k.a, stop words) and stems all others. Additionally, it uses additive smoothing in both classification methods.
== Installation Add this line to your application's Gemfile:
gem 'ankusa'
Ensure that if you're using the HBase, Cassandra, or MongoDB backends that you also add the correct dependency gem to your Gemfile:
gem 'hbaserb'
gem 'cassandra'
gem 'mongo'
If you're using HBase, make sure the HBase Thrift interface has been started as well.
== Basic Usage Using the naive Bayes classifier:
require 'ankusa' require 'ankusa/hbase_storage'
storage = Ankusa::HBaseStorage.new 'localhost' c = Ankusa::NaiveBayesClassifier.new storage
c.train :spam, "This is some spammy text" c.train :good, "This is not the bad stuff"
puts c.classify "This is some spammy text"
puts c.classifications "This is some spammy text"
puts c.log_likelihoods "This is some spammy text"
puts c.classnames
storage.close
== KL Diverence Classifier There is a Kullback–Leibler divergence classifier as well. KL divergence is a distance measure (though not a true metric because it does not satisfy the triangle inequality). The KL classifier simply measures the relative entropy between the text you want to classify and each of the classes. The class with the shortest "distance" is the best class. You may find that for a especially large corpus it may be slightly faster to use this classifier (since prior probablities are never calculated, only likelihoods).
The API is the same as the NaiveBayesClassifier, except rather than calling "classifications" if you want actual numbers you call "distances".
require 'ankusa' require 'ankusa/hbase_storage'
storage = Ankusa::HBaseStorage.new 'localhost' c = Ankusa::KLDivergenceClassifier.new storage
c.train :spam, "This is some spammy text" c.train :good, "This is not the bad stuff"
puts c.classify "This is some spammy text"
puts c.distances "This is some spammy text"
puts c.classnames
storage.close
== Storage Methods Ankusa has a generalized storage interface that has been implemented for HBase, Cassandra, Mongo, single file, and in-memory storage.
Memory storage can be used when you have a very small corpora
require 'ankusa/memory_storage' storage = Ankusa::MemoryStorage.new
FileSystem storage can be used when you have a very small corpora and want to persist the classification results.
require 'ankusa/file_system_storage' storage = Ankusa::FileSystemStorage.new '/path/to/file'
storage.save
The FileSystem storage does NOT save to the filesystem automatically, the #save method must be invoked to save and persist the results
HBase storage:
require 'ankusa/hbase_storage'
storage = Ankusa::HBaseStorage.new host, port, frequency_tablename, summary_tablename
For Cassandra storage:
To use the Cassandra storage class:
require 'ankusa/cassandra_storage'
storage = Ankusa::CassandraStorage.new host, port, keyspace, max_classes
For MongoDB storage:
require 'ankusa/mongo_db_storage' storage = Ankusa::MongoDbStorage.new :host => "localhost", :port => 27017, :db => "ankusa"
== Running Tests You can run the tests for any of the four storage methods. For instance, for memory storage: rake test_memory
For the other methods you will need to edit the file test/config.yml and set the configuration params. Then: rake test_hbase
rake test_cassandra
rake test_filesystem #or rake test_mongo_db
No open issues yet, or sync has not completed.