Skip to content
This repository has been archived by the owner on Sep 12, 2018. It is now read-only.

Commit

Permalink
Modernized version number conventions and added YARD type annotations.
Browse files Browse the repository at this point in the history
  • Loading branch information
artob committed Dec 26, 2009
1 parent 6742692 commit f94d3b8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
35 changes: 27 additions & 8 deletions lib/crm114.rb
@@ -1,33 +1,40 @@
# Author:: Arto Bendiken (mailto:arto.bendiken@gmail.com)
# License:: Public domain
require 'crm114/version'

module Classifier

class CRM114

VERSION = '1.0.2'

CLASSIFICATION_TYPE = '<osb unique microgroom>'
FILE_EXTENSION = '.css'
CMD_CRM = '/usr/bin/env crm'
OPT_LEARN = '-{ learn %s ( %s ) }'
OPT_CLASSIFY = '-{ isolate (:stats:); classify %s ( %s ) (:stats:); match [:stats:] (:: :best: :prob:) /Best match to file .. \\(%s\\/([[:graph:]]+)\\%s\\) prob: ([0-9.]+)/; output /:*:best:\\t:*:prob:/ }'

##
# Returns a string containg the installed CRM114 engine version in a
# format such as "20060118-BlameTheReavers".
#
# @return [String, nil]
def self.version
$1 if IO.popen(CMD_CRM + ' -v', 'r') { |pipe| pipe.readline } =~ /CRM114, version ([\d\w\-\.]+)/
end

##
# Returns a new CRM114 classifier defined by the given _categories_.
#
# @param [Array<#to_s>] categories
# @option options [String] :path ('.')
def initialize(categories, options = {})
@categories = categories.to_a.collect { |category| category.to_s.to_sym }
@path = File.expand_path(options[:path] || '.')
@debug = options[:debug] || false
end

##
# Trains the classifier to consider the given _text_ to be a sample from
# the set named by _category_.
#
# @param [#to_s] category
# @param [String] text
# @return [void]
def learn!(category, text, &block)
cmd = CMD_CRM + " '" + (OPT_LEARN % [CLASSIFICATION_TYPE, css_file_path(category)]) + "'"
puts cmd if @debug
Expand All @@ -36,15 +43,22 @@ def learn!(category, text, &block)

alias_method :train!, :learn!

##
# @raise NotImplementedError
# @return [void]
def unlearn!(category, text, &block) # :nodoc:
raise 'unlearning not supported at present'
raise NotImplementedError.new('unlearning not supported at present')
end

alias_method :untrain!, :unlearn! #:nodoc:

##
# Returns the classification of the provided _text_ as a tuple
# containing the highest-probability category and a confidence indicator
# in the range of 0.5..1.0.
#
# @param [String] text
# @return [Array(Symbol, Float)]
def classify(text = nil, &block)
files = @categories.collect { |category| css_file_path(category) }
cmd = CMD_CRM + " '" + (OPT_CLASSIFY % [CLASSIFICATION_TYPE, files.join(' '), @path.gsub(/\//, '\/'), FILE_EXTENSION]) + "'"
Expand Down Expand Up @@ -73,15 +87,20 @@ def method_missing(symbol, *args) # :nodoc:

protected

##
# @param [String] file
# @return [void]
def self.create_css_file(file)
cmd = CMD_CRM + " '" + (OPT_LEARN % [CLASSIFICATION_TYPE, file]) + "'"
IO.popen(cmd, 'w') { |pipe| pipe.close }
end

##
# @param [#to_s] category
# @return [String]
def css_file_path(category)
File.join(@path, category.to_s + FILE_EXTENSION)
end

end

end
19 changes: 19 additions & 0 deletions lib/crm114/version.rb
@@ -0,0 +1,19 @@
module Classifier class CRM114
module VERSION
MAJOR = 1
MINOR = 0
TINY = 2
EXTRA = nil

STRING = [MAJOR, MINOR, TINY].join('.')
STRING << "-#{EXTRA}" if EXTRA

##
# @return [String]
def self.to_s() STRING end

##
# @return [String]
def self.to_str() STRING end
end
end end

0 comments on commit f94d3b8

Please sign in to comment.