Class: Treat::Learning::Export

Inherits:
Object
  • Object
show all
Defined in:
lib/treat/learning/export.rb

Overview

Represents a feature to be used in a classification task.

Direct Known Subclasses

Feature, Tag

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, default = nil, proc_string = nil) ⇒ Export

Initialize a feature for a classification problem.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/treat/learning/export.rb', line 20

def initialize(name, default = nil, proc_string = nil)
  unless name.is_a?(Symbol)
    raise Treat::Exception,
    "The first argument to initialize should "+
    "be a symbol representing the name of the export."
  end
  if proc_string && !proc_string.is_a?(String)
    raise Treat::Exception,
    "The third argument to initialize, " +
    "if supplied, should be a string that " +
    "can be evaluated to yield a Proc."
  end
  @name, @default, @proc_string =
  name, default, proc_string
  begin
    @proc = proc_string ? 
    eval(proc_string) : nil
  rescue Exception => e
    raise Treat::Exception,
    "The third argument to initialize " +
    "did not evaluate without errors " +
    "(#{e.message})."
  end
  if @proc && !@proc.is_a?(Proc)
    raise Treat::Exception,
    "The third argument did not evaluate to a Proc."
  end
end

Instance Attribute Details

#defaultObject (readonly)

The feature’s default value, if nil.



12
13
14
# File 'lib/treat/learning/export.rb', line 12

def default
  @default
end

#nameObject (readonly)

The name of the feature. If no proc is supplied, this assumes that the target of your classification problem responds to the method corresponding to this name.



10
11
12
# File 'lib/treat/learning/export.rb', line 10

def name
  @name
end

#procObject

A proc that can be used to perform calculations before storing a feature.



15
16
17
# File 'lib/treat/learning/export.rb', line 15

def proc
  @proc
end

#proc_stringObject

The proc as a string value.



17
18
19
# File 'lib/treat/learning/export.rb', line 17

def proc_string
  @proc_string
end

Instance Method Details

#==(feature) ⇒ Object

Custom comparison operator for features.



50
51
52
53
54
# File 'lib/treat/learning/export.rb', line 50

def ==(feature)
  @name == feature.name &&
  @default == feature.default &&
  @proc_string == feature.proc_string
end