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 / Omega / tests / FuzzyReference.swift

59 lines · 3306 bytes Raw Blame History
1import Foundation
2
3/// The Swift matcher Hop shipped up to 0.4.0, kept only as the reference the Omega core is checked against.
4/// How well typed text matches a name. Used for apps in the one box: a prefix beats a word start, which beats
5/// initials, a plain "contains", one wrong letter, and letters in order with gaps. nil means no match.
6enum FuzzyReference {
7 nonisolated static func score(_ query: String, in name: String) -> Int? {
8 let q = query.lowercased().trimmingCharacters(in: .whitespaces), n = name.lowercased()
9 guard !q.isEmpty, !n.isEmpty else { return nil }
10 if let whole = one(q, n) { return whole }
11 let terms = q.split(separator: " ").map(String.init)
12 guard terms.count > 1 else { return nil }
13 var sum = 0
14 for t in terms { guard let s = one(t, n) else { return nil }; sum += s } // every word must match
15 return sum / terms.count - 10
16 }
17
18 private nonisolated static func one(_ q: String, _ n: String) -> Int? {
19 if n == q { return 200 }
20 if n.hasPrefix(q) { return 160 - min(30, n.count - q.count) } // shorter names first
21 let words = n.split(whereSeparator: { !($0.isLetter || $0.isNumber) }).map(String.init)
22 if words.dropFirst().contains(where: { $0.hasPrefix(q) }) { return 120 - min(20, n.count / 3) }
23 if q.count >= 2, words.count > 1, String(words.compactMap(\.first)).hasPrefix(q) { return 110 } // "vsc": Visual Studio Code
24 if n.contains(q) { return 80 - min(20, n.count / 3) }
25 if q.count >= 4 { // one wrong, missing, extra or swapped letter
26 let qs = Array(q), ns = Array(n)
27 for len in [qs.count, qs.count - 1, qs.count + 1] where len <= ns.count && len > 0 {
28 if distance(qs, Array(ns.prefix(len)), atMost: 1) <= 1 { return 70 }
29 }
30 }
31 guard q.count >= 3 else { return nil }
32 var qi = q.startIndex, gaps = 0, last = -1, first = -1, i = 0 // letters in order, with few gaps
33 for ch in n {
34 if qi < q.endIndex, ch == q[qi] {
35 if last >= 0 { gaps += i - last - 1 }
36 if first < 0 { first = i }
37 last = i; qi = q.index(after: qi)
38 }
39 i += 1
40 }
41 guard qi == q.endIndex, gaps <= q.count else { return nil }
42 return max(15, 60 - gaps * 6 - first * 3)
43 }
44
45 /// Edit distance where a swap of two neighbours counts as one. Stops caring above `atMost`.
46 private nonisolated static func distance(_ a: [Character], _ b: [Character], atMost: Int) -> Int {
47 if abs(a.count - b.count) > atMost { return atMost + 1 }
48 var prev2 = [Int](repeating: 0, count: b.count + 1), prev = Array(0...b.count), cur = [Int](repeating: 0, count: b.count + 1)
49 for i in 1...a.count {
50 cur[0] = i
51 for j in 1...b.count {
52 cur[j] = min(prev[j] + 1, cur[j - 1] + 1, prev[j - 1] + (a[i - 1] == b[j - 1] ? 0 : 1))
53 if i > 1, j > 1, a[i - 1] == b[j - 2], a[i - 2] == b[j - 1] { cur[j] = min(cur[j], prev2[j - 2] + 1) }
54 }
55 (prev2, prev, cur) = (prev, cur, prev2)
56 }
57 return prev[b.count]
58 }
59}