Class: Desiru::Core::Prediction

Inherits:
Object
  • Object
show all
Defined in:
lib/desiru/core/prediction.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(example = nil, **kwargs) ⇒ Prediction

Returns a new instance of Prediction.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/desiru/core/prediction.rb', line 8

def initialize(example = nil, **kwargs)
  @example = example || Example.new
  @completions = {}
   = {}

  kwargs.each do |key, value|
    if key == :completions
      @completions = value
    elsif key == :metadata
       = value
    else
      @completions[key] = value
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/desiru/core/prediction.rb', line 69

def method_missing(method_name, *args, &)
  if method_name.to_s.end_with?('=')
    key = method_name.to_s.chop.to_sym
    self[key] = args.first
  elsif @completions.key?(method_name)
    @completions[method_name]
  elsif @example.respond_to?(method_name)
    @example.send(method_name, *args, &)
  else
    super
  end
end

Instance Attribute Details

#completionsObject (readonly)

Returns the value of attribute completions.



6
7
8
# File 'lib/desiru/core/prediction.rb', line 6

def completions
  @completions
end

#exampleObject (readonly)

Returns the value of attribute example.



6
7
8
# File 'lib/desiru/core/prediction.rb', line 6

def example
  @example
end

Class Method Details

.from_example(example) ⇒ Object

Class method to create a Prediction from an Example



103
104
105
# File 'lib/desiru/core/prediction.rb', line 103

def self.from_example(example)
  new(example)
end

Instance Method Details

#==(other) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/desiru/core/prediction.rb', line 89

def ==(other)
  return false unless other.is_a?(self.class)

  @completions == other.completions &&
    @example == other.example &&
     == other.instance_variable_get(:@metadata)
end

#[](key) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/desiru/core/prediction.rb', line 24

def [](key)
  if @completions.key?(key)
    @completions[key]
  elsif @example
    # First check the raw data in the example
    @example[key] ||
      # Then check the inputs and labels
      @example.inputs[key] ||
      @example.labels[key]
  end
end

#[]=(key, value) ⇒ Object



36
37
38
# File 'lib/desiru/core/prediction.rb', line 36

def []=(key, value)
  @completions[key] = value
end

#get(key, default = nil) ⇒ Object



40
41
42
# File 'lib/desiru/core/prediction.rb', line 40

def get(key, default = nil)
  @completions.fetch(key) { @example[key] || default }
end

#inspectObject



97
98
99
100
# File 'lib/desiru/core/prediction.rb', line 97

def inspect
  "#<#{self.class.name} completions=#{@completions.inspect} " \
    "example=#{@example.inspect} metadata=#{@metadata.inspect}>"
end

#keysObject



44
45
46
# File 'lib/desiru/core/prediction.rb', line 44

def keys
  (@completions.keys + (@example&.keys || [])).uniq
end

#metadataObject



61
62
63
# File 'lib/desiru/core/prediction.rb', line 61

def 
  .dup
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
85
86
87
# File 'lib/desiru/core/prediction.rb', line 82

def respond_to_missing?(method_name, include_private = false)
  method_name.to_s.end_with?('=') ||
    @completions.key?(method_name) ||
    @example.respond_to?(method_name) ||
    super
end

#set_metadata(key, value) ⇒ Object



65
66
67
# File 'lib/desiru/core/prediction.rb', line 65

def (key, value)
  [key] = value
end

#to_exampleObject



57
58
59
# File 'lib/desiru/core/prediction.rb', line 57

def to_example
  Example.new(**to_h)
end

#to_hObject



52
53
54
55
# File 'lib/desiru/core/prediction.rb', line 52

def to_h
  result = @example ? @example.to_h : {}
  result.merge(@completions)
end

#valuesObject



48
49
50
# File 'lib/desiru/core/prediction.rb', line 48

def values
  keys.map { |k| self[k] }
end