Sha256: 42f72eb85ec21bab86f1eb371e19851e61c333fe0c78f244aa5db5c1ffd5e354

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

require 'rb-libsvm/version'
require 'rb-libsvm/libsvm'

module Libsvm

  module CoreExtensions
    module Collection
      def to_example
        Node.features(self)
      end
    end
  end

  class Node

    class << self

      def features(*vargs)
        array_of_nodes = []
				if vargs.size == 1
					if vargs.first.class == Array
						vargs.first.each_with_index do |value, index|
							array_of_nodes << Node.new(index.to_i, value.to_f)
						end
					elsif vargs.first.class == Hash
						vargs.first.each do |index, value|
							array_of_nodes << Node.new(index.to_i, value.to_f)
						end
					else
						raise(ArgumentError.new("Node features need to be a Hash, Array or Floats"))
					end
				else
					vargs.each_with_index do |value, index|
						array_of_nodes << Node.new(index.to_i, value.to_f)
					end
				end
				array_of_nodes
      end

      def [](index, value)
        new(index, value)
      end
    end

    def initialize(index=0, value=0.0)
      self.index = index
      self.value = value
    end

    def ==(other)
      index == other.index && value == other.value
    end
  end

end


class Hash
  include Libsvm::CoreExtensions::Collection
end
class Array
  include Libsvm::CoreExtensions::Collection
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rb-libsvm-1.0.5 lib/libsvm.rb
rb-libsvm-1.0.1 lib/libsvm.rb