Tekkon // (v1.4.2) Integrate phonabetKeyForQuery().

This commit is contained in:
ShikiSuen 2023-05-05 12:54:07 +08:00
parent 3f427ae94f
commit c946064385
2 changed files with 60 additions and 73 deletions

View File

@ -46,6 +46,9 @@ public extension Tekkon {
consonant.value + semivowel.value + vowel.value + intonation.value consonant.value + semivowel.value + vowel.value + intonation.value
} }
///
public var isPinyinMode: Bool { parser.rawValue >= 100 }
/// value / /// value /
/// 調 /// 調
/// - Parameters: /// - Parameters:
@ -66,8 +69,7 @@ public extension Tekkon {
/// - Parameters: /// - Parameters:
/// - isHanyuPinyin: /// - isHanyuPinyin:
public func getInlineCompositionForDisplay(isHanyuPinyin: Bool = false) -> String { public func getInlineCompositionForDisplay(isHanyuPinyin: Bool = false) -> String {
switch parser { guard isPinyinMode else { return getComposition(isHanyuPinyin: isHanyuPinyin) }
case .ofHanyuPinyin, .ofSecondaryPinyin, .ofYalePinyin, .ofHualuoPinyin, .ofUniversalPinyin, .ofWadeGilesPinyin:
var toneReturned = "" var toneReturned = ""
switch intonation.value { switch intonation.value {
case " ": toneReturned = "1" case " ": toneReturned = "1"
@ -78,17 +80,12 @@ public extension Tekkon {
default: break default: break
} }
return romajiBuffer.replacingOccurrences(of: "v", with: "ü") + toneReturned return romajiBuffer.replacingOccurrences(of: "v", with: "ü") + toneReturned
default: return getComposition(isHanyuPinyin: isHanyuPinyin)
}
} }
/// ///
public var isEmpty: Bool { public var isEmpty: Bool {
switch parser { guard !isPinyinMode else { return intonation.isEmpty && romajiBuffer.isEmpty }
case .ofHanyuPinyin, .ofSecondaryPinyin, .ofYalePinyin, .ofHualuoPinyin, .ofUniversalPinyin, .ofWadeGilesPinyin: return intonation.isEmpty && vowel.isEmpty && semivowel.isEmpty && consonant.isEmpty
return intonation.isEmpty && romajiBuffer.isEmpty
default: return intonation.isEmpty && vowel.isEmpty && semivowel.isEmpty && consonant.isEmpty
}
} }
/// ///
@ -197,8 +194,10 @@ public extension Tekkon {
/// - Parameters: /// - Parameters:
/// - fromString: String /// - fromString: String
public mutating func receiveKey(fromString input: String = "") { public mutating func receiveKey(fromString input: String = "") {
switch parser { guard isPinyinMode else {
case .ofHanyuPinyin, .ofSecondaryPinyin, .ofYalePinyin, .ofHualuoPinyin, .ofUniversalPinyin, .ofWadeGilesPinyin: receiveKey(fromPhonabet: translate(key: input))
return
}
if mapArayuruPinyinIntonation.keys.contains(input) { if mapArayuruPinyinIntonation.keys.contains(input) {
if let theTone = mapArayuruPinyinIntonation[input] { if let theTone = mapArayuruPinyinIntonation[input] {
intonation = Phonabet(theTone) intonation = Phonabet(theTone)
@ -213,8 +212,6 @@ public extension Tekkon {
receiveSequence(romajiBufferBackup, isRomaji: true) receiveSequence(romajiBufferBackup, isRomaji: true)
romajiBuffer = romajiBufferBackup romajiBuffer = romajiBufferBackup
} }
default: receiveKey(fromPhonabet: translate(key: input))
}
} }
/// UniChar /// UniChar
@ -325,9 +322,7 @@ public extension Tekkon {
/// ///
/// ///
public mutating func doBackSpace() { public mutating func doBackSpace() {
if [.ofHanyuPinyin, .ofSecondaryPinyin, .ofYalePinyin, .ofHualuoPinyin, .ofUniversalPinyin, .ofWadeGilesPinyin].contains(parser), if isPinyinMode, !romajiBuffer.isEmpty {
!romajiBuffer.isEmpty
{
if !intonation.isEmpty { if !intonation.isEmpty {
intonation.clear() intonation.clear()
} else { } else {
@ -361,7 +356,29 @@ public extension Tekkon {
parser = arrange parser = arrange
} }
// MARK: - Parser Processings ///
///
///
/// - 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
// //
@ -371,6 +388,7 @@ public extension Tekkon {
/// - Parameters: /// - Parameters:
/// - key: String /// - key: String
public mutating func translate(key: String = "") -> String { public mutating func translate(key: String = "") -> String {
guard !isPinyinMode else { return "" }
switch parser { switch parser {
case .ofDachen: case .ofDachen:
return Tekkon.mapQwertyDachen[key] ?? "" return Tekkon.mapQwertyDachen[key] ?? ""
@ -392,11 +410,10 @@ public extension Tekkon {
return Tekkon.mapFakeSeigyou[key] ?? "" return Tekkon.mapFakeSeigyou[key] ?? ""
case .ofStarlight: case .ofStarlight:
return handleStarlight(key: key) return handleStarlight(key: key)
case .ofHanyuPinyin, .ofSecondaryPinyin, .ofYalePinyin, .ofHualuoPinyin, .ofUniversalPinyin, .ofWadeGilesPinyin: default:
break //
}
return "" return ""
} }
}
/// ///
/// - Parameter incomingPhonabet: Phonabet /// - Parameter incomingPhonabet: Phonabet

View File

@ -94,7 +94,7 @@ extension InputHandler {
return handleEnter(input: input, readingOnly: true) return handleEnter(input: input, readingOnly: true)
} }
// //
guard let readingKey = composer.keyForQuery(pronouncable: true) else { break ifComposeReading } guard let readingKey = composer.phonabetKeyForQuery(pronouncable: true) else { break ifComposeReading }
// //
if !currentLM.hasUnigramsFor(keyArray: [readingKey]) { if !currentLM.hasUnigramsFor(keyArray: [readingKey]) {
delegate.callError("B49C0979語彙庫內無「\(readingKey)」的匹配記錄。") delegate.callError("B49C0979語彙庫內無「\(readingKey)」的匹配記錄。")
@ -167,7 +167,7 @@ extension InputHandler {
/// 調 /// 調
if keyConsumedByReading { if keyConsumedByReading {
// strict false // strict false
if composer.keyForQuery(pronouncable: false) == nil { if composer.phonabetKeyForQuery(pronouncable: false) == nil {
// 調 // 調
if !composer.isPinyinMode, input.isSpace, if !composer.isPinyinMode, input.isSpace,
compositor.insertKey(existedIntonation.value) compositor.insertKey(existedIntonation.value)
@ -379,33 +379,3 @@ extension InputHandler {
} }
} }
} }
// MARK: - Private Extensions for Tekkon Composer
//
extension Tekkon.Composer {
///
var isPinyinMode: Bool { parser.rawValue >= 100 }
///
/// 使
/// - Remark:
/// - Parameter pronouncable:
/// - Returns: nil
func keyForQuery(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
}
}