FuzzyMatchingSwift
致谢
本项目包含的大部分模糊匹配逻辑取自Neil Fraser's google-diff-match-patch
用法
字符串匹配
FuzzyMatchOptions
可以传递给这些方法中的任何一个,以改变模糊匹配算法操作的严格程度。
FuzzyMatchOptions
中的threshold
定义了您在模糊匹配时想要多严格。0.0 的值相当于精确匹配。1.0 的值表示对是否找到匹配项有非常宽松的理解。distance
在FuzzyMatchOptions
中定义了在哪里(在主字符串中)查找模式。
"abcdef".fuzzyMatchPattern("ab") // returns 0
"abcdef".fuzzyMatchPattern("z") // returns nil
"🐶🐱🐶🐶🐶".fuzzyMatchPattern("🐱") // returns 1
字符串数组匹配
返回一个新的数组实例,该数组按最近的模糊匹配进行排序。不会就地排序主数组。始终返回与主数组中找到的元素数量相同。
["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"]
提供置信度
置信度允许客户端代码了解模糊搜索算法在主字符串中找到模式的概率。confidenceScore
返回一个表示我们有多自信可以在主字符串中找到模式的双精度值。低值(0.001)表示模式很可能被找到。高值(0.999)表示模式不太可能被找到。
"Stacee Lima".confidenceScore("SL") // returns 0.5
"abcdef".confidenceScore("g") // returns nil
"🐶🐱🐶🐶🐶".confidenceScore("🐱") // returns 0.001
"🐶🐱🐶🐶🐶".confidenceScore("🐱🐱🐱🐱🐱") // returns 0.8
文档
所有文档都维护在Cocoadocs。
需求
- iOS >= 13.2
- MacOS >= 10.15
- watchOS >= 6.2
- tvOS >= 13.2
安装
CocoaPods
FuzzyMatchingSwift 可以通过 CocoaPods 安装。只需将以下行添加到您的 Podfile 即可
pod "FuzzyMatchingSwift"
Carthage
FuzzyMatchingSwift 还可以通过 Carthage 安装。只需将以下条目添加到您的 Cartfile
github "seanoshea/FuzzyMatchingSwift"
修改完 Cartfile 后,只需运行 carthage update
。有关最新的安装说明,请参阅 Carthage 的 README。
SwiftLint
SwiftLint 可以运行在代码库上
swiftlint lint --config .swiftlint.yml
作者
seanoshea,[email protected]。有关此代码原始基础的详细信息,请参阅致谢部分。
许可协议
FuzzyMatchingSwift 采用 Apache 2 许可协议。有关更多信息,请参阅 LICENSE 文件。
贡献
有关详细信息,请参阅 贡献说明。