#!/usr/bin/env ruby class Classifier attr_accessor :CrmPath, :ClassificationType, :ClassificationExtension, :LearnCommand, :ClassifyCommand def initialize(path, categories) @path = path @categories = categories @CrmPath = "crm" @ClassificationType = "" @ClassificationExtension = ".css" @LearnCommand = " '-{ learn %s ( %s ) }'" @ClassifyCommand = " '-{ isolate (:stats:); classify %s ( %s ) (:stats:); match [:stats:] (:: :best: :prob:) /Best match to file .. \\(%s\\/([[:graph:]]+)\\%s\\) prob: ([0-9.]+)/; output /:*:best:\\t:*:prob:/ }'" makeFiles end def learn(category, text) command = @CrmPath + (@LearnCommand % [@ClassificationType, @path + '/' + category + @ClassificationExtension]) pipe = IO.popen(command, "w") pipe.write(text) pipe.close end def classify(text) command = @CrmPath + ( @ClassifyCommand % [@ClassificationType, getFileListString, @path.gsub('/', '\/'), @ClassificationExtension] ) list = String.new IO.popen(command, "w+") { |pipe| pipe.write(text) pipe.close_write list = pipe.readline.split pipe.close } if list.empty? then return [ '', 0.0 ] else category = list[0] probability = list[1] return [ category, probability ] end end def makeFiles Dir.mkdir(@path) if not File.exist?(@path) @categories.each { |category| learn( category, '' ) } end def getFileList @categories.collect { |category| @path + '/' + category + @ClassificationExtension } end def getFileListString return getFileList.join(' ') end def test puts self.getFileList learn( "good", "this is a test" ) learn( "bad", "this is very bad" ) cls, prob = classify("this is a test") puts "class was: %s, prob was: %f" % [ cls, prob ] end end c = Classifier.new("/tmp", [ "good", "bad" ] ) c.test