Class: Kotoshu::Models::Suggestion
- Inherits:
-
Object
- Object
- Kotoshu::Models::Suggestion
- Defined in:
- lib/kotoshu/models/suggestion.rb
Overview
Value object for correction suggestions.
Represents a suggested correction for a detected error, with confidence score and metadata.
Instance Attribute Summary collapse
-
#confidence ⇒ Object
readonly
Returns the value of attribute confidence.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#word ⇒ Object
readonly
Returns the value of attribute word.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
Comparison for sorting (higher confidence = better).
-
#==(other) ⇒ Boolean
(also: #eql?)
Check if this equals another suggestion.
-
#edit_distance ⇒ Float?
Get the edit distance if available.
-
#embedding ⇒ WordEmbedding?
Get the embedding if available.
-
#explanation ⇒ String?
Get explanation text if available.
-
#hash ⇒ Integer
Hash code for hash table usage.
-
#high_confidence? ⇒ Boolean
Check if this is a high-confidence suggestion.
-
#initialize(word, confidence:, source: nil, metadata: {}) ⇒ Suggestion
constructor
Create a new suggestion.
-
#to_s ⇒ String
(also: #inspect)
String representation with percentage.
Constructor Details
#initialize(word, confidence:, source: nil, metadata: {}) ⇒ Suggestion
Create a new suggestion.
26 27 28 29 30 31 32 33 34 |
# File 'lib/kotoshu/models/suggestion.rb', line 26 def initialize(word, confidence:, source: nil, metadata: {}) raise ArgumentError, "Confidence must be 0-1" unless confidence.between?(0.0, 1.0) @word = word @confidence = confidence @source = source || :unknown @metadata = .freeze freeze end |
Instance Attribute Details
#confidence ⇒ Object (readonly)
Returns the value of attribute confidence.
14 15 16 |
# File 'lib/kotoshu/models/suggestion.rb', line 14 def confidence @confidence end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
14 15 16 |
# File 'lib/kotoshu/models/suggestion.rb', line 14 def @metadata end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
14 15 16 |
# File 'lib/kotoshu/models/suggestion.rb', line 14 def source @source end |
#word ⇒ Object (readonly)
Returns the value of attribute word.
14 15 16 |
# File 'lib/kotoshu/models/suggestion.rb', line 14 def word @word end |
Instance Method Details
#<=>(other) ⇒ Integer
Comparison for sorting (higher confidence = better).
40 41 42 43 44 45 |
# File 'lib/kotoshu/models/suggestion.rb', line 40 def <=>(other) return 0 unless other.is_a?(Suggestion) # Higher confidence = better rank (sort descending) other.confidence <=> @confidence end |
#==(other) ⇒ Boolean Also known as: eql?
Check if this equals another suggestion.
51 52 53 54 55 |
# File 'lib/kotoshu/models/suggestion.rb', line 51 def ==(other) return false unless other.is_a?(Suggestion) @word == other.word end |
#edit_distance ⇒ Float?
Get the edit distance if available.
87 88 89 |
# File 'lib/kotoshu/models/suggestion.rb', line 87 def edit_distance @metadata[:edit_distance] end |
#embedding ⇒ WordEmbedding?
Get the embedding if available.
80 81 82 |
# File 'lib/kotoshu/models/suggestion.rb', line 80 def @metadata[:embedding] end |
#explanation ⇒ String?
Get explanation text if available.
101 102 103 |
# File 'lib/kotoshu/models/suggestion.rb', line 101 def explanation @metadata[:explanation] end |
#hash ⇒ Integer
Hash code for hash table usage.
61 62 63 |
# File 'lib/kotoshu/models/suggestion.rb', line 61 def hash @word.hash end |
#high_confidence? ⇒ Boolean
Check if this is a high-confidence suggestion.
94 95 96 |
# File 'lib/kotoshu/models/suggestion.rb', line 94 def high_confidence? @confidence > 0.8 end |
#to_s ⇒ String Also known as: inspect
String representation with percentage.
68 69 70 71 72 73 74 |
# File 'lib/kotoshu/models/suggestion.rb', line 68 def to_s if @source && @source != :unknown "#{@word} [#{(@confidence * 100).to_i}%] (#{@source})" else "#{@word} [#{(@confidence * 100).to_i}%]" end end |