mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-30 04:28:44 +00:00
Test the custom FTS5 tokenizer with a "stop words" tokenizer
This commit is contained in:
1 parent
f7c8bd6d1c
commit
ba42932024
1 file changed
+70
-23
@@ -7,23 +7,56 @@ import XCTest
|
||||
import GRDB
|
||||
#endif
|
||||
|
||||
private final class CustomTokenizer : FTS5CustomTokenizer {
|
||||
static let name = "custom"
|
||||
// A custom tokenizer that ignores some tokens
|
||||
private final class StopWordsTokenizer : FTS5CustomTokenizer {
|
||||
static let name = "stopWords"
|
||||
|
||||
let porter: FTS5Tokenizer
|
||||
let ignoredTokens: [String]
|
||||
|
||||
init(db: Database, arguments: [String]) throws {
|
||||
// TODO: test wrapped tokenizer options
|
||||
porter = try db.makeTokenizer(.porter())
|
||||
print(arguments)
|
||||
// TODO: find a way to provide stop words through arguments
|
||||
ignoredTokens = ["bar"]
|
||||
}
|
||||
|
||||
deinit {
|
||||
print("CustomTokenizer deinit")
|
||||
print("StopWordsTokenizer deinit")
|
||||
}
|
||||
|
||||
func tokenize(_ context: UnsafeMutableRawPointer?, _ flags: FTS5TokenizeFlags, _ pText: UnsafePointer<Int8>?, _ nText: Int32, _ xToken: FTS5TokenCallback?) -> Int32 {
|
||||
return porter.tokenize(context, flags, pText, nText, xToken)
|
||||
|
||||
// The way we implement stop words is by letting porter do its job, but
|
||||
// intercepting its tokens before they feed SQLite.
|
||||
|
||||
struct WrapperContext {
|
||||
let ignoredTokens: [String]
|
||||
let context: UnsafeMutableRawPointer
|
||||
let xToken: FTS5TokenCallback
|
||||
}
|
||||
var wrapperContext = WrapperContext(ignoredTokens: ignoredTokens, context: context!, xToken: xToken!)
|
||||
|
||||
return withUnsafeMutablePointer(to: &wrapperContext) { wrapperContextPointer in
|
||||
// Intercept raw porter tokens, and strip stop words
|
||||
return porter.tokenize(wrapperContextPointer, flags, pText, nText) { (wrapperContextPointer, flags, pToken, nToken, iStart, iEnd) in
|
||||
// Extract context
|
||||
let wrapperContext = wrapperContextPointer!.assumingMemoryBound(to: WrapperContext.self).pointee
|
||||
|
||||
// Extract token
|
||||
let token = pToken.flatMap {
|
||||
String(data: Data(bytesNoCopy: UnsafeMutableRawPointer(mutating: $0), count: Int(nToken), deallocator: .none), encoding: .utf8)
|
||||
}
|
||||
|
||||
// Ignore stop words
|
||||
if let token = token, wrapperContext.ignoredTokens.contains(token) {
|
||||
return 0 // SQLITE_OK
|
||||
}
|
||||
|
||||
// Notify token
|
||||
return wrapperContext.xToken(wrapperContext.context, flags, pToken, nToken, iStart, iEnd)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,18 +65,24 @@ class FTS5CustomTokenizerTests: GRDBTestCase {
|
||||
func testDatabaseQueue() {
|
||||
assertNoError {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
dbQueue.add(tokenizer: CustomTokenizer.self)
|
||||
dbQueue.add(tokenizer: StopWordsTokenizer.self)
|
||||
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.create(virtualTable: "documents", using: FTS5()) { t in
|
||||
t.tokenizer = CustomTokenizer.tokenizer(arguments: ["foo", "bar"])
|
||||
// TODO: improve this API
|
||||
t.tokenizer = StopWordsTokenizer.tokenizer()
|
||||
t.column("content")
|
||||
}
|
||||
|
||||
try db.execute("INSERT INTO documents VALUES (?)", arguments: ["foo bar"])
|
||||
try db.execute("INSERT INTO documents VALUES (?)", arguments: ["foo"])
|
||||
let countFoo = Int.fetchOne(db, "SELECT COUNT(*) FROM documents WHERE documents MATCH ?", arguments: ["foo"])
|
||||
print(countFoo)
|
||||
let countBar = Int.fetchOne(db, "SELECT COUNT(*) FROM documents WHERE documents MATCH ?", arguments: ["bar"])
|
||||
print(countBar)
|
||||
try db.execute("INSERT INTO documents VALUES (?)", arguments: ["foo baz"])
|
||||
|
||||
// foo is not ignored
|
||||
XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM documents WHERE documents MATCH ?", arguments: ["foo"]), 2)
|
||||
// bar is ignored
|
||||
XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM documents WHERE documents MATCH ?", arguments: ["bar"]), 0)
|
||||
// bar is ignored in queries too: the "foo bar baz" phrase matches the "foo baz" content
|
||||
XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM documents WHERE documents MATCH ?", arguments: ["\"foo bar baz\""]), 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -51,24 +90,32 @@ class FTS5CustomTokenizerTests: GRDBTestCase {
|
||||
func testDatabasePool() {
|
||||
assertNoError {
|
||||
let dbPool = try makeDatabaseQueue()
|
||||
dbPool.add(tokenizer: CustomTokenizer.self)
|
||||
dbPool.add(tokenizer: StopWordsTokenizer.self)
|
||||
|
||||
try dbPool.write { db in
|
||||
try db.create(virtualTable: "documents", using: FTS5()) { t in
|
||||
t.tokenizer = CustomTokenizer.tokenizer(arguments: ["foo", "bar"])
|
||||
t.tokenizer = StopWordsTokenizer.tokenizer(arguments: ["foo", "bar"])
|
||||
t.column("content")
|
||||
}
|
||||
|
||||
try db.execute("INSERT INTO documents VALUES (?)", arguments: ["foo bar"])
|
||||
try db.execute("INSERT INTO documents VALUES (?)", arguments: ["foo"])
|
||||
let countFoo = Int.fetchOne(db, "SELECT COUNT(*) FROM documents WHERE documents MATCH ?", arguments: ["foo"])
|
||||
print(countFoo)
|
||||
let countBar = Int.fetchOne(db, "SELECT COUNT(*) FROM documents WHERE documents MATCH ?", arguments: ["bar"])
|
||||
print(countBar)
|
||||
try db.execute("INSERT INTO documents VALUES (?)", arguments: ["foo baz"])
|
||||
|
||||
// foo is not ignored
|
||||
XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM documents WHERE documents MATCH ?", arguments: ["foo"]), 2)
|
||||
// bar is ignored
|
||||
XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM documents WHERE documents MATCH ?", arguments: ["bar"]), 0)
|
||||
// bar is ignored in queries too: the "foo bar baz" phrase matches the "foo baz" content
|
||||
XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM documents WHERE documents MATCH ?", arguments: ["\"foo bar baz\""]), 1)
|
||||
}
|
||||
|
||||
dbPool.read { db in
|
||||
let countFoo = Int.fetchOne(db, "SELECT COUNT(*) FROM documents WHERE documents MATCH ?", arguments: ["foo"])
|
||||
print(countFoo)
|
||||
let countBar = Int.fetchOne(db, "SELECT COUNT(*) FROM documents WHERE documents MATCH ?", arguments: ["bar"])
|
||||
print(countBar)
|
||||
// foo is not ignored
|
||||
XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM documents WHERE documents MATCH ?", arguments: ["foo"]), 2)
|
||||
// bar is ignored
|
||||
XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM documents WHERE documents MATCH ?", arguments: ["bar"]), 0)
|
||||
// bar is ignored in queries too: the "foo bar baz" phrase matches the "foo baz" content
|
||||
XCTAssertEqual(Int.fetchOne(db, "SELECT COUNT(*) FROM documents WHERE documents MATCH ?", arguments: ["\"foo bar baz\""]), 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user