Scoop API

scoop:scoop.ui.component:0.1.0

src/command/score.scoop

Selected production source · src/command/score.scoop

1package scoop.ui.component
3import scoop.charset.unicodeCasefold
4import scoop.collections.HashMap
5import scoop.math.pow
6import scoop.text.isBlank
8// Adapted from dip/cmdk's command-score.ts, Copyright (c) 2022 Paco Coursey.
9// MIT license: ../../LICENSE-CMDK. Indices below are Unicode scalars, not UTF-16 units.
10private class CommandScoreText(val folded: Array<Char>, val original: Array<Char>)
12private fun commandScoreText(text: String): CommandScoreText {
13 val lower = ArrayList<Char>()
14 val source = ArrayList<Char>()
15 for (character in text) {
16 for (folded in unicodeCasefold(character.toString())) {
17 lower.add(
18 if (commandSpace(folded))
19 ' '
20 else
21 folded
22 )
23 source.add(character)
24 }
25 }
26 return CommandScoreText(lower.toArray(), source.toArray())
29private fun commandSpace(character: Char): Boolean =
30 character == '-' || character.toString().isBlank() || character.code == 65279
32private fun commandGap(character: Char): Boolean =
33 character ==
34 '\\' ||
35 character ==
36 '/' ||
37 character ==
38 '_' ||
39 character ==
40 '+' ||
41 character ==
42 '.' ||
43 character ==
44 '#' ||
45 character ==
46 '"' ||
47 character ==
48 '@' ||
49 character ==
50 '[' ||
51 character ==
52 '(' ||
53 character ==
54 '{' ||
55 character ==
56 '&'
58/** cmdk's finite fuzzy score, including aliases, word boundaries and adjacent transposition. */
59public fun commandScore(text: String, query: String, keywords: Array<String> = []): Double {
60 val combined = StringBuilder()
61 combined.append(text)
62 for (keyword in keywords) {
63 combined.append(" ");
64 combined.append(keyword)
65 }
66 return CommandScorer(commandScoreText(combined.toString()), commandScoreText(query)).score(
67 0L,
68 0L
69 )
72private class CommandScorer(
73 private val text: CommandScoreText,
74 private val query: CommandScoreText
75) {
76 private val memo =
77 Array<HashMap<Long, Double>>(query.folded.size) { _ -> HashMap<Long, Double>() }
78 fun score(start: Long, part: Long): Double {
79 if (part >= query.folded.size)
80 return if (start == text.folded.size)
81 1.0
82 else
83 0.99
84 val cache = memo[part]
85 when (cache[start]) {
86 case Some(val value) -> return value;
87 case None -> {}
88 }
89 var high = 0.0
90 for (index in start ..< text.folded.size) {
91 if (text.folded[index] != query.folded[part])
92 continue
93 var current = score(index + 1L, part + 1L)
94 if (current > high) {
95 if (index != start) {
96 val gap = commandGap(text.original[index - 1L])
97 val space = commandSpace(text.original[index - 1L])
98 current *= if (gap)
99 0.8
100 else
101 if (space)
102 0.9
103 else
104 0.17
105 if (start > 0L) {
106 var skipped =
107 if (!gap && !space)
108 index - start
109 else
110 0L
111 if (gap || space)
112 for (at in start ..< index - 1L) {
113 if (
114 (gap && commandGap(text.original[at])) ||
115 (!gap && commandSpace(text.original[at]))
116 )
117 skipped += 1L
118 }
119 current *= pow(0.999, skipped.toDouble())
120 }
121 }
122 if (text.original[index] != query.original[part])
123 current *= 0.9999
124 }
125 if (
126 part +
127 1L <
128 query.folded.size &&
129 index >
130 0L &&
131 (
132 (current < 0.1 && text.folded[index - 1L] == query.folded[part + 1L]) ||
133 (
134 query.folded[part + 1L] ==
135 query.folded[part] &&
136 text.folded[index - 1L] !=
137 query.folded[part]
138 )
139 )
140 ) {
141 val transposed = score(index + 1L, part + 2L) * 0.1
142 if (transposed > current)
143 current = transposed
144 }
145 if (current > high)
146 high = current
147 }
148 cache.put(start, high)
149 return high
150 }