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

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

Overview

Note:

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.

Examples:

Creating a correct word result

result = WordResult.new("hello", correct: true)
result.correct?     # => true
result.word         # => "hello"
result.suggestions  # => SuggestionSet.empty

Creating an incorrect word result with suggestions

suggestions = SuggestionSet.from_words(%w[hello help], source: :test)
result = WordResult.new("helo", correct: false, suggestions: suggestions)
result.correct?     # => false
result.has_suggestions?  # => true

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Create a new WordResult.

Parameters:

  • word (String)

    The word that was checked

  • correct (Boolean)

    Whether the word is correct

  • suggestions (Suggestions::SuggestionSet) (defaults to: nil)

    Suggestions (optional)

  • position (Integer) (defaults to: nil)

    Position in source text (optional)

  • metadata (Hash) (defaults to: {})

    Additional metadata (optional)



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

#correctBoolean (readonly)

Returns Whether the word is spelled correctly.

Returns:

  • (Boolean)

    Whether the word is spelled correctly



31
32
33
# File 'lib/kotoshu/core/models/result/word_result.rb', line 31

def correct
  @correct
end

#metadataHash (readonly)

Returns Additional metadata.

Returns:

  • (Hash)

    Additional metadata



40
41
42
# File 'lib/kotoshu/core/models/result/word_result.rb', line 40

def 
  @metadata
end

#positionInteger (readonly)

Returns The position of the word in the source text (optional).

Returns:

  • (Integer)

    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

#suggestionsSuggestions::SuggestionSet (readonly)

Returns Suggestions for correction.

Returns:



34
35
36
# File 'lib/kotoshu/core/models/result/word_result.rb', line 34

def suggestions
  @suggestions
end

#wordString (readonly)

Returns The word that was checked.

Returns:

  • (String)

    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.

Examples:

WordResult.correct("hello")

Parameters:

  • word (String)

    The word

  • position (Integer) (defaults to: nil)

    Position in source (optional)

Returns:

  • (WordResult)

    New result indicating correct spelling



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.

Examples:

With SuggestionSet

suggestions = SuggestionSet.from_words(%w[hello help], source: :test)
WordResult.incorrect("helo", suggestions: suggestions)

With array of words

WordResult.incorrect("helo", suggestions: %w[hello help])

Parameters:

  • word (String)

    The misspelled word

  • suggestions (Suggestions::SuggestionSet, Array<String>) (defaults to: nil)

    Suggestions

  • position (Integer) (defaults to: nil)

    Position in source (optional)

Returns:

  • (WordResult)

    New result indicating incorrect spelling



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.

Parameters:

Returns:

  • (Boolean)

    True if equal



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_jsonHash

Convert to JSON-compatible hash.

Returns:

  • (Hash)

    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.

Returns:

  • (Boolean)

    True if the word is spelled correctly



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

def correct?
  @correct
end

#first_suggestionString?

Get the first (best) suggestion.

Returns:

  • (String, nil)

    The best suggestion or nil



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.

Returns:

  • (Boolean)

    True if suggestions are available



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

def has_suggestions?
  !@suggestions.empty?
end

#hashInteger

Hash based on word and correctness.

Returns:

  • (Integer)

    Hash code



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.

Returns:

  • (Boolean)

    True if the word is misspelled



71
72
73
# File 'lib/kotoshu/core/models/result/word_result.rb', line 71

def incorrect?
  !@correct
end

#suggestion_countInteger

Get the number of suggestions.

Returns:

  • (Integer)

    Number of suggestions



85
86
87
# File 'lib/kotoshu/core/models/result/word_result.rb', line 85

def suggestion_count
  @suggestions.size
end

#to_hHash

Convert to hash.

Returns:

  • (Hash)

    Hash representation



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_sString Also known as: inspect

String representation.

Returns:

  • (String)

    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.

Parameters:

  • n (Integer) (defaults to: 3)

    Number of suggestions to return

Returns:

  • (Array<String>)

    Top N suggestion words



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