Class: Kotoshu::Models::SemanticError
- Inherits:
-
Object
- Object
- Kotoshu::Models::SemanticError
- Defined in:
- lib/kotoshu/models/semantic_error.rb
Overview
Unified semantic error (NO artificial spelling/grammar split!).
Represents ANY kind of language error detected through semantic analysis. Uses semantic categories instead of traditional “spelling” vs “grammar” labels.
Error types (semantic categories):
-
:word_choice - Wrong word for context (e.g., “desert” vs “dessert”)
-
:verb_agreement - Subject-verb mismatch (e.g., “they is” → “they are”)
-
:tense - Temporal inconsistency (e.g., “Yesterday I will go”)
-
:orthographic - Actual typo/misspelling (e.g., “wrold” → “world”)
-
:preposition - Wrong preposition (e.g., “bored of” → “bored with”)
-
:article - Wrong article (e.g., “a apple” → “an apple”)
-
:morphology - Wrong word form (e.g., “goed” → “went”)
-
:capitalization - Capitalization error (e.g., “i am” → “I am”)
-
:punctuation - Punctuation error (e.g., “its” vs “it’s”)
-
:style - Style/usage suggestion
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.
-
#suggestions ⇒ Object
readonly
Returns the value of attribute suggestions.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
Comparison for sorting (by location, 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:, location:, original:, suggestions:, error_type:, confidence:, context:) ⇒ 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:, location:, original:, suggestions:, error_type:, confidence:, context:) ⇒ SemanticError
Create a new semantic error.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/kotoshu/models/semantic_error.rb', line 62 def initialize(id:, location:, original:, suggestions:, error_type:, confidence:, context:) 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) raise ArgumentError, "Suggestions cannot be empty" if suggestions.nil? || suggestions.empty? @id = id.to_s @location = location @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.
50 51 52 |
# File 'lib/kotoshu/models/semantic_error.rb', line 50 def confidence @confidence end |
#context ⇒ Object (readonly)
Returns the value of attribute context.
50 51 52 |
# File 'lib/kotoshu/models/semantic_error.rb', line 50 def context @context end |
#error_type ⇒ Object (readonly)
Returns the value of attribute error_type.
50 51 52 |
# File 'lib/kotoshu/models/semantic_error.rb', line 50 def error_type @error_type end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
50 51 52 |
# File 'lib/kotoshu/models/semantic_error.rb', line 50 def id @id end |
#location ⇒ Object (readonly)
Returns the value of attribute location.
50 51 52 |
# File 'lib/kotoshu/models/semantic_error.rb', line 50 def location @location end |
#original ⇒ Object (readonly)
Returns the value of attribute original.
50 51 52 |
# File 'lib/kotoshu/models/semantic_error.rb', line 50 def original @original end |
#suggestions ⇒ Object (readonly)
Returns the value of attribute suggestions.
50 51 52 |
# File 'lib/kotoshu/models/semantic_error.rb', line 50 def suggestions @suggestions end |
Instance Method Details
#<=>(other) ⇒ Integer
Comparison for sorting (by location, then confidence).
Errors are sorted by:
-
Document location (line number, then column)
-
Confidence (highest first)
134 135 136 137 138 139 140 141 142 143 |
# File 'lib/kotoshu/models/semantic_error.rb', line 134 def <=>(other) return 0 unless other.is_a?(SemanticError) # First by location (line, then column) loc_cmp = @location <=> other.location return loc_cmp unless loc_cmp.zero? # Then by confidence (highest first) other.confidence <=> @confidence end |
#==(other) ⇒ Boolean Also known as: eql?
Check if this error equals another.
112 113 114 115 116 |
# File 'lib/kotoshu/models/semantic_error.rb', line 112 def ==(other) return false unless other.is_a?(SemanticError) @id == other.id end |
#abbreviated(max_length: 80) ⇒ String
Create an abbreviated display for lists.
157 158 159 160 161 162 |
# File 'lib/kotoshu/models/semantic_error.rb', line 157 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.
95 96 97 98 99 |
# File 'lib/kotoshu/models/semantic_error.rb', line 95 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.
81 82 83 |
# File 'lib/kotoshu/models/semantic_error.rb', line 81 def display_type ERROR_TYPES[@error_type] || @error_type.to_s.capitalize end |
#hash ⇒ Integer
Hash code for hash table usage.
122 123 124 |
# File 'lib/kotoshu/models/semantic_error.rb', line 122 def hash @id.hash end |
#high_confidence? ⇒ Boolean
Check if this is a high-confidence error.
88 89 90 |
# File 'lib/kotoshu/models/semantic_error.rb', line 88 def high_confidence? @confidence > 0.8 end |
#recommended_suggestion ⇒ Suggestion
Get the recommended (top) suggestion.
104 105 106 |
# File 'lib/kotoshu/models/semantic_error.rb', line 104 def recommended_suggestion @suggestions.first end |
#to_s ⇒ String Also known as: inspect
String representation.
148 149 150 |
# File 'lib/kotoshu/models/semantic_error.rb', line 148 def to_s "#{@location}: '#{@original}' → #{recommended_suggestion.word} [#{(@confidence * 100).to_i}%]" end |