make text replacer customizable (#35)

This commit is contained in:
Miwa / Ensan
2024-02-17 22:32:19 +09:00
committed by GitHub
parent a7269fbbbe
commit b02f6eb899
3 changed files with 23 additions and 15 deletions

View File

@@ -27,8 +27,9 @@ public struct ConvertRequestOptions: Sendable {
/// - dictionaryResourceURL:
/// - memoryDirectoryURL:
/// - sharedContainerURL:
/// - textReplacer:
/// - metadata: `ConvertRequestOptions.Metadata`
public init(N_best: Int = 10, requireJapanesePrediction: Bool, requireEnglishPrediction: Bool, keyboardLanguage: KeyboardLanguage, typographyLetterCandidate: Bool = false, unicodeCandidate: Bool = true, englishCandidateInRoman2KanaInput: Bool = false, fullWidthRomanCandidate: Bool = false, halfWidthKanaCandidate: Bool = false, learningType: LearningType, maxMemoryCount: Int = 65536, shouldResetMemory: Bool = false, dictionaryResourceURL: URL, memoryDirectoryURL: URL, sharedContainerURL: URL, metadata: ConvertRequestOptions.Metadata) {
public init(N_best: Int = 10, requireJapanesePrediction: Bool, requireEnglishPrediction: Bool, keyboardLanguage: KeyboardLanguage, typographyLetterCandidate: Bool = false, unicodeCandidate: Bool = true, englishCandidateInRoman2KanaInput: Bool = false, fullWidthRomanCandidate: Bool = false, halfWidthKanaCandidate: Bool = false, learningType: LearningType, maxMemoryCount: Int = 65536, shouldResetMemory: Bool = false, dictionaryResourceURL: URL, memoryDirectoryURL: URL, sharedContainerURL: URL, textReplacer: TextReplacer = TextReplacer(), metadata: ConvertRequestOptions.Metadata) {
self.N_best = N_best
self.requireJapanesePrediction = requireJapanesePrediction
self.requireEnglishPrediction = requireEnglishPrediction
@@ -44,6 +45,7 @@ public struct ConvertRequestOptions: Sendable {
self.memoryDirectoryURL = memoryDirectoryURL
self.sharedContainerURL = sharedContainerURL
self.metadata = metadata
self.textReplacer = textReplacer
self.dictionaryResourceURL = dictionaryResourceURL
}
@@ -60,6 +62,8 @@ public struct ConvertRequestOptions: Sendable {
public var learningType: LearningType
public var maxMemoryCount: Int
public var shouldResetMemory: Bool
///
public var textReplacer: TextReplacer
//
public var memoryDirectoryURL: URL
public var sharedContainerURL: URL

View File

@@ -651,7 +651,7 @@ import SwiftUtils
//
let predictionResults = self.converter.getPredictionCandidates(prepart: leftSideCandidate, N_best: 15)
//
let replacer = TextReplacer()
let replacer = options.textReplacer
var emojiCandidates: [PostCompositionPredictionCandidate] = []
for data in leftSideCandidate.data where DicdataStore.includeMMValueCalculation(data) {
let result = replacer.getSearchResult(query: data.word, target: [.emoji], ignoreNonBaseEmoji: true)

View File

@@ -13,26 +13,17 @@ import SwiftUtils
/// tha|nkthinkthanks
///
///
public struct TextReplacer {
public struct TextReplacer: Sendable {
// TODO: prefix trie便
private var emojiSearchDict: [String: [String]] = [:]
private var emojiGroups: [EmojiGroup] = []
private var nonBaseEmojis: Set<String> = []
public init() {
let fileURL: URL
//
if #available(iOS 16.4, *) {
fileURL = Bundle.main.bundleURL.appendingPathComponent("emoji_all_E15.0.txt.gen", isDirectory: false)
} else if #available(iOS 15.4, *) {
fileURL = Bundle.main.bundleURL.appendingPathComponent("emoji_all_E14.0.txt.gen", isDirectory: false)
} else {
fileURL = Bundle.main.bundleURL.appendingPathComponent("emoji_all_E13.1.txt.gen", isDirectory: false)
}
public init(emojiDataProvider: () -> URL) {
var emojiSearchDict: [String: [String]] = [:]
var emojiGroups: [EmojiGroup] = []
do {
let string = try String(contentsOf: fileURL, encoding: .utf8)
let string = try String(contentsOf: emojiDataProvider(), encoding: .utf8)
let lines = string.split(separator: "\n")
for line in lines {
let splited = line.split(separator: "\t", omittingEmptySubsequences: false)
@@ -62,6 +53,19 @@ public struct TextReplacer {
}
}
@available(*, deprecated, renamed: "init(emojiDataProvider:)", message: "init() is depreacted and will be removed in v1.0. Use init(emojiDataProvider:) instead")
public init() {
self.init {
if #available(iOS 16.4, *) {
Bundle.main.bundleURL.appendingPathComponent("emoji_all_E15.0.txt.gen", isDirectory: false)
} else if #available(iOS 15.4, *) {
Bundle.main.bundleURL.appendingPathComponent("emoji_all_E14.0.txt.gen", isDirectory: false)
} else {
Bundle.main.bundleURL.appendingPathComponent("emoji_all_E13.1.txt.gen", isDirectory: false)
}
}
}
public func getSearchResult(query: String, target: [ConverterBehaviorSemantics.ReplacementTarget], ignoreNonBaseEmoji: Bool = false) -> [SearchResultItem] {
//
let query = query.lowercased().toHiragana()
@@ -110,7 +114,7 @@ public struct TextReplacer {
}
///
private struct EmojiGroup {
private struct EmojiGroup: Sendable {
var base: String
var variations: [String]
var all: [String] {