Class: Kotoshu::Suggestions::SuggestionSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/kotoshu/suggestions/suggestion_set.rb

Overview

A collection of suggestions with rich query methods. This is MORE OOP than Spylls which returns plain iterators of strings.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(suggestions = [], max_size: 10) ⇒ SuggestionSet

Returns a new instance of SuggestionSet.

Parameters:

  • suggestions (Array<Suggestion>) (defaults to: [])

    Initial suggestions

  • max_size (Integer) (defaults to: 10)

    Maximum number of suggestions to keep



14
15
16
17
18
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 14

def initialize(suggestions = [], max_size: 10)
  @suggestions = suggestions
  @max_size = max_size
  sort_and_limit!
end

Instance Attribute Details

#max_sizeObject (readonly)

Returns the value of attribute max_size.



10
11
12
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 10

def max_size
  @max_size
end

#suggestionsObject (readonly)

Returns the value of attribute suggestions.



10
11
12
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 10

def suggestions
  @suggestions
end

Class Method Details

.empty(max_size: 10) ⇒ SuggestionSet

Create an empty suggestion set.

Parameters:

  • max_size (Integer) (defaults to: 10)

    Maximum size

Returns:



213
214
215
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 213

def self.empty(max_size: 10)
  new([], max_size: max_size)
end

.from_words(words, source: :unknown, max_size: 10) ⇒ SuggestionSet

Create a suggestion set from an array of words.

Parameters:

  • words (Array<String>)

    Array of words

  • source (String, Symbol) (defaults to: :unknown)

    The source

  • max_size (Integer) (defaults to: 10)

    Maximum size

Returns:



223
224
225
226
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 223

def self.from_words(words, source: :unknown, max_size: 10)
  suggestions = words.map { |w| Suggestion.from_word(w, source: source) }
  new(suggestions, max_size: max_size)
end

Instance Method Details

#add(suggestion) ⇒ SuggestionSet Also known as: <<

Add a suggestion to the set.

Parameters:

  • suggestion (Suggestion)

    The suggestion to add

Returns:



24
25
26
27
28
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 24

def add(suggestion)
  @suggestions << suggestion
  sort_and_limit!
  self
end

#as_jsonArray<Hash>

Convert to JSON-compatible array.

Returns:

  • (Array<Hash>)

    JSON-compatible array



187
188
189
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 187

def as_json(*)
  to_hashes
end

#concat(new_suggestions) ⇒ SuggestionSet

Add multiple suggestions.

Parameters:

  • new_suggestions (Array<Suggestion>)

    Suggestions to add

Returns:



35
36
37
38
39
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 35

def concat(new_suggestions)
  @suggestions.concat(new_suggestions)
  sort_and_limit!
  self
end

#each {|suggestion| ... } ⇒ Enumerator

Iterate over suggestions.

Yields:

  • (suggestion)

    Each suggestion

Returns:

  • (Enumerator)

    Enumerator if no block given



143
144
145
146
147
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 143

def each(&)
  return enum_for(:each) unless block_given?

  @suggestions.each(&)
end

#empty?Boolean

Check if the set is empty.

Returns:

  • (Boolean)

    True if no suggestions



126
127
128
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 126

def empty?
  @suggestions.empty?
end

#find_word(word) ⇒ Suggestion?

Find a suggestion by word.

Parameters:

  • word (String)

    The word to find

Returns:



97
98
99
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 97

def find_word(word)
  @suggestions.find { |s| s.same_word?(word) }
end

#firstSuggestion?

Get the first (best) suggestion.

Returns:

  • (Suggestion, nil)

    The best suggestion or nil



112
113
114
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 112

def first
  @suggestions.first
end

#from_source(source) ⇒ SuggestionSet

Get suggestions by source.

Parameters:

  • source (String, Symbol)

    The source to filter by

Returns:



54
55
56
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 54

def from_source(source)
  SuggestionSet.new(@suggestions.select { |s| s.from_source?(source) }, max_size: @max_size)
end

#high_confidenceSuggestionSet

Get high-confidence suggestions.

Returns:



61
62
63
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 61

def high_confidence
  SuggestionSet.new(@suggestions.select(&:high_confidence?), max_size: @max_size)
end

#include?(word) ⇒ Boolean Also known as: has_word?

Check if set contains a specific word.

Parameters:

  • word (String)

    The word to check

Returns:

  • (Boolean)

    True if word is in suggestions



88
89
90
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 88

def include?(word)
  @suggestions.any? { |s| s.same_word?(word) }
end

#inspectString

Inspect the suggestion set.

Returns:

  • (String)

    Inspection string



201
202
203
204
205
206
207
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 201

def inspect
  if @suggestions.empty?
    to_s
  else
    "#{self} [#{@suggestions.map(&:word).join(', ')}]"
  end
end

#lastSuggestion?

Get the last suggestion.

Returns:

  • (Suggestion, nil)

    The last suggestion or nil



119
120
121
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 119

def last
  @suggestions.last
end

#low_confidenceSuggestionSet

Get low-confidence suggestions.

Returns:



68
69
70
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 68

def low_confidence
  SuggestionSet.new(@suggestions.select(&:low_confidence?), max_size: @max_size)
end

#merge!(other) ⇒ SuggestionSet

Merge another suggestion set into this one.

Parameters:

Returns:



45
46
47
48
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 45

def merge!(other)
  concat(other.suggestions)
  self
end

#sizeInteger Also known as: count, length

Get the number of suggestions.

Returns:

  • (Integer)

    Number of suggestions



133
134
135
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 133

def size
  @suggestions.size
end

#to_hashesArray<Hash>

Convert to an array of hashes for serialization.

Use this when you need the Hash shape (e.g. JSON output). For the enumerable contract, #to_a / #each yield Kotoshu::Suggestions::Suggestion objects.

Returns:

  • (Array<Hash>)

    Array of suggestion hashes



180
181
182
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 180

def to_hashes
  @suggestions.map(&:to_hash)
end

#to_sString

String representation.

Returns:

  • (String)

    String representation



194
195
196
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 194

def to_s
  "SuggestionSet(size: #{size}, max_size: #{@max_size})"
end

#to_wordsArray<String> Also known as: words

Convert to array of words.

Returns:

  • (Array<String>)

    Array of suggestion words



169
170
171
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 169

def to_words
  @suggestions.map(&:word)
end

#top(n) ⇒ Array<Suggestion>

Get the top N suggestions.

Parameters:

  • n (Integer)

    Number of suggestions to get

Returns:



105
106
107
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 105

def top(n)
  @suggestions.first(n)
end

#uniqueSuggestionSet

Get unique suggestions (by word, case-insensitive).

Returns:



152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 152

def unique
  seen = {}
  unique_suggestions = @suggestions.select do |s|
    word = s.word.downcase
    if seen[word]
      false
    else
      seen[word] = true
      true
    end
  end
  SuggestionSet.new(unique_suggestions, max_size: @max_size)
end

#within_distance(min_distance: 0, max_distance: 2) ⇒ SuggestionSet

Get suggestions within a distance range.

Parameters:

  • min_distance (Integer) (defaults to: 0)

    Minimum distance

  • max_distance (Integer) (defaults to: 2)

    Maximum distance

Returns:



77
78
79
80
81
82
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 77

def within_distance(min_distance: 0, max_distance: 2)
  filtered = @suggestions.select do |s|
    s.distance >= min_distance && s.distance <= max_distance
  end
  SuggestionSet.new(filtered, max_size: @max_size)
end