Skip to content

Commit

Permalink
make text replacer customizable (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
ensan-hcl authored Feb 17, 2024
1 parent a7269fb commit b02f6eb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -44,6 +45,7 @@ public struct ConvertRequestOptions: Sendable {
self.memoryDirectoryURL = memoryDirectoryURL
self.sharedContainerURL = sharedContainerURL
self.metadata = metadata
self.textReplacer = textReplacer
self.dictionaryResourceURL = dictionaryResourceURL
}

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 17 additions & 13 deletions Sources/KanaKanjiConverterModule/Replacer/TextReplacer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,17 @@ import SwiftUtils
/// 例えば、「tha|nk」と入力があるとき、「think」や「thanks」などを候補として表示することが考えられる。
///
/// 現在の機能は「絵文字」のバリエーションを表示することに限定する。
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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -110,7 +114,7 @@ public struct TextReplacer {
}

/// 「同一」の絵文字のグループ
private struct EmojiGroup {
private struct EmojiGroup: Sendable {
var base: String
var variations: [String]
var all: [String] {
Expand Down

0 comments on commit b02f6eb

Please sign in to comment.