Class: Kotoshu::Models::SemanticError
- Inherits:
-
Object
- Object
- Kotoshu::Models::SemanticError
- 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
-
#confidence ⇒ Object
readonly
Returns the value of attribute confidence.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#error_type ⇒ Object
readonly
Returns the value of attribute error_type.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#location ⇒ Object
readonly
Returns the value of attribute location.
-
#original ⇒ Object
readonly
Returns the value of attribute original.
-
#source_range ⇒ Object
readonly
Returns the value of attribute source_range.
-
#suggestions ⇒ Object
readonly
Returns the value of attribute suggestions.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
Comparison for sorting (by source position, then confidence).
-
#==(other) ⇒ Boolean
(also: #eql?)
Check if this error equals another.
-
#abbreviated(max_length: 80) ⇒ String
Create an abbreviated display for lists.
-
#confidence_level ⇒ Symbol
Get confidence level category.
-
#display_type ⇒ String
Get user-friendly display type name.
-
#hash ⇒ Integer
Hash code for hash table usage.
-
#high_confidence? ⇒ Boolean
Check if this is a high-confidence error.
-
#initialize(id:, original:, suggestions:, error_type:, confidence:, source_range: nil, location: nil, context: nil, allow_empty_suggestions: false) ⇒ SemanticError
constructor
Create a new semantic error.
-
#recommended_suggestion ⇒ Suggestion
Get the recommended (top) suggestion.
-
#to_s ⇒ String
(also: #inspect)
String representation.
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.
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
#confidence ⇒ Object (readonly)
Returns the value of attribute confidence.
54 55 56 |
# File 'lib/kotoshu/models/semantic_error.rb', line 54 def confidence @confidence end |
#context ⇒ Object (readonly)
Returns the value of attribute context.
54 55 56 |
# File 'lib/kotoshu/models/semantic_error.rb', line 54 def context @context end |
#error_type ⇒ Object (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 |
#id ⇒ Object (readonly)
Returns the value of attribute id.
54 55 56 |
# File 'lib/kotoshu/models/semantic_error.rb', line 54 def id @id end |
#location ⇒ Object (readonly)
Returns the value of attribute location.
54 55 56 |
# File 'lib/kotoshu/models/semantic_error.rb', line 54 def location @location end |
#original ⇒ Object (readonly)
Returns the value of attribute original.
54 55 56 |
# File 'lib/kotoshu/models/semantic_error.rb', line 54 def original @original end |
#source_range ⇒ Object (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 |
#suggestions ⇒ Object (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:
- Source position (source_range.start when present; falls back
to legacy
locationwhen source_range is nil) - Confidence (highest first)
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.
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.
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_level ⇒ Symbol
Get confidence level category.
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_type ⇒ String
Get user-friendly 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 |
#hash ⇒ Integer
Hash code for hash table usage.
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.
111 112 113 |
# File 'lib/kotoshu/models/semantic_error.rb', line 111 def high_confidence? @confidence > 0.8 end |
#recommended_suggestion ⇒ Suggestion
Get the recommended (top) suggestion.
128 129 130 |
# File 'lib/kotoshu/models/semantic_error.rb', line 128 def recommended_suggestion @suggestions.first end |
#to_s ⇒ String Also known as: inspect
String 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 |