first
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
import Cocoa
|
||||
import InputMethodKit
|
||||
import AutoZhuyinCore
|
||||
|
||||
/// Resources shared by every client connection.
|
||||
enum Shared {
|
||||
static var server: IMKServer!
|
||||
static var candidates: IMKCandidates!
|
||||
|
||||
static let learnedKey = "LearnedPreferences"
|
||||
|
||||
static let classifier: Classifier = {
|
||||
let bundle = Bundle.main
|
||||
let lexicon = Lexicon()
|
||||
if let url = bundle.url(forResource: "lexicon", withExtension: "txt"),
|
||||
let text = try? String(contentsOf: url, encoding: .utf8) {
|
||||
lexicon.load(text)
|
||||
}
|
||||
// Optional user phrases: ~/Library/Application Support/AutoZhuyin/user-phrases.txt
|
||||
if let support = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first {
|
||||
let userFile = support.appendingPathComponent("AutoZhuyin/user-phrases.txt")
|
||||
if let text = try? String(contentsOf: userFile, encoding: .utf8) { lexicon.load(text) }
|
||||
}
|
||||
let english = EnglishModel(commonWordsURL: bundle.url(forResource: "english-common", withExtension: "txt"))
|
||||
let saved = UserDefaults.standard.dictionary(forKey: learnedKey) as? [String: Int] ?? [:]
|
||||
let learned = LearnedPreferences(weights: saved) { weights in
|
||||
UserDefaults.standard.set(weights, forKey: learnedKey)
|
||||
}
|
||||
return Classifier(lexicon: lexicon, english: english, learned: learned)
|
||||
}()
|
||||
}
|
||||
|
||||
@objc(AutoZhuyinInputController)
|
||||
final class InputController: IMKInputController {
|
||||
private let engine = CompositionEngine(classifier: Shared.classifier)
|
||||
|
||||
// MARK: - Key events
|
||||
|
||||
override func recognizedEvents(_ sender: Any!) -> Int {
|
||||
Int(NSEvent.EventTypeMask.keyDown.rawValue)
|
||||
}
|
||||
|
||||
override func handle(_ event: NSEvent!, client sender: Any!) -> Bool {
|
||||
guard let event, event.type == .keyDown, let client = sender as? IMKTextInput else { return false }
|
||||
|
||||
// Shortcuts (⌘C, ⌃A, ⌥…) go to the app; commit any composition first.
|
||||
if !event.modifierFlags.intersection([.command, .control, .option]).isEmpty {
|
||||
commit(engine.commitAll(), to: client)
|
||||
return false
|
||||
}
|
||||
|
||||
// While the candidate panel is up, let it handle navigation keys itself.
|
||||
if engine.isShowingCandidates, [125, 126, 123, 124, 116, 121, 36, 76].contains(event.keyCode) {
|
||||
Shared.candidates.interpretKeyEvents([event])
|
||||
return true
|
||||
}
|
||||
|
||||
guard let key = keyInput(for: event) else { return false }
|
||||
let result = engine.handle(key)
|
||||
if let text = result.commit { commit(text, to: client) }
|
||||
updateComposition(client)
|
||||
return result.handled
|
||||
}
|
||||
|
||||
private func keyInput(for event: NSEvent) -> KeyInput? {
|
||||
switch event.keyCode {
|
||||
case 51: return .backspace
|
||||
case 36, 76: return .enter
|
||||
case 53: return .escape
|
||||
case 48: return .tab
|
||||
case 125: return .down
|
||||
case 126: return .up
|
||||
case 123: return .left
|
||||
case 124: return .right
|
||||
default:
|
||||
guard let c = event.characters?.first, !c.isNewline else { return nil }
|
||||
// Caps Lock without Shift: treat as lowercase so Zhuyin still works.
|
||||
if event.modifierFlags.contains(.capsLock) && !event.modifierFlags.contains(.shift) {
|
||||
return .character(Character(c.lowercased()))
|
||||
}
|
||||
return .character(c)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Client updates
|
||||
|
||||
private func commit(_ text: String, to client: IMKTextInput) {
|
||||
guard !text.isEmpty else { return }
|
||||
client.insertText(text, replacementRange: NSRange(location: NSNotFound, length: 0))
|
||||
}
|
||||
|
||||
private func updateComposition(_ client: IMKTextInput) {
|
||||
let text = engine.markedText
|
||||
let attrs: [NSAttributedString.Key: Any] = [
|
||||
.underlineStyle: NSUnderlineStyle.single.rawValue,
|
||||
// Literal (English) composition is shown in a different colour as a hint.
|
||||
.foregroundColor: engine.mode == .literal ? NSColor.systemBlue : NSColor.textColor,
|
||||
]
|
||||
let marked = NSAttributedString(string: text, attributes: text.isEmpty ? [:] : attrs)
|
||||
client.setMarkedText(marked,
|
||||
selectionRange: NSRange(location: (text as NSString).length, length: 0),
|
||||
replacementRange: NSRange(location: NSNotFound, length: 0))
|
||||
|
||||
if engine.isShowingCandidates {
|
||||
Shared.candidates.update()
|
||||
Shared.candidates.show(kIMKLocateCandidatesBelowHint)
|
||||
} else {
|
||||
Shared.candidates.hide()
|
||||
}
|
||||
}
|
||||
|
||||
override func commitComposition(_ sender: Any!) {
|
||||
guard let client = sender as? IMKTextInput else { return }
|
||||
commit(engine.commitAll(), to: client)
|
||||
Shared.candidates.hide()
|
||||
}
|
||||
|
||||
override func deactivateServer(_ sender: Any!) {
|
||||
commitComposition(sender)
|
||||
super.deactivateServer(sender)
|
||||
}
|
||||
|
||||
// MARK: - Candidate panel
|
||||
|
||||
override func candidates(_ sender: Any!) -> [Any]! {
|
||||
engine.candidates.map(\.text)
|
||||
}
|
||||
|
||||
override func candidateSelectionChanged(_ candidateString: NSAttributedString!) {
|
||||
if let s = candidateString?.string, let i = engine.candidates.firstIndex(where: { $0.text == s }) {
|
||||
engine.candidateIndex = i
|
||||
}
|
||||
}
|
||||
|
||||
override func candidateSelected(_ candidateString: NSAttributedString!) {
|
||||
guard let s = candidateString?.string,
|
||||
let i = engine.candidates.firstIndex(where: { $0.text == s }) else { return }
|
||||
engine.selectCandidate(i)
|
||||
if let client = client() { updateComposition(client) }
|
||||
}
|
||||
|
||||
// MARK: - Menu
|
||||
|
||||
override func menu() -> NSMenu! {
|
||||
let menu = NSMenu()
|
||||
menu.addItem(withTitle: "清除學習紀錄", action: #selector(clearLearned(_:)), keyEquivalent: "")
|
||||
return menu
|
||||
}
|
||||
|
||||
@objc private func clearLearned(_ sender: Any?) {
|
||||
Shared.classifier.learned.clear()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import Carbon
|
||||
import Cocoa
|
||||
import InputMethodKit
|
||||
|
||||
// `AutoZhuyin --register`: register this bundle with Text Input Sources so it shows up
|
||||
// in System Settings without logging out (used by the installer's postinstall script).
|
||||
if CommandLine.arguments.contains("--register") {
|
||||
let status = TISRegisterInputSource(Bundle.main.bundleURL as CFURL)
|
||||
print(status == noErr ? "Registered \(Bundle.main.bundlePath)" : "TISRegisterInputSource failed: \(status)")
|
||||
exit(status == noErr ? 0 : 1)
|
||||
}
|
||||
|
||||
// Entry point of the input method server. macOS launches this process when the
|
||||
// input method is selected; IMKServer then creates one InputController per client.
|
||||
guard let connectionName = Bundle.main.infoDictionary?["InputMethodConnectionName"] as? String,
|
||||
let server = IMKServer(name: connectionName, bundleIdentifier: Bundle.main.bundleIdentifier)
|
||||
else {
|
||||
NSLog("AutoZhuyin: failed to start IMKServer (is this running from the .app bundle?)")
|
||||
exit(1)
|
||||
}
|
||||
|
||||
Shared.server = server
|
||||
Shared.candidates = IMKCandidates(server: server, panelType: kIMKSingleColumnScrollingCandidatePanel)
|
||||
_ = Shared.classifier // load the lexicon now rather than on the first keystroke
|
||||
|
||||
NSApplication.shared.run()
|
||||
@@ -0,0 +1,98 @@
|
||||
import Foundation
|
||||
|
||||
/// Slot a Bopomofo symbol occupies inside a syllable.
|
||||
public enum BopomofoSlot: Int, Comparable {
|
||||
case initial = 0, medial = 1, final = 2
|
||||
|
||||
public static func < (a: BopomofoSlot, b: BopomofoSlot) -> Bool { a.rawValue < b.rawValue }
|
||||
}
|
||||
|
||||
public enum Tone: Int {
|
||||
case first = 1, second, third, fourth, neutral
|
||||
|
||||
/// Mark appended to a reading. First tone is written without a mark.
|
||||
public var mark: String {
|
||||
switch self {
|
||||
case .first: return ""
|
||||
case .second: return "ˊ"
|
||||
case .third: return "ˇ"
|
||||
case .fourth: return "ˋ"
|
||||
case .neutral: return "˙"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Standard Taiwanese "大千" (Dachen) keyboard layout.
|
||||
public enum DachenLayout {
|
||||
public static let symbols: [Character: Character] = [
|
||||
"1": "ㄅ", "q": "ㄆ", "a": "ㄇ", "z": "ㄈ",
|
||||
"2": "ㄉ", "w": "ㄊ", "s": "ㄋ", "x": "ㄌ",
|
||||
"e": "ㄍ", "d": "ㄎ", "c": "ㄏ",
|
||||
"r": "ㄐ", "f": "ㄑ", "v": "ㄒ",
|
||||
"5": "ㄓ", "t": "ㄔ", "g": "ㄕ", "b": "ㄖ",
|
||||
"y": "ㄗ", "h": "ㄘ", "n": "ㄙ",
|
||||
"u": "ㄧ", "j": "ㄨ", "m": "ㄩ",
|
||||
"8": "ㄚ", "i": "ㄛ", "k": "ㄜ", ",": "ㄝ",
|
||||
"9": "ㄞ", "o": "ㄟ", "l": "ㄠ", ".": "ㄡ",
|
||||
"0": "ㄢ", "p": "ㄣ", ";": "ㄤ", "/": "ㄥ", "-": "ㄦ",
|
||||
]
|
||||
|
||||
public static let tones: [Character: Tone] = [
|
||||
" ": .first, "6": .second, "3": .third, "4": .fourth, "7": .neutral,
|
||||
]
|
||||
|
||||
/// Every key that has a meaning in Zhuyin mode.
|
||||
public static func isZhuyinKey(_ c: Character) -> Bool {
|
||||
symbols[c] != nil || tones[c] != nil
|
||||
}
|
||||
|
||||
static let initials: Set<Character> = Set("ㄅㄆㄇㄈㄉㄊㄋㄌㄍㄎㄏㄐㄑㄒㄓㄔㄕㄖㄗㄘㄙ")
|
||||
static let medials: Set<Character> = Set("ㄧㄨㄩ")
|
||||
|
||||
public static func slot(of symbol: Character) -> BopomofoSlot {
|
||||
if initials.contains(symbol) { return .initial }
|
||||
if medials.contains(symbol) { return .medial }
|
||||
return .final
|
||||
}
|
||||
}
|
||||
|
||||
/// One syllable as composed from keystrokes.
|
||||
public struct Syllable: Equatable {
|
||||
public var initial: Character?
|
||||
public var medial: Character?
|
||||
public var final: Character?
|
||||
public var tone: Tone?
|
||||
|
||||
public init(initial: Character? = nil, medial: Character? = nil, final: Character? = nil, tone: Tone? = nil) {
|
||||
self.initial = initial
|
||||
self.medial = medial
|
||||
self.final = final
|
||||
self.tone = tone
|
||||
}
|
||||
|
||||
public var isEmpty: Bool { initial == nil && medial == nil && final == nil }
|
||||
|
||||
/// Bopomofo without tone, e.g. "ㄋㄧ".
|
||||
public var toneless: String {
|
||||
String([initial, medial, final].compactMap { $0 })
|
||||
}
|
||||
|
||||
/// Reading with tone mark, e.g. "ㄋㄧˇ". Untoned syllables return just the symbols.
|
||||
public var reading: String { toneless + (tone?.mark ?? "") }
|
||||
|
||||
public func filled(_ slot: BopomofoSlot) -> Bool {
|
||||
switch slot {
|
||||
case .initial: return initial != nil
|
||||
case .medial: return medial != nil
|
||||
case .final: return final != nil
|
||||
}
|
||||
}
|
||||
|
||||
public mutating func set(_ symbol: Character, in slot: BopomofoSlot) {
|
||||
switch slot {
|
||||
case .initial: initial = symbol
|
||||
case .medial: medial = symbol
|
||||
case .final: final = symbol
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
import Foundation
|
||||
|
||||
public enum InputMode: String, Codable {
|
||||
/// Keys are Dachen Zhuyin and get converted to Chinese.
|
||||
case zhuyin
|
||||
/// Keys are output as typed (English words, numbers).
|
||||
case literal
|
||||
}
|
||||
|
||||
public struct Classification {
|
||||
public var mode: InputMode
|
||||
public var zhuyinScore: Double
|
||||
public var literalScore: Double
|
||||
}
|
||||
|
||||
/// Remembers the user's corrections (Tab toggles) per key sequence.
|
||||
public final class LearnedPreferences {
|
||||
/// Positive = prefers Zhuyin, negative = prefers literal.
|
||||
public private(set) var weights: [String: Int]
|
||||
private let onChange: ([String: Int]) -> Void
|
||||
|
||||
public init(weights: [String: Int] = [:], onChange: @escaping ([String: Int]) -> Void = { _ in }) {
|
||||
self.weights = weights
|
||||
self.onChange = onChange
|
||||
}
|
||||
|
||||
public func record(_ keys: String, as mode: InputMode) {
|
||||
let delta = mode == .zhuyin ? 1 : -1
|
||||
weights[keys] = max(-3, min(3, (weights[keys] ?? 0) + delta))
|
||||
onChange(weights)
|
||||
}
|
||||
|
||||
public func clear() {
|
||||
weights = [:]
|
||||
onChange(weights)
|
||||
}
|
||||
|
||||
public func bias(for keys: String) -> Double { Double(weights[keys] ?? 0) * 4 }
|
||||
}
|
||||
|
||||
/// Decides whether a key sequence is Zhuyin or literal (English / numbers).
|
||||
public final class Classifier {
|
||||
public let lexicon: Lexicon
|
||||
public let english: EnglishModel
|
||||
public let learned: LearnedPreferences
|
||||
|
||||
/// Traditional-Chinese users type Zhuyin most of the time: a small prior toward it.
|
||||
public var zhuyinPrior = 0.5
|
||||
/// How much the previous segment's mode pulls the current one.
|
||||
public var contextWeight = 1.5
|
||||
|
||||
public init(lexicon: Lexicon, english: EnglishModel, learned: LearnedPreferences = LearnedPreferences()) {
|
||||
self.lexicon = lexicon
|
||||
self.english = english
|
||||
self.learned = learned
|
||||
}
|
||||
|
||||
/// - Parameters:
|
||||
/// - finishing: the user pressed space. Zhuyin is scored as if the space were a
|
||||
/// first-tone key; literal is scored as a completed word.
|
||||
/// - previous: mode of the previously committed segment, if recent.
|
||||
public func classify(_ keys: [Character], finishing: Bool = false, previous: InputMode? = nil) -> Classification {
|
||||
var zh = zhuyinScore(finishing ? keys + [" "] : keys) + zhuyinPrior
|
||||
var lit = literalScore(keys, complete: finishing)
|
||||
switch previous {
|
||||
case .zhuyin?: zh += contextWeight
|
||||
case .literal?: lit += contextWeight
|
||||
case nil: break
|
||||
}
|
||||
let learnedBias = learned.bias(for: String(keys))
|
||||
if learnedBias > 0 { zh += learnedBias } else { lit -= learnedBias }
|
||||
return Classification(mode: zh >= lit ? .zhuyin : .literal, zhuyinScore: zh, literalScore: lit)
|
||||
}
|
||||
|
||||
public func zhuyinScore(_ keys: [Character]) -> Double {
|
||||
let parse = ZhuyinParser.parse(keys)
|
||||
if parse.foreignKeys > 0 || parse.syllables.isEmpty { return -.infinity }
|
||||
var s = -5.0 * Double(parse.strayTones)
|
||||
for (i, p) in parse.syllables.enumerated() {
|
||||
let isLast = i == parse.syllables.count - 1
|
||||
// Out-of-order typing is accepted, but is weaker evidence than normal Zhuyin.
|
||||
if p.reordered { s -= 0.5 }
|
||||
let valid = SyllableTable.isValid(p.syllable)
|
||||
if p.toned {
|
||||
if valid {
|
||||
s += 2 + (lexicon.bestScore(for: p.syllable) != nil ? 0.8 : -1.5)
|
||||
} else {
|
||||
s -= 5
|
||||
}
|
||||
} else if isLast {
|
||||
s += SyllableTable.isValidPrefix(p.syllable) ? 0 : -5
|
||||
} else {
|
||||
// Untoned syllable in the middle: allowed (tone omission) but unusual.
|
||||
s += valid ? -2.5 : -5
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
public func literalScore(_ keys: [Character], complete: Bool) -> Double {
|
||||
if keys.isEmpty || keys.contains(" ") { return -.infinity }
|
||||
var score = 0.0
|
||||
if keys.contains(where: { $0.isUppercase }) { score += 20 } // Shift = clearly English
|
||||
|
||||
let letters = String(keys.filter { $0.isLetter })
|
||||
let digits = keys.filter { $0.isNumber }.count
|
||||
let others = keys.count - letters.count - digits
|
||||
|
||||
if letters.isEmpty {
|
||||
// Pure number like "2024" (with possible "." or ",").
|
||||
return score + 0.8 * Double(digits) - 1.0 * Double(others)
|
||||
}
|
||||
score += english.score(letters, complete: complete || digits > 0)
|
||||
score -= 1.5 * Double(digits)
|
||||
// `'` inside a word (don't) is fine; Zhuyin punctuation keys inside a word are not.
|
||||
let apostrophes = keys.filter { $0 == "'" }.count
|
||||
score -= 1.5 * Double(others - apostrophes)
|
||||
return score
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,223 @@
|
||||
import Foundation
|
||||
|
||||
public enum KeyInput: Equatable {
|
||||
case character(Character)
|
||||
case backspace, enter, escape, tab
|
||||
case up, down, left, right
|
||||
}
|
||||
|
||||
public struct KeyResult: Equatable {
|
||||
/// false → the key should go to the app untouched.
|
||||
public var handled: Bool
|
||||
/// Text to insert into the app (finalised), if any.
|
||||
public var commit: String?
|
||||
|
||||
public static let passThrough = KeyResult(handled: false, commit: nil)
|
||||
public static let consumed = KeyResult(handled: true, commit: nil)
|
||||
}
|
||||
|
||||
/// The whole input-method state machine, independent of InputMethodKit.
|
||||
///
|
||||
/// Keystrokes accumulate in a buffer. On every key the classifier decides whether the
|
||||
/// buffer is Zhuyin or literal text; the composition (marked text) shows the result
|
||||
/// live. Space / Enter / punctuation commit it. Tab flips the decision and the
|
||||
/// correction is learned.
|
||||
public final class CompositionEngine {
|
||||
public let classifier: Classifier
|
||||
public let converter: Converter
|
||||
|
||||
public private(set) var keys: [Character] = []
|
||||
/// Set by Tab: overrides the classifier for the current buffer.
|
||||
public private(set) var forcedMode: InputMode?
|
||||
/// User-picked phrases, by syllable range.
|
||||
public private(set) var fixed: [Range<Int>: String] = [:]
|
||||
public private(set) var candidates: [Candidate] = []
|
||||
public var candidateIndex = 0
|
||||
public var isShowingCandidates: Bool { !candidates.isEmpty }
|
||||
|
||||
private var previousMode: InputMode?
|
||||
private var previousCommitTime = Date.distantPast
|
||||
/// How long the previous segment's mode keeps influencing the next one.
|
||||
public var contextWindow: TimeInterval = 20
|
||||
public var now: () -> Date = Date.init
|
||||
|
||||
public init(classifier: Classifier) {
|
||||
self.classifier = classifier
|
||||
self.converter = Converter(lexicon: classifier.lexicon)
|
||||
}
|
||||
|
||||
// MARK: - Derived state
|
||||
|
||||
private var context: InputMode? {
|
||||
now().timeIntervalSince(previousCommitTime) < contextWindow ? previousMode : nil
|
||||
}
|
||||
|
||||
public var mode: InputMode {
|
||||
forcedMode ?? classifier.classify(keys, previous: context).mode
|
||||
}
|
||||
|
||||
public var parse: ZhuyinParse { ZhuyinParser.parse(keys) }
|
||||
|
||||
/// What to show underlined in the app while composing.
|
||||
public var markedText: String {
|
||||
guard !keys.isEmpty else { return "" }
|
||||
switch mode {
|
||||
case .literal:
|
||||
return String(keys)
|
||||
case .zhuyin:
|
||||
let p = parse
|
||||
let done = converter.convert(p.completed.map(\.syllable), fixed: validFixed(p)).map(\.text).joined()
|
||||
return done + (p.pending?.syllable.reading ?? "")
|
||||
}
|
||||
}
|
||||
|
||||
private func validFixed(_ p: ZhuyinParse) -> [Range<Int>: String] {
|
||||
fixed.filter { $0.key.upperBound <= p.completed.count }
|
||||
}
|
||||
|
||||
private func zhuyinCommitText() -> String {
|
||||
let p = parse
|
||||
// An unfinished trailing syllable is converted as untoned.
|
||||
return converter.convert(p.syllables.map(\.syllable), fixed: validFixed(p)).map(\.text).joined()
|
||||
}
|
||||
|
||||
// MARK: - Key handling
|
||||
|
||||
public func handle(_ key: KeyInput) -> KeyResult {
|
||||
if isShowingCandidates { return handleCandidateKey(key) }
|
||||
|
||||
switch key {
|
||||
case .character(let c):
|
||||
return handleCharacter(c)
|
||||
case .backspace:
|
||||
guard !keys.isEmpty else { return .passThrough }
|
||||
keys.removeLast()
|
||||
fixed = validFixed(parse)
|
||||
if keys.isEmpty { forcedMode = nil; fixed = [:] }
|
||||
return .consumed
|
||||
case .enter:
|
||||
guard !keys.isEmpty else { return .passThrough }
|
||||
return KeyResult(handled: true, commit: commitCurrent())
|
||||
case .escape:
|
||||
guard !keys.isEmpty else { return .passThrough }
|
||||
reset()
|
||||
return .consumed
|
||||
case .tab:
|
||||
guard !keys.isEmpty else { return .passThrough }
|
||||
forcedMode = (mode == .zhuyin) ? .literal : .zhuyin
|
||||
return .consumed
|
||||
case .down:
|
||||
guard !keys.isEmpty else { return .passThrough }
|
||||
openCandidates()
|
||||
return .consumed
|
||||
case .up, .left, .right:
|
||||
return keys.isEmpty ? .passThrough : .consumed
|
||||
}
|
||||
}
|
||||
|
||||
private func handleCharacter(_ c: Character) -> KeyResult {
|
||||
if c == " " {
|
||||
guard !keys.isEmpty else { return .passThrough }
|
||||
let decided = forcedMode ?? classifier.classify(keys, finishing: true, previous: context).mode
|
||||
if decided == .literal {
|
||||
return KeyResult(handled: true, commit: commitCurrent(as: .literal) + " ")
|
||||
}
|
||||
if parse.pending != nil {
|
||||
keys.append(" ") // first tone
|
||||
return .consumed
|
||||
}
|
||||
return KeyResult(handled: true, commit: commitCurrent(as: .zhuyin))
|
||||
}
|
||||
|
||||
let composable = DachenLayout.isZhuyinKey(c) || (c.isASCII && c.isLetter) || (c == "'" && !keys.isEmpty)
|
||||
if composable {
|
||||
// ", . / ; -" are Zhuyin symbols (ㄝ ㄡ ㄥ ㄤ ㄦ) but act as punctuation after an
|
||||
// English word. Decide with the key included: "2;" is ㄉㄤ (檔…), "hello," is English.
|
||||
if !keys.isEmpty, DachenLayout.symbols[c] != nil, !c.isLetter, !c.isNumber, forcedMode != .zhuyin {
|
||||
let withKey = forcedMode ?? classifier.classify(keys + [c], previous: context).mode
|
||||
if withKey == .literal && !keys.allSatisfy(\.isNumber) {
|
||||
return KeyResult(handled: true, commit: commitCurrent(as: .literal) + String(c))
|
||||
}
|
||||
}
|
||||
keys.append(c)
|
||||
return .consumed
|
||||
}
|
||||
|
||||
// Any other character (Shift-punctuation, symbols): commit, then emit it.
|
||||
let chineseContext = keys.isEmpty ? (context == .zhuyin) : (mode == .zhuyin)
|
||||
let committed = keys.isEmpty ? "" : commitCurrent()
|
||||
let punct = chineseContext ? (CompositionEngine.fullwidth[c] ?? String(c)) : String(c)
|
||||
if committed.isEmpty && punct == String(c) { return .passThrough }
|
||||
return KeyResult(handled: true, commit: committed + punct)
|
||||
}
|
||||
|
||||
static let fullwidth: [Character: String] = [
|
||||
"<": ",", ">": "。", "?": "?", "!": "!", ":": ":", "\"": "「", "(": "(", ")": ")",
|
||||
"{": "『", "}": "』", "[": "「", "]": "」", "~": "~", "_": "——",
|
||||
]
|
||||
|
||||
// MARK: - Candidates
|
||||
|
||||
private func openCandidates() {
|
||||
let p = parse
|
||||
guard mode == .zhuyin, !p.completed.isEmpty else { return }
|
||||
candidates = converter.candidates(p.completed.map(\.syllable), endingAt: p.completed.count)
|
||||
candidateIndex = 0
|
||||
}
|
||||
|
||||
public func selectCandidate(_ index: Int) {
|
||||
guard candidates.indices.contains(index) else { return }
|
||||
let c = candidates[index]
|
||||
fixed = fixed.filter { !$0.key.overlaps(c.range) }
|
||||
fixed[c.range] = c.text
|
||||
candidates = []
|
||||
}
|
||||
|
||||
private func handleCandidateKey(_ key: KeyInput) -> KeyResult {
|
||||
switch key {
|
||||
case .character(let c) where c.isNumber && c != "0":
|
||||
selectCandidate(Int(String(c))! - 1 + pageStart)
|
||||
case .character(" "), .enter:
|
||||
selectCandidate(candidateIndex)
|
||||
case .down, .right:
|
||||
candidateIndex = min(candidates.count - 1, candidateIndex + 1)
|
||||
case .up, .left:
|
||||
candidateIndex = max(0, candidateIndex - 1)
|
||||
case .escape, .backspace:
|
||||
candidates = []
|
||||
default:
|
||||
break
|
||||
}
|
||||
return .consumed
|
||||
}
|
||||
|
||||
public var pageSize = 9
|
||||
private var pageStart: Int { (candidateIndex / pageSize) * pageSize }
|
||||
|
||||
// MARK: - Commit
|
||||
|
||||
@discardableResult
|
||||
private func commitCurrent(as decided: InputMode? = nil) -> String {
|
||||
let m = decided ?? mode
|
||||
let text = m == .literal ? String(keys) : zhuyinCommitText()
|
||||
if let forced = forcedMode {
|
||||
classifier.learned.record(String(keys), as: forced)
|
||||
}
|
||||
previousMode = m
|
||||
previousCommitTime = now()
|
||||
reset()
|
||||
return text
|
||||
}
|
||||
|
||||
/// Commits whatever is composed (e.g. the app lost focus). Returns "" when empty.
|
||||
public func commitAll() -> String {
|
||||
keys.isEmpty ? "" : commitCurrent()
|
||||
}
|
||||
|
||||
public func reset() {
|
||||
keys = []
|
||||
forcedMode = nil
|
||||
fixed = [:]
|
||||
candidates = []
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
import Foundation
|
||||
|
||||
public struct ConvertedSegment: Equatable {
|
||||
public var range: Range<Int> // syllable indices
|
||||
public var text: String
|
||||
}
|
||||
|
||||
public struct Candidate: Equatable {
|
||||
public var range: Range<Int>
|
||||
public var text: String
|
||||
}
|
||||
|
||||
/// Turns a syllable sequence into the most likely Chinese text (Viterbi over the lexicon).
|
||||
public struct Converter {
|
||||
public let lexicon: Lexicon
|
||||
/// Score used when a syllable has no lexicon entry; the Bopomofo itself is emitted.
|
||||
static let unknownScore = -30.0
|
||||
/// Penalty for matching an untoned syllable against all tones.
|
||||
static let tonelessPenalty = -1.5
|
||||
|
||||
public init(lexicon: Lexicon) { self.lexicon = lexicon }
|
||||
|
||||
/// - Parameter fixed: user-chosen phrases by syllable range; they are always kept.
|
||||
public func convert(_ syllables: [Syllable], fixed: [Range<Int>: String] = [:]) -> [ConvertedSegment] {
|
||||
let n = syllables.count
|
||||
guard n > 0 else { return [] }
|
||||
let maxLen = max(1, lexicon.maxPhraseLength)
|
||||
|
||||
// best[j] = (score, backpointer segment) for the prefix ending at j.
|
||||
var best: [(score: Double, seg: ConvertedSegment?)] = Array(repeating: (-.infinity, nil), count: n + 1)
|
||||
best[0] = (0, nil)
|
||||
|
||||
for j in 1...n {
|
||||
for len in 1...min(maxLen, j) {
|
||||
let i = j - len
|
||||
guard best[i].score > -.infinity else { continue }
|
||||
let range = i..<j
|
||||
for (text, score) in options(for: range, syllables: syllables, fixed: fixed) {
|
||||
let total = best[i].score + score
|
||||
if total > best[j].score { best[j] = (total, ConvertedSegment(range: range, text: text)) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var segments: [ConvertedSegment] = []
|
||||
var j = n
|
||||
while j > 0, let seg = best[j].seg {
|
||||
segments.append(seg)
|
||||
j = seg.range.lowerBound
|
||||
}
|
||||
return segments.reversed()
|
||||
}
|
||||
|
||||
private func options(for range: Range<Int>, syllables: [Syllable], fixed: [Range<Int>: String]) -> [(String, Double)] {
|
||||
if let text = fixed[range] { return [(text, 0)] }
|
||||
// A span may not partially overlap or swallow a fixed span.
|
||||
if fixed.keys.contains(where: { $0.overlaps(range) }) { return [] }
|
||||
|
||||
let slice = Array(syllables[range])
|
||||
var result = lexicon.entries(for: slice).map { entry -> (String, Double) in
|
||||
let penalty = slice.contains { $0.tone == nil } ? Converter.tonelessPenalty : 0
|
||||
return (entry.phrase, entry.score + penalty)
|
||||
}
|
||||
if result.isEmpty && range.count == 1 {
|
||||
result = [(slice[0].reading, Converter.unknownScore)]
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/// Candidates for phrases ending at syllable index `end - 1`, longest phrases first.
|
||||
public func candidates(_ syllables: [Syllable], endingAt end: Int) -> [Candidate] {
|
||||
guard end > 0, end <= syllables.count else { return [] }
|
||||
var out: [Candidate] = []
|
||||
var seen = Set<String>()
|
||||
for len in stride(from: min(lexicon.maxPhraseLength, end), through: 1, by: -1) {
|
||||
let range = (end - len)..<end
|
||||
for entry in lexicon.entries(for: Array(syllables[range])) where !seen.contains(entry.phrase) {
|
||||
seen.insert(entry.phrase)
|
||||
out.append(Candidate(range: range, text: entry.phrase))
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
import Foundation
|
||||
|
||||
/// Scores how much a typed string looks like English.
|
||||
///
|
||||
/// Three sources of evidence:
|
||||
/// - a list of common words (strong evidence, includes modern words like "ok", "email"),
|
||||
/// - a large dictionary (e.g. /usr/share/dict/words, weaker evidence),
|
||||
/// - a letter-bigram model trained on those words, for words not in either list.
|
||||
public final class EnglishModel {
|
||||
private var common = Set<String>()
|
||||
private var dictionary = Set<String>()
|
||||
private var sortedWords: [String] = []
|
||||
private var sortedCommon: [String] = []
|
||||
private var bigram: [String: Double] = [:] // "ab" -> log2 P(b|a); "^a", "a$" for boundaries
|
||||
private var unseenBigram = -12.0
|
||||
|
||||
public init(commonWords: [String], dictionaryWords: [String] = []) {
|
||||
common = Set(commonWords.map { $0.lowercased() }.filter { !$0.isEmpty })
|
||||
dictionary = Set(dictionaryWords.map { $0.lowercased() }.filter { $0.count > 2 })
|
||||
sortedCommon = common.sorted()
|
||||
sortedWords = common.union(dictionary).sorted()
|
||||
trainBigram()
|
||||
}
|
||||
|
||||
/// Loads common words from a bundle resource file and the system dictionary if present.
|
||||
public convenience init(commonWordsURL: URL?, systemDictionary: URL? = URL(fileURLWithPath: "/usr/share/dict/words")) {
|
||||
func lines(_ url: URL?) -> [String] {
|
||||
guard let url, let text = try? String(contentsOf: url, encoding: .utf8) else { return [] }
|
||||
return text.split(whereSeparator: \.isNewline)
|
||||
.map { $0.trimmingCharacters(in: .whitespaces) }
|
||||
.filter { !$0.hasPrefix("#") }
|
||||
}
|
||||
self.init(commonWords: lines(commonWordsURL), dictionaryWords: lines(systemDictionary))
|
||||
}
|
||||
|
||||
public func isCommon(_ w: String) -> Bool { common.contains(w.lowercased()) }
|
||||
public func isWord(_ w: String) -> Bool { let l = w.lowercased(); return common.contains(l) || dictionary.contains(l) }
|
||||
|
||||
/// - Parameter complete: true when the user ended the word (space / punctuation),
|
||||
/// false while still typing (prefixes of words get partial credit).
|
||||
public func score(_ word: String, complete: Bool) -> Double {
|
||||
let w = word.lowercased()
|
||||
let n = Double(w.count)
|
||||
guard !w.isEmpty else { return 0 }
|
||||
if common.contains(w) { return 3 + n }
|
||||
if dictionary.contains(w) { return 1 + 0.6 * n }
|
||||
if !complete {
|
||||
if hasPrefix(w, in: sortedCommon) { return 0.4 * n }
|
||||
if hasPrefix(w, in: sortedWords) { return 0.2 * n }
|
||||
}
|
||||
// Unknown word: judge by how English-like its letter sequence is.
|
||||
let plaus = plausibility(w)
|
||||
return min(0.5 * n, max(-2 * n, n * (plaus + 4.2)))
|
||||
}
|
||||
|
||||
/// Mean log2 probability per letter transition (≈ -3.5 for English, lower for gibberish).
|
||||
public func plausibility(_ w: String) -> Double {
|
||||
let chars = ["^"] + w.map(String.init) + ["$"]
|
||||
var total = 0.0
|
||||
for i in 1..<chars.count { total += bigram[chars[i - 1] + chars[i]] ?? unseenBigram }
|
||||
return total / Double(chars.count - 1)
|
||||
}
|
||||
|
||||
private func hasPrefix(_ p: String, in words: [String]) -> Bool {
|
||||
var lo = 0, hi = words.count
|
||||
while lo < hi {
|
||||
let mid = (lo + hi) / 2
|
||||
if words[mid] < p { lo = mid + 1 } else { hi = mid }
|
||||
}
|
||||
// words[lo] is the first word >= p; skip an exact match, then test the next one.
|
||||
if lo < words.count && words[lo] == p { lo += 1 }
|
||||
return lo < words.count && words[lo].hasPrefix(p)
|
||||
}
|
||||
|
||||
private func trainBigram() {
|
||||
var pair: [String: Double] = [:]
|
||||
var first: [String: Double] = [:]
|
||||
for w in sortedWords where w.allSatisfy({ $0.isLetter && $0.isASCII }) {
|
||||
let weight = common.contains(w) ? 5.0 : 1.0
|
||||
let chars = ["^"] + w.map(String.init) + ["$"]
|
||||
for i in 1..<chars.count {
|
||||
pair[chars[i - 1] + chars[i], default: 0] += weight
|
||||
first[chars[i - 1], default: 0] += weight
|
||||
}
|
||||
}
|
||||
// Add-one smoothing over 27 possible successors.
|
||||
for (k, c) in pair {
|
||||
let a = String(k.prefix(1))
|
||||
bigram[k] = log2((c + 1) / ((first[a] ?? 0) + 27))
|
||||
}
|
||||
unseenBigram = log2(1.0 / ((first.values.max() ?? 1000) + 27))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
import Foundation
|
||||
|
||||
public struct LexiconEntry: Equatable {
|
||||
public var phrase: String
|
||||
/// log10 probability (higher is more likely), McBopomofo-style.
|
||||
public var score: Double
|
||||
}
|
||||
|
||||
/// Maps Bopomofo readings (syllables joined by "-", e.g. "ㄋㄧˇ-ㄏㄠˇ") to phrases.
|
||||
///
|
||||
/// File format, one entry per line (`#` starts a comment):
|
||||
/// ㄋㄧˇ-ㄏㄠˇ 你好 -4.2
|
||||
/// The phrase/reading columns may be in either order; the score is optional.
|
||||
public final class Lexicon {
|
||||
private var exact: [String: [LexiconEntry]] = [:]
|
||||
private var toneless: [String: [LexiconEntry]] = [:]
|
||||
public private(set) var maxPhraseLength = 1
|
||||
|
||||
public init() {}
|
||||
|
||||
public convenience init(contentsOf url: URL) throws {
|
||||
self.init()
|
||||
load(try String(contentsOf: url, encoding: .utf8))
|
||||
}
|
||||
|
||||
public func load(_ text: String) {
|
||||
for line in text.split(whereSeparator: \.isNewline) {
|
||||
let trimmed = line.trimmingCharacters(in: .whitespaces)
|
||||
if trimmed.isEmpty || trimmed.hasPrefix("#") { continue }
|
||||
let cols = trimmed.split(whereSeparator: { $0 == " " || $0 == "\t" }).map(String.init)
|
||||
guard cols.count >= 2 else { continue }
|
||||
let (reading, phrase) = Lexicon.isBopomofo(cols[0]) ? (cols[0], cols[1]) : (cols[1], cols[0])
|
||||
guard Lexicon.isBopomofo(reading) else { continue }
|
||||
let score = cols.count >= 3 ? Double(cols[2]) ?? -6 : -6
|
||||
add(reading: reading, phrase: phrase, score: score)
|
||||
}
|
||||
for key in exact.keys { exact[key]?.sort { $0.score > $1.score } }
|
||||
for key in toneless.keys { toneless[key]?.sort { $0.score > $1.score } }
|
||||
}
|
||||
|
||||
public func add(reading rawReading: String, phrase: String, score: Double) {
|
||||
let entry = LexiconEntry(phrase: phrase, score: score)
|
||||
// Some data sets write the neutral tone as a prefix ("˙ㄉㄜ"); we use a suffix.
|
||||
let syllables = rawReading.split(separator: "-").map { s -> String in
|
||||
s.hasPrefix("˙") ? String(s.dropFirst()) + "˙" : String(s)
|
||||
}
|
||||
let reading = syllables.joined(separator: "-")
|
||||
maxPhraseLength = max(maxPhraseLength, syllables.count)
|
||||
if !(exact[reading]?.contains { $0.phrase == phrase } ?? false) {
|
||||
exact[reading, default: []].append(entry)
|
||||
}
|
||||
let key = Lexicon.stripTones(reading)
|
||||
if let i = toneless[key]?.firstIndex(where: { $0.phrase == phrase }) {
|
||||
toneless[key]![i].score = max(toneless[key]![i].score, score)
|
||||
} else {
|
||||
toneless[key, default: []].append(entry)
|
||||
}
|
||||
}
|
||||
|
||||
/// Entries for a sequence of readings. Untoned syllables match any tone.
|
||||
public func entries(for syllables: [Syllable]) -> [LexiconEntry] {
|
||||
if syllables.allSatisfy({ $0.tone != nil }) {
|
||||
return exact[syllables.map(\.reading).joined(separator: "-")] ?? []
|
||||
}
|
||||
return toneless[syllables.map(\.toneless).joined(separator: "-")] ?? []
|
||||
}
|
||||
|
||||
public func bestScore(for syllable: Syllable) -> Double? {
|
||||
entries(for: [syllable]).first?.score
|
||||
}
|
||||
|
||||
public var isEmpty: Bool { exact.isEmpty }
|
||||
|
||||
static let toneMarks: Set<Character> = ["ˊ", "ˇ", "ˋ", "˙"]
|
||||
|
||||
static func stripTones(_ reading: String) -> String {
|
||||
String(reading.filter { !toneMarks.contains($0) })
|
||||
}
|
||||
|
||||
static func isBopomofo(_ s: String) -> Bool {
|
||||
guard let first = s.unicodeScalars.first else { return false }
|
||||
return (0x3105...0x312F).contains(first.value) || first == "˙"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
import Foundation
|
||||
|
||||
/// The inventory of valid (toneless) Mandarin syllables in Bopomofo.
|
||||
public enum SyllableTable {
|
||||
private static let raw = """
|
||||
ㄚ ㄛ ㄜ ㄝ ㄞ ㄟ ㄠ ㄡ ㄢ ㄣ ㄤ ㄥ ㄦ
|
||||
ㄧ ㄧㄚ ㄧㄛ ㄧㄝ ㄧㄞ ㄧㄠ ㄧㄡ ㄧㄢ ㄧㄣ ㄧㄤ ㄧㄥ
|
||||
ㄨ ㄨㄚ ㄨㄛ ㄨㄞ ㄨㄟ ㄨㄢ ㄨㄣ ㄨㄤ ㄨㄥ
|
||||
ㄩ ㄩㄝ ㄩㄢ ㄩㄣ ㄩㄥ
|
||||
ㄅㄚ ㄅㄛ ㄅㄞ ㄅㄟ ㄅㄠ ㄅㄢ ㄅㄣ ㄅㄤ ㄅㄥ ㄅㄧ ㄅㄧㄝ ㄅㄧㄠ ㄅㄧㄢ ㄅㄧㄣ ㄅㄧㄥ ㄅㄨ
|
||||
ㄆㄚ ㄆㄛ ㄆㄞ ㄆㄟ ㄆㄠ ㄆㄡ ㄆㄢ ㄆㄣ ㄆㄤ ㄆㄥ ㄆㄧ ㄆㄧㄝ ㄆㄧㄠ ㄆㄧㄢ ㄆㄧㄣ ㄆㄧㄥ ㄆㄨ
|
||||
ㄇㄚ ㄇㄛ ㄇㄜ ㄇㄞ ㄇㄟ ㄇㄠ ㄇㄡ ㄇㄢ ㄇㄣ ㄇㄤ ㄇㄥ ㄇㄧ ㄇㄧㄝ ㄇㄧㄠ ㄇㄧㄡ ㄇㄧㄢ ㄇㄧㄣ ㄇㄧㄥ ㄇㄨ
|
||||
ㄈㄚ ㄈㄛ ㄈㄟ ㄈㄡ ㄈㄢ ㄈㄣ ㄈㄤ ㄈㄥ ㄈㄨ
|
||||
ㄉㄚ ㄉㄜ ㄉㄞ ㄉㄟ ㄉㄠ ㄉㄡ ㄉㄢ ㄉㄣ ㄉㄤ ㄉㄥ ㄉㄧ ㄉㄧㄚ ㄉㄧㄝ ㄉㄧㄠ ㄉㄧㄡ ㄉㄧㄢ ㄉㄧㄥ ㄉㄨ ㄉㄨㄛ ㄉㄨㄟ ㄉㄨㄢ ㄉㄨㄣ ㄉㄨㄥ
|
||||
ㄊㄚ ㄊㄜ ㄊㄞ ㄊㄠ ㄊㄡ ㄊㄢ ㄊㄤ ㄊㄥ ㄊㄧ ㄊㄧㄝ ㄊㄧㄠ ㄊㄧㄢ ㄊㄧㄥ ㄊㄨ ㄊㄨㄛ ㄊㄨㄟ ㄊㄨㄢ ㄊㄨㄣ ㄊㄨㄥ
|
||||
ㄋㄚ ㄋㄜ ㄋㄞ ㄋㄟ ㄋㄠ ㄋㄡ ㄋㄢ ㄋㄣ ㄋㄤ ㄋㄥ ㄋㄧ ㄋㄧㄝ ㄋㄧㄠ ㄋㄧㄡ ㄋㄧㄢ ㄋㄧㄣ ㄋㄧㄤ ㄋㄧㄥ ㄋㄨ ㄋㄨㄛ ㄋㄨㄢ ㄋㄨㄥ ㄋㄩ ㄋㄩㄝ
|
||||
ㄌㄚ ㄌㄛ ㄌㄜ ㄌㄞ ㄌㄟ ㄌㄠ ㄌㄡ ㄌㄢ ㄌㄤ ㄌㄥ ㄌㄧ ㄌㄧㄚ ㄌㄧㄝ ㄌㄧㄠ ㄌㄧㄡ ㄌㄧㄢ ㄌㄧㄣ ㄌㄧㄤ ㄌㄧㄥ ㄌㄨ ㄌㄨㄛ ㄌㄨㄢ ㄌㄨㄣ ㄌㄨㄥ ㄌㄩ ㄌㄩㄝ ㄌㄩㄢ
|
||||
ㄍㄚ ㄍㄜ ㄍㄞ ㄍㄟ ㄍㄠ ㄍㄡ ㄍㄢ ㄍㄣ ㄍㄤ ㄍㄥ ㄍㄨ ㄍㄨㄚ ㄍㄨㄛ ㄍㄨㄞ ㄍㄨㄟ ㄍㄨㄢ ㄍㄨㄣ ㄍㄨㄤ ㄍㄨㄥ
|
||||
ㄎㄚ ㄎㄜ ㄎㄞ ㄎㄟ ㄎㄠ ㄎㄡ ㄎㄢ ㄎㄣ ㄎㄤ ㄎㄥ ㄎㄨ ㄎㄨㄚ ㄎㄨㄛ ㄎㄨㄞ ㄎㄨㄟ ㄎㄨㄢ ㄎㄨㄣ ㄎㄨㄤ ㄎㄨㄥ
|
||||
ㄏㄚ ㄏㄜ ㄏㄞ ㄏㄟ ㄏㄠ ㄏㄡ ㄏㄢ ㄏㄣ ㄏㄤ ㄏㄥ ㄏㄨ ㄏㄨㄚ ㄏㄨㄛ ㄏㄨㄞ ㄏㄨㄟ ㄏㄨㄢ ㄏㄨㄣ ㄏㄨㄤ ㄏㄨㄥ
|
||||
ㄐㄧ ㄐㄧㄚ ㄐㄧㄝ ㄐㄧㄠ ㄐㄧㄡ ㄐㄧㄢ ㄐㄧㄣ ㄐㄧㄤ ㄐㄧㄥ ㄐㄩ ㄐㄩㄝ ㄐㄩㄢ ㄐㄩㄣ ㄐㄩㄥ
|
||||
ㄑㄧ ㄑㄧㄚ ㄑㄧㄝ ㄑㄧㄠ ㄑㄧㄡ ㄑㄧㄢ ㄑㄧㄣ ㄑㄧㄤ ㄑㄧㄥ ㄑㄩ ㄑㄩㄝ ㄑㄩㄢ ㄑㄩㄣ ㄑㄩㄥ
|
||||
ㄒㄧ ㄒㄧㄚ ㄒㄧㄝ ㄒㄧㄠ ㄒㄧㄡ ㄒㄧㄢ ㄒㄧㄣ ㄒㄧㄤ ㄒㄧㄥ ㄒㄩ ㄒㄩㄝ ㄒㄩㄢ ㄒㄩㄣ ㄒㄩㄥ
|
||||
ㄓ ㄓㄚ ㄓㄜ ㄓㄞ ㄓㄟ ㄓㄠ ㄓㄡ ㄓㄢ ㄓㄣ ㄓㄤ ㄓㄥ ㄓㄨ ㄓㄨㄚ ㄓㄨㄛ ㄓㄨㄞ ㄓㄨㄟ ㄓㄨㄢ ㄓㄨㄣ ㄓㄨㄤ ㄓㄨㄥ
|
||||
ㄔ ㄔㄚ ㄔㄜ ㄔㄞ ㄔㄠ ㄔㄡ ㄔㄢ ㄔㄣ ㄔㄤ ㄔㄥ ㄔㄨ ㄔㄨㄚ ㄔㄨㄛ ㄔㄨㄞ ㄔㄨㄟ ㄔㄨㄢ ㄔㄨㄣ ㄔㄨㄤ ㄔㄨㄥ
|
||||
ㄕ ㄕㄚ ㄕㄜ ㄕㄞ ㄕㄟ ㄕㄠ ㄕㄡ ㄕㄢ ㄕㄣ ㄕㄤ ㄕㄥ ㄕㄨ ㄕㄨㄚ ㄕㄨㄛ ㄕㄨㄞ ㄕㄨㄟ ㄕㄨㄢ ㄕㄨㄣ ㄕㄨㄤ
|
||||
ㄖ ㄖㄜ ㄖㄠ ㄖㄡ ㄖㄢ ㄖㄣ ㄖㄤ ㄖㄥ ㄖㄨ ㄖㄨㄛ ㄖㄨㄟ ㄖㄨㄢ ㄖㄨㄣ ㄖㄨㄥ
|
||||
ㄗ ㄗㄚ ㄗㄜ ㄗㄞ ㄗㄟ ㄗㄠ ㄗㄡ ㄗㄢ ㄗㄣ ㄗㄤ ㄗㄥ ㄗㄨ ㄗㄨㄛ ㄗㄨㄟ ㄗㄨㄢ ㄗㄨㄣ ㄗㄨㄥ
|
||||
ㄘ ㄘㄚ ㄘㄜ ㄘㄞ ㄘㄠ ㄘㄡ ㄘㄢ ㄘㄣ ㄘㄤ ㄘㄥ ㄘㄨ ㄘㄨㄛ ㄘㄨㄟ ㄘㄨㄢ ㄘㄨㄣ ㄘㄨㄥ
|
||||
ㄙ ㄙㄚ ㄙㄜ ㄙㄞ ㄙㄠ ㄙㄡ ㄙㄢ ㄙㄣ ㄙㄤ ㄙㄥ ㄙㄨ ㄙㄨㄛ ㄙㄨㄟ ㄙㄨㄢ ㄙㄨㄣ ㄙㄨㄥ
|
||||
"""
|
||||
|
||||
public static let valid: Set<String> = Set(raw.split(whereSeparator: { $0 == " " || $0 == "\n" }).map(String.init))
|
||||
|
||||
/// Every prefix (in initial→medial→final order) of a valid syllable, e.g. "ㄋ", "ㄋㄧ".
|
||||
public static let validPrefixes: Set<String> = {
|
||||
var s = Set<String>()
|
||||
for syl in valid {
|
||||
let chars = Array(syl)
|
||||
for n in 1...chars.count { s.insert(String(chars[0..<n])) }
|
||||
}
|
||||
return s
|
||||
}()
|
||||
|
||||
/// Every valid syllable with any of its slots blanked out, as "initial|medial|final"
|
||||
/// with "_" for an empty slot (e.g. "ㄒ_ㄠ" from ㄒㄧㄠ). Used to accept symbols typed
|
||||
/// out of order: a partial syllable is fine if some valid syllable completes it.
|
||||
private static let compatiblePatterns: Set<String> = {
|
||||
var s = Set<String>()
|
||||
for syl in valid {
|
||||
var slots: [Character?] = [nil, nil, nil]
|
||||
for c in syl { slots[DachenLayout.slot(of: c).rawValue] = c }
|
||||
for mask in 0..<8 {
|
||||
let masked = (0..<3).map { i -> Character in
|
||||
mask & (1 << i) != 0 ? (slots[i] ?? "_") : "_"
|
||||
}
|
||||
s.insert(String(masked))
|
||||
}
|
||||
}
|
||||
return s
|
||||
}()
|
||||
|
||||
public static func isCompatible(_ syllable: Syllable) -> Bool {
|
||||
compatiblePatterns.contains(String([syllable.initial ?? "_", syllable.medial ?? "_", syllable.final ?? "_"]))
|
||||
}
|
||||
|
||||
public static func isValid(_ syllable: Syllable) -> Bool { valid.contains(syllable.toneless) }
|
||||
public static func isValidPrefix(_ syllable: Syllable) -> Bool {
|
||||
syllable.isEmpty || validPrefixes.contains(syllable.toneless)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
import Foundation
|
||||
|
||||
/// A syllable parsed out of a key sequence, with the key range that produced it.
|
||||
public struct ParsedSyllable: Equatable {
|
||||
public var syllable: Syllable
|
||||
public var keyRange: Range<Int>
|
||||
/// True when its symbols were typed out of initial→medial→final order.
|
||||
public var reordered = false
|
||||
/// True when the syllable was closed by a tone key.
|
||||
public var toned: Bool { syllable.tone != nil }
|
||||
}
|
||||
|
||||
public struct ZhuyinParse: Equatable {
|
||||
public var syllables: [ParsedSyllable]
|
||||
/// Number of tone keys that had no syllable to attach to (e.g. "33").
|
||||
public var strayTones: Int
|
||||
/// Keys that are not part of the Dachen layout at all (uppercase, `'`, …).
|
||||
public var foreignKeys: Int
|
||||
|
||||
/// The trailing syllable that is still being composed (not closed by a tone), if any.
|
||||
public var pending: ParsedSyllable? {
|
||||
guard let last = syllables.last, !last.toned else { return nil }
|
||||
return last
|
||||
}
|
||||
|
||||
/// Syllables that are finished: either toned, or untoned but followed by another syllable.
|
||||
public var completed: [ParsedSyllable] {
|
||||
pending == nil ? syllables : Array(syllables.dropLast())
|
||||
}
|
||||
}
|
||||
|
||||
/// Splits raw keystrokes into syllables following Dachen composition rules:
|
||||
/// symbols fill initial → medial → final; a symbol whose slot (or a later one)
|
||||
/// is already filled starts a new syllable; a tone key closes the syllable.
|
||||
public enum ZhuyinParser {
|
||||
public static func parse(_ keys: [Character]) -> ZhuyinParse {
|
||||
var result = ZhuyinParse(syllables: [], strayTones: 0, foreignKeys: 0)
|
||||
var current = Syllable()
|
||||
var start = 0
|
||||
var reordered = false
|
||||
|
||||
func close(at end: Int) {
|
||||
if !current.isEmpty {
|
||||
result.syllables.append(ParsedSyllable(syllable: current, keyRange: start..<end, reordered: reordered))
|
||||
}
|
||||
current = Syllable()
|
||||
start = end
|
||||
reordered = false
|
||||
}
|
||||
|
||||
for (i, key) in keys.enumerated() {
|
||||
if let tone = DachenLayout.tones[key] {
|
||||
if current.isEmpty {
|
||||
result.strayTones += 1
|
||||
start = i + 1
|
||||
} else {
|
||||
current.tone = tone
|
||||
close(at: i + 1)
|
||||
}
|
||||
} else if let symbol = DachenLayout.symbols[key] {
|
||||
let slot = DachenLayout.slot(of: symbol)
|
||||
let laterFilled = [BopomofoSlot.initial, .medial, .final].contains { $0 > slot && current.filled($0) }
|
||||
if current.filled(slot) {
|
||||
close(at: i)
|
||||
} else if laterFilled {
|
||||
// Typed out of order (e.g. ㄧ then ㄒ for ㄒㄧ). Merge into the current
|
||||
// syllable when some valid syllable fits; otherwise start a new one.
|
||||
var merged = current
|
||||
merged.set(symbol, in: slot)
|
||||
if SyllableTable.isCompatible(merged) {
|
||||
reordered = true
|
||||
} else {
|
||||
close(at: i)
|
||||
}
|
||||
}
|
||||
if current.isEmpty { start = i }
|
||||
current.set(symbol, in: slot)
|
||||
} else {
|
||||
close(at: i)
|
||||
result.foreignKeys += 1
|
||||
start = i + 1
|
||||
}
|
||||
}
|
||||
close(at: keys.count)
|
||||
return result
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user