Class: Fuzzy
- Inherits:
-
Object
- Object
- Fuzzy
- Defined in:
- lib/fuzzy.rb
Overview
Fuzzy string matching with scoring and highlight positions
Usage:
entries = [
{ text: "2024-01-15-project", base_score: 3.2 },
{ text: "2024-02-20-another", base_score: 1.5 },
]
fuzzy = Fuzzy.new(entries)
# Get all matches
fuzzy.match("proj").each do |entry, positions, score|
puts "#{entry[:text]} score=#{score} highlight=#{positions.inspect}"
end
# Limit results
fuzzy.match("proj").limit(10).each { |entry, positions, score| ... }
Defined Under Namespace
Classes: Entry, MatchResult
Instance Method Summary collapse
-
#initialize(entries) ⇒ Fuzzy
constructor
A new instance of Fuzzy.
-
#match(query) ⇒ Object
Returns a MatchResult enumerator for the query.
Constructor Details
#initialize(entries) ⇒ Fuzzy
Returns a new instance of Fuzzy.
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/fuzzy.rb', line 23 def initialize(entries) @entries = entries.map do |e| text = e[:text] || e["text"] Entry.new( data: e, text: text, text_lower: text.downcase, base_score: e[:base_score] || e["base_score"] || 0.0 ) end end |
Instance Method Details
#match(query) ⇒ Object
Returns a MatchResult enumerator for the query
36 37 38 |
# File 'lib/fuzzy.rb', line 36 def match(query) MatchResult.new(@entries, query.to_s) end |