Documentation

This commit is contained in:
Gwendal Roué committed 2016-10-11 06:55:31 +02:00
1 parent 52ef16dba5
commit e56a4ed9e2
6 files changed
+90 -25

No files matched your search

+69 -4
View File
@@ -8,7 +8,7 @@ GRDB lets you define your own custom FST5 tokenizers, and extend SQLite built-in
- Have "fi" match the ligature "fi" (U+FB01)
- Have "first" match "1st"
- Have "Encyclopaedia" match "Encyclopædia"
- Have "Mueller" match "Müller", and "Grossman" match "Großmann"
- Have "Mueller" match "Müller", and "Grossmann" match "Großmann"
- Have "romaji" match "ローマ字"
- Have "pinyin" match "拼音"
- Prevent "the" and other stop words from matching any document
@@ -58,14 +58,14 @@ It only requires a tokenization method that matches the `xTokenize` C function d
```swift
protocol FTS5Tokenizer : class {
func tokenize(context: UnsafeMutableRawPointer?, flags: FTS5TokenizeFlags, pText: UnsafePointer<Int8>?, nText: Int32, tokenCallback: FTS5TokenCallback?) -> Int32
func tokenize(context: UnsafeMutableRawPointer?, flags: FTS5TokenizationFlags, pText: UnsafePointer<Int8>?, nText: Int32, tokenCallback: FTS5TokenCallback?) -> Int32
}
```
You can instantiate tokenizers with the Database.makeTokenizer() method:
```swift
let ascii = db.makeTokenizer(.ascii()) // FTS5Tokenizer
let ascii = try db.makeTokenizer(.ascii()) // FTS5Tokenizer
```
Tokenizers can tokenize (and can produce different tokens depending on whether they are tokenizing a *document*, or a *query*):
@@ -134,12 +134,77 @@ final class BlackHoleTokenizer : FTS5CustomTokenizer {
init(db: Database, arguments: [String]) throws {
}
func tokenize(context: UnsafeMutableRawPointer?, flags: FTS5TokenizeFlags, pText: UnsafePointer<Int8>?, nText: Int32, tokenCallback: FTS5TokenCallback?) -> Int32 {
func tokenize(context: UnsafeMutableRawPointer?, flags: FTS5TokenizationFlags, pText: UnsafePointer<Int8>?, nText: Int32, tokenCallback: FTS5TokenCallback?) -> Int32 {
return 0 // SQLITE_OK
}
}
```
Since tokenization is hard, and pointers to bytes buffers uneasy to deal with, you may enjoy then [FTS5WrapperTokenizer](#fts5wrappertokenizer) protocol.
## FTS5WrapperTokenizer
**FTS5WrapperTokenizer** is the low-level protocol for your custom tokenizers.
With this protocol, a custom tokenizer post-processes the tokens produced by another tokenizer, the "wrapped tokenizer", and does not have to implement the dreadful low-level `tokenize(context:flags:pText:nText:tokenCallback:)` method.
```swift
protocol FTS5WrapperTokenizer : FTS5CustomTokenizer {
var wrappedTokenizer: FTS5Tokenizer { get }
func customizesTokenization(flags: FTS5TokenizationFlags) -> Bool
func accept(token: String, flags: FTS5TokenFlags, tokenCallback: FTS5WrapperTokenCallback) throws
}
```
As all custom tokenizers, wrapper tokenizers must have a name:
```swift
final class MyTokenizer : FTS5WrapperTokenizer {
static let name = "custom"
}
```
The `wrappedTokenizer` property is the wrapped tokenizer. You instantiate it in the initializer:
```swift
final class MyTokenizer : FTS5WrapperTokenizer {
let wrappedTokenizer: FTS5Tokenizer
init(db: Database, arguments: [String]) throws {
// Wrap the unicode61 tokenizer
wrappedTokenizer = try db.makeTokenizer(.unicode61())
}
}
```
Some wrapper tokenizers sometimes want to opt-out token customization (see [Synonyms](#synonyms)). Most of them return true from their customizesTokenization method:
```swift
final class MyTokenizer : FTS5WrapperTokenizer {
func customizesTokenization(flags: FTS5TokenizationFlags) -> Bool {
return true
}
}
```
Finally, wrapper tokenizers can process tokens produced by their wrapped tokenizer. They can ignore tokens, modify tokens, and even notify several tokens to the FTS5 engine:
```swift
final class MyTokenizer : FTS5WrapperTokenizer {
func accept(token: String, flags: FTS5TokenFlags, tokenCallback: FTS5WrapperTokenCallback) throws {
// pass through
try tokenCallback(token, flags)
}
}
```
When implementing the accept method, there are a few rules to observe:
- Errors thrown by `tokenCallback` must not be caught.
- The input `flags` should be given unmodified to the tokenCallback function, with one exception: [synonyms tokens](#synonyms).
## Synonyms
TODO
+1 -1
View File
@@ -124,7 +124,7 @@
guard let tokenizer = object as? FTS5Tokenizer else {
return SQLITE_ERROR
}
return tokenizer.tokenize(context: context, flags: FTS5TokenizeFlags(rawValue: flags), pText: pText, nText: nText, tokenCallback: tokenCallback)
return tokenizer.tokenize(context: context, flags: FTS5TokenizationFlags(rawValue: flags), pText: pText, nText: nText, tokenCallback: tokenCallback)
}
var xTokenizer = fts5_tokenizer(xCreate: xCreateTokenizer, xDelete: xDeleteTokenizer, xTokenize: xTokenize)
+10 -10
View File
@@ -7,7 +7,7 @@
/// Flags that indicate the reason why FTS5 is requesting tokenization.
///
/// See https://www.sqlite.org/fts5.html#custom_tokenizers
public struct FTS5TokenizeFlags : OptionSet {
public struct FTS5TokenizationFlags : OptionSet {
public let rawValue: Int32
public init(rawValue: Int32) {
@@ -15,16 +15,16 @@
}
/// FTS5_TOKENIZE_QUERY
public static let query = FTS5TokenizeFlags(rawValue: FTS5_TOKENIZE_QUERY)
public static let query = FTS5TokenizationFlags(rawValue: FTS5_TOKENIZE_QUERY)
/// FTS5_TOKENIZE_PREFIX
public static let prefix = FTS5TokenizeFlags(rawValue: FTS5_TOKENIZE_PREFIX)
public static let prefix = FTS5TokenizationFlags(rawValue: FTS5_TOKENIZE_PREFIX)
/// FTS5_TOKENIZE_DOCUMENT
public static let document = FTS5TokenizeFlags(rawValue: FTS5_TOKENIZE_DOCUMENT)
public static let document = FTS5TokenizationFlags(rawValue: FTS5_TOKENIZE_DOCUMENT)
/// FTS5_TOKENIZE_AUX
public static let aux = FTS5TokenizeFlags(rawValue: FTS5_TOKENIZE_AUX)
public static let aux = FTS5TokenizationFlags(rawValue: FTS5_TOKENIZE_AUX)
}
/// The protocol for FTS5 tokenizers
@@ -44,14 +44,14 @@
/// - nText: The number of bytes in the tokenized text.
/// - tokenCallback: The function to call for each found token.
/// It matches the `xToken` callback at https://www.sqlite.org/fts5.html#custom_tokenizers
func tokenize(context: UnsafeMutableRawPointer?, flags: FTS5TokenizeFlags, pText: UnsafePointer<Int8>?, nText: Int32, tokenCallback: FTS5TokenCallback?) -> Int32
func tokenize(context: UnsafeMutableRawPointer?, flags: FTS5TokenizationFlags, pText: UnsafePointer<Int8>?, nText: Int32, tokenCallback: FTS5TokenCallback?) -> Int32
}
extension FTS5Tokenizer {
/// Returns an array of tokens found in the string argument.
///
/// let tokenizer = db.makeTokenizer(.ascii())
/// let tokenizer = try db.makeTokenizer(.ascii())
/// try tokenizer.tokenize("foo bar", flags: .document) // ["foo", "bar"]
///
/// - parameter string: The string to tokenize
@@ -59,7 +59,7 @@
/// - .document: Tokenize like a document being inserted into an FTS table.
/// - .query: Tokenize like the search pattern of the MATCH operator.
/// - parameter tokenizer: A FTS5TokenizerDescriptor such as .ascii()
public func tokenize(_ string: String, flags: FTS5TokenizeFlags) throws -> [String] {
public func tokenize(_ string: String, flags: FTS5TokenizationFlags) throws -> [String] {
return try ContiguousArray(string.utf8).withUnsafeBufferPointer { buffer -> [String] in
guard let addr = buffer.baseAddress else {
return []
@@ -150,7 +150,7 @@
}
}
func tokenize(context: UnsafeMutableRawPointer?, flags: FTS5TokenizeFlags, pText: UnsafePointer<Int8>?, nText: Int32, tokenCallback: FTS5TokenCallback?) -> Int32 {
func tokenize(context: UnsafeMutableRawPointer?, flags: FTS5TokenizationFlags, pText: UnsafePointer<Int8>?, nText: Int32, tokenCallback: FTS5TokenCallback?) -> Int32 {
guard let xTokenize = xTokenizer.xTokenize else {
return SQLITE_ERROR
}
@@ -160,7 +160,7 @@
/// Creates an FTS5 tokenizer, given its descriptor.
///
/// let unicode61 = db.makeTokenizer(.unicode61())
/// let unicode61 = try db.makeTokenizer(.unicode61())
///
/// It is a programmer error to use the tokenizer outside of a protected
/// database queue, or after the database has been closed.
+4 -4
View File
@@ -36,7 +36,7 @@
/// wrappedTokenizer = try db.makeTokenizer(.ascii())
/// }
///
/// func customizesWrappedTokenizer(flags: FTS5TokenizeFlags) -> Bool {
/// func customizesTokenization(flags: FTS5TokenizationFlags) -> Bool {
/// return true
/// }
///
@@ -53,7 +53,7 @@
///
/// - parameter flags: Flags that indicate the reason why FTS5 is
/// requesting tokenization.
func customizesWrappedTokenizer(flags: FTS5TokenizeFlags) -> Bool
func customizesTokenization(flags: FTS5TokenizationFlags) -> Bool
/// Given a token produced by the wrapped tokenizer, notifies customized
/// tokens to the `tokenCallback` function.
@@ -94,9 +94,9 @@
extension FTS5WrapperTokenizer {
/// Default implementation
public func tokenize(context: UnsafeMutableRawPointer?, flags: FTS5TokenizeFlags, pText: UnsafePointer<Int8>?, nText: Int32, tokenCallback: FTS5TokenCallback?) -> Int32 {
public func tokenize(context: UnsafeMutableRawPointer?, flags: FTS5TokenizationFlags, pText: UnsafePointer<Int8>?, nText: Int32, tokenCallback: FTS5TokenCallback?) -> Int32 {
// Let wrappedTokenizer do the job unless we customize
guard customizesWrappedTokenizer(flags: flags) else {
guard customizesTokenization(flags: flags) else {
return wrappedTokenizer.tokenize(context: context, flags: flags, pText: pText, nText: nText, tokenCallback: tokenCallback)
}
@@ -28,7 +28,7 @@ private final class StopWordsTokenizer : FTS5CustomTokenizer {
// TODO: test that deinit is called
}
func tokenize(context: UnsafeMutableRawPointer?, flags: FTS5TokenizeFlags, pText: UnsafePointer<Int8>?, nText: Int32, tokenCallback: FTS5TokenCallback?) -> Int32 {
func tokenize(context: UnsafeMutableRawPointer?, flags: FTS5TokenizationFlags, pText: UnsafePointer<Int8>?, nText: Int32, tokenCallback: FTS5TokenCallback?) -> Int32 {
// The way we implement stop words is by letting wrappedTokenizer do its
// job but intercepting its tokens before they feed SQLite.
@@ -81,7 +81,7 @@ private final class NFKCTokenizer : FTS5CustomTokenizer {
// TODO: test that deinit is called
}
func tokenize(context: UnsafeMutableRawPointer?, flags: FTS5TokenizeFlags, pText: UnsafePointer<Int8>?, nText: Int32, tokenCallback: FTS5TokenCallback?) -> Int32 {
func tokenize(context: UnsafeMutableRawPointer?, flags: FTS5TokenizationFlags, pText: UnsafePointer<Int8>?, nText: Int32, tokenCallback: FTS5TokenCallback?) -> Int32 {
// The way we implement NFKC conversion is by letting wrappedTokenizer
// do its job, but intercepting its tokens before they feed SQLite.
@@ -140,7 +140,7 @@ private final class SynonymsTokenizer : FTS5CustomTokenizer {
// TODO: test that deinit is called
}
func tokenize(context: UnsafeMutableRawPointer?, flags: FTS5TokenizeFlags, pText: UnsafePointer<Int8>?, nText: Int32, tokenCallback: FTS5TokenCallback?) -> Int32 {
func tokenize(context: UnsafeMutableRawPointer?, flags: FTS5TokenizationFlags, pText: UnsafePointer<Int8>?, nText: Int32, tokenCallback: FTS5TokenCallback?) -> Int32 {
// Don't look for synonyms when tokenizing queries, as advised by
// https://www.sqlite.org/fts5.html#synonym_support
if flags.contains(.query) {
@@ -25,7 +25,7 @@ private final class StopWordsTokenizer : FTS5WrapperTokenizer {
return token == "bar"
}
func customizesWrappedTokenizer(flags: FTS5TokenizeFlags) -> Bool {
func customizesTokenization(flags: FTS5TokenizationFlags) -> Bool {
return true
}
@@ -50,7 +50,7 @@ private final class NFKCTokenizer : FTS5WrapperTokenizer {
}
}
func customizesWrappedTokenizer(flags: FTS5TokenizeFlags) -> Bool {
func customizesTokenization(flags: FTS5TokenizationFlags) -> Bool {
return true
}
@@ -78,7 +78,7 @@ private final class SynonymsTokenizer : FTS5WrapperTokenizer {
return synonyms.first(where: { $0.contains(token) })
}
func customizesWrappedTokenizer(flags: FTS5TokenizeFlags) -> Bool {
func customizesTokenization(flags: FTS5TokenizationFlags) -> Bool {
// Don't look for synonyms when tokenizing queries, as advised by
// https://www.sqlite.org/fts5.html#synonym_support
return !flags.contains(.query)