Class: Kotoshu::Models::SemanticError

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

Examples:

Creating a semantic error

error = SemanticError.new(
  id: "error_1",
  location: Location.new(line: 5, column: 12),
  original: "desert",
  suggestions: [Suggestion.new("dessert", confidence: 0.92)],
  error_type: :word_choice,
  confidence: 0.92,
  context: context
)

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:, location:, original:, suggestions:, error_type:, confidence:, context:) ⇒ SemanticError

Create a new semantic error.

Parameters:

  • id (String, Symbol)

    Unique identifier for this error

  • location (Documents::Location)

    Location of error in document

  • 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)

    Context around the error

Raises:

  • (ArgumentError)

    if error_type is invalid



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

#confidenceObject (readonly)

Returns the value of attribute confidence.



50
51
52
# File 'lib/kotoshu/models/semantic_error.rb', line 50

def confidence
  @confidence
end

#contextObject (readonly)

Returns the value of attribute context.



50
51
52
# File 'lib/kotoshu/models/semantic_error.rb', line 50

def context
  @context
end

#error_typeObject (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

#idObject (readonly)

Returns the value of attribute id.



50
51
52
# File 'lib/kotoshu/models/semantic_error.rb', line 50

def id
  @id
end

#locationObject (readonly)

Returns the value of attribute location.



50
51
52
# File 'lib/kotoshu/models/semantic_error.rb', line 50

def location
  @location
end

#originalObject (readonly)

Returns the value of attribute original.



50
51
52
# File 'lib/kotoshu/models/semantic_error.rb', line 50

def original
  @original
end

#suggestionsObject (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:

  1. Document location (line number, then column)

  2. Confidence (highest first)

Parameters:

Returns:

  • (Integer)

    Comparison result (-1, 0, 1)



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.

Parameters:

  • other (Object)

    Another object

Returns:

  • (Boolean)

    True if IDs match



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.

Parameters:

  • max_length (Integer) (defaults to: 80)

    Maximum line length

Returns:

  • (String)

    Abbreviated representation



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_levelSymbol

Get confidence level category.

Returns:

  • (Symbol)

    :high, :medium, or :low



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_typeString

Get user-friendly display type name.

Returns:

  • (String)

    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

#hashInteger

Hash code for hash table usage.

Returns:

  • (Integer)

    Hash code



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.

Returns:

  • (Boolean)

    True if confidence > 0.8



88
89
90
# File 'lib/kotoshu/models/semantic_error.rb', line 88

def high_confidence?
  @confidence > 0.8
end

Get the recommended (top) suggestion.

Returns:

  • (Suggestion)

    The highest-confidence suggestion



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

def recommended_suggestion
  @suggestions.first
end

#to_sString Also known as: inspect

String representation.

Returns:

  • (String)

    Human-readable 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