From ba42932024260c65717f65379876e76c7d6f9be2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 9 Oct 2016 23:09:22 +0200 Subject: [PATCH] Test the custom FTS5 tokenizer with a "stop words" tokenizer --- .../Public/FTS/FTS5CustomTokenizerTests.swift | 93 ++++++++++++++----- 1 file changed, 70 insertions(+), 23 deletions(-) diff --git a/Tests/Public/FTS/FTS5CustomTokenizerTests.swift b/Tests/Public/FTS/FTS5CustomTokenizerTests.swift index f39a60e4f..ef7de0b6b 100644 --- a/Tests/Public/FTS/FTS5CustomTokenizerTests.swift +++ b/Tests/Public/FTS/FTS5CustomTokenizerTests.swift @@ -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?, _ 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) } } }