vChewing-macOS/Packages/vChewing_Tekkon/Sources/Tekkon/Tekkon_SyllableComposer.swift

787 lines
31 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.

// (c) 2022 and onwards The vChewing Project (MIT-NTL License).
// ====================
// This code is released under the MIT license (SPDX-License-Identifier: MIT)
// ... with NTL restriction stating that:
// No trademark license is granted to use the trade names, trademarks, service
// marks, or product names of Contributor, except as required to fulfill notice
// requirements defined in MIT License.
import Foundation
public extension Tekkon {
// MARK: - Syllable Composer
/// Syllable Composer
/// 使 Struct
/// 調
///
/// String Literal @input
/// @arrange .ofDachen
@frozen struct Composer: Equatable, Hashable, ExpressibleByStringLiteral {
///
public var consonant: Phonabet = ""
///
public var semivowel: Phonabet = ""
///
public var vowel: Phonabet = ""
/// 調
public var intonation: Phonabet = ""
///
public var romajiBuffer: String = ""
/// Windows / macOS
public var parser: MandarinParser = .ofDachen
///
public var phonabetCombinationCorrectionEnabled = false
/// 調
/// 調
/// 使.getComposition().value
public var value: String {
consonant.value + semivowel.value + vowel.value + intonation.value
}
///
public var isPinyinMode: Bool { parser.rawValue >= 100 }
/// 調
/// - Parameter withIntonation: 調
/// - Returns: Phonabet
public func count(withIntonation: Bool = false) -> Int {
[consonant.isValid, semivowel.isValid, vowel.isValid]
.reduce((withIntonation && intonation.isValid) ? 1 : 0) { $0 + ($1 ? 1 : 0) }
}
/// value /
/// 調
/// - Parameters:
/// - isHanyuPinyin:
/// - isTextBookStyle: /
public func getComposition(isHanyuPinyin: Bool = false, isTextBookStyle: Bool = false) -> String {
switch isHanyuPinyin {
case false: //
let valReturnZhuyin = value.replacingOccurrences(of: " ", with: "")
return isTextBookStyle ? cnvPhonaToTextbookReading(target: valReturnZhuyin) : valReturnZhuyin
case true: //
let valReturnPinyin = Tekkon.cnvPhonaToHanyuPinyin(targetJoined: value)
return isTextBookStyle ? Tekkon.cnvHanyuPinyinToTextbookStyle(targetJoined: valReturnPinyin) : valReturnPinyin
}
}
// macOS InputMethod Kit 使
/// - Parameters:
/// - isHanyuPinyin:
public func getInlineCompositionForDisplay(isHanyuPinyin: Bool = false) -> String {
guard isPinyinMode else { return getComposition(isHanyuPinyin: isHanyuPinyin) }
var toneReturned = ""
switch intonation.value {
case " ": toneReturned = "1"
case "ˊ": toneReturned = "2"
case "ˇ": toneReturned = "3"
case "ˋ": toneReturned = "4"
case "˙": toneReturned = "5"
default: break
}
return romajiBuffer.replacingOccurrences(of: "v", with: "ü") + toneReturned
}
///
public var isEmpty: Bool {
guard !isPinyinMode else { return intonation.isEmpty && romajiBuffer.isEmpty }
return intonation.isEmpty && vowel.isEmpty && semivowel.isEmpty && consonant.isEmpty
}
///
public var isPronouncable: Bool {
!vowel.isEmpty || !semivowel.isEmpty || !consonant.isEmpty
}
// MARK:
/// @input
/// @arrange .ofDachen
/// - Parameters:
/// - input: String
/// - arrange: 使
/// - correction:
public init(_ input: String = "", arrange parser: MandarinParser = .ofDachen, correction: Bool = false) {
phonabetCombinationCorrectionEnabled = correction
ensureParser(arrange: parser)
receiveKey(fromString: input)
}
/// 調
/// ensureParser
public mutating func clear() {
consonant.clear()
semivowel.clear()
vowel.clear()
intonation.clear()
romajiBuffer = ""
}
// MARK: - Public Functions
///
///
/// parser
/// - Parameters:
/// - key: UniChar
public func inputValidityCheck(key inputKey: UniChar = 0) -> Bool {
if let scalar = UnicodeScalar(inputKey) {
let input = String(scalar)
switch parser {
case .ofDachen:
return Tekkon.mapQwertyDachen[input] != nil
case .ofDachen26:
return Tekkon.mapDachenCP26StaticKeys[input] != nil
case .ofETen:
return Tekkon.mapQwertyETenTraditional[input] != nil
case .ofHsu:
return Tekkon.mapHsuStaticKeys[input] != nil
case .ofETen26:
return Tekkon.mapETen26StaticKeys[input] != nil
case .ofIBM:
return Tekkon.mapQwertyIBM[input] != nil
case .ofMiTAC:
return Tekkon.mapQwertyMiTAC[input] != nil
case .ofSeigyou:
return Tekkon.mapSeigyou[input] != nil
case .ofFakeSeigyou:
return Tekkon.mapFakeSeigyou[input] != nil
case .ofStarlight:
return Tekkon.mapStarlightStaticKeys[input] != nil
case .ofAlvinLiu:
return Tekkon.mapAlvinLiuStaticKeys[input] != nil
case .ofWadeGilesPinyin:
return Tekkon.mapWadeGilesPinyinKeys.contains(input)
case .ofHanyuPinyin, .ofSecondaryPinyin, .ofYalePinyin, .ofHualuoPinyin, .ofUniversalPinyin:
return Tekkon.mapArayuruPinyin.contains(input)
}
}
return false
}
///
mutating func updateRomajiBuffer() {
romajiBuffer = Tekkon.cnvPhonaToHanyuPinyin(targetJoined: consonant.value + semivowel.value + vowel.value)
}
///
/// - Parameters:
/// - strOf:
/// - strWith:
mutating func fixValue(_ strOf: String, _ strWith: String = "") {
guard !strOf.isEmpty, !strWith.isEmpty else { return }
let theOld = Phonabet(strOf)
switch theOld {
case consonant: consonant.clear()
case semivowel: semivowel.clear()
case vowel: vowel.clear()
case intonation: intonation.clear()
default: return
}
receiveKey(fromPhonabet: strWith)
}
/// String
/// UniChar
///
/// 調
/// - Parameters:
/// - fromString: String
public mutating func receiveKey(fromString input: String = "") {
guard isPinyinMode else {
receiveKey(fromPhonabet: translate(key: input))
return
}
if mapArayuruPinyinIntonation.keys.contains(input) {
if let theTone = mapArayuruPinyinIntonation[input] {
intonation = Phonabet(theTone)
}
} else {
// romajiBuffer
let maxCount: Int = (parser == .ofWadeGilesPinyin) ? 7 : 6
if romajiBuffer.count > maxCount - 1 {
romajiBuffer = String(romajiBuffer.dropFirst())
}
let romajiBufferBackup = romajiBuffer + input
receiveSequence(romajiBufferBackup, isRomaji: true)
romajiBuffer = romajiBufferBackup
}
}
/// UniChar
/// UniChar String
///
/// 調
/// - Parameters:
/// - fromCharCode: UniChar
public mutating func receiveKey(fromCharCode inputCharCode: UniChar = 0) {
if let scalar = UnicodeScalar(inputCharCode) {
receiveKey(fromString: String(scalar))
}
}
///
///
/// - Parameters:
/// - fromPhonabet:
public mutating func receiveKey(fromPhonabet phonabet: String = "") {
var thePhone: Phonabet = .init(phonabet)
if phonabetCombinationCorrectionEnabled {
switch phonabet {
case "", "":
if vowel.value == "" { vowel = "" }
case "":
if "".doesHave(semivowel.value) { semivowel = "" }
if "ㄧㄩ".doesHave(semivowel.value) { thePhone = "" }
case "":
if "".doesHave(semivowel.value) { semivowel = "" }
case "", "":
if phonabet == "", semivowel.value == "" { semivowel = "" }
if "ㄅㄆㄇㄈ".doesHave(consonant.value), semivowel.value == "" { semivowel.clear() }
case "":
if "ㄋㄌ".doesHave(consonant.value), semivowel.value == "" { semivowel.clear() }
case "":
if "ㄅㄆㄇㄈ".doesHave(consonant.value), "ㄛㄥ".doesHave(vowel.value) { vowel.clear() }
if "ㄋㄌ".doesHave(consonant.value), "".doesHave(vowel.value) { vowel.clear() }
if "".doesHave(vowel.value) { vowel = "" }
if "".doesHave(vowel.value) { thePhone = "" }
case "", "", "", "":
if ["ㄨㄛ", "ㄨㄥ"].contains(semivowel.value + vowel.value) { semivowel.clear() }
default: break
}
if [.vowel, .intonation].contains(thePhone.type), "ㄓㄔㄕㄗㄘㄙ".doesHave(consonant.value) {
switch semivowel.value {
case "": semivowel.clear()
case "":
switch consonant {
case _ where "ㄓㄗ".doesHave(consonant.value): consonant = ""
case _ where "ㄔㄘ".doesHave(consonant.value): consonant = ""
case _ where "ㄕㄙ".doesHave(consonant.value): consonant = ""
default: break
}
default: break
}
}
}
switch thePhone.type {
case .consonant: consonant = thePhone
case .semivowel: semivowel = thePhone
case .vowel: vowel = thePhone
case .intonation: intonation = thePhone
default: break
}
updateRomajiBuffer()
}
///
/// - Remark: 調
/// - Parameters:
/// - givenSequence: String
/// - isRomaji: 西
public mutating func receiveSequence(_ givenSequence: String = "", isRomaji: Bool = false) {
clear()
guard isRomaji else {
givenSequence.forEach { receiveKey(fromString: $0.description) }
return
}
var dictResult: String?
switch parser {
case .ofHanyuPinyin:
dictResult = mapHanyuPinyin[givenSequence]
case .ofSecondaryPinyin:
dictResult = mapSecondaryPinyin[givenSequence]
case .ofYalePinyin:
dictResult = mapYalePinyin[givenSequence]
case .ofHualuoPinyin:
dictResult = mapHualuoPinyin[givenSequence]
case .ofUniversalPinyin:
dictResult = mapUniversalPinyin[givenSequence]
case .ofWadeGilesPinyin:
dictResult = mapWadeGilesPinyin[givenSequence]
default: break
}
dictResult?.forEach { receiveKey(fromPhonabet: $0.description) }
}
///
/// - Parameters:
/// - givenSequence: String
public mutating func convertSequenceToRawComposition(_ givenSequence: String = "") -> String {
receiveSequence(givenSequence)
return value
}
/// 使 BackSpace
/// 調
///
///
public mutating func doBackSpace() {
if isPinyinMode, !romajiBuffer.isEmpty {
if !intonation.isEmpty {
intonation.clear()
} else {
romajiBuffer = String(romajiBuffer.dropLast())
}
} else if !intonation.isEmpty {
intonation.clear()
} else if !vowel.isEmpty {
vowel.clear()
} else if !semivowel.isEmpty {
semivowel.clear()
} else if !consonant.isEmpty {
consonant.clear()
}
}
/// 調調
/// - Parameters:
/// - withNothingElse: 調
public func hasIntonation(withNothingElse: Bool = false) -> Bool {
if !withNothingElse {
return !intonation.isEmpty
}
return !intonation.isEmpty && vowel.isEmpty && semivowel.isEmpty && consonant.isEmpty
}
// Composer
/// - Parameters:
/// - arrange:
public mutating func ensureParser(arrange: MandarinParser = .ofDachen) {
parser = arrange
}
///
///
///
/// - Remark:
/// - Parameter pronouncable:
/// - Returns: nil
public func phonabetKeyForQuery(pronouncable: Bool) -> String? {
let readingKey = getComposition()
var validKeyGeneratable = false
switch isPinyinMode {
case false:
switch pronouncable {
case false:
validKeyGeneratable = !readingKey.isEmpty
case true:
validKeyGeneratable = isPronouncable
}
case true: validKeyGeneratable = isPronouncable
}
return validKeyGeneratable ? readingKey : nil
}
// MARK: - Parser Processing
//
/// String
///
///
/// - Parameters:
/// - key: String
public mutating func translate(key: String = "") -> String {
guard !isPinyinMode else { return "" }
switch parser {
case .ofDachen:
return Tekkon.mapQwertyDachen[key] ?? ""
case .ofDachen26:
return handleDachen26(key: key)
case .ofETen:
return Tekkon.mapQwertyETenTraditional[key] ?? ""
case .ofHsu:
return handleHsu(key: key)
case .ofETen26:
return handleETen26(key: key)
case .ofIBM:
return Tekkon.mapQwertyIBM[key] ?? ""
case .ofMiTAC:
return Tekkon.mapQwertyMiTAC[key] ?? ""
case .ofSeigyou:
return Tekkon.mapSeigyou[key] ?? ""
case .ofFakeSeigyou:
return Tekkon.mapFakeSeigyou[key] ?? ""
case .ofStarlight:
return handleStarlight(key: key)
case .ofAlvinLiu:
return handleAlvinLiu(key: key)
default:
return ""
}
}
///
/// - Parameter incomingPhonabet: Phonabet
public mutating func commonFixWhenHandlingDynamicArrangeInputs(target incomingPhonabet: Phonabet) {
//
switch incomingPhonabet.type {
case .semivowel:
switch consonant {
case "":
switch incomingPhonabet {
// 26
case "": consonant = "" //
case "": consonant = "" //
default: break
}
case "":
switch incomingPhonabet {
case "": consonant = "" //
case "": consonant = "" //
case "": consonant = "" //
default: break
}
case "":
switch incomingPhonabet {
case "": consonant = "" //
case "": consonant = "" //
case "": consonant = "" //
default: break
}
case "":
switch incomingPhonabet {
case "": consonant = "" //
case "": consonant = "" //
case "": consonant = "" //
default: break
}
default: break
}
if incomingPhonabet == "" {
fixValue("", "")
fixValue("", "")
fixValue("", "")
}
case .vowel:
if semivowel.isEmpty {
fixValue("", "")
fixValue("", "")
fixValue("", "")
}
default: break
}
}
///
///
///
/// - Parameters:
/// - key: String
public mutating func handleETen26(key: String = "") -> String {
var strReturn = Tekkon.mapETen26StaticKeys[key] ?? ""
let keysToHandleHere = "dfhjklmnpqtw"
switch key {
case "d" where isPronouncable: strReturn = "˙"
case "f" where isPronouncable: strReturn = "ˊ"
case "j" where isPronouncable: strReturn = "ˇ"
case "k" where isPronouncable: strReturn = "ˋ"
case "e" where consonant == "": consonant = ""
case "p" where !consonant.isEmpty || semivowel == "": strReturn = ""
case "h" where !consonant.isEmpty || !semivowel.isEmpty: strReturn = ""
case "l" where !consonant.isEmpty || !semivowel.isEmpty: strReturn = ""
case "m" where !consonant.isEmpty || !semivowel.isEmpty: strReturn = ""
case "n" where !consonant.isEmpty || !semivowel.isEmpty: strReturn = ""
case "q" where !consonant.isEmpty || !semivowel.isEmpty: strReturn = ""
case "t" where !consonant.isEmpty || !semivowel.isEmpty: strReturn = ""
case "w" where !consonant.isEmpty || !semivowel.isEmpty: strReturn = ""
default: break
}
if keysToHandleHere.doesHave(key) {
receiveKey(fromPhonabet: strReturn)
}
//
commonFixWhenHandlingDynamicArrangeInputs(target: Phonabet(strReturn))
if "dfjk ".doesHave(key), count() == 1 {
fixValue("", "")
fixValue("", "")
fixValue("", "")
fixValue("", "")
fixValue("", "")
fixValue("", "")
}
//
if value == "ㄍ˙" { consonant = "" }
//
if keysToHandleHere.doesHave(key) { strReturn = "" }
//
return strReturn
}
///
///
///
/// - Parameters:
/// - key: String
public mutating func handleHsu(key: String = "") -> String {
var strReturn = Tekkon.mapHsuStaticKeys[key] ?? ""
let keysToHandleHere = "acdefghjklmns"
switch key {
case "d" where isPronouncable: strReturn = "ˊ"
case "f" where isPronouncable: strReturn = "ˇ"
case "s" where isPronouncable: strReturn = "˙"
case "j" where isPronouncable: strReturn = "ˋ"
case "a" where !consonant.isEmpty || !semivowel.isEmpty: strReturn = ""
case "v" where !semivowel.isEmpty: strReturn = ""
case "c" where !semivowel.isEmpty: strReturn = ""
case "e" where !semivowel.isEmpty: strReturn = ""
case "g" where !consonant.isEmpty || !semivowel.isEmpty: strReturn = ""
case "h" where !consonant.isEmpty || !semivowel.isEmpty: strReturn = ""
case "k" where !consonant.isEmpty || !semivowel.isEmpty: strReturn = ""
case "m" where !consonant.isEmpty || !semivowel.isEmpty: strReturn = ""
case "n" where !consonant.isEmpty || !semivowel.isEmpty: strReturn = ""
case "l":
if value.isEmpty, !consonant.isEmpty, !semivowel.isEmpty {
strReturn = ""
} else if consonant.isEmpty, semivowel.isEmpty {
strReturn = ""
} else {
strReturn = ""
}
default: break
}
if keysToHandleHere.doesHave(key) {
receiveKey(fromPhonabet: strReturn)
}
//
commonFixWhenHandlingDynamicArrangeInputs(target: Phonabet(strReturn))
if "dfjs ".doesHave(key), count() == 1 {
fixValue("", "")
fixValue("", "")
fixValue("", "")
fixValue("", "")
fixValue("", "")
fixValue("", "")
fixValue("", "")
fixValue("", "")
fixValue("", "")
fixValue("", "")
}
//
if value == "ㄔ˙" { consonant = "" }
//
if keysToHandleHere.doesHave(key) { strReturn = "" }
//
return strReturn
}
///
///
///
/// - Parameters:
/// - key: String
public mutating func handleStarlight(key: String) -> String {
var strReturn = Tekkon.mapStarlightStaticKeys[key] ?? ""
let keysToHandleHere = "efgklmnt"
switch key {
case "e" where "ㄧㄩ".doesHave(semivowel.value): strReturn = ""
case "f" where !consonant.isEmpty || !semivowel.isEmpty: strReturn = ""
case "g" where !consonant.isEmpty || !semivowel.isEmpty: strReturn = ""
case "k" where !consonant.isEmpty || !semivowel.isEmpty: strReturn = ""
case "l" where !consonant.isEmpty || !semivowel.isEmpty: strReturn = ""
case "m" where !consonant.isEmpty || !semivowel.isEmpty: strReturn = ""
case "n" where !consonant.isEmpty || !semivowel.isEmpty: strReturn = ""
case "t" where !consonant.isEmpty || !semivowel.isEmpty: strReturn = ""
default: break
}
if keysToHandleHere.doesHave(key) {
receiveKey(fromPhonabet: strReturn)
}
//
commonFixWhenHandlingDynamicArrangeInputs(target: Phonabet(strReturn))
if "67890 ".doesHave(key), count() == 1 {
fixValue("", "")
fixValue("", "")
fixValue("", "")
fixValue("", "")
fixValue("", "")
fixValue("", "")
fixValue("", "")
}
//
if keysToHandleHere.doesHave(key) { strReturn = "" }
//
return strReturn
}
///
///
///
/// - Parameters:
/// - key: String
public mutating func handleDachen26(key: String = "") -> String {
var strReturn = Tekkon.mapDachenCP26StaticKeys[key] ?? ""
switch key {
case "e" where isPronouncable: strReturn = "ˊ"
case "r" where isPronouncable: strReturn = "ˇ"
case "d" where isPronouncable: strReturn = "ˋ"
case "y" where isPronouncable: strReturn = "˙"
case "b" where !consonant.isEmpty || !semivowel.isEmpty: strReturn = ""
case "i" where vowel.isEmpty || vowel == "": strReturn = ""
case "l" where vowel.isEmpty || vowel == "": strReturn = ""
case "n" where !consonant.isEmpty || !semivowel.isEmpty:
if value == "" { consonant.clear() }
strReturn = ""
case "o" where vowel.isEmpty || vowel == "": strReturn = ""
case "p" where vowel.isEmpty || vowel == "": strReturn = ""
case "q" where consonant.isEmpty || consonant == "": strReturn = ""
case "t" where consonant.isEmpty || consonant == "": strReturn = ""
case "w" where consonant.isEmpty || consonant == "": strReturn = ""
case "m":
if semivowel == "", vowel != "" {
semivowel.clear()
strReturn = ""
} else if semivowel != "", vowel == "" {
vowel.clear()
strReturn = ""
} else if !semivowel.isEmpty {
strReturn = ""
} else {
strReturn = "ㄐㄑㄒ".doesHave(consonant.value) ? "" : ""
}
case "u":
if semivowel == "", vowel != "" {
semivowel.clear()
strReturn = ""
} else if semivowel != "", vowel == "" {
strReturn = ""
} else if semivowel == "", vowel == "" {
semivowel.clear()
vowel.clear()
} else if !semivowel.isEmpty {
strReturn = ""
} else {
strReturn = ""
}
default: break
}
//
return strReturn
}
///
///
///
/// - Remark:
/// - Parameters:
/// - key: String
public mutating func handleAlvinLiu(key: String) -> String {
var strReturn = Tekkon.mapAlvinLiuStaticKeys[key] ?? ""
//
if strReturn != "" && !vowel.isEmpty { fixValue("", "") }
let keysToHandleHere = "dfjlegnhkbmc"
switch key {
case "d" where isPronouncable: strReturn = "˙"
case "f" where isPronouncable: strReturn = "ˊ"
case "j" where isPronouncable: strReturn = "ˇ"
case "l" where isPronouncable: strReturn = "ˋ"
case "e" where "ㄧㄩ".doesHave(semivowel.value): strReturn = ""
case "g" where !consonant.isEmpty || !semivowel.isEmpty: strReturn = ""
case "n" where !consonant.isEmpty || !semivowel.isEmpty: strReturn = ""
case "h" where !consonant.isEmpty || !semivowel.isEmpty: strReturn = ""
case "k" where !consonant.isEmpty || !semivowel.isEmpty: strReturn = ""
case "b" where !consonant.isEmpty || !semivowel.isEmpty: strReturn = ""
case "m" where !consonant.isEmpty || !semivowel.isEmpty: strReturn = ""
case "c" where !consonant.isEmpty || !semivowel.isEmpty: strReturn = ""
default: break
}
if keysToHandleHere.doesHave(key) {
receiveKey(fromPhonabet: strReturn)
}
//
commonFixWhenHandlingDynamicArrangeInputs(target: Phonabet(strReturn))
if "dfjl ".doesHave(key), count() == 1 {
fixValue("", "")
fixValue("", "")
fixValue("", "")
fixValue("", "")
fixValue("", "")
fixValue("", "")
fixValue("", "")
fixValue("", "")
fixValue("", "")
fixValue("", "")
fixValue("", "")
}
//
if keysToHandleHere.doesHave(key) { strReturn = "" }
//
return strReturn
}
// MARK: - Misc Definitions
/// 滿 "Equatable, Hashable, ExpressibleByStringLiteral"
public static func == (lhs: Composer, rhs: Composer) -> Bool {
lhs.value == rhs.value
}
public func hash(into hasher: inout Hasher) {
hasher.combine(consonant)
hasher.combine(semivowel)
hasher.combine(vowel)
hasher.combine(intonation)
}
public init(stringLiteral value: String) {
self.init(value)
}
public init(unicodeScalarLiteral value: String) {
self.init(stringLiteral: value)
}
public init(extendedGraphemeClusterLiteral value: String) {
self.init(stringLiteral: value)
}
}
}
private extension String {
func doesHave(_ target: String) -> Bool {
target.isEmpty ? isEmpty : contains(target)
}
}