Class: Kotoshu::Suggestions::SuggestionSet
- Inherits:
-
Object
- Object
- Kotoshu::Suggestions::SuggestionSet
- 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
-
#duplicates_removed ⇒ Integer
readonly
Number of duplicate suggestions removed by the last call to #deduplicate!.
-
#max_size ⇒ Integer
readonly
Number of duplicate suggestions removed by the last call to #deduplicate!.
-
#suggestions ⇒ Integer
readonly
Number of duplicate suggestions removed by the last call to #deduplicate!.
Class Method Summary collapse
-
.empty(max_size: 10) ⇒ SuggestionSet
Create an empty suggestion set.
-
.from_words(words, source: :unknown, max_size: 10) ⇒ SuggestionSet
Create a suggestion set from an array of words.
Instance Method Summary collapse
-
#add(suggestion) ⇒ SuggestionSet
(also: #<<)
Add a suggestion to the set.
-
#as_json ⇒ Array<Hash>
Convert to JSON-compatible array.
-
#concat(new_suggestions) ⇒ SuggestionSet
Add multiple suggestions.
-
#each {|suggestion| ... } ⇒ Enumerator
Iterate over suggestions.
-
#empty? ⇒ Boolean
Check if the set is empty.
-
#find_word(word) ⇒ Suggestion?
Find a suggestion by word.
-
#first ⇒ Suggestion?
Get the first (best) suggestion.
-
#from_source(source) ⇒ SuggestionSet
Get suggestions by source.
-
#high_confidence ⇒ SuggestionSet
Get high-confidence suggestions.
-
#include?(word) ⇒ Boolean
(also: #has_word?)
Check if set contains a specific word.
-
#initialize(suggestions = [], max_size: 10) ⇒ SuggestionSet
constructor
A new instance of SuggestionSet.
-
#inspect ⇒ String
Inspect the suggestion set.
-
#last ⇒ Suggestion?
Get the last suggestion.
-
#low_confidence ⇒ SuggestionSet
Get low-confidence suggestions.
-
#merge!(other) ⇒ SuggestionSet
Merge another suggestion set into this one.
-
#size ⇒ Integer
(also: #count, #length)
Get the number of suggestions.
-
#to_hashes ⇒ Array<Hash>
Convert to an array of hashes for serialization.
-
#to_s ⇒ String
String representation.
-
#to_words ⇒ Array<String>
(also: #words)
Convert to array of words.
-
#top(n) ⇒ Array<Suggestion>
Get the top N suggestions.
-
#unique ⇒ SuggestionSet
Get unique suggestions (by word, case-insensitive).
-
#within_distance(min_distance: 0, max_distance: 2) ⇒ SuggestionSet
Get suggestions within a distance range.
Constructor Details
#initialize(suggestions = [], max_size: 10) ⇒ SuggestionSet
Returns a new instance of SuggestionSet.
16 17 18 19 20 21 |
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 16 def initialize(suggestions = [], max_size: 10) @suggestions = suggestions @max_size = max_size @duplicates_removed = 0 sort_and_limit! end |
Instance Attribute Details
#duplicates_removed ⇒ Integer (readonly)
Returns Number of duplicate suggestions removed by the last call to #deduplicate!. Zero until deduplicate! runs.
12 13 14 |
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 12 def duplicates_removed @duplicates_removed end |
#max_size ⇒ Integer (readonly)
Returns Number of duplicate suggestions removed by the last call to #deduplicate!. Zero until deduplicate! runs.
12 13 14 |
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 12 def max_size @max_size end |
#suggestions ⇒ Integer (readonly)
Returns Number of duplicate suggestions removed by the last call to #deduplicate!. Zero until deduplicate! runs.
12 13 14 |
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 12 def suggestions @suggestions end |
Class Method Details
.empty(max_size: 10) ⇒ SuggestionSet
Create an empty suggestion set.
216 217 218 |
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 216 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.
226 227 228 229 |
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 226 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.
27 28 29 30 31 |
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 27 def add(suggestion) @suggestions << suggestion sort_and_limit! self end |
#as_json ⇒ Array<Hash>
Convert to JSON-compatible array.
190 191 192 |
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 190 def as_json(*) to_hashes end |
#concat(new_suggestions) ⇒ SuggestionSet
Add multiple suggestions.
38 39 40 41 42 |
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 38 def concat(new_suggestions) @suggestions.concat(new_suggestions) sort_and_limit! self end |
#each {|suggestion| ... } ⇒ Enumerator
Iterate over suggestions.
146 147 148 149 150 |
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 146 def each(&) return enum_for(:each) unless block_given? @suggestions.each(&) end |
#empty? ⇒ Boolean
Check if the set is empty.
129 130 131 |
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 129 def empty? @suggestions.empty? end |
#find_word(word) ⇒ Suggestion?
Find a suggestion by word.
100 101 102 |
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 100 def find_word(word) @suggestions.find { |s| s.same_word?(word) } end |
#first ⇒ Suggestion?
Get the first (best) suggestion.
115 116 117 |
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 115 def first @suggestions.first end |
#from_source(source) ⇒ SuggestionSet
Get suggestions by source.
57 58 59 |
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 57 def from_source(source) SuggestionSet.new(@suggestions.select { |s| s.from_source?(source) }, max_size: @max_size) end |
#high_confidence ⇒ SuggestionSet
Get high-confidence suggestions.
64 65 66 |
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 64 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.
91 92 93 |
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 91 def include?(word) @suggestions.any? { |s| s.same_word?(word) } end |
#inspect ⇒ String
Inspect the suggestion set.
204 205 206 207 208 209 210 |
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 204 def inspect if @suggestions.empty? to_s else "#{self} [#{@suggestions.map(&:word).join(', ')}]" end end |
#last ⇒ Suggestion?
Get the last suggestion.
122 123 124 |
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 122 def last @suggestions.last end |
#low_confidence ⇒ SuggestionSet
Get low-confidence suggestions.
71 72 73 |
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 71 def low_confidence SuggestionSet.new(@suggestions.select(&:low_confidence?), max_size: @max_size) end |
#merge!(other) ⇒ SuggestionSet
Merge another suggestion set into this one.
48 49 50 51 |
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 48 def merge!(other) concat(other.suggestions) self end |
#size ⇒ Integer Also known as: count, length
Get the number of suggestions.
136 137 138 |
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 136 def size @suggestions.size end |
#to_hashes ⇒ Array<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.
183 184 185 |
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 183 def to_hashes @suggestions.map(&:to_hash) end |
#to_s ⇒ String
String representation.
197 198 199 |
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 197 def to_s "SuggestionSet(size: #{size}, max_size: #{@max_size})" end |
#to_words ⇒ Array<String> Also known as: words
Convert to array of words.
172 173 174 |
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 172 def to_words @suggestions.map(&:word) end |
#top(n) ⇒ Array<Suggestion>
Get the top N suggestions.
108 109 110 |
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 108 def top(n) @suggestions.first(n) end |
#unique ⇒ SuggestionSet
Get unique suggestions (by word, case-insensitive).
155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 155 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.
80 81 82 83 84 85 |
# File 'lib/kotoshu/suggestions/suggestion_set.rb', line 80 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 |