Class: Kotoshu::Models::Result::WordResult

Inherits:
Lutaml::Model::Serializable
  • Object
show all
Defined in:
lib/kotoshu/core/models/result/word_result.rb

Overview

Result object for checking a single word.

Serialized via lutaml-model. The suggestions attribute is a collection of Suggestions::Suggestion. For rich query semantics (filtering by source, confidence, distance), wrap with Suggestions::SuggestionSet via #to_suggestion_set.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(word: "", correct: true, suggestions: nil, position: nil, metadata: {}, **kwargs) ⇒ WordResult

lutaml-model calls new(**attrs) when deserializing via from_hash. The signature accepts every attribute as a keyword plus a splat for framework-private keywords (e.g. lutaml_register) so the round-trip works without lutaml having to poke ivars directly.

suggestions accepts a Suggestions::SuggestionSet, an Array, or nil for ergonomic construction; the SuggestionSet case is unwrapped to its underlying Array.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/kotoshu/core/models/result/word_result.rb', line 30

def initialize(word: "", correct: true, suggestions: nil, position: nil, metadata: {}, **kwargs)
  suggestions_array =
    case suggestions
    when Suggestions::SuggestionSet then suggestions.suggestions
    when Array then suggestions
    when nil then []
    else raise ArgumentError, "suggestions must be SuggestionSet, Array, or nil"
    end

  super(
    word: word.to_s,
    correct: correct,
    position: position,
    suggestions: suggestions_array,
    metadata: ,
    **kwargs
  )
end

Class Method Details

.correct(word, position: nil) ⇒ Object



103
104
105
# File 'lib/kotoshu/core/models/result/word_result.rb', line 103

def self.correct(word, position: nil)
  new(word: word, correct: true, position: position)
end

.incorrect(word, suggestions: nil, position: nil) ⇒ Object



107
108
109
# File 'lib/kotoshu/core/models/result/word_result.rb', line 107

def self.incorrect(word, suggestions: nil, position: nil)
  new(word: word, correct: false, suggestions: suggestions, position: position)
end

Instance Method Details

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



81
82
83
84
85
# File 'lib/kotoshu/core/models/result/word_result.rb', line 81

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

  word == other.word && correct == other.correct
end

#correct?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/kotoshu/core/models/result/word_result.rb', line 49

def correct?
  correct
end

#first_suggestionObject



69
70
71
# File 'lib/kotoshu/core/models/result/word_result.rb', line 69

def first_suggestion
  suggestions.first&.word
end

#has_suggestions?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/kotoshu/core/models/result/word_result.rb', line 57

def has_suggestions?
  !suggestions.empty?
end

#hashObject



88
89
90
# File 'lib/kotoshu/core/models/result/word_result.rb', line 88

def hash
  [word, correct].hash
end

#incorrect?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/kotoshu/core/models/result/word_result.rb', line 53

def incorrect?
  !correct
end

#suggestion_countObject



61
62
63
# File 'lib/kotoshu/core/models/result/word_result.rb', line 61

def suggestion_count
  suggestions.size
end

#to_sObject Also known as: inspect



92
93
94
95
96
97
98
99
100
# File 'lib/kotoshu/core/models/result/word_result.rb', line 92

def to_s
  if correct
    word
  elsif has_suggestions?
    "#{word} (did you mean #{top_suggestions(3).join(', ')}?)"
  else
    "#{word} (no suggestions)"
  end
end

#to_suggestion_setSuggestions::SuggestionSet

Wrap the suggestions array in a Suggestions::SuggestionSet for rich query (filter by source, confidence, distance).



77
78
79
# File 'lib/kotoshu/core/models/result/word_result.rb', line 77

def to_suggestion_set
  Suggestions::SuggestionSet.new(suggestions)
end

#top_suggestions(n = 3) ⇒ Object



65
66
67
# File 'lib/kotoshu/core/models/result/word_result.rb', line 65

def top_suggestions(n = 3)
  suggestions.first(n).map(&:word)
end