Cotton code
30 lines · 1.2 KB · SwiftRaw
// The letter model on this Mac, for `pnpm ink:corpus`: the replay itself is// the core's (`cotton_core::ink::replay`, run by// `crates/cotton-core/examples/ink_corpus.rs`), and the model is Core ML,// which only Apple's platforms run. So the example starts this as a child:// each line on stdin is one group the core encoded,// `{"points": [[x, y, pen_up] × 64], "strokes": n}`, and each line on// stdout its 62 probabilities, a–z, A–Z, 0–9 — the same `InkModel` the app// ships (`Sources/CottonUI/Ink/InkModel.swift`), the same weights.import Foundationstruct Ask: Decodable {    let points: [[Double]]    let strokes: Double}let model = InkModel.shippedmodel.load()let decoder = JSONDecoder()while let line = readLine(strippingNewline: true) {    guard !line.isEmpty else { continue }    do {        let ask = try decoder.decode(Ask.self, from: Data(line.utf8))        let probabilities = model.classify(points: ask.points, strokes: ask.strokes)        let data = try JSONSerialization.data(withJSONObject: probabilities)        FileHandle.standardOutput.write(data + Data("\n".utf8))    } catch {        FileHandle.standardError.write(Data("ink-model: \(error)\n".utf8))        exit(1)    }}