mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
Merge pull request #1289 from idkclub/categories
Add support for passing in categories to the unicode61 tokenizer
This commit is contained in:
2 files changed
+28
No files matched your search
@@ -128,6 +128,8 @@ public struct FTS5TokenizerDescriptor {
|
||||
/// - parameters:
|
||||
/// - diacritics: By default SQLite will strip diacritics from
|
||||
/// latin characters.
|
||||
/// - categories: Unless empty (the default), SQLite will consider
|
||||
/// "L* N* Co" Unicode categories for tokens.
|
||||
/// - separators: Unless empty (the default), SQLite will consider
|
||||
/// these characters as token separators.
|
||||
/// - tokenCharacters: Unless empty (the default), SQLite will
|
||||
@@ -136,6 +138,7 @@ public struct FTS5TokenizerDescriptor {
|
||||
/// See <https://www.sqlite.org/fts5.html#unicode61_tokenizer>
|
||||
public static func unicode61(
|
||||
diacritics: FTS5.Diacritics = .removeLegacy,
|
||||
categories: String = "",
|
||||
separators: Set<Character> = [],
|
||||
tokenCharacters: Set<Character> = [])
|
||||
-> FTS5TokenizerDescriptor
|
||||
@@ -154,6 +157,17 @@ public struct FTS5TokenizerDescriptor {
|
||||
components.append(contentsOf: ["remove_diacritics", "2"])
|
||||
#endif
|
||||
}
|
||||
if !categories.isEmpty {
|
||||
let separatorComponents = try! DatabaseQueue().inDatabase { db in
|
||||
try [
|
||||
"categories",
|
||||
categories
|
||||
.sqlExpression
|
||||
.quotedSQL(db)
|
||||
]
|
||||
}
|
||||
components.append(contentsOf: separatorComponents)
|
||||
}
|
||||
if !separators.isEmpty {
|
||||
// TODO: test "=" and "\"", "(" and ")" as separators, with
|
||||
// both FTS3Pattern(matchingAnyTokenIn:tokenizer:)
|
||||
|
||||
@@ -197,6 +197,20 @@ class FTS5TokenizerTests: GRDBTestCase {
|
||||
}
|
||||
#endif
|
||||
|
||||
func testUnicode61TokenizerCategories() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.create(virtualTable: "documents", using: FTS5()) { t in
|
||||
t.tokenizer = .unicode61(categories: "L* N* S*")
|
||||
t.column("content")
|
||||
}
|
||||
|
||||
XCTAssertTrue(match(db, "👍", "👍"))
|
||||
XCTAssertTrue(match(db, "👁🗨", "🗨"))
|
||||
XCTAssertFalse(match(db, "🔎🐠", "🐠"))
|
||||
}
|
||||
}
|
||||
|
||||
func testUnicode61TokenizerSeparators() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
|
||||
Reference in new issue
Block a user