Fuse

CI Status Version License Platform Donate Donate

什么是 Fuse?

Fuse 是一个超轻量级的库,它提供了一种简单的方法来进行模糊搜索。

Demo

用法

示例 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)]

示例 2

在一个字符串数组中搜索文本模式。

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)
}

示例 3

class Book: Fuseable {
    dynamic var name: String
    dynamic var author: String

    var properties: [FuseProperty] {
        return [
            FuseProperty(name: "title", weight: 0.3),
            FuseProperty(name: "author", weight: 0.7),
        ]
    }
}

let books: [Book] = [
    Book(author: "John X", title: "Old Man's War fiction"),
    Book(author: "P.D. Mans", title: "Right Ho Jeeves")
]

let fuse = Fuse()
let results = fuse.search("man", in: books)

results.forEach { item in
    print("index: " + item.index)
    print("score: " + item.score)
    print("results: " + item.results)
    print("---------------")
}

// Output:
//
// index: 1
// score: 0.015
// results: [(key: "author", score: 0.015000000000000003, ranges: [CountableClosedRange(5...7)])]
// ---------------
// index: 0
// score: 0.028
// results: [(key: "title", score: 0.027999999999999997, ranges: [CountableClosedRange(4...6)])]

示例 4

let fuse = Fuse()
fuse.search("Man", in: books, completion: { results in
    print(results)
})

选项

Fuse 接受以下选项

示例项目

要运行示例项目,请克隆该仓库,然后首先从 Example 目录运行 pod install

要求

安装

Fuse 可通过 CocoaPods 获得。 要安装它,只需将以下行添加到您的 Podfile 中

pod "Fuse"

许可证

Fuse 在 MIT 许可证下可用。 有关更多信息,请参阅 LICENSE 文件。