A Crystal port of awesome Fzy fuzzy finder algorithm.
- Add the dependency to your
shard.yml
:
dependencies:
fzy:
github: hugopl/fzy
- Run
shards install
Basic usage:
require "fzy"
matches = Fzy.search("hey", %w(Hey Whatever Halley), store_positions: true)
matches.each do |match|
puts "value: #{match.item}"
puts "score: #{match.score}"
puts " pos: #{match.positions.inspect}"
end
Should print
value: Hey
score: Infinity
pos: [0, 1, 2]
value: Halley
score: 1.87
pos: [0, 4, 5]
If you need to do many searches on the same set of data you can speed up things by
creating an Fzy::PreparedHaystack
or an array of Fzy::Hay
.
require "fzy"
haystack = %w(Hey Halley Whatever)
prepared_haystack = Fzy::PreparedHaystack.new(haystack)
matches = Fzy.search("hey", prepared_haystack)
matches.each do |match|
puts "value: #{match.item}"
puts "score: #{match.score}"
puts " pos: #{match.positions.inspect}"
end
# Reusing the prepared haystack makes the search faster.
matches = Fzy.search("ho let's go!", prepared_haystack)
- Fork it (https://github.com/hugopl/fzy/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
- Hugo Parente Lima - creator and maintainer