mirror of
https://github.com/mii443/AzooKeyKanaKanjiConverter.git
synced 2025-08-22 06:55:26 +00:00
feat: use azooKey_emoji_dictionary_storage (#146)
* feat: use azooKey_emoji_dictionary_storage * feat: add test * fix: test case * feat: fix build error * fix: build error * fix: fix android build * fix: confirm libicu-dev is installed * test: add non-empty test case * fix: continue on empty line
This commit is contained in:
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -1,3 +1,6 @@
|
||||
[submodule "Sources/KanaKanjiConverterModuleWithDefaultDictionary/azooKey_dictionary_storage"]
|
||||
path = Sources/KanaKanjiConverterModuleWithDefaultDictionary/azooKey_dictionary_storage
|
||||
url = https://github.com/ensan-hcl/azooKey_dictionary_storage
|
||||
[submodule "Sources/KanaKanjiConverterModuleWithDefaultDictionary/azooKey_emoji_dictionary_storage"]
|
||||
path = Sources/KanaKanjiConverterModuleWithDefaultDictionary/azooKey_emoji_dictionary_storage
|
||||
url = https://github.com/ensan-hcl/azooKey_emoji_dictionary_storage
|
||||
|
@ -42,9 +42,14 @@ var targets: [Target] = [
|
||||
exclude: [
|
||||
"azooKey_dictionary_storage/README.md",
|
||||
"azooKey_dictionary_storage/LICENSE",
|
||||
"azooKey_emoji_dictionary_storage/data",
|
||||
"azooKey_emoji_dictionary_storage/scripts",
|
||||
"azooKey_emoji_dictionary_storage/requirements.txt",
|
||||
"azooKey_emoji_dictionary_storage/README.md",
|
||||
],
|
||||
resources: [
|
||||
.copy("azooKey_dictionary_storage/Dictionary"),
|
||||
.copy("azooKey_emoji_dictionary_storage/EmojiDictionary"),
|
||||
],
|
||||
swiftSettings: swiftSettings
|
||||
),
|
||||
|
@ -19,13 +19,21 @@ public struct TextReplacer: Sendable {
|
||||
private var emojiGroups: [EmojiGroup] = []
|
||||
private var nonBaseEmojis: Set<String> = []
|
||||
|
||||
/// データを正しく持てているかを確認するためのプロパティ
|
||||
var isEmpty: Bool {
|
||||
emojiSearchDict.isEmpty && emojiGroups.isEmpty && nonBaseEmojis.isEmpty
|
||||
}
|
||||
|
||||
public init(emojiDataProvider: () -> URL) {
|
||||
var emojiSearchDict: [String: [String]] = [:]
|
||||
var emojiGroups: [EmojiGroup] = []
|
||||
do {
|
||||
let string = try String(contentsOf: emojiDataProvider(), encoding: .utf8)
|
||||
let lines = string.split(separator: "\n")
|
||||
let lines = string.components(separatedBy: .newlines)
|
||||
for line in lines {
|
||||
if line.isEmpty {
|
||||
continue
|
||||
}
|
||||
let splited = line.split(separator: "\t", omittingEmptySubsequences: false)
|
||||
guard splited.count == 3 else {
|
||||
debug("error", line)
|
||||
@ -84,7 +92,7 @@ public struct TextReplacer: Sendable {
|
||||
}
|
||||
}
|
||||
|
||||
public struct SearchResultItem: Sendable {
|
||||
public struct SearchResultItem: Sendable, Equatable, Hashable {
|
||||
public var query: String
|
||||
public var text: String
|
||||
public var inputable: Bool {
|
||||
|
@ -18,7 +18,7 @@ public extension ConvertRequestOptions {
|
||||
memoryDirectoryURL: URL,
|
||||
sharedContainerURL: URL,
|
||||
zenzaiMode: ZenzaiMode = .off,
|
||||
textReplacer: TextReplacer = TextReplacer(),
|
||||
textReplacer: TextReplacer = .withDefaultEmojiDictionary(),
|
||||
metadata: ConvertRequestOptions.Metadata?
|
||||
) -> Self {
|
||||
#if os(iOS) || os(watchOS) || os(tvOS) || os(visionOS)
|
||||
@ -50,3 +50,35 @@ public extension ConvertRequestOptions {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public extension TextReplacer {
|
||||
static func withDefaultEmojiDictionary() -> Self {
|
||||
self.init {
|
||||
let directoryName = "EmojiDictionary"
|
||||
#if os(iOS) || os(watchOS) || os(tvOS) || os(visionOS)
|
||||
let directory = Bundle.module.bundleURL.appendingPathComponent(directoryName, isDirectory: true)
|
||||
return if #available(iOS 17.4, *) {
|
||||
directory.appendingPathComponent("emoji_all_E15.1.txt", isDirectory: false)
|
||||
} else if #available(iOS 16.4, *) {
|
||||
directory.appendingPathComponent("emoji_all_E15.0.txt", isDirectory: false)
|
||||
} else if #available(iOS 15.4, *) {
|
||||
directory.appendingPathComponent("emoji_all_E14.0.txt", isDirectory: false)
|
||||
} else {
|
||||
directory.appendingPathComponent("emoji_all_E13.1.txt", isDirectory: false)
|
||||
}
|
||||
#elseif os(macOS)
|
||||
let directory = Bundle.module.resourceURL!.appendingPathComponent(directoryName, isDirectory: true)
|
||||
return if #available(macOS 14.4, *) {
|
||||
directory.appendingPathComponent("emoji_all_E15.1.txt", isDirectory: false)
|
||||
} else {
|
||||
directory.appendingPathComponent("emoji_all_E15.0.txt", isDirectory: false)
|
||||
}
|
||||
#else
|
||||
return Bundle.module.resourceURL!
|
||||
.appendingPathComponent(directoryName, isDirectory: true)
|
||||
.appendingPathComponent("emoji_all_E15.1.txt", isDirectory: false)
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,13 @@
|
||||
@testable import KanaKanjiConverterModule
|
||||
@testable import KanaKanjiConverterModuleWithDefaultDictionary
|
||||
import XCTest
|
||||
|
||||
final class TextReplacerTests: XCTestCase {
|
||||
func testEmojiTextReplacer() throws {
|
||||
let textReplacer = TextReplacer.withDefaultEmojiDictionary()
|
||||
XCTAssertFalse(textReplacer.isEmpty)
|
||||
let searchResult = textReplacer.getSearchResult(query: "カニ", target: [.emoji])
|
||||
XCTAssertEqual(searchResult.count, 1)
|
||||
XCTAssertEqual(searchResult[0], .init(query: "かに", text: "🦀️"))
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user