class Forematter::Commands::Classify
Public Instance Methods
run()
click to toggle source
# File lib/forematter/commands/classify.rb, line 28 def run load_classifier puts 'Getting categories' categories_for(files_with(field)).each { |cat| bayes.add_category(cat) } if bayes.categories.empty? $stderr.puts "No categories found in #{field}, unable to classify" exit 1 else found = bayes.categories.length puts "#{found} #{found == 1 ? 'category' : 'categories'} found" end puts 'Training classifier' files_with(field).each { |file| train(file) } puts 'Classifying files' files_to_classify.each { |file| file.write if classify(file) } end
Protected Instance Methods
bayes()
click to toggle source
# File lib/forematter/commands/classify.rb, line 58 def bayes @bayes ||= Classifier::Bayes.new end
categories_for(files)
click to toggle source
# File lib/forematter/commands/classify.rb, line 62 def categories_for(files) files .map { |file| file[field].to_ruby } .select { |f| f.is_a?(String) } .uniq .map(&:to_sym) end
classify(file)
click to toggle source
# File lib/forematter/commands/classify.rb, line 85 def classify(file) old = file[field].to_ruby file[field] = bayes.classify(file.content).to_s file[field].to_ruby != old end
files_to_classify()
click to toggle source
# File lib/forematter/commands/classify.rb, line 70 def files_to_classify files.reject do |file| file.key?(field) unless options[:override] end end
load_classifier()
click to toggle source
# File lib/forematter/commands/classify.rb, line 51 def load_classifier require 'classifier' rescue LoadError $stderr.puts 'Install "classifier" gem to generate suggestions' exit 1 end
train(file)
click to toggle source
# File lib/forematter/commands/classify.rb, line 76 def train(file) val = file[field].to_ruby unless val.is_a?(String) skip file, "unable to train, #{field} is not a string" return end bayes.train(val.to_sym, file.content) end