The majority of the fuzzy matching logic included in this project is taken from Neil Fraser's google-diff-match-patch
FuzzyMatchOptions
can be passed to any of these methods to alter how the strict or loose the fuzzy matching algorithm operates.
threshold
inFuzzyMatchOptions
defines how strict you want to be when fuzzy matching. A value of 0.0 is equivalent to an exact match. A value of 1.0 indicates a very loose understanding of whether a match has been found.distance
inFuzzyMatchOptions
defines where in the host String to look for the pattern.
"abcdef".fuzzyMatchPattern("ab") // returns 0
"abcdef".fuzzyMatchPattern("z") // returns nil
"🐶🐱🐶🐶🐶".fuzzyMatchPattern("🐱") // returns 1
Returns a new instance of an Array which is sorted by the closest fuzzy match. Does not sort the host Array in place. Will always return the same number of elements that are found in the host Array.
["one", "two", "three"].sortedByFuzzyMatchPattern("on")
// returns ["one", "two", "three"]
["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"].sortedByFuzzyMatchPattern("on")
// returns ["one", "nine", "two", "four", "seven", "ten", "three", "five", "six", "eight"]
["one", "one", "two"].sortedByFuzzyMatchPattern("on")
// returns ["one", "one", "two"]
A confidence level allows client code to understand how likely the fuzzy searching algorithm is to find a pattern within a host String. confidenceScore
returns a Double which indicates how confident we are that the pattern can be found in the host String. A low value (0.001) indicates that the pattern is likely to be found. A high value (0.999) indicates that the pattern is not likely to be found.
"Stacee Lima".confidenceScore("SL") // returns 0.5
"abcdef".confidenceScore("g") // returns nil
"🐶🐱🐶🐶🐶".confidenceScore("🐱") // returns 0.001
"🐶🐱🐶🐶🐶".confidenceScore("🐱🐱🐱🐱🐱") // returns 0.8
All documentation is maintained at Cocoadocs
- iOS >= 18.0
- MacOS >= 15.0
- watchOS >= 11.0
- tvOS >= 8.0
FuzzyMatchingSwift is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "FuzzyMatchingSwift"
FuzzyMatchingSwift is available via Carthage. To install, just add this entry to your Cartfile:
github "seanoshea/FuzzyMatchingSwift"
Once you've altered your Cartfile, simply run carthage update
. Check out the instructions in Carthage's README for up to date installation instructions.
SwiftLint can be run on the codebase with:
swiftlint lint --config .swiftlint.yml
seanoshea, oshea.ie@gmail.com. See the Acknowledgements section for the original basis for this code.
FuzzyMatchingSwift is available under the Apache 2 license. See the LICENSE file for more info.
See the Contributing Instructions for details.