Fuzzy Matching inspired by this blog post for Crystal.
Fuzzy Match provides search functionality similar to code editors such as Sublime Text 2 file searching.
- Add the dependency to your
shard.yml
:
dependencies:
fuzzy_match:
github: acoustep/fuzzy_match.cr
version: ~> 0.3.0
- Run
shards install
The Simple
struct provides a simple yes/no check on whether a pattern matches a string.
The Full
struct provides a score which can then be sorted.
For most use cases, you'll want to pass a pattern and a list of file paths. For this you can use FuzzyMatch.search
.
require "fuzzy_match"
# Simple
FuzzyMatch::Simple.new("mvc", "ModelViewController").matches? # true
FuzzyMatch::Simple.new("xyz", "ModelViewController").matches? # false
# Full
FuzzyMatch::Full.new("view", "ModelViewController").matches? # true
FuzzyMatch::Full.new("view", "ModelViewController").score # 60
[
FuzzyMatch::Full.new("view", "ModelViewController"),
FuzzyMatch::Full.new("view", "SearchViewController"),
FuzzyMatch::Full.new("view", ".gitignore"),
]
.select { |q| q.matches? }
.sort(&.score)
# Convenience method for searching multiple at a time
results = FuzzyMatch.search("view", ["ModelViewController", "SearchViewController", ".gitignore"])
results.size # 2
results[0].str # ModelViewController
results[0].score # 60
results[0].matches? # true
Sometimes you want to be able to access matched letters. For example, you may wish to underline them with HTML.
query = FuzzyMatch::Full.new("mvc", "ModelViewController")
str = String::Builder.build do |builder|
query.str.chars.each_with_index do |char, index|
builder << "<span class='underline'>" if query.matched_indexes.includes?(index)
builder << query.str[index]
builder << "</span>" if query.matched_indexes.includes?(index)
end
end
p str # <span class='underline'>M</span>odel<span class='underline'>V</span>iew<span class='underline'>C</span>ontroller
- Web demo
To test speed build benchmark.cr
and run. This runs through a list of roughly 3000 files.
crystal build --release --no-debug src/benchmark.cr
./benchmark
- Fork it (https://github.com/acoustep/fuzzy_match/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
- Mitch Stanley - creator and maintainer