Class: Kotoshu::Models::SemanticError

Inherits:
Object
  • Object
show all
Defined in:
lib/kotoshu/models/semantic_error.rb

Constant Summary collapse

ERROR_TYPES =

Error type definitions with display names

{
  word_choice: 'Word Choice',
  verb_agreement: 'Verb Agreement',
  tense: 'Tense',
  orthographic: 'Spelling',
  preposition: 'Preposition',
  article: 'Article',
  morphology: 'Word Form',
  capitalization: 'Capitalization',
  punctuation: 'Punctuation',
  style: 'Style'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, original:, suggestions:, error_type:, confidence:, source_range: nil, location: nil, context: nil, allow_empty_suggestions: false) ⇒ SemanticError

Create a new semantic error.

Parameters:

  • id (String, Symbol)

    Unique identifier for this error

  • source_range (Kotoshu::Documents::SourceRange, nil) (defaults to: nil)

    Where the offending text lives in the original markup-bearing source. Carried verbatim so editors/plugins can highlight the actual range the user wrote.

  • location (Object, nil) (defaults to: nil)

    Legacy location holder (line/column object). Kept for backward compat with old callers; new code should pass source_range instead.

  • original (String)

    The original (incorrect) word/text

  • suggestions (Array<Suggestion>)

    Suggested corrections

  • error_type (Symbol)

    Error type (must be in ERROR_TYPES)

  • confidence (Float)

    Confidence score (0.0 to 1.0)

  • context (Context, nil) (defaults to: nil)

    Context around the error

  • allow_empty_suggestions (Boolean) (defaults to: false)

    When false (default), raises EmptySuggestionsError instead of building an error with zero suggestions.

Raises:

  • (ArgumentError)

    if error_type is invalid

  • (ArgumentError)

    if confidence is outside [0, 1]

  • (EmptySuggestionsError)

    if suggestions is empty and allow_empty_suggestions is false



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/kotoshu/models/semantic_error.rb', line 79

def initialize(id:, original:, suggestions:, error_type:, confidence:, source_range: nil, location: nil, context: nil,
               allow_empty_suggestions: false)
  raise ArgumentError, "Invalid error type: #{error_type}" unless ERROR_TYPES.key?(error_type)
  raise ArgumentError, "Confidence must be 0-1" unless confidence.between?(0.0, 1.0)

  if (suggestions.nil? || suggestions.empty?) && !allow_empty_suggestions
    raise EmptySuggestionsError,
          "Suggestions cannot be empty (pass allow_empty_suggestions: true to override)"
  end

  @id = id.to_s
  @source_range = source_range
  @location = location || source_range&.start
  @original = original
  @suggestions = (suggestions || []).sort_by(&:confidence).reverse.freeze
  @error_type = error_type
  @confidence = confidence
  @context = context

  freeze
end

Instance Attribute Details

#confidenceObject (readonly)

Returns the value of attribute confidence.



54
55
56
# File 'lib/kotoshu/models/semantic_error.rb', line 54

def confidence
  @confidence
end

#contextObject (readonly)

Returns the value of attribute context.



54
55
56
# File 'lib/kotoshu/models/semantic_error.rb', line 54

def context
  @context
end

#error_typeObject (readonly)

Returns the value of attribute error_type.



54
55
56
# File 'lib/kotoshu/models/semantic_error.rb', line 54

def error_type
  @error_type
end

#idObject (readonly)

Returns the value of attribute id.



54
55
56
# File 'lib/kotoshu/models/semantic_error.rb', line 54

def id
  @id
end

#locationObject (readonly)

Returns the value of attribute location.



54
55
56
# File 'lib/kotoshu/models/semantic_error.rb', line 54

def location
  @location
end

#originalObject (readonly)

Returns the value of attribute original.



54
55
56
# File 'lib/kotoshu/models/semantic_error.rb', line 54

def original
  @original
end

#source_rangeObject (readonly)

Returns the value of attribute source_range.



54
55
56
# File 'lib/kotoshu/models/semantic_error.rb', line 54

def source_range
  @source_range
end

#suggestionsObject (readonly)

Returns the value of attribute suggestions.



54
55
56
# File 'lib/kotoshu/models/semantic_error.rb', line 54

def suggestions
  @suggestions
end

Instance Method Details

#<=>(other) ⇒ Integer

Comparison for sorting (by source position, then confidence).

Errors are sorted by:

  1. Source position (source_range.start when present; falls back to legacy location when source_range is nil)
  2. Confidence (highest first)

Parameters:

Returns:

  • (Integer)

    Comparison result (-1, 0, 1)



159
160
161
162
163
164
165
166
167
168
# File 'lib/kotoshu/models/semantic_error.rb', line 159

def <=>(other)
  return 0 unless other.is_a?(SemanticError)

  a_pos = sort_position
  b_pos = other.sort_position
  pos_cmp = a_pos <=> b_pos
  return pos_cmp unless pos_cmp.zero?

  other.confidence <=> @confidence
end

#==(other) ⇒ Boolean Also known as: eql?

Check if this error equals another.

Parameters:

  • other (Object)

    Another object

Returns:

  • (Boolean)

    True if IDs match



136
137
138
139
140
# File 'lib/kotoshu/models/semantic_error.rb', line 136

def ==(other)
  return false unless other.is_a?(SemanticError)

  @id == other.id
end

#abbreviated(max_length: 80) ⇒ String

Create an abbreviated display for lists.

Parameters:

  • max_length (Integer) (defaults to: 80)

    Maximum line length

Returns:

  • (String)

    Abbreviated representation



182
183
184
185
186
187
# File 'lib/kotoshu/models/semantic_error.rb', line 182

def abbreviated(max_length: 80)
  orig_display = "'#{@original}'"
  sugg_display = "'#{recommended_suggestion.word}'"

  "#{@location}: #{orig_display}#{sugg_display} [#{(@confidence * 100).to_i}%]"
end

#confidence_levelSymbol

Get confidence level category.

Returns:

  • (Symbol)

    :high, :medium, or :low



118
119
120
121
122
123
# File 'lib/kotoshu/models/semantic_error.rb', line 118

def confidence_level
  return :high if @confidence > 0.8
  return :medium if @confidence > 0.5

  :low
end

#display_typeString

Get user-friendly display type name.

Returns:

  • (String)

    Display type name



104
105
106
# File 'lib/kotoshu/models/semantic_error.rb', line 104

def display_type
  ERROR_TYPES[@error_type] || @error_type.to_s.capitalize
end

#hashInteger

Hash code for hash table usage.

Returns:

  • (Integer)

    Hash code



146
147
148
# File 'lib/kotoshu/models/semantic_error.rb', line 146

def hash
  @id.hash
end

#high_confidence?Boolean

Check if this is a high-confidence error.

Returns:

  • (Boolean)

    True if confidence > 0.8



111
112
113
# File 'lib/kotoshu/models/semantic_error.rb', line 111

def high_confidence?
  @confidence > 0.8
end

Get the recommended (top) suggestion.

Returns:

  • (Suggestion)

    The highest-confidence suggestion



128
129
130
# File 'lib/kotoshu/models/semantic_error.rb', line 128

def recommended_suggestion
  @suggestions.first
end

#to_sString Also known as: inspect

String representation.

Returns:

  • (String)

    Human-readable representation



173
174
175
# File 'lib/kotoshu/models/semantic_error.rb', line 173

def to_s
  "#{@location}: '#{@original}' → #{recommended_suggestion.word} [#{(@confidence * 100).to_i}%]"
end