class Search
Constants
- VERSION
Public Class Methods
new(search, haystack:)
click to toggle source
Initializes a new search.
search - A String describing the term to search for. haystack - A String to search.
# File lib/search.rb, line 9 def initialize(search, haystack:) @search = search @haystack = haystack end
Public Instance Methods
quality()
click to toggle source
Get the match quality.
The quality is determined by how many of the search words the haystack contains.
Example
search = Search.new('one two' haystack: 'one two three four' ) search.quality # => 50
Returns an Integer describing the percentage of search words present in the haystack.
# File lib/search.rb, line 51 def quality (search_words_present_in_haystack.count.to_f / search_words.count.to_f * 100).round rescue ZeroDivisionError 0 end
suggestions()
click to toggle source
Get suggestions for narrowing the search.
Example
search = Search.new('what is the meaning of', haystack: 'What is the meaning of life? And what is the meaning of lol?' ) search.suggestions # => [ # [' life', ' lol'] # ]
Returns an Array of Strings that would narrow the search.
# File lib/search.rb, line 28 def suggestions suggestions = [] @haystack.scan /#{@search}(?<suggestion>\b[^.?!]+\b{1,5})/io do |match| suggestions << match.first end suggestions end
Private Instance Methods
haystack_words()
click to toggle source
# File lib/search.rb, line 63 def haystack_words @haystack_words ||= words_in(@haystack) end
search_words()
click to toggle source
# File lib/search.rb, line 59 def search_words @search_words ||= words_in(@search) end
search_words_present_in_haystack()
click to toggle source
# File lib/search.rb, line 67 def search_words_present_in_haystack search_words.select do |search_word| haystack_words.include? search_word end end
words_in(string)
click to toggle source
# File lib/search.rb, line 73 def words_in(string) string.split(/[^[[:word:]]]+/) end