Support for remove_diacritics=2 in FTS5

This commit is contained in:
Gwendal Roué committed 2019-02-24 17:38:04 +01:00
1 parent bcac197910
commit 27156fd215
5 files changed
+78 -10

No files matched your search

+19 -1
View File
@@ -8,7 +8,25 @@
///
/// See https://www.sqlite.org/fts5.html
public struct FTS5 : VirtualTableModule {
/// Options for Latin script characters. Matches the raw "remove_diacritics"
/// tokenizer argument.
///
/// See https://www.sqlite.org/fts5.html
public enum Diacritics {
/// Do not remove diacritics from Latin script characters. This
/// option matches the raw "remove_diacritics=0" tokenizer argument.
case keep
/// Remove diacritics from Latin script characters. This
/// option matches the raw "remove_diacritics=1" tokenizer argument.
case removeLegacy
#if GRDBCUSTOMSQLITE
/// Remove diacritics from Latin script characters. This
/// option matches the raw "remove_diacritics=2" tokenizer argument,
/// available from SQLite 3.27.0
case remove
#endif
}
/// Creates a FTS5 module suitable for the Database
/// `create(virtualTable:using:)` method.
///
+11 -5
View File
@@ -90,19 +90,25 @@ public struct FTS5TokenizerDescriptor {
/// }
///
/// - parameters:
/// - removeDiacritics: If true (the default), then SQLite will
/// strip diacritics from latin characters.
/// - diacritics: By default SQLite will strip diacritics from
/// latin characters.
/// - separators: Unless empty (the default), SQLite will consider
/// these characters as token separators.
/// - tokenCharacters: Unless empty (the default), SQLite will
/// consider these characters as token characters.
///
/// See https://www.sqlite.org/fts5.html#unicode61_tokenizer
public static func unicode61(removeDiacritics: Bool = true, separators: Set<Character> = [], tokenCharacters: Set<Character> = []) -> FTS5TokenizerDescriptor {
public static func unicode61(diacritics: FTS5.Diacritics = .removeLegacy, separators: Set<Character> = [], tokenCharacters: Set<Character> = []) -> FTS5TokenizerDescriptor {
var components: [String] = ["unicode61"]
// TODO: Support for remove_diacritics=2 (SQLite 3.27.0)
if !removeDiacritics {
switch diacritics {
case .removeLegacy:
break
case .keep:
components.append(contentsOf: ["remove_diacritics", "0"])
#if GRDBCUSTOMSQLITE
case .remove:
components.append(contentsOf: ["remove_diacritics", "2"])
#endif
}
if !separators.isEmpty {
// TODO: test "=" and "\"", "(" and ")" as separators, with both FTS3Pattern(matchingAnyTokenIn:tokenizer:) and Database.create(virtualTable:using:)
+7
View File
@@ -113,3 +113,10 @@ extension FTS3TokenizerDescriptor {
@available(*, unavailable, renamed: "unicode61(diacritics:separators:tokenCharacters:)")
public static func unicode61(removeDiacritics: Bool, separators: Set<Character> = [], tokenCharacters: Set<Character> = []) -> FTS3TokenizerDescriptor { preconditionFailure() }
}
#if SQLITE_ENABLE_FTS5
extension FTS5TokenizerDescriptor {
@available(*, unavailable, renamed: "unicode61(diacritics:separators:tokenCharacters:)")
public static func unicode61(removeDiacritics: Bool = true, separators: Set<Character> = [], tokenCharacters: Set<Character> = []) -> FTS5TokenizerDescriptor { preconditionFailure() }
}
#endif
+15 -2
View File
@@ -102,17 +102,30 @@ class FTS5TableBuilderTests: GRDBTestCase {
}
}
func testUnicode61TokenizerRemoveDiacritics() throws {
func testUnicode61TokenizerDiacriticsKeep() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
try db.create(virtualTable: "documents", using: FTS5()) { t in
t.tokenizer = .unicode61(removeDiacritics: false)
t.tokenizer = .unicode61(diacritics: .keep)
t.column("content")
}
assertDidExecute(sql: "CREATE VIRTUAL TABLE \"documents\" USING fts5(content, tokenize='unicode61 remove_diacritics 0')")
}
}
#if GRDBCUSTOMSQLITE
func testUnicode61TokenizerDiacriticsRemove() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
try db.create(virtualTable: "documents", using: FTS5()) { t in
t.tokenizer = .unicode61(diacritics: .remove)
t.column("content")
}
assertDidExecute(sql: "CREATE VIRTUAL TABLE \"documents\" USING fts5(content, tokenize='unicode61 remove_diacritics 2')")
}
}
#endif
func testUnicode61TokenizerSeparators() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
+26 -2
View File
@@ -128,11 +128,11 @@ class FTS5TokenizerTests: GRDBTestCase {
}
}
func testUnicode61TokenizerRemoveDiacritics() throws {
func testUnicode61TokenizerDiacriticsKeep() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
try db.create(virtualTable: "documents", using: FTS5()) { t in
t.tokenizer = .unicode61(removeDiacritics: false)
t.tokenizer = .unicode61(diacritics: .keep)
t.column("content")
}
@@ -149,6 +149,30 @@ class FTS5TokenizerTests: GRDBTestCase {
XCTAssertTrue(match(db, "jérôme", "JÉRÔME"))
}
}
#if GRDBCUSTOMSQLITE
func testUnicode61TokenizerDiacriticsRemove() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
try db.create(virtualTable: "documents", using: FTS5()) { t in
t.tokenizer = .unicode61(diacritics: .remove)
t.column("content")
}
// simple match
XCTAssertTrue(match(db, "abcDÉF", "abcDÉF"))
// English stemming
XCTAssertFalse(match(db, "database", "databases"))
// diacritics in latin characters
XCTAssertTrue(match(db, "eéÉ", "Èèe"))
// unicode case
XCTAssertTrue(match(db, "jérôme", "JÉRÔME"))
}
}
#endif
func testUnicode61TokenizerSeparators() throws {
let dbQueue = try makeDatabaseQueue()