JnbClassifier
Naive Bayes classifier for japanse text.
Description
JnbClassifier helps you classify japanese text.
You need Morphological Analysis Tool, Mecab and the dictionary to use JnbClassifier.
If you use linux(ubuntu), you can install Mecab and the dictionary as follows.
1:Mecab
download site: https://drive.google.com/uc?export=download&id=0B4y35FiV1wh7cENtOXlicTFaRUE
$ cd mecab-X.X
$ ./configure
$ make
$ make check
$ sudo make install
2:the dictionary for Mecab
download site: https://drive.google.com/uc?export=download&id=0B4y35FiV1wh7MWVlSDBCSXZMTXM
$ tar zxfv mecab-ipadic-2.7.0-XXXX.tar.gz
$ cd mecab-ipadic-2.7.0-XXXX
$ ./configure --with-charset=utf8
$ make
$ sudo make install
Installation
Add this line to your application's Gemfile
gem 'jnb_classifier'
And then execute:
$ bundle
Or install it yourself as:
$ gem install jnb_classifier
Usage
1: Generate Document(label, attributes) from text
First you need create documents which neeed two parameters: category name and text.
document1 = JnbClassifier::Document.new('hayao','私の住んでいる場所は風の谷ではありません。')
#=> @label="hayao", @attributes={"私"=>1, "場所"=>1, "風"=>1, "谷"=>1}
document2 = JnbClassifier::Document.new('hayao','私は魔女の宅急便が大好きです')
#=> @label="hayao", @attributes={"私"=>1, "魔女"=>1, "宅急便"=>1, "大好き"=>1}
document3 = JnbClassifier::Document.new('hujiko','僕、ドラえもん。')
#=> @label="hujiko", @attributes={"僕"=>1, "ドラえもん"=>1}
2: Learn documents
Your classifier can learn from documents you created.
And the more you give apropriate samples, the smarter your classifier will be.
nb = nb = JnbClassifier::Classifier.new
mb.learn(document1)
mb.learn(document2)
mb.learn(document3)
3:Classify
When you input attributes(hash), ,classify method return the category which is selected by Naive Bayes.
nb.classify({"私"=>1, "魔女"=>1, "宅急便"=>1} )
#=> "hayao"
nb.result
#=> {"hayao"=>-16.72902719126572, "hujiko"=>-20.435583764494627} # the values is logarithm.
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/jnb_classifier. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the JnbClassifier project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.