Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/hop/blob/main/Omega/tests/TextReference.swift # hop Hop: a small native macOS launcher. Clipboard history, find, on-device dictation and your coding agent behind one key. Brought by drakestapleton Sign in to star 1 stars 1 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `hop / Omega / tests / TextReference.swift` 121 lines · 7692 bytes Raw Blame History 1 import Foundation 2 3 /// The Swift text decisions Hop shipped up to 0.4.0, kept only as the reference the Omega core is checked against. 4 enum TextReference { 5 static let questionWords: Set = ["what", "what's", "whats", "why", "how", "how's", "who", "who's", "whom", "whose", 6 "when", "where", "which", "explain", "define", "translate", "summarize", "summarise", "describe"] 7 8 /// A question for the model rather than a name to look up: it ends with "?", or starts with a question word 9 /// and runs to three words or more. 10 static func questionLike(_ query: String) -> Bool { 11 let q = query.trimmingCharacters(in: .whitespacesAndNewlines) 12 if q.hasSuffix("?") && q.count > 1 { return true } 13 let words = q.lowercased().split(whereSeparator: \.isWhitespace) 14 guard words.count >= 3, let first = words.first else { return false } 15 return questionWords.contains(String(first)) 16 } 17 18 /// Endings of a bare address typed without "https://". Not "app": on a Mac "Notes.app" is an app, not a website 19 /// (an address at a .app domain still opens when typed with its scheme). 20 static let domainEndings: Set = ["com", "org", "net", "io", "dev", "ai", "co", "edu", "gov", "xyz", "me", "us", "uk", "ca", "de", "tv", "gg"] 21 22 /// The address to open when the text is a web address ("https://…", or a bare "example.com/page"), else nil. 23 static func webAddress(_ text: String) -> String? { 24 let t = text.trimmingCharacters(in: .whitespacesAndNewlines) 25 guard !t.isEmpty, t.utf8.count < 2000, !t.contains(where: \.isWhitespace) else { return nil } 26 let low = t.lowercased() 27 if low.hasPrefix("http://") || low.hasPrefix("https://") { return URL(string: t)?.host == nil ? nil : t } 28 let host = low.split(separator: "/", maxSplits: 1).first.map(String.init) ?? "" 29 let parts = host.split(separator: ".", omittingEmptySubsequences: false) 30 guard parts.count >= 2, let end = parts.last, domainEndings.contains(String(end)), 31 parts.allSatisfy({ !$0.isEmpty && $0.allSatisfy { $0.isLetter || $0.isNumber || $0 == "-" } }), 32 URL(string: "https://" + t) != nil else { return nil } 33 return "https://" + t 34 } 35 36 static let stop: Set = ["the", "and", "for", "with", "that", "this", "what", "when", "where", "does", "did", "how", "why", 37 "can", "you", "are", "was", "were", "have", "has", "from", "into", "about", "please", "make", "need", "want", "just", "our", "my", "its", "not"] 38 static func keywords(_ q: String) -> [String] { 39 let words = q.lowercased().split(whereSeparator: { !($0.isLetter || $0.isNumber || $0 == "-" || $0 == "_" || $0 == ".") }).map(String.init) 40 var seen = Set() 41 return Array(words.filter { $0.count >= 3 && !stop.contains($0) && seen.insert($0).inserted }.sorted { $0.count > $1.count }.prefix(4)) 42 } 43 44 /// How alike two spellings are, 0 to 1, on letters and digits only, ignoring case. 45 static func likeness(_ a: String, _ b: String) -> Double { 46 let x = Array(a.lowercased().filter { $0.isLetter || $0.isNumber }), y = Array(b.lowercased().filter { $0.isLetter || $0.isNumber }) 47 guard !x.isEmpty || !y.isEmpty else { return 1 } 48 var row = Array(0...y.count) 49 for i in 1...max(x.count, 1) where !x.isEmpty { 50 var prev = row[0]; row[0] = i 51 for j in stride(from: 1, through: y.count, by: 1) { 52 let cur = row[j] 53 row[j] = min(row[j] + 1, row[j - 1] + 1, prev + (x[i - 1] == y[j - 1] ? 0 : 1)) 54 prev = cur 55 } 56 } 57 return 1 - Double(row[y.count]) / Double(max(x.count, y.count)) 58 } 59 60 static let maxWords = 3, maxInserted = 4, minLikeness = 0.5, maxCells = 250_000 61 static let edges = CharacterSet.punctuationCharacters.union(.symbols) // built once, not per word 62 /// The words of a text without the punctuation around them ("Hello," and "hello" differ only in case). 63 static func words(_ s: String) -> [String] { 64 s.split(whereSeparator: \.isWhitespace) 65 .map { $0.trimmingCharacters(in: edges) } 66 .filter { !$0.isEmpty } 67 } 68 69 /// The mishearings the user fixed: `before` is the field right after Hop typed `dictated`, `after` the same field 70 /// later. Returns nothing when the dictation cannot be found, or when more than half of it changed (a rewrite). 71 static func corrections(dictated: String, before: String, after: String) -> [(heard: String, write: String)] { 72 let dw = words(dictated), bw = words(before), aw = words(after) 73 guard !dw.isEmpty, bw.count >= dw.count else { return [] } 74 // Where the dictation sits in the field: its last whole occurrence (Hop typed it at the cursor, most recently). 75 var start = -1 76 for i in stride(from: bw.count - dw.count, through: 0, by: -1) where Array(bw[i..<(i + dw.count)]) == dw { start = i; break } 77 guard start >= 0 else { return [] } 78 let span = start..<(start + dw.count) 79 // Same words at both ends are not part of any edit. 80 var p = 0 81 while p < bw.count, p < aw.count, bw[p] == aw[p] { p += 1 } 82 var s = 0 83 while s < bw.count - p, s < aw.count - p, bw[bw.count - 1 - s] == aw[aw.count - 1 - s] { s += 1 } 84 let b = Array(bw[p..<(bw.count - s)]), a = Array(aw[p..<(aw.count - s)]) 85 guard !b.isEmpty, b.count * max(a.count, 1) <= maxCells else { return [] } 86 // Longest common subsequence of the middle, then the runs of removed and inserted words between matches. 87 var t = Array(repeating: Array(repeating: 0, count: a.count + 1), count: b.count + 1) 88 for i in stride(from: b.count - 1, through: 0, by: -1) { 89 for j in stride(from: a.count - 1, through: 0, by: -1) { 90 t[i][j] = b[i] == a[j] ? t[i + 1][j + 1] + 1 : max(t[i + 1][j], t[i][j + 1]) 91 } 92 } 93 var hunks: [(removed: [Int], inserted: [String])] = [] 94 var removed: [Int] = [], inserted: [String] = [] 95 func close() { if !removed.isEmpty || !inserted.isEmpty { hunks.append((removed, inserted)) }; removed = []; inserted = [] } 96 var i = 0, j = 0 97 while i < b.count || j < a.count { 98 if i < b.count, j < a.count, b[i] == a[j] { close(); i += 1; j += 1 } 99 else if i < b.count, j == a.count || t[i + 1][j] >= t[i][j + 1] { removed.append(p + i); i += 1 } 100 else { inserted.append(a[j]); j += 1 } 101 } 102 close() 103 let removedInSpan = hunks.reduce(0) { $0 + $1.removed.filter { span.contains($0) }.count } 104 guard removedInSpan * 2 <= max(dw.count, 2) else { return [] } // a rewrite, not a fix 105 var out: [(heard: String, write: String)] = [] 106 for h in hunks where !h.removed.isEmpty && !h.inserted.isEmpty { 107 guard h.removed.allSatisfy({ span.contains($0) }), h.removed.count <= maxWords else { continue } 108 let heard = h.removed.map { bw[$0] }.joined(separator: " ") 109 // Words typed on after the fix join the same run ("ocid" -> "ORCID Then I kept typing"): keep the start 110 // of the run that reads most like what was heard, at most `maxInserted` words. 111 var write = "", best = -1.0 112 for k in 1...min(maxInserted, h.inserted.count) { 113 let w = h.inserted[0.. best { best = l; write = w } 115 } 116 guard heard != write, best >= minLikeness else { continue } 117 out.append((heard, write)) 118 } 119 return out 120 } 121 }