mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
FTS3Pattern initializers use a tokenizer when relevant
This commit is contained in:
1 parent
811ed6efe5
commit
c1946e57b7
2 files changed
+51
-18
No files matched your search
+49
-16
@@ -2,7 +2,7 @@
|
||||
public struct FTS3Pattern {
|
||||
|
||||
/// The raw pattern string. Guaranteed to be a valid FTS3/4 pattern.
|
||||
let rawPattern: String
|
||||
public let rawPattern: String
|
||||
|
||||
/// Creates a pattern from a raw pattern string; throws DatabaseError on
|
||||
/// invalid syntax.
|
||||
@@ -39,12 +39,19 @@ public struct FTS3Pattern {
|
||||
///
|
||||
/// FTS3Pattern(matchingAnyTokenIn: "") // nil
|
||||
/// FTS3Pattern(matchingAnyTokenIn: "foo bar") // foo OR bar
|
||||
public init?(matchingAnyTokenIn string: String) {
|
||||
// Tokenize with the simple tokenizer, because it does not lose
|
||||
// information used by other tokenizers, and turns special syntax
|
||||
// elements such as "AND" and "OR" into their neutral lowercase
|
||||
// equivalents.
|
||||
let tokens = FTS3Pattern.tokenize(string, tokenizer: "simple")
|
||||
///
|
||||
/// The string is tokenized with the `simple` tokenizer unless specified
|
||||
/// otherwise:
|
||||
///
|
||||
/// // frustration
|
||||
/// FTS3Pattern(matchingAnyTokenIn: "frustration")
|
||||
/// // frustrat
|
||||
/// FTS3Pattern(matchingAnyTokenIn: "frustration", tokenizer: .porter)
|
||||
///
|
||||
/// - parameter string: The string to turn into an FTS3 pattern
|
||||
/// - parameter tokenizer: An FTS3Tokenizer - defaults "simple"
|
||||
public init?(matchingAnyTokenIn string: String, tokenizer: FTS3Tokenizer = .simple) {
|
||||
let tokens = FTS3Pattern.tokenize(string, tokenizer: tokenizer)
|
||||
guard !tokens.isEmpty else { return nil }
|
||||
try? self.init(rawPattern: tokens.joined(separator: " OR "))
|
||||
}
|
||||
@@ -54,9 +61,19 @@ public struct FTS3Pattern {
|
||||
///
|
||||
/// FTS3Pattern(matchingAllTokensIn: "") // nil
|
||||
/// FTS3Pattern(matchingAllTokensIn: "foo bar") // foo AND bar
|
||||
public init?(matchingAllTokensIn string: String) {
|
||||
// See init(matchingAnyTokenIn:) comment on the choice of the "simple" tokenizer
|
||||
let tokens = FTS3Pattern.tokenize(string, tokenizer: "simple")
|
||||
///
|
||||
/// The string is tokenized with the `simple` tokenizer unless specified
|
||||
/// otherwise:
|
||||
///
|
||||
/// // frustration
|
||||
/// FTS3Pattern(matchingAllTokensIn: "frustration")
|
||||
/// // frustrat
|
||||
/// FTS3Pattern(matchingAllTokensIn: "frustration", tokenizer: .porter)
|
||||
///
|
||||
/// - parameter string: The string to turn into an FTS3 pattern
|
||||
/// - parameter tokenizer: An FTS3Tokenizer - defaults "simple"
|
||||
public init?(matchingAllTokensIn string: String, tokenizer: FTS3Tokenizer = .simple) {
|
||||
let tokens = FTS3Pattern.tokenize(string, tokenizer: tokenizer)
|
||||
guard !tokens.isEmpty else { return nil }
|
||||
try? self.init(rawPattern: tokens.joined(separator: " AND "))
|
||||
}
|
||||
@@ -66,19 +83,35 @@ public struct FTS3Pattern {
|
||||
///
|
||||
/// FTS3Pattern(matchingPhrase: "") // nil
|
||||
/// FTS3Pattern(matchingPhrase: "foo bar") // "foo bar"
|
||||
public init?(matchingPhrase string: String) {
|
||||
// See init(matchingAnyTokenIn:) comment on the choice of the "simple" tokenizer
|
||||
let tokens = FTS3Pattern.tokenize(string, tokenizer: "simple")
|
||||
///
|
||||
/// The string is tokenized with the `simple` tokenizer unless specified
|
||||
/// otherwise:
|
||||
///
|
||||
/// // "frustration"
|
||||
/// FTS3Pattern(matchingPhrase: "frustration")
|
||||
/// // "frustrat"
|
||||
/// FTS3Pattern(matchingPhrase: "frustration", tokenizer: .porter)
|
||||
///
|
||||
/// - parameter string: The string to turn into an FTS3 pattern
|
||||
/// - parameter tokenizer: An FTS3Tokenizer - defaults "simple"
|
||||
public init?(matchingPhrase string: String, tokenizer: FTS3Tokenizer = .simple) {
|
||||
let tokens = FTS3Pattern.tokenize(string, tokenizer: tokenizer)
|
||||
guard !tokens.isEmpty else { return nil }
|
||||
try? self.init(rawPattern: "\"" + tokens.joined(separator: " ") + "\"")
|
||||
}
|
||||
|
||||
/// Returns an array of tokens found in the string argument.
|
||||
///
|
||||
/// FTS3Pattern.tokenize("foo bar", tokenizer: "simple") // ["foo", "bar"]
|
||||
private static func tokenize(_ string: String, tokenizer: String) -> [String] {
|
||||
/// FTS3Pattern.tokenize("foo bar", tokenizer: .simple) // ["foo", "bar"]
|
||||
private static func tokenize(_ string: String, tokenizer: FTS3Tokenizer) -> [String] {
|
||||
return DatabaseQueue().inDatabase { db in
|
||||
try! db.execute("CREATE VIRTUAL TABLE tokens USING fts3tokenize(\(tokenizer.sqlExpression.sql))") // literal tokenizer required
|
||||
var tokenizerChunks: [String] = []
|
||||
tokenizerChunks.append(tokenizer.name)
|
||||
for option in tokenizer.options {
|
||||
tokenizerChunks.append("\"\(option)\"")
|
||||
}
|
||||
let tokenizerSQL = tokenizerChunks.joined(separator: ", ")
|
||||
try! db.execute("CREATE VIRTUAL TABLE tokens USING fts3tokenize(\(tokenizerSQL))")
|
||||
return String.fetchAll(db, "SELECT token FROM tokens WHERE input = ? ORDER BY position", arguments: [string])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,11 +29,11 @@ public struct FTS3Tokenizer {
|
||||
options.append("remove_diacritics=0")
|
||||
}
|
||||
if let separators = separators {
|
||||
// TODO: test "=" and "\"" as separators
|
||||
// TODO: test "=" and "\"", "(" and ")" as separators, with both FTS3Pattern(matchingAnyTokenIn:tokenizer:) and Database.create(virtualTable:using:)
|
||||
options.append("separators=" + separators.map { String($0) }.joined(separator: ""))
|
||||
}
|
||||
if let tokenCharacters = tokenCharacters {
|
||||
// TODO: test "=" and "\"" as tokenCharacters
|
||||
// TODO: test "=" and "\"", "(" and ")" as tokenCharacters, with both FTS3Pattern(matchingAnyTokenIn:tokenizer:) and Database.create(virtualTable:using:)
|
||||
options.append("tokenchars=" + tokenCharacters.map { String($0) }.joined(separator: ""))
|
||||
}
|
||||
return FTS3Tokenizer("unicode61", options: options)
|
||||
|
||||
Reference in new issue
Block a user