From b90620de04da417f6738dd187343bbfce7fa0f74 Mon Sep 17 00:00:00 2001 From: Sawood Alam Date: Thu, 19 Jan 2017 00:06:34 -0500 Subject: [PATCH] Removed magic train untrain methods from docs, #140 (#141) --- README.markdown | 4 ++-- docs/bayes.md | 38 ++++++++++++++++++-------------------- docs/index.md | 6 +++--- 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/README.markdown b/README.markdown index 6e3058e..b7be748 100644 --- a/README.markdown +++ b/README.markdown @@ -24,8 +24,8 @@ $ gem install classifier-reborn $ irb irb(main):001:0> require 'classifier-reborn' irb(main):002:0> classifier = ClassifierReborn::Bayes.new 'Ham', 'Spam' -irb(main):003:0> classifier.train_ham "Sunday is a holiday. Say no to work on Sunday!" -irb(main):004:0> classifier.train_spam "You are the lucky winner! Claim your holiday prize." +irb(main):003:0> classifier.train "Ham", "Sunday is a holiday. Say no to work on Sunday!" +irb(main):004:0> classifier.train "Spam", "You are the lucky winner! Claim your holiday prize." irb(main):005:0> classifier.classify "What's the plan for Sunday?" #=> "Ham" ``` diff --git a/docs/bayes.md b/docs/bayes.md index 150a3fd..ac39030 100644 --- a/docs/bayes.md +++ b/docs/bayes.md @@ -15,8 +15,8 @@ Bayesian Classifiers are accurate, fast, and have modest memory requirements. require 'classifier-reborn' classifier = ClassifierReborn::Bayes.new 'Interesting', 'Uninteresting' -classifier.train_interesting "Here are some good words. I hope you love them." -classifier.train_uninteresting "Here are some bad words, I hate you." +classifier.train "Interesting", "Here are some good words. I hope you love them." +classifier.train "Uninteresting", "Here are some bad words, I hate you." classifier.classify "I hate bad words and you." #=> 'Uninteresting' classifier_snapshot = Marshal.dump classifier @@ -126,15 +126,13 @@ cat: That dang cat keeps scratching the furniture ``` If no categories are specified at initialization then `:auto_categorize` is set to `true` by default. -However, dynamic methods like `train_some_category` or `untrain_some_category` will not work unless corresponding categories exist. ```ruby require 'classifier-reborn' classifier = ClassifierReborn::Bayes.new -classifier.train("cat", "I can has cat") -# The above method will work, but the following will throw an error -# classifier.train_cat "I can has cat" +classifier.train("Cat", "I can has cat") +classifier.train("Dog", "I don't always bark at night") ``` ## Custom Stopwords @@ -205,15 +203,15 @@ Or suppose you just want the ability to have multiple categories and a `None of When you initialize the `ClassifierReborn::Bayes` classifier there are several options which can be set that control threshold processing. ```ruby -b = ClassifierReborn::Bayes.new( - 'good', # one or more categories - enable_threshold: true, # default: false - threshold: -10.0 # default: 0.0 - ) -b.train_good 'Good stuff from Dobie Gillis' +classifier = ClassifierReborn::Bayes.new( + "Good", # one or more categories + enable_threshold: true, # default: false + threshold: -10.0 # default: 0.0 + ) +classifier.train "Good", "Good stuff from Dobie Gillis" # ... -text = 'Bad junk from Maynard G. Krebs' -result = b.classify text +text = "Bad junk from Maynard G. Krebs" +result = classifier.classify text if result.nil? STDERR.puts "ALERT: This is not good: #{text}" let_loose_the_dogs_of_war! # method definition left to the reader @@ -226,12 +224,12 @@ When you see a nil value returned from the `classify` method it means that none ### Threshold-related Convenience Methods ```ruby -b.threshold # get the current threshold -b.threshold = -10.0 # set the threshold -b.threshold_enabled? # Boolean: is the threshold enabled? -b.threshold_disabled? # Boolean: is the threshold disabled? -b.enable_threshold # enables threshold processing -b.disable_threshold # disables threshold processing +classifier.threshold # get the current threshold +classifier.threshold = -10.0 # set the threshold +classifier.threshold_enabled? # Boolean: is the threshold enabled? +classifier.threshold_disabled? # Boolean: is the threshold disabled? +classifier.enable_threshold # enables threshold processing +classifier.disable_threshold # disables threshold processing ``` Using these convenience methods your applications can dynamically adjust threshold processing as required. diff --git a/docs/index.md b/docs/index.md index 76b83e0..60a38dc 100644 --- a/docs/index.md +++ b/docs/index.md @@ -16,8 +16,8 @@ $ gem install classifier-reborn $ irb irb(main):001:0> require 'classifier-reborn' irb(main):002:0> classifier = ClassifierReborn::Bayes.new 'Ham', 'Spam' -irb(main):003:0> classifier.train_ham "Sunday is a holiday. Say no to work on Sunday!" -irb(main):004:0> classifier.train_spam "You are the lucky winner! Claim your holiday prize." +irb(main):003:0> classifier.train "Ham", "Sunday is a holiday. Say no to work on Sunday!" +irb(main):004:0> classifier.train "Spam", "You are the lucky winner! Claim your holiday prize." irb(main):005:0> classifier.classify "What's the plan for Sunday?" #=> "Ham" ``` @@ -26,7 +26,7 @@ Here is a line-by-line explanation of what we just did. * Installed the `classifier-reborn` gem (assuming that [Ruby](https://www.ruby-lang.org/en/) is installed already). * Started the Interactive Ruby Shell (IRB). -* Loaded the `classifier-reborn` gem in the interactive Ruby session. +* Loaded the `classifier-reborn` gem in the interactive Ruby session. * Created an instance of `Bayesian` classifier with two classes `Ham` and `Spam`. * Trained the classifier with an example of `Ham`. * Trained the classifier with an example of `Spam`.