Files
AzooKeyKanaKanjiConverter/Sources/KanaKanjiConverterModule/ConversionAlgorithms/PostCompositionPredictionCandidate.swift
2025-07-01 23:42:05 +09:00

67 lines
2.3 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// PostCompositionPredictionCandidate.swift
//
//
// Created by miwa on 2023/09/19.
//
import Foundation
///
public struct PostCompositionPredictionCandidate {
public init(text: String, value: PValue, type: PostCompositionPredictionCandidate.PredictionType) {
self.text = text
self.value = value
self.type = type
if Set(["", ".", ""]).contains(text) {
self.isTerminal = true
} else {
self.isTerminal = false
}
}
public var text: String
public var value: PValue
public var type: PredictionType
///
public var isTerminal: Bool
public func join(to candidate: Candidate) -> Candidate {
var candidate = candidate
switch self.type {
case .additional(let data):
for data in data {
candidate.text.append(contentsOf: data.word)
candidate.data.append(data)
}
candidate.value = self.value
candidate.correspondingCount = candidate.data.reduce(into: 0) { $0 += $1.ruby.count }
candidate.lastMid = data.last(where: DicdataStore.includeMMValueCalculation)?.mid ?? candidate.lastMid
return candidate
case .replacement(let targetData, let replacementData):
candidate.data.removeLast(targetData.count)
candidate.data.append(contentsOf: replacementData)
candidate.text = candidate.data.reduce(into: "") {$0 += $1.word}
candidate.value = self.value
candidate.lastMid = candidate.data.last(where: DicdataStore.includeMMValueCalculation)?.mid ?? MIDData.BOS.mid
candidate.correspondingCount = candidate.data.reduce(into: 0) { $0 += $1.ruby.count }
return candidate
}
}
public enum PredictionType: Sendable, Hashable {
case additional(data: [DicdataElement])
case replacement(targetData: [DicdataElement], replacementData: [DicdataElement])
}
var lastData: DicdataElement? {
switch self.type {
case .additional(let data):
return data.last
case .replacement(_, let replacementData):
return replacementData.last
}
}
}