Class: Kotoshu::Models::Result::WordResult
- Inherits:
-
Object
- Object
- Kotoshu::Models::Result::WordResult
- Defined in:
- lib/kotoshu/core/models/result/word_result.rb
Overview
This class is immutable and frozen on initialization.
Result object for checking a single word.
This is a value object that represents the result of checking a single word for spelling errors, including any suggestions.
Instance Attribute Summary collapse
-
#correct ⇒ Boolean
readonly
Whether the word is spelled correctly.
-
#metadata ⇒ Hash
readonly
Additional metadata.
-
#position ⇒ Integer
readonly
The position of the word in the source text (optional).
-
#suggestions ⇒ Suggestions::SuggestionSet
readonly
Suggestions for correction.
-
#word ⇒ String
readonly
The word that was checked.
Class Method Summary collapse
-
.correct(word, position: nil) ⇒ WordResult
Create a correct word result.
-
.incorrect(word, suggestions: nil, position: nil) ⇒ WordResult
Create an incorrect word result with suggestions.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Check equality based on word and correctness.
-
#as_json ⇒ Hash
Convert to JSON-compatible hash.
-
#correct? ⇒ Boolean
Check if the word is correct.
-
#first_suggestion ⇒ String?
Get the first (best) suggestion.
-
#has_suggestions? ⇒ Boolean
Check if there are suggestions.
-
#hash ⇒ Integer
Hash based on word and correctness.
-
#incorrect? ⇒ Boolean
Check if the word is incorrect.
-
#initialize(word, correct:, suggestions: nil, position: nil, metadata: {}) ⇒ WordResult
constructor
Create a new WordResult.
-
#suggestion_count ⇒ Integer
Get the number of suggestions.
-
#to_h ⇒ Hash
Convert to hash.
-
#to_s ⇒ String
(also: #inspect)
String representation.
-
#top_suggestions(n = 3) ⇒ Array<String>
Get the top N suggestions.
Constructor Details
#initialize(word, correct:, suggestions: nil, position: nil, metadata: {}) ⇒ WordResult
Create a new WordResult.
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/kotoshu/core/models/result/word_result.rb', line 49 def initialize(word, correct:, suggestions: nil, position: nil, metadata: {}) word = "" if word.nil? @word = word.dup.freeze @correct = correct @suggestions = suggestions || Suggestions::SuggestionSet.empty @position = position @metadata = .dup.freeze freeze end |
Instance Attribute Details
#correct ⇒ Boolean (readonly)
Returns Whether the word is spelled correctly.
31 32 33 |
# File 'lib/kotoshu/core/models/result/word_result.rb', line 31 def correct @correct end |
#metadata ⇒ Hash (readonly)
Returns Additional metadata.
40 41 42 |
# File 'lib/kotoshu/core/models/result/word_result.rb', line 40 def @metadata end |
#position ⇒ Integer (readonly)
Returns The position of the word in the source text (optional).
37 38 39 |
# File 'lib/kotoshu/core/models/result/word_result.rb', line 37 def position @position end |
#suggestions ⇒ Suggestions::SuggestionSet (readonly)
Returns Suggestions for correction.
34 35 36 |
# File 'lib/kotoshu/core/models/result/word_result.rb', line 34 def suggestions @suggestions end |
#word ⇒ String (readonly)
Returns The word that was checked.
28 29 30 |
# File 'lib/kotoshu/core/models/result/word_result.rb', line 28 def word @word end |
Class Method Details
.correct(word, position: nil) ⇒ WordResult
Create a correct word result.
172 173 174 |
# File 'lib/kotoshu/core/models/result/word_result.rb', line 172 def self.correct(word, position: nil) new(word, correct: true, position: position) end |
.incorrect(word, suggestions: nil, position: nil) ⇒ WordResult
Create an incorrect word result with suggestions.
189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/kotoshu/core/models/result/word_result.rb', line 189 def self.incorrect(word, suggestions: nil, position: nil) suggestions_set = if suggestions.is_a?(Suggestions::SuggestionSet) suggestions elsif suggestions.is_a?(Array) Suggestions::SuggestionSet.from_words(suggestions, source: :default) else Suggestions::SuggestionSet.empty end new(word, correct: false, suggestions: suggestions_set, position: position) end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Check equality based on word and correctness.
136 137 138 139 140 |
# File 'lib/kotoshu/core/models/result/word_result.rb', line 136 def ==(other) return false unless other.is_a?(WordResult) @word == other.word && @correct == other.correct end |
#as_json ⇒ Hash
Convert to JSON-compatible hash.
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/kotoshu/core/models/result/word_result.rb', line 121 def as_json { "word" => @word, "correct" => @correct, "position" => @position, "suggestionCount" => suggestion_count, "suggestions" => top_suggestions(10), "metadata" => @metadata } end |
#correct? ⇒ Boolean
Check if the word is correct.
64 65 66 |
# File 'lib/kotoshu/core/models/result/word_result.rb', line 64 def correct? @correct end |
#first_suggestion ⇒ String?
Get the first (best) suggestion.
100 101 102 |
# File 'lib/kotoshu/core/models/result/word_result.rb', line 100 def first_suggestion @suggestions.first&.word end |
#has_suggestions? ⇒ Boolean
Check if there are suggestions.
78 79 80 |
# File 'lib/kotoshu/core/models/result/word_result.rb', line 78 def has_suggestions? !@suggestions.empty? end |
#hash ⇒ Integer
Hash based on word and correctness.
146 147 148 |
# File 'lib/kotoshu/core/models/result/word_result.rb', line 146 def hash [@word, @correct].hash end |
#incorrect? ⇒ Boolean
Check if the word is incorrect.
71 72 73 |
# File 'lib/kotoshu/core/models/result/word_result.rb', line 71 def incorrect? !@correct end |
#suggestion_count ⇒ Integer
Get the number of suggestions.
85 86 87 |
# File 'lib/kotoshu/core/models/result/word_result.rb', line 85 def suggestion_count @suggestions.size end |
#to_h ⇒ Hash
Convert to hash.
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/kotoshu/core/models/result/word_result.rb', line 107 def to_h { word: @word, correct: @correct, position: @position, suggestion_count: suggestion_count, suggestions: top_suggestions(10), metadata: @metadata } end |
#to_s ⇒ String Also known as: inspect
String representation.
153 154 155 156 157 158 159 160 161 |
# File 'lib/kotoshu/core/models/result/word_result.rb', line 153 def to_s if @correct @word elsif has_suggestions? "#{@word} (did you mean #{top_suggestions(3).join(", ")}?)" else "#{@word} (no suggestions)" end end |
#top_suggestions(n = 3) ⇒ Array<String>
Get the top N suggestions.
93 94 95 |
# File 'lib/kotoshu/core/models/result/word_result.rb', line 93 def top_suggestions(n = 3) @suggestions.top(n).map(&:word) end |