From 80be12f3ef736c318ae44c3609ced3e2c333a8a2 Mon Sep 17 00:00:00 2001 From: Miwa / Ensan Date: Mon, 21 Jul 2025 11:18:18 +0900 Subject: [PATCH] feat: add session CLI for dynamic user dictionary --- .../CliTool/Subcommands/EvaluateCommand.swift | 16 ++++++++-------- .../CliTool/Subcommands/SessionCommand.swift | 17 +++++++++++++++++ .../TemplateConversionTests.swift | 8 ++++++++ 3 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 Tests/KanaKanjiConverterModuleTests/ConverterTests/TemplateConversionTests.swift diff --git a/Sources/CliTool/Subcommands/EvaluateCommand.swift b/Sources/CliTool/Subcommands/EvaluateCommand.swift index c822fda..40bcb3b 100644 --- a/Sources/CliTool/Subcommands/EvaluateCommand.swift +++ b/Sources/CliTool/Subcommands/EvaluateCommand.swift @@ -146,15 +146,15 @@ extension Subcommands { /// ユーザ辞書 var user_dictionary: [InputUserDictionaryItem]? = nil + } - struct InputUserDictionaryItem: Codable { - /// 漢字 - var word: String - /// 読み - var reading: String - /// ヒント - var hint: String? = nil - } + struct InputUserDictionaryItem: Codable { + /// 漢字 + var word: String + /// 読み + var reading: String + /// ヒント + var hint: String? = nil } struct EvaluateResult: Codable { diff --git a/Sources/CliTool/Subcommands/SessionCommand.swift b/Sources/CliTool/Subcommands/SessionCommand.swift index 9cf7019..3c76704 100644 --- a/Sources/CliTool/Subcommands/SessionCommand.swift +++ b/Sources/CliTool/Subcommands/SessionCommand.swift @@ -30,6 +30,8 @@ extension Subcommands { var reportScore = false @Flag(name: [.customLong("roman2kana")], help: "Use roman2kana input.") var roman2kana = false + @Option(name: [.customLong("config_user_dictionary")], help: "User Dictionary JSON file path") + var configUserDictionary: String? = nil @Option(name: [.customLong("config_zenzai_inference_limit")], help: "inference limit for zenzai.") var configZenzaiInferenceLimit: Int = .max @Flag(name: [.customLong("config_zenzai_rich_n_best")], help: "enable rich n_best generation for zenzai.") @@ -70,6 +72,15 @@ extension Subcommands { } } + private func parseUserDictionaryFile() throws -> [InputUserDictionaryItem] { + guard let configUserDictionary else { + return [] + } + let url = URL(fileURLWithPath: configUserDictionary) + let data = try Data(contentsOf: url) + return try JSONDecoder().decode([InputUserDictionaryItem].self, from: data) + } + @MainActor mutating func run() async { if self.zenzV1 || self.zenzV2 { print("\(bold: "We strongly recommend to use zenz-v3 models")") @@ -80,6 +91,11 @@ extension Subcommands { if !self.zenzWeightPath.isEmpty && (!self.zenzV1 && !self.zenzV2 && !self.zenzV3) { print("zenz version is not specified. By default, zenz-v3 will be used.") } + + let userDictionary = try! self.parseUserDictionaryFile().map { + DicdataElement(word: $0.word, ruby: $0.reading.toKatakana(), cid: CIDData.固有名詞.cid, mid: MIDData.一般.mid, value: -10) + } + let learningType: LearningType = if self.readOnlyMemoryPath != nil { // 読み取りのみ .onlyOutput @@ -107,6 +123,7 @@ extension Subcommands { converter.sendToDicdataStore( .setRequestOptions(requestOptions(learningType: learningType, memoryDirectory: memoryDirectory, leftSideContext: nil)) ) + converter.sendToDicdataStore(.importDynamicUserDict(userDictionary)) var composingText = ComposingText() let inputStyle: InputStyle = self.roman2kana ? .roman2kana : .direct var lastCandidates: [Candidate] = [] diff --git a/Tests/KanaKanjiConverterModuleTests/ConverterTests/TemplateConversionTests.swift b/Tests/KanaKanjiConverterModuleTests/ConverterTests/TemplateConversionTests.swift new file mode 100644 index 0000000..4f64520 --- /dev/null +++ b/Tests/KanaKanjiConverterModuleTests/ConverterTests/TemplateConversionTests.swift @@ -0,0 +1,8 @@ +// +// File.swift +// AzooKeyKanakanjiConverter +// +// Created by miwa on 2025/07/21. +// + +import Foundation