interplane.Get Omega
Read in your language

hop

Hop: a small native macOS launcher. Clipboard history, find, on-device dictation and your coding agent behind one key.

Brought by drakestapleton

branch main Branches Tags History

hop / Sources / OmegaText.swift

90 lines · 5360 bytes Raw Blame History
1import Foundation
2
3/// What Hop asks its Omega core about a text (`Omega/hopcore.om`, kinds 3 and 4). This file prepares the request
4/// and reads the answer, and nothing more: it lowercases, splits a text into words and numbers them, and asks
5/// macOS whether an address parses. The rules are in the Omega code. Omega/tests/check-core.sh checks this very
6/// file against the Swift that Hop shipped before.
7enum OmegaText {
8 /// A question for the model rather than a name to look up (fn qlike).
9 static func questionLike(_ query: String) -> Bool {
10 OmegaCore.shared.text(op: 1, Array(query.lowercased()))?.first == 1
11 }
12
13 /// The address to open when the text is a web address, else nil (fn weba). The last check, that macOS can
14 /// parse it, stays here because it is Apple's parser.
15 static func webAddress(_ text: String) -> String? {
16 let t = text.trimmingCharacters(in: .whitespacesAndNewlines)
17 switch OmegaCore.shared.text(op: 3, Array(t.lowercased()), aux: t.utf8.count)?.first {
18 case 1: return URL(string: t)?.host == nil ? nil : t
19 case 2: return URL(string: "https://" + t) != nil ? "https://" + t : nil
20 default: return nil
21 }
22 }
23
24 /// The words to search for (fn keyw); a text over the core's limit gives none.
25 static func keywords(_ q: String) -> [String] {
26 let c = Array(q.lowercased())
27 guard let r = OmegaCore.shared.text(op: 2, c), let k = r.first.map(Int.init), r.count == 1 + 4 * k else { return [] }
28 return (0..<k).compactMap { j in
29 let s = Int(r[1 + 4 * j]) | Int(r[2 + 4 * j]) << 8, l = Int(r[3 + 4 * j]) | Int(r[4 + 4 * j]) << 8
30 return s + l <= c.count ? String(c[s..<(s + l)]) : nil
31 }
32 }
33
34 /// How alike two spellings are, 0 to 1 (fn alike counts the edits); 0 when a spelling is over the core's limit.
35 static func likeness(_ a: String, _ b: String) -> Double {
36 let x = Array(a.lowercased()), y = Array(b.lowercased())
37 guard let r = OmegaCore.shared.text(op: 4, x + y, aux: x.count), r.count == 4 else { return 0 }
38 let d = Int(r[0]) | Int(r[1]) << 8, m = Int(r[2]) | Int(r[3]) << 8
39 return m == 0 ? 1 : 1 - Double(d) / Double(m)
40 }
41
42 private static let edges = CharacterSet.punctuationCharacters.union(.symbols) // built once, not per word
43 /// The words of a text without the punctuation around them ("Hello," and "hello" differ only in case).
44 static func words(_ s: String) -> [String] {
45 s.split(whereSeparator: \.isWhitespace)
46 .map { $0.trimmingCharacters(in: edges) }
47 .filter { !$0.isEmpty }
48 }
49
50 /// The mishearings the user fixed: `before` is the field right after Hop typed `dictated`, `after` the same field
51 /// later. Which words were replaced is found by fn wdiff, and which of the typed words are the fix by fn pick.
52 /// Nothing when the texts are over the core's limits (262141 words in all, 4096 fixes).
53 static func corrections(dictated: String, before: String, after: String) -> [(heard: String, write: String)] {
54 let dw = words(dictated), bw = words(before), aw = words(after)
55 guard 12 + 4 * (dw.count + bw.count + aw.count) <= 1_048_576 else { return [] }
56 var body = [UInt8](); body.reserveCapacity(12 + 4 * (dw.count + bw.count + aw.count))
57 func put(_ v: UInt32) { body += [UInt8(v & 255), UInt8((v >> 8) & 255), UInt8((v >> 16) & 255), UInt8(v >> 24)] }
58 put(UInt32(dw.count)); put(UInt32(bw.count)); put(UInt32(aw.count))
59 var numbers: [String: UInt32] = [:] // equal words get equal numbers
60 for list in [dw, bw, aw] {
61 for w in list {
62 if let k = numbers[w] { put(k) } else { let k = UInt32(numbers.count); numbers[w] = k; put(k) }
63 }
64 }
65 guard let r = OmegaCore.shared.request(kind: 4, body: body), r.count >= 2 else { return [] }
66 let n = Int(r[0]) | Int(r[1]) << 8
67 guard r.count == 2 + 14 * n else { return [] }
68 func u32(_ o: Int) -> Int { Int(r[o]) | Int(r[o + 1]) << 8 | Int(r[o + 2]) << 16 | Int(r[o + 3]) << 24 }
69 var out: [(heard: String, write: String)] = []
70 for h in 0..<n {
71 let o = 2 + 14 * h
72 let rs = u32(o), rc = Int(r[o + 4]) | Int(r[o + 5]) << 8, ts = u32(o + 6), tc = u32(o + 10)
73 guard rc >= 1, tc >= 1, rs + rc <= bw.count, ts + tc <= aw.count else { continue }
74 let heard = bw[rs..<(rs + rc)].joined(separator: " ")
75 let typed = Array(aw[ts..<(ts + min(tc, 4))])
76 // What was heard, then the first typed word, the first two, and so on: the core says which run is the fix.
77 var chars = Array(heard.lowercased()), ends: [Int] = [], same: UInt32 = 0
78 let aux = chars.count
79 for k in 1...typed.count {
80 let run = typed[0..<k].joined(separator: " ")
81 if run == heard { same |= 1 << UInt32(k - 1) }
82 chars += Array(run.lowercased()); ends.append(chars.count - 1)
83 }
84 guard let k = OmegaCore.shared.text(op: 5, chars, aux: aux, extra: same, ends: ends)?.first.map(Int.init),
85 k >= 1, k <= typed.count else { continue }
86 out.append((heard, typed[0..<k].joined(separator: " ")))
87 }
88 return out
89 }
90}