Documentation

This commit is contained in:
Gwendal Roué committed 2016-10-10 20:14:53 +02:00
1 parent ad98339de7
commit d8eb8499ef
3 files changed
+46 -19

No files matched your search

+3 -2
View File
@@ -4,7 +4,7 @@
/// See FTS5Tokenizer.tokenize(context:flags:pText:nText:tokenCallback:)
public typealias FTS5TokenCallback = @convention(c) (_ context: UnsafeMutableRawPointer?, _ flags: Int32, _ pToken: UnsafePointer<Int8>?, _ nToken: Int32, _ iStart: Int32, _ iEnd: Int32) -> Int32
/// Flags that tells tokenizers how to tokenize text.
/// Flags that indicate the reason why FTS5 is requesting tokenization.
///
/// See https://www.sqlite.org/fts5.html#custom_tokenizers
public struct FTS5TokenizeFlags : OptionSet {
@@ -37,7 +37,8 @@
/// - parameters:
/// - context: An opaque pointer that is the first argument to
/// the `tokenCallback` function
/// - flags: Flags that tells how to tokenize the text.
/// - flags: Flags that indicate the reason why FTS5 is
/// requesting tokenization.
/// - pText: The tokenized text bytes. May or may not be
/// nul-terminated.
/// - nText: The number of bytes in the tokenized text.
+24 -3
View File
@@ -1,4 +1,7 @@
#if SQLITE_ENABLE_FTS5
/// Flags that tell SQLite how to register a token.
///
/// See https://www.sqlite.org/fts5.html#custom_tokenizers
public struct FTS5TokenFlags : OptionSet {
public let rawValue: Int32
@@ -10,12 +13,29 @@
public static let colocated = FTS5TokenFlags(rawValue: FTS5_TOKEN_COLOCATED)
}
public typealias FTS5TokenNotifier = (_ token: String, _ flags: FTS5TokenFlags) throws -> ()
/// A function that lets tokenizers notify tokens.
public typealias FTS5WrapperTokenCallback = (_ token: String, _ flags: FTS5TokenFlags) throws -> ()
/// The protocol for custom FTS5 tokenizers that wrap another tokenizer.
public protocol FTS5WrapperTokenizer : FTS5CustomTokenizer {
/// The wrapped tokenizer
var wrappedTokenizer: FTS5Tokenizer { get }
/// Returns whether the tokens emitted by the wrapped tokenizer should
/// be processed by `accept(token:flags:tokenCallback:)`.
///
/// - parameter flags: Flags that indicate the reason why FTS5 is
/// requesting tokenization.
func customizesWrappedTokenizer(flags: FTS5TokenizeFlags) -> Bool
func accept(token: String, flags: FTS5TokenFlags, notify: FTS5TokenNotifier) throws
/// Given a token produced by the wrapped tokenizer, notifies custom
/// tokens to the `tokenCallback` function.
///
/// - parameters:
/// - token: A token produced by the wrapped tokenizer
/// - flags: Flags that tell SQLite how to register a token.
/// - tokenCallback: The function to call for each found token.
func accept(token: String, flags: FTS5TokenFlags, tokenCallback: FTS5WrapperTokenCallback) throws
}
private struct FTS5WrapperContext {
@@ -25,6 +45,7 @@
}
extension FTS5WrapperTokenizer {
/// Default implementation
public func tokenize(context: UnsafeMutableRawPointer?, flags: FTS5TokenizeFlags, pText: UnsafePointer<Int8>?, nText: Int32, tokenCallback: FTS5TokenCallback?) -> Int32 {
// Let wrappedTokenizer do the job unless we customize
guard customizesWrappedTokenizer(flags: flags) else {
@@ -51,7 +72,7 @@
// Process token produced by wrapped tokenizer
do {
try tokenizer.accept(token: token, flags: FTS5TokenFlags(rawValue: flags), notify: { (token, flags) in
try tokenizer.accept(token: token, flags: FTS5TokenFlags(rawValue: flags), tokenCallback: { (token, flags) in
// Turn token into bytes
return try ContiguousArray(token.utf8).withUnsafeBufferPointer { buffer in
guard let addr = buffer.baseAddress else {
@@ -12,7 +12,6 @@ import Foundation
private final class StopWordsTokenizer : FTS5WrapperTokenizer {
static let name = "stopWords"
var wrappedTokenizer: FTS5Tokenizer
let ignoredTokens: [String]
init(db: Database, arguments: [String]) throws {
if arguments.isEmpty {
@@ -20,17 +19,20 @@ private final class StopWordsTokenizer : FTS5WrapperTokenizer {
} else {
wrappedTokenizer = try db.makeTokenizer(FTS5TokenizerDescriptor(components: arguments))
}
ignoredTokens = ["bar"]
}
func ignores(_ token: String) -> Bool {
return token == "bar"
}
func customizesWrappedTokenizer(flags: FTS5TokenizeFlags) -> Bool {
return true
}
func accept(token: String, flags: FTS5TokenFlags, notify: FTS5TokenNotifier) throws {
func accept(token: String, flags: FTS5TokenFlags, tokenCallback: FTS5WrapperTokenCallback) throws {
// Notify token unless ignored
if !ignoredTokens.contains(token){
try notify(token, flags)
if !ignores(token){
try tokenCallback(token, flags)
}
}
}
@@ -52,9 +54,9 @@ private final class NFKCTokenizer : FTS5WrapperTokenizer {
return true
}
func accept(token: String, flags: FTS5TokenFlags, notify: FTS5TokenNotifier) throws {
func accept(token: String, flags: FTS5TokenFlags, tokenCallback: FTS5WrapperTokenCallback) throws {
// Convert token to NFKC
try notify(token.precomposedStringWithCompatibilityMapping, flags)
try tokenCallback(token.precomposedStringWithCompatibilityMapping, flags)
}
}
@@ -62,15 +64,18 @@ private final class NFKCTokenizer : FTS5WrapperTokenizer {
private final class SynonymsTokenizer : FTS5WrapperTokenizer {
static let name = "synonyms"
let wrappedTokenizer: FTS5Tokenizer
let synonyms: [Set<String>]
init(db: Database, arguments: [String]) throws {
if arguments.isEmpty {
wrappedTokenizer = try db.makeTokenizer(.unicode61())
} else {
wrappedTokenizer = try db.makeTokenizer(FTS5TokenizerDescriptor(components: arguments))
}
synonyms = [["first", "1st"]]
}
func synonyms(for token: String) -> Set<String>? {
let synonyms: [Set<String>] = [["first", "1st"]]
return synonyms.first(where: { $0.contains(token) })
}
func customizesWrappedTokenizer(flags: FTS5TokenizeFlags) -> Bool {
@@ -79,18 +84,18 @@ private final class SynonymsTokenizer : FTS5WrapperTokenizer {
return !flags.contains(.query)
}
func accept(token: String, flags: FTS5TokenFlags, notify: FTS5TokenNotifier) throws {
if let synonyms = synonyms.first(where: { $0.contains(token) }) {
func accept(token: String, flags: FTS5TokenFlags, tokenCallback: FTS5WrapperTokenCallback) throws {
if let synonyms = synonyms(for: token) {
for (index, synonym) in synonyms.enumerated() {
// Notify each synonym, and set the colocated flag for all but
// the first, as documented by
// https://www.sqlite.org/fts5.html#synonym_support
let synonymFlags = (index == 0) ? flags : flags.union(.colocated)
try notify(synonym, synonymFlags)
try tokenCallback(synonym, synonymFlags)
}
} else {
// Token has no synonym
try notify(token, flags)
try tokenCallback(token, flags)
}
}
}