/FuzzyMatchingSwift

Fuzzy matching String extensions

Primary LanguageSwiftOtherNOASSERTION

FuzzyMatchingSwift

CI Status Code Coverage PRs Welcome License Version Platform Carthage compatible Swift 5.2.x Languages Top Language Open Issues Closed Issues Twitter: @seanoshea

Acknowledgements

The majority of the fuzzy matching logic included in this project is taken from Neil Fraser's google-diff-match-patch

Usage

Matching on Strings

FuzzyMatchOptions can be passed to any of these methods to alter how the strict or loose the fuzzy matching algorithm operates.

  • threshold in FuzzyMatchOptions 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 in FuzzyMatchOptions defines where in the host String to look for the pattern.
"abcdef".fuzzyMatchPattern("ab") // returns 0
"abcdef".fuzzyMatchPattern("z") // returns nil
"🐢🐱🐢🐢🐢".fuzzyMatchPattern("🐱") // returns 1

Matching on Arrays of Strings

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"]

Providing a confidence level

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

Documentation

All documentation is maintained at Cocoadocs

Requirements

  • iOS >= 18.0
  • MacOS >= 15.0
  • watchOS >= 11.0
  • tvOS >= 8.0

Installation

CocoaPods

FuzzyMatchingSwift is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "FuzzyMatchingSwift"

Carthage

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

SwiftLint can be run on the codebase with:

swiftlint lint --config .swiftlint.yml

Author

seanoshea, oshea.ie@gmail.com. See the Acknowledgements section for the original basis for this code.

License

FuzzyMatchingSwift is available under the Apache 2 license. See the LICENSE file for more info.

Contributing

See the Contributing Instructions for details.