Skip to content

Commit

Permalink
Implement phonabetKeyForQuery().
Browse files Browse the repository at this point in the history
  • Loading branch information
ShikiSuen committed May 5, 2023
1 parent ef0102f commit b3eb6dd
Showing 1 changed file with 33 additions and 8 deletions.
41 changes: 33 additions & 8 deletions Sources/Tekkon/Tekkon_SyllableComposer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public extension Tekkon {
consonant.value + semivowel.value + vowel.value + intonation.value
}

/// 當前注拼槽是否處於拼音模式。
var isPinyinMode: Bool { parser.rawValue >= 100 }

/// 與 value 類似,這個函式就是用來決定輸入法組字區內顯示的注音/拼音內容,
/// 但可以指定是否輸出教科書格式(拼音的調號在字母上方、注音的輕聲寫在左側)。
/// - Parameters:
Expand All @@ -67,7 +70,7 @@ public extension Tekkon {
/// - isHanyuPinyin: 是否將輸出結果轉成漢語拼音。
public func getInlineCompositionForDisplay(isHanyuPinyin: Bool = false) -> String {
switch parser {
case .ofHanyuPinyin, .ofSecondaryPinyin, .ofYalePinyin, .ofHualuoPinyin, .ofUniversalPinyin, .ofWadeGilesPinyin:
case _ where isPinyinMode:
var toneReturned = ""
switch intonation.value {
case " ": toneReturned = "1"
Expand All @@ -85,7 +88,7 @@ public extension Tekkon {
/// 注拼槽內容是否為空。
public var isEmpty: Bool {
switch parser {
case .ofHanyuPinyin, .ofSecondaryPinyin, .ofYalePinyin, .ofHualuoPinyin, .ofUniversalPinyin, .ofWadeGilesPinyin:
case _ where isPinyinMode:
return intonation.isEmpty && romajiBuffer.isEmpty
default: return intonation.isEmpty && vowel.isEmpty && semivowel.isEmpty && consonant.isEmpty
}
Expand Down Expand Up @@ -198,7 +201,7 @@ public extension Tekkon {
/// - fromString: 傳入的 String 內容。
public mutating func receiveKey(fromString input: String = "") {
switch parser {
case .ofHanyuPinyin, .ofSecondaryPinyin, .ofYalePinyin, .ofHualuoPinyin, .ofUniversalPinyin, .ofWadeGilesPinyin:
case _ where isPinyinMode:
if mapArayuruPinyinIntonation.keys.contains(input) {
if let theTone = mapArayuruPinyinIntonation[input] {
intonation = Phonabet(theTone)
Expand Down Expand Up @@ -325,9 +328,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 {
Expand Down Expand Up @@ -361,6 +362,30 @@ public extension Tekkon {
parser = arrange
}

/// 拿取用來進行索引檢索用的注音。
///
/// 如果輸入法的辭典索引是漢語拼音的話,你可能用不上這個函式。
/// - Remark: 該字串不能為空,否則組字引擎會炸。
/// - Parameter strict: 當注拼槽處於注音模式時,同時檢查拼音組音區是否為空。
/// 鐵恨注拼引擎會在 receiveKey 的時候更新 romajiBuffer 的內容。
/// 如果只有聲調輸入的話,romajiBuffer 是空的。
/// - Returns: 可用的查詢用注音字串,或者 nil。
func phonabetKeyForQuery(strict: Bool) -> String? {
let readingKey = getComposition()
var validKeyGeneratable = false
switch isPinyinMode {
case false:
switch strict {
case false:
validKeyGeneratable = !readingKey.isEmpty
case true:
validKeyGeneratable = !readingKey.isEmpty && !romajiBuffer.isEmpty
}
case true: validKeyGeneratable = !romajiBuffer.isEmpty
}
return validKeyGeneratable ? readingKey : nil
}

// MARK: - Parser Processings

// 注拼槽對內處理用函式都在這一小節。
Expand All @@ -372,6 +397,7 @@ public extension Tekkon {
/// - key: 傳入的 String 訊號。
public mutating func translate(key: String = "") -> String {
switch parser {
case _ where isPinyinMode: break // 漢語拼音單獨用另外的函式處理
case .ofDachen:
return Tekkon.mapQwertyDachen[key] ?? ""
case .ofDachen26:
Expand All @@ -392,8 +418,7 @@ public extension Tekkon {
return Tekkon.mapFakeSeigyou[key] ?? ""
case .ofStarlight:
return handleStarlight(key: key)
case .ofHanyuPinyin, .ofSecondaryPinyin, .ofYalePinyin, .ofHualuoPinyin, .ofUniversalPinyin, .ofWadeGilesPinyin:
break // 漢語拼音單獨用另外的函式處理
default: break
}
return ""
}
Expand Down

0 comments on commit b3eb6dd

Please sign in to comment.