一款轻量的模糊查询工具库fuse-swift

一款轻量的模糊查询工具库fuse-swift

  • 开源协议:MIT
  • 操作系统: IOS
  • 开发语言:Swift
  • 项目所有者:krisk
  • 收录时间:2017-05-31
  • 分享:
编辑评级
3

项目详细介绍

什么是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) // [CountableRange(0..<0), CountableRange(2..<6), CountableRange(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: [CountableRange(5..<7)])]
// ---------------
// index: 0
// score: 0.028
// results: [(key: "title", score: 0.027999999999999997, ranges: [CountableRange(4..<6)])]

选项

Fuse 需要以下选项:

  • location:大概在文本中是预期找到的模式。默认为0
  • distance:确定匹配必须与模糊location(上面指定)有多近。distance与模糊位置相距字符的确切字母匹配将作为完全不匹配得分。的距离0需要匹配在准确location指定一个distance1000将需要一个完美的匹配是内800模糊位置的字符,以使用阈值0.8中找到。默认为100
  • threshold:匹配算法何时放弃。一个门槛0.0需要一个完美的匹配(字母和位置),一个阈值1.0将匹配任何东西。默认为0.6
  • maxPatternLength:最大有效花样长度。模式越长,搜索操作就越集中。如果模式超过maxPatternLengthsearch操作将返回nil。为什么这很重要?阅读此。默认为32
  • isCaseSensitive:表示比较是否区分大小写。默认为false

示例项目

要运行示例项目,请克隆repo,然后pod install从Example目录运行。