Fuse is a super lightweight library which provides a simple way to do fuzzy searching.
Usage
Example 1
let fuse = Fuse()
let result = fuse.search("od mn war", in: "Old Man's War")
print(result?.score) // 0.44444444444444442
print(result?.ranges) // [CountableClosedRange(0...0), CountableClosedRange(2...6), CountableClosedRange(9...12)]
Example 2
Search for a text pattern in an array of srings.
let books = ["The Silmarillion", "The Lock Artist", "The Lost Symbol"]
let fuse = Fuse()
// Improve performance by creating the pattern once
let pattern = fuse.createPattern(from: "Te silm")
// Search for the pattern in every book
books.forEach {
let result = fuse.search(pattern, in: $0)
print(result?.score)
print(result?.ranges)
}
let fuse = Fuse()
fuse.search("Man", in: books, completion: { results in
print(results)
})
Options
Fuse takes the following options:
location: Approximately where in the text is the pattern expected to be found. Defaults to 0
distance: Determines how close the match must be to the fuzzy location (specified above). An exact letter match which is distance characters away from the fuzzy location would score as a complete mismatch. A distance of 0 requires the match be at the exact location specified, a distance of 1000 would require a perfect match to be within 800 characters of the fuzzy location to be found using a 0.8 threshold. Defaults to 100
threshold: At what point does the match algorithm give up. A threshold of 0.0 requires a perfect match (of both letters and location), a threshold of 1.0 would match anything. Defaults to 0.6
maxPatternLength: The maximum valid pattern length. The longer the pattern, the more intensive the search operation will be. If the pattern exceeds the maxPatternLength, the search operation will return nil. Why is this important? Read this. Defaults to 32
isCaseSensitive: Indicates whether comparisons should be case sensitive. Defaults to false
Example Project
To run the example project, clone the repo, and run pod install from the Example directory first.
Requirements
Installation
Fuse is available through CocoaPods. To install
it, simply add the following line to your Podfile:
pod "Fuse"
License
Fuse is available under the MIT license. See the LICENSE file for more info.
Fuse
What is Fuse?
Fuse is a super lightweight library which provides a simple way to do fuzzy searching.
Usage
Example 1
Example 2
Search for a text pattern in an array of srings.
Example 3
Example 4
Options
Fusetakes the following options:location: Approximately where in the text is the pattern expected to be found. Defaults to0distance: Determines how close the match must be to the fuzzylocation(specified above). An exact letter match which isdistancecharacters away from the fuzzy location would score as a complete mismatch. A distance of0requires the match be at the exactlocationspecified, adistanceof1000would require a perfect match to be within800characters of the fuzzy location to be found using a 0.8 threshold. Defaults to100threshold: At what point does the match algorithm give up. A threshold of0.0requires a perfect match (of both letters and location), a threshold of1.0would match anything. Defaults to0.6maxPatternLength: The maximum valid pattern length. The longer the pattern, the more intensive the search operation will be. If the pattern exceeds themaxPatternLength, thesearchoperation will returnnil. Why is this important? Read this. Defaults to32isCaseSensitive: Indicates whether comparisons should be case sensitive. Defaults tofalseExample Project
To run the example project, clone the repo, and run
pod installfrom the Example directory first.Requirements
Installation
Fuse is available through CocoaPods. To install it, simply add the following line to your Podfile:
License
Fuse is available under the MIT license. See the LICENSE file for more info.