class TinyClassifier::Command::Base

Attributes

classifier[W]
tokenizer[R]

Public Class Methods

new(argv=[]) click to toggle source
# File lib/tiny-classifier/command/base.rb, line 38
def initialize(argv=[])
  @categories = nil
  @tokenizer = Tokenizer.new
  @data_dir = Dir.pwd
  @verbose = false
end
run(argv=nil) click to toggle source
# File lib/tiny-classifier/command/base.rb, line 28
def run(argv=nil)
  argv ||= ARGV.dup
  command = new(argv)
  command.run
end

Public Instance Methods

classifier() click to toggle source
# File lib/tiny-classifier/command/base.rb, line 53
def classifier
  @classifier ||= prepare_classifier
end
data_file_name() click to toggle source
# File lib/tiny-classifier/command/base.rb, line 57
def data_file_name
  "tc.#{@categories.basename}.dat"
end
data_file_path() click to toggle source
# File lib/tiny-classifier/command/base.rb, line 61
def data_file_path
  @data_file_path ||= prepare_data_file_path
end
parse_command_line_options(command_line_options) click to toggle source
# File lib/tiny-classifier/command/base.rb, line 49
def parse_command_line_options(command_line_options)
  option_parser.parse!(command_line_options)
end
run() click to toggle source
# File lib/tiny-classifier/command/base.rb, line 45
def run
  raise NoCategories.new unless @categories
end

Private Instance Methods

create_option_parser() click to toggle source
# File lib/tiny-classifier/command/base.rb, line 70
def create_option_parser
  parser = OptionParser.new

  parser.on("-d PATH", "--data-dir=PATH",
            "Path to the directory to store training data file (default=current directory)") do |data_dir|
    @data_dir = data_dir
  end

  parser.on("-c CATEGORIES", "--categories=CATEGORIES",
            "List of categories (comma-separated)") do |categories|
    @categories = CategoryManager.new(categories)
  end

  parser.on("-t TOKENIZER", "--tokenizer=TOKENIZER",
            "Tokenizer (default=#{@tokenizer})") do |tokenizer|
    @tokenizer.type = tokenizer
  end

  parser.on("-v", "--verbose",
            "Output internal information (for debugging)") do |verbose|
    @verbose = verbose
  end

  parser
end
error(message) click to toggle source
# File lib/tiny-classifier/command/base.rb, line 153
def error(message)
  $stderr.puts(message)
end
handle_error(error) click to toggle source
# File lib/tiny-classifier/command/base.rb, line 143
def handle_error(error)
  case error
  when TinyClassifierError
    error(error.message)
  else
    error(error.inspect)
  end
  false
end
input() click to toggle source
# File lib/tiny-classifier/command/base.rb, line 119
def input
  @input ||= prepare_input
end
log(message) click to toggle source
# File lib/tiny-classifier/command/base.rb, line 157
def log(message)
  $stderr.puts(message) if @verbose
end
option_parser() click to toggle source
# File lib/tiny-classifier/command/base.rb, line 66
def option_parser
  @option_parser ||= create_option_parser
end
prepare_category(category) click to toggle source
# File lib/tiny-classifier/command/base.rb, line 132
def prepare_category(category)
  raise NoCategory.new unless category

  category = @categories.normalize(category)

  unless @categories.valid?(category)
    raise InvalidCategory.new(category, @categories.all)
  end
  category
end
prepare_classifier() click to toggle source
# File lib/tiny-classifier/command/base.rb, line 103
def prepare_classifier
  if data_file_path.exist?
    data = File.read(data_file_path.to_s)
    Marshal.load(data)
  else
    ClassifierReborn::Bayes.new(*@categories.all)
  end
end
prepare_data_file_path() click to toggle source
# File lib/tiny-classifier/command/base.rb, line 96
def prepare_data_file_path
  path = Pathname(@data_dir)
  path += data_file_name
  log("file: #{path}")
  path
end
prepare_input() click to toggle source
# File lib/tiny-classifier/command/base.rb, line 123
def prepare_input
  input = Input.new
  raise NoInput.new unless input.given?
  tokenized = @tokenizer.tokenize(input.read)
  log("tokenizer: #{@tokenizer.type}")
  log("tokenized: #{tokenized}")
  tokenized
end
save() click to toggle source
# File lib/tiny-classifier/command/base.rb, line 112
def save
  data = Marshal.dump(classifier)
  File.open(data_file_path, "w") do |file|
    file.write(data)
  end
end