Class: Desiru::Core::Prediction
- Inherits:
-
Object
- Object
- Desiru::Core::Prediction
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 = {}
@metadata = {}
kwargs.each do |key, value|
if key == :completions
@completions = value
elsif key == :metadata
@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
#completions ⇒ Object
Returns the value of attribute completions.
6
7
8
|
# File 'lib/desiru/core/prediction.rb', line 6
def completions
@completions
end
|
#example ⇒ Object
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 &&
@metadata == 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
@example[key] ||
@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
|
#inspect ⇒ Object
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
|
#keys ⇒ Object
44
45
46
|
# File 'lib/desiru/core/prediction.rb', line 44
def keys
(@completions.keys + (@example&.keys || [])).uniq
end
|
61
62
63
|
# File 'lib/desiru/core/prediction.rb', line 61
def metadata
@metadata.dup
end
|
#respond_to_missing?(method_name, include_private = false) ⇒ 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
|
65
66
67
|
# File 'lib/desiru/core/prediction.rb', line 65
def set_metadata(key, value)
@metadata[key] = value
end
|
#to_example ⇒ Object
57
58
59
|
# File 'lib/desiru/core/prediction.rb', line 57
def to_example
Example.new(**to_h)
end
|
#to_h ⇒ Object
52
53
54
55
|
# File 'lib/desiru/core/prediction.rb', line 52
def to_h
result = @example ? @example.to_h : {}
result.merge(@completions)
end
|
#values ⇒ Object
48
49
50
|
# File 'lib/desiru/core/prediction.rb', line 48
def values
keys.map { |k| self[k] }
end
|