Class: RedditPostClassifierBot::NBayesClassifier

Inherits:
Object
  • Object
show all
Defined in:
lib/RedditPostClassifierBot/nbayes_classifier.rb

Constant Summary collapse

NBAYES_FILE =
ENV.fetch "NBAYES_FILE_PATH", "./RPCB-nbayes.yml"

Instance Method Summary collapse

Constructor Details

#initializeNBayesClassifier

Returns a new instance of NBayesClassifier.



5
6
7
# File 'lib/RedditPostClassifierBot/nbayes_classifier.rb', line 5

def initialize
  @nbayes = NBayes::Base.new
end

Instance Method Details

#classify(subreddit, title, post) ⇒ Object



14
15
16
# File 'lib/RedditPostClassifierBot/nbayes_classifier.rb', line 14

def classify(subreddit, title, post)
  @nbayes.classify tokenize("#{subreddit}\n#{title}\n#{post}")
end

#dumpObject



18
19
20
# File 'lib/RedditPostClassifierBot/nbayes_classifier.rb', line 18

def dump
  @nbayes.dump NBAYES_FILE
end

#loadObject



22
23
24
25
26
27
28
# File 'lib/RedditPostClassifierBot/nbayes_classifier.rb', line 22

def load
  @nbayes.load NBAYES_FILE
  true
rescue Errno::ENOENT, NoMethodError
  FileUtils.touch NBAYES_FILE unless File.exists?(NBAYES_FILE)
  false
end

#train(text, classification) ⇒ Object



9
10
11
12
# File 'lib/RedditPostClassifierBot/nbayes_classifier.rb', line 9

def train(text, classification)
  @nbayes.train tokenize(text), classification
  self
end