Class: Weka::Core::Attribute

Inherits:
Object
  • Object
show all
Includes:
Weka::Concerns::Persistent
Defined in:
lib/weka/core/attribute.rb

Constant Summary collapse

TYPES =
%i[numeric nominal string date].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Weka::Concerns::Persistent

included

Class Method Details

.new_date(name, format) ⇒ Object



21
22
23
# File 'lib/weka/core/attribute.rb', line 21

def new_date(name, format)
  new(name.to_s, format.to_s)
end

.new_nominal(name, values) ⇒ Object



17
18
19
# File 'lib/weka/core/attribute.rb', line 17

def new_nominal(name, values)
  new(name.to_s, Array(values).map(&:to_s))
end

.new_numeric(name) ⇒ Object



13
14
15
# File 'lib/weka/core/attribute.rb', line 13

def new_numeric(name)
  new(name.to_s)
end

.new_string(name) ⇒ Object

Creates a new Attribute instance of type string.

The java class defines the same constructor: Attribute(java.lang.String, java.util.List<java.lang.String>) for nominal and string attributes and handles the type internally based on the second argument.

In Java you would write following code to create a string Attribute:

Attribute attribute = new Attribute("name", (FastVector) null);

When we use a similar approach in JRuby:

attribute = Attribute.new('name', nil)

then a Java::JavaLang::NullPointerException is thrown.

Thus, we use refelection here and call the contructor explicitly, see github.com/jruby/jruby/wiki/CallingJavaFromJRuby#constructors

The object returned from Java constructor only has class Java::JavaObject so we need to cast it to the proper class

See also: stackoverflow.com/questions/1792495/casting-objects-in-jruby



48
49
50
51
52
53
54
55
# File 'lib/weka/core/attribute.rb', line 48

def new_string(name)
  constructor = Attribute.java_class.declared_constructor(
    java.lang.String,
    java.util.List
  )

  constructor.new_instance(name.to_s, nil).to_java(Attribute)
end

Instance Method Details

#internal_value_of(value) ⇒ Object

The order of the if statements is important here, because a date is also a numeric.



73
74
75
76
77
78
79
# File 'lib/weka/core/attribute.rb', line 73

def internal_value_of(value)
  return value                      if value.respond_to?(:nan?) && value.nan?
  return Float::NAN                 if [nil, '?'].include?(value)
  return parse_date(value.to_s)     if date?
  return value.to_f                 if numeric?
  return index_of_value(value.to_s) if nominal? || string?
end

#typeObject

Returns the string representation of the attribute’s type. Overwrites the weka.core.Attribute type Java method, which returns an integer representation of the type based on the defined type constants.



66
67
68
# File 'lib/weka/core/attribute.rb', line 66

def type
  self.class.type_to_string(self)
end

#valuesObject



58
59
60
# File 'lib/weka/core/attribute.rb', line 58

def values
  enumerate_values.to_a
end