Fuzzi
Swift 中本地搜索变得简单(并且模糊化)。
Wat??
Fuzzi 允许您高效地搜索大量数据。它使用 BK-Tree 以及基于字符串的 Levenshtein 距离的加权大小分数来查找正确的结果并进行相应排序。
安装
Fuzzi 可在 Cocoapods 上使用。(至少很快会。)只需将其添加到您的 Podfile 中
pod 'Fuzzi'
用法
首先定义您想要搜索的内容。让它实现 Searchable
协议。这将要求您的数据既是可散列的、可比较的,也可编码和解码,同时还需要提供用于搜索数据的字符串。
例如:
struct Repo {
let name: String
let description: String
let link: URL
}
extension Repo: Searchable {
var hashValue: Int {
return name.hashValue ^ description.hashValue ^ link.hashValue
}
static func == (lhs: Repo, rhs: Repo) -> Bool {
return lhs.name == rhs.name &&
lhs.description == rhs.description &&
lhs.link == rhs.link
}
var searchableProperties: [KeyPath<Repo, String>] {
return [
\.name,
\.description,
]
}
}
每当您有一组这些对象时,只需使用 .tree()
创建搜索树。
let tree = repos.tree()
tree.search(for: "Fzzi") // Should return the "Fuzzi" repo
贡献
欢迎提交pull request!