require 'ai4r' Object::instance_eval do def acts_as_cabalist(options = {}) # == Set defaults ======================================================= # collection = options[:collection] || :all algorithm = options[:algorithm] || :id3 # == END ================================================================ # # == Determine a classifier ============================================= # classifier = case algorithm when :hyperpipes then Ai4r::Classifiers::Hyperpipes when :ib1 then Ai4r::Classifiers::IB1 when :id3 then Ai4r::Classifiers::ID3 when :one_r then Ai4r::Classifiers::OneR when :prism then Ai4r::Classifiers::Prism when :zero_r then Ai4r::Classifiers::ZeroR else raise 'Unknown algorithm provided' end # == END ================================================================ # # == Set classification methods ========================================= # get_signals_proc = Proc::new do options[:fields].map do |field| field.is_a?(Proc) ? field.call(self) : self.send(field) end end get_category_proc = Proc::new do self.send(options[:category]) end set_category_proc = Proc::new do |val| self.send("#{options[:category]}=".to_sym, val) self end send(:define_method, 'get_signals'.to_sym, &get_signals_proc) send(:define_method, 'get_category'.to_sym, &get_category_proc) send(:define_method, 'set_category'.to_sym, &set_category_proc) proc = Proc::new do data_items = send(collection).map do |el| el.get_signals.push(el.get_category) end data_set = Ai4r::Data::DataSet::new({ :data_items => data_items }) classifier::new.build(data_set) end # == END ================================================================ # # == Register classifier ================================================ # Cabalist::Manager.register_classifier(self, proc) # == END ================================================================ # end end