Files
AzooKeyKanaKanjiConverter/Sources/KanaKanjiConverterModule/Converter/ConvertRequestOptions.swift

332 lines
22 KiB
Swift

//
// ConvertRequestOptions.swift
// Keyboard
//
// Created by ensan on 2022/12/20.
// Copyright © 2022 ensan. All rights reserved.
//
import Foundation
public struct ConvertRequestOptions: Sendable {
///
///
/// - parameters:
/// - N_best: `N`
/// - requireJapanesePrediction: `false`
/// - requireEnglishPrediction: `false``false`
/// - keyboardLanguage:
/// - englishCandidateInRoman2KanaInput: `true``false`
/// - fullWidthRomanCandidate: `true`
/// - halfWidthKanaCandidate: `true`
/// - learningType: `LearningType`
/// - maxMemoryCount: `0``learningType``nothing`
/// - shouldResetMemory: `true`
/// - dictionaryResourceURL:
/// - memoryDirectoryURL:
/// - sharedContainerURL:
/// - textReplacer:
/// - specialCandidateProviders:
/// - metadata: `ConvertRequestOptions.Metadata`
public init(N_best: Int = 10, requireJapanesePrediction: Bool, requireEnglishPrediction: Bool, keyboardLanguage: KeyboardLanguage, 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, specialCandidateProviders: [any SpecialCandidateProvider]?, zenzaiMode: ZenzaiMode = .off, preloadDictionary: Bool = false, metadata: ConvertRequestOptions.Metadata?) {
self.N_best = N_best
self.requireJapanesePrediction = requireJapanesePrediction
self.requireEnglishPrediction = requireEnglishPrediction
self.keyboardLanguage = keyboardLanguage
self.englishCandidateInRoman2KanaInput = englishCandidateInRoman2KanaInput
self.fullWidthRomanCandidate = fullWidthRomanCandidate
self.halfWidthKanaCandidate = halfWidthKanaCandidate
self.learningType = learningType
self.maxMemoryCount = maxMemoryCount
self.shouldResetMemory = shouldResetMemory
self.memoryDirectoryURL = memoryDirectoryURL
self.sharedContainerURL = sharedContainerURL
self.metadata = metadata
self.textReplacer = textReplacer
self.specialCandidateProviders = specialCandidateProviders ?? KanaKanjiConverter.defaultSpecialCandidateProviders
self.zenzaiMode = zenzaiMode
self.preloadDictionary = preloadDictionary
self.dictionaryResourceURL = dictionaryResourceURL
}
///
///
/// - parameters:
/// - N_best: `N`
/// - requireJapanesePrediction: `false`
/// - requireEnglishPrediction: `false``false`
/// - keyboardLanguage:
/// - typographyLetterCandidate: `true``KanaKanjiConverter.typographicalCandidates(_:)`
/// - unicodeCandidate: `true``U+xxxx`Unicode`KanaKanjiConverter.unicodeCandidates(_:)``
/// - englishCandidateInRoman2KanaInput: `true``false`
/// - fullWidthRomanCandidate: `true`
/// - halfWidthKanaCandidate: `true`
/// - learningType: `LearningType`
/// - maxMemoryCount: `0``learningType``nothing`
/// - shouldResetMemory: `true`
/// - dictionaryResourceURL:
/// - memoryDirectoryURL:
/// - sharedContainerURL:
/// - textReplacer:
/// - metadata: `ConvertRequestOptions.Metadata`
@available(*, deprecated, message: "it be removed in AzooKeyKanaKanjiConverter v1.0")
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, zenzaiMode: ZenzaiMode = .off, preloadDictionary: Bool = false, metadata: ConvertRequestOptions.Metadata?) {
var specialCandidateProviders = [any SpecialCandidateProvider]()
if typographyLetterCandidate {
specialCandidateProviders.append(.typography)
}
if unicodeCandidate {
specialCandidateProviders.append(.unicode)
}
specialCandidateProviders.append(.emailAddress)
specialCandidateProviders.append(.timeExpression)
specialCandidateProviders.append(.calendar)
specialCandidateProviders.append(.version)
specialCandidateProviders.append(.commaSeparatedNumber)
self.N_best = N_best
self.requireJapanesePrediction = requireJapanesePrediction
self.requireEnglishPrediction = requireEnglishPrediction
self.keyboardLanguage = keyboardLanguage
self.englishCandidateInRoman2KanaInput = englishCandidateInRoman2KanaInput
self.englishCandidateInRoman2KanaInput = englishCandidateInRoman2KanaInput
self.fullWidthRomanCandidate = fullWidthRomanCandidate
self.halfWidthKanaCandidate = halfWidthKanaCandidate
self.learningType = learningType
self.maxMemoryCount = maxMemoryCount
self.shouldResetMemory = shouldResetMemory
self.memoryDirectoryURL = memoryDirectoryURL
self.sharedContainerURL = sharedContainerURL
self.metadata = metadata
self.textReplacer = textReplacer
self.specialCandidateProviders = specialCandidateProviders
self.zenzaiMode = zenzaiMode
self.preloadDictionary = preloadDictionary
self.dictionaryResourceURL = dictionaryResourceURL
}
///
///
/// - parameters:
/// - N_best: `N`
/// - requireJapanesePrediction: `false`
/// - requireEnglishPrediction: `false``false`
/// - keyboardLanguage:
/// - typographyLetterCandidate: `true``KanaKanjiConverter.typographicalCandidates(_:)`
/// - unicodeCandidate: `true``U+xxxx`Unicode`KanaKanjiConverter.unicodeCandidates(_:)``
/// - englishCandidateInRoman2KanaInput: `true``false`
/// - fullWidthRomanCandidate: `true`
/// - halfWidthKanaCandidate: `true`
/// - learningType: `LearningType`
/// - maxMemoryCount: `0``learningType``nothing`
/// - shouldResetMemory: `true`
/// - dictionaryResourceURL:
/// - memoryDirectoryURL:
/// - sharedContainerURL:
/// - textReplacer:
/// - preloadDictionary:
/// - metadata: `ConvertRequestOptions.Metadata`
@available(*, deprecated, message: "it be removed in AzooKeyKanaKanjiConverter v1.0")
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, zenzaiMode: ZenzaiMode = .off, preloadDictionary: Bool = false, metadata: ConvertRequestOptions.Metadata?) {
self.init(
N_best: N_best,
requireJapanesePrediction: requireJapanesePrediction,
requireEnglishPrediction: requireEnglishPrediction,
keyboardLanguage: keyboardLanguage,
typographyLetterCandidate: typographyLetterCandidate,
unicodeCandidate: unicodeCandidate,
englishCandidateInRoman2KanaInput: englishCandidateInRoman2KanaInput,
fullWidthRomanCandidate: fullWidthRomanCandidate,
halfWidthKanaCandidate: halfWidthKanaCandidate,
learningType: learningType,
maxMemoryCount: maxMemoryCount,
shouldResetMemory: shouldResetMemory,
dictionaryResourceURL: dictionaryResourceURL,
memoryDirectoryURL: memoryDirectoryURL,
sharedContainerURL: sharedContainerURL,
// MARK: using deprecated initializer here
textReplacer: TextReplacer(),
zenzaiMode: zenzaiMode,
preloadDictionary: preloadDictionary,
metadata: metadata
)
}
public var N_best: Int
public var requireJapanesePrediction: Bool
public var requireEnglishPrediction: Bool
public var keyboardLanguage: KeyboardLanguage
// KeyboardSettinginjection
public var englishCandidateInRoman2KanaInput: Bool
public var fullWidthRomanCandidate: Bool
public var halfWidthKanaCandidate: Bool
public var learningType: LearningType
public var maxMemoryCount: Int
public var shouldResetMemory: Bool
///
public var textReplacer: TextReplacer
//
public var memoryDirectoryURL: URL
public var sharedContainerURL: URL
public var dictionaryResourceURL: URL
/// providers to generate "special" candidates such as Unicode conversion.
public var specialCandidateProviders: [any SpecialCandidateProvider]
public var zenzaiMode: ZenzaiMode
public var preloadDictionary: Bool
//
public var metadata: Metadata?
// MARK: API
package var requestQuery: RequestQuery = .default
static var `default`: Self {
Self(
N_best: 10,
requireJapanesePrediction: true,
requireEnglishPrediction: true,
keyboardLanguage: .ja_JP,
englishCandidateInRoman2KanaInput: true,
learningType: .inputAndOutput,
maxMemoryCount: 65536,
shouldResetMemory: false,
// dummy data, won't work
dictionaryResourceURL: Bundle.main.bundleURL,
// dummy data, won't work
memoryDirectoryURL: (try? FileManager.default.url(for: .libraryDirectory, in: .userDomainMask, appropriateFor: nil, create: false)) ?? Bundle.main.bundleURL,
// dummy data, won't work
sharedContainerURL: Bundle.main.bundleURL,
textReplacer: .empty,
specialCandidateProviders: nil,
preloadDictionary: false,
metadata: nil
)
}
public struct Metadata: Sendable {
/// - parameters:
/// - appVersionString: `KanaKanjiCovnerter.toVersionCandidate(_:)`
@available(*, deprecated, renamed: "init(versionString:)", message: "it be removed in AzooKeyKanaKanjiConverter v1.0")
public init(appVersionString: String) {
self.versionString = "azooKey Version " + appVersionString
}
/// - parameters:
/// - versionString: `"MyIME Version 0.7.1"``KanaKanjiCovnerter.toVersionCandidate(_:)`
public init(versionString: String = "Powererd by AzooKeyKanaKanjiConverter") {
self.versionString = versionString
}
var versionString: String
}
package enum RequestQuery: Sendable {
case `default`
case
}
public struct ZenzaiV2DependentMode: Sendable, Equatable, Hashable {
public init(profile: String? = nil, leftSideContext: String? = nil) {
self.profile = profile
self.leftSideContext = leftSideContext
}
/// 1020
public var profile: String?
///
public var leftSideContext: String?
}
public struct ZenzaiV3DependentMode: Sendable, Equatable, Hashable {
public init(profile: String? = nil, topic: String? = nil, style: String? = nil, preference: String? = nil, leftSideContext: String? = nil) {
self.profile = profile
self.topic = topic
self.style = style
self.preference = preference
self.leftSideContext = leftSideContext
}
/// 1020
public var profile: String?
/// topic1020
public var topic: String?
/// style1020
public var style: String?
/// preferencepreference1020
public var preference: String?
///
public var leftSideContext: String?
}
public enum ZenzVersion: Sendable, Equatable, Hashable {
case v1
case v2
case v3
}
public enum ZenzaiVersionDependentMode: Sendable, Equatable, Hashable {
case v1
case v2(ZenzaiV2DependentMode)
case v3(ZenzaiV3DependentMode)
public var version: ZenzVersion {
switch self {
case .v1:
return .v1
case .v2:
return .v2
case .v3:
return .v3
}
}
}
public struct ZenzaiMode: Sendable, Equatable {
public struct PersonalizationMode: Sendable, Equatable {
public init(baseNgramLanguageModel: String, personalNgramLanguageModel: String, n: Int = 5, d: Double = 0.75, alpha: Float = 0.5) {
self.baseNgramLanguageModel = baseNgramLanguageModel
self.personalNgramLanguageModel = personalNgramLanguageModel
self.n = n
self.d = d
self.alpha = alpha
}
var n: Int = 5
var d: Double = 0.75
var alpha: Float = 0.5
var baseNgramLanguageModel: String
var personalNgramLanguageModel: String
}
public static let off = ZenzaiMode(
enabled: false,
weightURL: URL(fileURLWithPath: ""),
inferenceLimit: 10,
requestRichCandidates: false,
versionDependentMode: .v3(.init())
)
/// activate *Zenzai* - Neural Kana-Kanji Conversiion Engine
/// - Parameters:
/// - weight: path for model weight (gguf)
/// - inferenceLimit: applying inference count limitation. Smaller limit makes conversion faster but quality will be worse. (Default: 10)
/// - requestRichCandidates: when this flag is true, the converter spends more time but generate richer N-Best candidates for candidate list view. Usually this option is not recommended for live conversion.
/// - personalizationMode: values for personalization.
/// - versionDependentMode: specify zenz model version and its configuration.
public static func on(weight: URL, inferenceLimit: Int = 10, requestRichCandidates: Bool = false, personalizationMode: PersonalizationMode?, versionDependentMode: ZenzaiVersionDependentMode = .v3(.init())) -> Self {
ZenzaiMode(
enabled: true,
weightURL: weight,
inferenceLimit: inferenceLimit,
requestRichCandidates: requestRichCandidates,
personalizationMode: personalizationMode,
versionDependentMode: versionDependentMode
)
}
var enabled: Bool
var weightURL: URL
var inferenceLimit: Int
var requestRichCandidates: Bool
var personalizationMode: PersonalizationMode?
var versionDependentMode: ZenzaiVersionDependentMode
}
}