Fuse 1.4.0

Fuse 1.4.0

测试已测试
语言语言 SwiftSwift
许可协议 MIT 协议
发布最新发布2020 年 5 月
SPM支持 SPM

Kirollos Risk维护。



Fuse 1.4.0

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 下列选项:

  • location:模式预计在文本中的大致位置。默认为 0
  • distance:决定了匹配必须接近到模糊 location(如上所述)的程度。与模糊位置 distance 字符之外的精确字母匹配会被计为完全不匹配。距离为 0 意味着匹配必须在指定的精确 location,距离为 1000 则意味着完美匹配必须在找到模糊位置的情况下处于模糊位置的 800 字符内,使用 0.8 阈值。默认为 100
  • threshold:匹配算法在什么情况下放弃。阈值为 0.0 需要完全匹配(大小写和位置),阈值为 1.0 则匹配任何内容。默认为 0.6
  • maxPatternLength:最大有效模式长度。模式越长,搜索操作越密集。如果模式超过 maxPatternLength,则 search 操作会返回 nil。这为什么重要?阅读此内容。默认为 32
  • isCaseSensitive:指示比较应否区分大小写。默认为 false

示例项目

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

需求

安装

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

pod "Fuse"

许可协议

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