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
}
///
public var isPinyinMode: Bool { parser.rawValue >= 100 }
/// value /
/// 調
/// - Parameters:
@ -66,29 +69,23 @@ public extension Tekkon {
/// - Parameters:
/// - isHanyuPinyin:
public func getInlineCompositionForDisplay(isHanyuPinyin: Bool = false) -> String {
switch parser {
case .ofHanyuPinyin, .ofSecondaryPinyin, .ofYalePinyin, .ofHualuoPinyin, .ofUniversalPinyin, .ofWadeGilesPinyin:
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
default: return getComposition(isHanyuPinyin: isHanyuPinyin)
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 {
switch parser {
case .ofHanyuPinyin, .ofSecondaryPinyin, .ofYalePinyin, .ofHualuoPinyin, .ofUniversalPinyin, .ofWadeGilesPinyin:
return intonation.isEmpty && romajiBuffer.isEmpty
default: return intonation.isEmpty && vowel.isEmpty && semivowel.isEmpty && consonant.isEmpty
}
guard !isPinyinMode else { return intonation.isEmpty && romajiBuffer.isEmpty }
return intonation.isEmpty && vowel.isEmpty && semivowel.isEmpty && consonant.isEmpty
}
///
@ -197,23 +194,23 @@ public extension Tekkon {
/// - Parameters:
/// - fromString: String
public mutating func receiveKey(fromString input: String = "") {
switch parser {
case .ofHanyuPinyin, .ofSecondaryPinyin, .ofYalePinyin, .ofHualuoPinyin, .ofUniversalPinyin, .ofWadeGilesPinyin:
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
guard isPinyinMode else {
receiveKey(fromPhonabet: translate(key: input))
return
}
if mapArayuruPinyinIntonation.keys.contains(input) {
if let theTone = mapArayuruPinyinIntonation[input] {
intonation = Phonabet(theTone)
}
default: receiveKey(fromPhonabet: translate(key: input))
} 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
}
}
@ -325,9 +322,7 @@ public extension Tekkon {
///
///
public mutating func doBackSpace() {
if [.ofHanyuPinyin, .ofSecondaryPinyin, .ofYalePinyin, .ofHualuoPinyin, .ofUniversalPinyin, .ofWadeGilesPinyin].contains(parser),
!romajiBuffer.isEmpty
{
if isPinyinMode, !romajiBuffer.isEmpty {
if !intonation.isEmpty {
intonation.clear()
} else {
@ -361,7 +356,29 @@ public extension Tekkon {
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:
/// - key: String
public mutating func translate(key: String = "") -> String {
guard !isPinyinMode else { return "" }
switch parser {
case .ofDachen:
return Tekkon.mapQwertyDachen[key] ?? ""
@ -392,10 +410,9 @@ public extension Tekkon {
return Tekkon.mapFakeSeigyou[key] ?? ""
case .ofStarlight:
return handleStarlight(key: key)
case .ofHanyuPinyin, .ofSecondaryPinyin, .ofYalePinyin, .ofHualuoPinyin, .ofUniversalPinyin, .ofWadeGilesPinyin:
break //
default:
return ""
}
return ""
}
///

View File

@ -94,7 +94,7 @@ extension InputHandler {
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]) {
delegate.callError("B49C0979語彙庫內無「\(readingKey)」的匹配記錄。")
@ -167,7 +167,7 @@ extension InputHandler {
/// 調
if keyConsumedByReading {
// strict false
if composer.keyForQuery(pronouncable: false) == nil {
if composer.phonabetKeyForQuery(pronouncable: false) == nil {
// 調
if !composer.isPinyinMode, input.isSpace,
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
}
}