Class: Kotoshu::Models::Suggestion

Inherits:
Object
  • Object
show all
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.

Examples:

Creating a suggestion

suggestion = Suggestion.new("dessert", confidence: 0.92, source: :semantic)
suggestion.to_s  # => "dessert [92%]"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(word, confidence:, source: nil, metadata: {}) ⇒ Suggestion

Create a new suggestion.

Parameters:

  • word (String)

    The suggested word

  • confidence (Float)

    Confidence score (0.0 to 1.0)

  • source (Symbol, nil) (defaults to: nil)

    Source of the suggestion (e.g., :semantic, :edit_distance)

  • metadata (Hash) (defaults to: {})

    Additional metadata (optional)

Options Hash (metadata:):

  • :embedding (WordEmbedding, nil)

    The word embedding

  • :edit_distance (Float)

    Edit distance score

  • :frequency_bonus (Float)

    Frequency score bonus

  • :explanation (String)

    Explanation for the suggestion

Raises:

  • (ArgumentError)


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

#confidenceObject (readonly)

Returns the value of attribute confidence.



14
15
16
# File 'lib/kotoshu/models/suggestion.rb', line 14

def confidence
  @confidence
end

#metadataObject (readonly)

Returns the value of attribute metadata.



14
15
16
# File 'lib/kotoshu/models/suggestion.rb', line 14

def 
  @metadata
end

#sourceObject (readonly)

Returns the value of attribute source.



14
15
16
# File 'lib/kotoshu/models/suggestion.rb', line 14

def source
  @source
end

#wordObject (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).

Parameters:

Returns:

  • (Integer)

    Comparison result (-1, 0, 1)



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.

Parameters:

  • other (Object)

    Another object

Returns:

  • (Boolean)

    True if words match



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_distanceFloat?

Get the edit distance if available.

Returns:

  • (Float, nil)

    Edit distance or nil



87
88
89
# File 'lib/kotoshu/models/suggestion.rb', line 87

def edit_distance
  @metadata[:edit_distance]
end

#embeddingWordEmbedding?

Get the embedding if available.

Returns:



80
81
82
# File 'lib/kotoshu/models/suggestion.rb', line 80

def embedding
  @metadata[:embedding]
end

#explanationString?

Get explanation text if available.

Returns:

  • (String, nil)

    Explanation or nil



101
102
103
# File 'lib/kotoshu/models/suggestion.rb', line 101

def explanation
  @metadata[:explanation]
end

#hashInteger

Hash code for hash table usage.

Returns:

  • (Integer)

    Hash code



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.

Returns:

  • (Boolean)

    True if confidence > 0.8



94
95
96
# File 'lib/kotoshu/models/suggestion.rb', line 94

def high_confidence?
  @confidence > 0.8
end

#to_sString Also known as: inspect

String representation with percentage.

Returns:

  • (String)

    Human-readable representation



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