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

689 lines
29 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
extension Tekkon {
// MARK: - Syllable Composer
/// Syllable Composer
/// 使 Struct
/// 調
///
/// String Literal @input
/// @arrange .ofDachen
@frozen public 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
}
/// 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 ? cnvZhuyinChainToTextbookReading(target: valReturnZhuyin) : valReturnZhuyin
case true: //
let valReturnPinyin = Tekkon.cnvPhonaToHanyuPinyin(target: value)
return isTextBookStyle ? Tekkon.cnvHanyuPinyinToTextbookStyle(target: valReturnPinyin) : valReturnPinyin
}
}
// macOS InputMethod Kit 使
/// - Parameters:
/// - isHanyuPinyin:
public func getInlineCompositionForDisplay(isHanyuPinyin: Bool = false) -> String {
switch parser {
case .ofHanyuPinyin, .ofSecondaryPinyin, .ofYalePinyin, .ofHualuoPinyin, .ofUniversalPinyin:
var toneReturned = ""
switch intonation.value {
case " ": toneReturned = "1"
case "ˊ": toneReturned = "2"
case "ˇ": toneReturned = "3"
case "ˋ": toneReturned = "4"
case "˙": toneReturned = "5"
default: break
}
return romajiBuffer + toneReturned
default: return getComposition(isHanyuPinyin: isHanyuPinyin)
}
}
///
public var isEmpty: Bool {
switch parser {
case .ofHanyuPinyin, .ofSecondaryPinyin, .ofYalePinyin, .ofHualuoPinyin, .ofUniversalPinyin:
return intonation.isEmpty && romajiBuffer.isEmpty
default: 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 .ofHanyuPinyin, .ofSecondaryPinyin, .ofYalePinyin, .ofHualuoPinyin, .ofUniversalPinyin:
return Tekkon.mapArayuruPinyin.contains(input)
}
}
return false
}
/// String
/// UniChar
///
/// 調
/// - Parameters:
/// - fromString: String
public mutating func receiveKey(fromString input: String = "") {
switch parser {
case .ofHanyuPinyin, .ofSecondaryPinyin, .ofYalePinyin, .ofHualuoPinyin, .ofUniversalPinyin:
if mapArayuruPinyinIntonation.keys.contains(input) {
if let theTone = mapArayuruPinyinIntonation[input] {
intonation = Phonabet(theTone)
}
} else {
// romajiBuffer
if romajiBuffer.count > 5 {
romajiBuffer = String(romajiBuffer.dropFirst())
}
let romajiBufferBackup = romajiBuffer + input
receiveSequence(romajiBufferBackup, isRomaji: true)
romajiBuffer = romajiBufferBackup
}
default: receiveKey(fromPhonabet: translate(key: input))
}
}
/// 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 "".contains(semivowel.value) { semivowel = "" }
if "ㄧㄩ".contains(semivowel.value) { thePhone = "" }
case "":
if "".contains(semivowel.value) { semivowel = "" }
case "", "":
if "ㄅㄆㄇㄈ".contains(consonant.value), semivowel.value == "" { semivowel.clear() }
case "":
if "ㄋㄌ".contains(consonant.value), semivowel.value == "" { semivowel.clear() }
case "":
if "ㄅㄆㄇㄈ".contains(consonant.value), "ㄛㄥ".contains(vowel.value) { vowel.clear() }
if "ㄋㄌ".contains(consonant.value), "".contains(vowel.value) { vowel.clear() }
if "".contains(vowel.value) { vowel = "" }
if "".contains(vowel.value) { thePhone = "" }
case "", "", "", "":
if ["ㄨㄛ", "ㄨㄥ"].contains(semivowel.value + vowel.value) { semivowel.clear() }
default: break
}
}
switch thePhone.type {
case .consonant: consonant = thePhone
case .semivowel: semivowel = thePhone
case .vowel: vowel = thePhone
case .intonation: intonation = thePhone
default: break
}
}
///
/// - Remark: 調
/// - Parameters:
/// - givenSequence: String
/// - isRomaji: 西
public mutating func receiveSequence(_ givenSequence: String = "", isRomaji: Bool = false) {
clear()
if isRomaji {
switch parser {
case .ofHanyuPinyin:
if let dictResult = mapHanyuPinyin[givenSequence] {
for phonabet in dictResult {
receiveKey(fromPhonabet: String(phonabet))
}
}
case .ofSecondaryPinyin:
if let dictResult = mapSecondaryPinyin[givenSequence] {
for phonabet in dictResult {
receiveKey(fromPhonabet: String(phonabet))
}
}
case .ofYalePinyin:
if let dictResult = mapYalePinyin[givenSequence] {
for phonabet in dictResult {
receiveKey(fromPhonabet: String(phonabet))
}
}
case .ofHualuoPinyin:
if let dictResult = mapHualuoPinyin[givenSequence] {
for phonabet in dictResult {
receiveKey(fromPhonabet: String(phonabet))
}
}
case .ofUniversalPinyin:
if let dictResult = mapUniversalPinyin[givenSequence] {
for phonabet in dictResult {
receiveKey(fromPhonabet: String(phonabet))
}
}
default: break
}
} else {
for key in givenSequence {
receiveKey(fromString: String(key))
}
}
}
///
/// - Parameters:
/// - givenSequence: String
public mutating func convertSequenceToRawComposition(_ givenSequence: String = "") -> String {
receiveSequence(givenSequence)
return value
}
/// 使 BackSpace
/// 調
///
///
public mutating func doBackSpace() {
if [.ofHanyuPinyin, .ofSecondaryPinyin, .ofYalePinyin, .ofHualuoPinyin, .ofUniversalPinyin].contains(parser),
!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
}
// MARK: - Parser Processings
//
/// String
///
///
/// - Parameters:
/// - key: String
public mutating func translate(key: String = "") -> String {
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 .ofHanyuPinyin, .ofSecondaryPinyin, .ofYalePinyin, .ofHualuoPinyin, .ofUniversalPinyin:
break //
}
return ""
}
///
/// - Parameter incomingPhonabet: Phonabet
public mutating func commonFixWhenHandlingDynamicArrangeInputs(target incomingPhonabet: Phonabet) {
//
switch incomingPhonabet.type {
case .semivowel:
switch consonant {
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
}
case "":
switch incomingPhonabet {
case "": consonant = "" //
case "": consonant = "" //
case "": consonant = "" //
default: break
}
default: break
}
case .vowel:
if semivowel.isEmpty {
consonant.selfReplace("", "")
consonant.selfReplace("", "")
consonant.selfReplace("", "")
}
default: break
}
}
///
///
///
/// - Parameters:
/// - key: String
public mutating func handleETen26(key: String = "") -> String {
var strReturn = Tekkon.mapETen26StaticKeys[key] ?? ""
let incomingPhonabet = Phonabet(strReturn)
switch key {
case "d": if !isPronouncable { consonant = "" } else { intonation = "˙" }
case "f": if !isPronouncable { consonant = "" } else { intonation = "ˊ" }
case "j": if !isPronouncable { consonant = "" } else { intonation = "ˇ" }
case "k": if !isPronouncable { consonant = "" } else { intonation = "ˋ" }
case "h": if consonant.isEmpty, semivowel.isEmpty { consonant = "" } else { vowel = "" }
case "l": if consonant.isEmpty, semivowel.isEmpty { consonant = "" } else { vowel = "" }
case "m": if consonant.isEmpty, semivowel.isEmpty { consonant = "" } else { vowel = "" }
case "n": if consonant.isEmpty, semivowel.isEmpty { consonant = "" } else { vowel = "" }
case "q": if consonant.isEmpty, semivowel.isEmpty { consonant = "" } else { vowel = "" }
case "t": if consonant.isEmpty, semivowel.isEmpty { consonant = "" } else { vowel = "" }
case "w": if consonant.isEmpty, semivowel.isEmpty { consonant = "" } else { vowel = "" }
case "p":
if consonant.isEmpty, semivowel.isEmpty {
consonant = ""
} else if consonant.isEmpty, semivowel == "" {
vowel = ""
} else if consonant.isEmpty {
vowel = ""
} else {
vowel = ""
}
default: break
}
//
commonFixWhenHandlingDynamicArrangeInputs(target: incomingPhonabet)
if "dfjk ".contains(key),
!consonant.isEmpty, semivowel.isEmpty, vowel.isEmpty
{
consonant.selfReplace("", "")
consonant.selfReplace("", "")
consonant.selfReplace("", "")
consonant.selfReplace("", "")
consonant.selfReplace("", "")
consonant.selfReplace("", "")
}
//
if value == "ㄍ˙" { consonant = "" }
//
if "dfhjklmnpqtw".contains(key) { strReturn = "" }
//
return strReturn
}
///
///
///
/// - Parameters:
/// - key: String
public mutating func handleHsu(key: String = "") -> String {
var strReturn = Tekkon.mapHsuStaticKeys[key] ?? ""
let incomingPhonabet = Phonabet(strReturn)
if key == " ", value == "" {
consonant.clear()
vowel = ""
}
switch key {
case "d": if isPronouncable { intonation = "ˊ" } else { consonant = "" }
case "f": if isPronouncable { intonation = "ˇ" } else { consonant = "" }
case "s": if isPronouncable { intonation = "˙" } else { consonant = "" }
case "j": if isPronouncable { intonation = "ˋ" } else { consonant = "" }
case "a": if consonant.isEmpty, semivowel.isEmpty { consonant = "" } else { vowel = "" }
case "v": if semivowel.isEmpty { consonant = "" } else { consonant = "" }
case "c": if semivowel.isEmpty { consonant = "" } else { consonant = "" }
case "e": if semivowel.isEmpty { semivowel = "" } else { vowel = "" }
case "g": if consonant.isEmpty, semivowel.isEmpty { consonant = "" } else { vowel = "" }
case "h": if consonant.isEmpty, semivowel.isEmpty { consonant = "" } else { vowel = "" }
case "k": if consonant.isEmpty, semivowel.isEmpty { consonant = "" } else { vowel = "" }
case "m": if consonant.isEmpty, semivowel.isEmpty { consonant = "" } else { vowel = "" }
case "n": if consonant.isEmpty, semivowel.isEmpty { consonant = "" } else { vowel = "" }
case "l":
if value.isEmpty, !consonant.isEmpty, !semivowel.isEmpty {
vowel = ""
} else if consonant.isEmpty, semivowel.isEmpty {
consonant = ""
} else {
vowel = ""
}
default: break
}
//
commonFixWhenHandlingDynamicArrangeInputs(target: incomingPhonabet)
if "dfjs ".contains(key) {
if !consonant.isEmpty, semivowel.isEmpty, vowel.isEmpty {
consonant.selfReplace("", "")
consonant.selfReplace("", "")
consonant.selfReplace("", "")
consonant.selfReplace("", "")
consonant.selfReplace("", "")
}
if !consonant.isEmpty, vowel.isEmpty {
consonant.selfReplace("", "")
}
if "ㄢㄣㄤㄥ".contains(vowel.value), semivowel.isEmpty {
consonant.selfReplace("", "")
consonant.selfReplace("", "")
consonant.selfReplace("", "")
}
if "ㄐㄑㄒ".contains(consonant.value), semivowel.isEmpty {
consonant.selfReplace("", "")
consonant.selfReplace("", "")
consonant.selfReplace("", "")
}
if consonant == "", semivowel.isEmpty, vowel.isEmpty {
consonant.clear()
vowel = ""
}
}
//
if value == "ㄔ˙" { consonant = "" }
//
if "acdefghjklmns".contains(key) { strReturn = "" }
//
return strReturn
}
///
///
///
/// - Parameters:
/// - key: String
public mutating func handleStarlight(key: String) -> String {
var strReturn = Tekkon.mapStarlightStaticKeys[key] ?? ""
let incomingPhonabet = Phonabet(strReturn)
switch key {
case "e": return "ㄧㄩ".contains(semivowel.value) ? "" : ""
case "f": return vowel == "" || !isPronouncable ? "" : ""
case "g": return vowel == "" || !isPronouncable ? "" : ""
case "k": return vowel == "" || !isPronouncable ? "" : ""
case "l": return vowel == "" || !isPronouncable ? "" : ""
case "m": return vowel == "" || !isPronouncable ? "" : ""
case "n": return vowel == "" || !isPronouncable ? "" : ""
case "t": return vowel == "" || !isPronouncable ? "" : ""
default: break
}
//
commonFixWhenHandlingDynamicArrangeInputs(target: incomingPhonabet)
if "67890 ".contains(key) {
if !consonant.isEmpty, semivowel.isEmpty, vowel.isEmpty {
consonant.selfReplace("", "")
consonant.selfReplace("", "")
consonant.selfReplace("", "")
consonant.selfReplace("", "")
consonant.selfReplace("", "")
consonant.selfReplace("", "")
consonant.selfReplace("", "")
}
}
//
if "efgklmn".contains(key) { strReturn = "" }
//
return strReturn
}
///
///
///
/// - Parameters:
/// - key: String
public mutating func handleDachen26(key: String = "") -> String {
var strReturn = Tekkon.mapDachenCP26StaticKeys[key] ?? ""
switch key {
case "e": if isPronouncable { intonation = "ˊ" } else { consonant = "" }
case "r": if isPronouncable { intonation = "ˇ" } else { consonant = "" }
case "d": if isPronouncable { intonation = "ˋ" } else { consonant = "" }
case "y": if isPronouncable { intonation = "˙" } else { consonant = "" }
case "b": if !consonant.isEmpty || !semivowel.isEmpty { vowel = "" } else { consonant = "" }
case "i": if vowel.isEmpty || vowel == "" { vowel = "" } else { vowel = "" }
case "l": if vowel.isEmpty || vowel == "" { vowel = "" } else { vowel = "" }
case "n":
if !consonant.isEmpty || !semivowel.isEmpty {
if consonant == "", semivowel.isEmpty, vowel.isEmpty { consonant.clear() }
vowel = ""
} else {
consonant = ""
}
case "o": if vowel.isEmpty || vowel == "" { vowel = "" } else { vowel = "" }
case "p": if vowel.isEmpty || vowel == "" { vowel = "" } else { vowel = "" }
case "q": if consonant.isEmpty || consonant == "" { consonant = "" } else { consonant = "" }
case "t": if consonant.isEmpty || consonant == "" { consonant = "" } else { consonant = "" }
case "w": if consonant.isEmpty || consonant == "" { consonant = "" } else { consonant = "" }
case "m":
if semivowel == "", vowel != "" {
semivowel.clear()
vowel = ""
} else if semivowel != "", vowel == "" {
semivowel = ""
vowel.clear()
} else if !semivowel.isEmpty {
vowel = ""
} else {
receiveKey(fromPhonabet: "ㄐㄑㄒ".contains(consonant.value) ? "" : "")
}
case "u":
if semivowel == "", vowel != "" {
semivowel.clear()
vowel = ""
} else if semivowel != "", vowel == "" {
semivowel = ""
} else if semivowel == "", vowel == "" {
semivowel.clear()
vowel.clear()
} else if !semivowel.isEmpty {
vowel = ""
} else {
semivowel = ""
}
default: break
}
//
if "qwtilopnbmuerdy".contains(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)
}
}
}