@bitbaum/searchkit 0.0.0-stage → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +163 -2
- package/dist/distance.d.ts +24 -0
- package/dist/distance.js +91 -0
- package/dist/engine.d.ts +76 -0
- package/dist/engine.js +350 -0
- package/dist/evaluate.d.ts +46 -0
- package/dist/evaluate.js +127 -0
- package/dist/expand.d.ts +27 -0
- package/dist/expand.js +63 -0
- package/dist/highlight.d.ts +28 -0
- package/dist/highlight.js +95 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +8 -0
- package/dist/normalize.d.ts +45 -0
- package/dist/normalize.js +103 -0
- package/dist/react/index.d.ts +41 -0
- package/dist/react/index.js +59 -0
- package/dist/typo.d.ts +23 -0
- package/dist/typo.js +22 -0
- package/dist/vocabulary.d.ts +38 -0
- package/dist/vocabulary.js +118 -0
- package/package.json +55 -4
- package/src/distance.ts +93 -0
- package/src/engine.ts +492 -0
- package/src/evaluate.ts +162 -0
- package/src/expand.ts +89 -0
- package/src/highlight.ts +115 -0
- package/src/index.ts +52 -0
- package/src/normalize.ts +127 -0
- package/src/react/index.ts +103 -0
- package/src/typo.ts +31 -0
- package/src/vocabulary.ts +164 -0
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The words a collection actually contains, and which of them a typed word
|
|
3
|
+
* could mean.
|
|
4
|
+
*
|
|
5
|
+
* Correcting against the collection's OWN vocabulary is what makes typo
|
|
6
|
+
* tolerance safe: "cqto" is corrected to "cato" because Cato is here, never to
|
|
7
|
+
* a dictionary word nobody listed. It is also what lets a database without
|
|
8
|
+
* typo tolerance have it: expand the typed words into the vocabulary words
|
|
9
|
+
* they could mean (`expandQuery`), and send those.
|
|
10
|
+
*/
|
|
11
|
+
import { editDistance, prefixDistance } from './distance.ts';
|
|
12
|
+
import { DEFAULT_TYPO_POLICY, typoBudget, type TypoPolicy } from './typo.ts';
|
|
13
|
+
|
|
14
|
+
/** How a vocabulary word answers a typed word. */
|
|
15
|
+
export interface Candidate {
|
|
16
|
+
term: string;
|
|
17
|
+
/** Edits between what was typed and the word (or its start, for a prefix). */
|
|
18
|
+
typos: number;
|
|
19
|
+
/** The typed word matched only the start of this word. */
|
|
20
|
+
prefix: boolean;
|
|
21
|
+
/** How often the word occurs in the collection. */
|
|
22
|
+
count: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface Vocabulary {
|
|
26
|
+
/** Every distinct word, sorted, for prefix ranges. */
|
|
27
|
+
readonly terms: readonly string[];
|
|
28
|
+
readonly counts: ReadonlyMap<string, number>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** A vocabulary from words (repeats are counted) or from [word, count] pairs. */
|
|
32
|
+
export function createVocabulary(
|
|
33
|
+
entries: Iterable<string | readonly [string, number]>
|
|
34
|
+
): Vocabulary {
|
|
35
|
+
const counts = new Map<string, number>();
|
|
36
|
+
for (const entry of entries) {
|
|
37
|
+
const [term, n] = typeof entry === 'string' ? [entry, 1] : entry;
|
|
38
|
+
if (term) {
|
|
39
|
+
counts.set(term, (counts.get(term) ?? 0) + n);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return { terms: [...counts.keys()].sort(), counts };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** First index in sorted `terms` not less than `key`. */
|
|
46
|
+
function lowerBound(terms: readonly string[], key: string): number {
|
|
47
|
+
let lo = 0;
|
|
48
|
+
let hi = terms.length;
|
|
49
|
+
while (lo < hi) {
|
|
50
|
+
const mid = (lo + hi) >>> 1;
|
|
51
|
+
if (terms[mid]! < key) {
|
|
52
|
+
lo = mid + 1;
|
|
53
|
+
} else {
|
|
54
|
+
hi = mid;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return lo;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface CandidateOptions {
|
|
61
|
+
/** Let the typed word match the start of a longer word. Default true. */
|
|
62
|
+
prefix?: boolean;
|
|
63
|
+
policy?: TypoPolicy;
|
|
64
|
+
/** At most this many, best first. Default 50. */
|
|
65
|
+
limit?: number;
|
|
66
|
+
/**
|
|
67
|
+
* How many letters the word had as written, when a site spelling rule
|
|
68
|
+
* (`SearchSpec.normalize`) shortened it: the typo budget is the written
|
|
69
|
+
* word's. Heidi reads doubled letters as one, so "vill" arrives as "vil" —
|
|
70
|
+
* and a three-letter word would get no typo at all.
|
|
71
|
+
*/
|
|
72
|
+
writtenLength?: number;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* The vocabulary words a typed (folded) word could mean, best first: exact,
|
|
77
|
+
* then exact prefixes, then within the typo budget — fewer typos first, then
|
|
78
|
+
* the more common word.
|
|
79
|
+
*/
|
|
80
|
+
export function candidates(
|
|
81
|
+
vocab: Vocabulary,
|
|
82
|
+
typed: string,
|
|
83
|
+
options: CandidateOptions = {}
|
|
84
|
+
): Candidate[] {
|
|
85
|
+
const { prefix = true, policy = DEFAULT_TYPO_POLICY, limit = 50, writtenLength } = options;
|
|
86
|
+
if (!typed) {
|
|
87
|
+
return [];
|
|
88
|
+
}
|
|
89
|
+
const found = new Map<string, Candidate>();
|
|
90
|
+
const add = (term: string, typos: number, isPrefix: boolean) => {
|
|
91
|
+
const seen = found.get(term);
|
|
92
|
+
if (!seen || typos < seen.typos || (typos === seen.typos && seen.prefix && !isPrefix)) {
|
|
93
|
+
found.set(term, { term, typos, prefix: isPrefix, count: vocab.counts.get(term) ?? 0 });
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
if (vocab.counts.has(typed)) {
|
|
98
|
+
add(typed, 0, false);
|
|
99
|
+
}
|
|
100
|
+
if (prefix) {
|
|
101
|
+
for (let i = lowerBound(vocab.terms, typed); i < vocab.terms.length; i++) {
|
|
102
|
+
const term = vocab.terms[i]!;
|
|
103
|
+
if (!term.startsWith(typed)) {
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
if (term !== typed) {
|
|
107
|
+
add(term, 0, true);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const length = Array.from(typed).length;
|
|
113
|
+
const budgetLength = Math.max(length, writtenLength ?? 0);
|
|
114
|
+
let budget = typoBudget(budgetLength, policy);
|
|
115
|
+
// The budget belongs to the word that was MEANT. Drop a letter from "Loki"
|
|
116
|
+
// and what was typed ("lki") is too short for a typo — so when a short word
|
|
117
|
+
// matches nothing at all, it may still be one slip from a word long enough
|
|
118
|
+
// to be allowed one. Only then: "cat" must stay "cat" when "cat" exists.
|
|
119
|
+
let minTermLength = 0;
|
|
120
|
+
if (budget === 0 && found.size === 0 && budgetLength >= policy.oneTypoFrom - 1) {
|
|
121
|
+
budget = 1;
|
|
122
|
+
minTermLength = policy.oneTypoFrom;
|
|
123
|
+
}
|
|
124
|
+
if (budget > 0) {
|
|
125
|
+
for (const term of vocab.terms) {
|
|
126
|
+
if (found.get(term)?.typos === 0) {
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
const termLength = term.length;
|
|
130
|
+
if (termLength < minTermLength) {
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
if (termLength + budget < length) {
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
if (termLength <= length + budget) {
|
|
137
|
+
const d = editDistance(typed, term, budget);
|
|
138
|
+
if (d <= budget) {
|
|
139
|
+
add(term, d, false);
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
// A slip in a word's START is not forgiven on the fallback: "cqt" would
|
|
144
|
+
// be one edit from the start of every word beginning "ca".
|
|
145
|
+
if (prefix && minTermLength === 0 && termLength > length) {
|
|
146
|
+
const d = prefixDistance(typed, term, budget);
|
|
147
|
+
if (d <= budget) {
|
|
148
|
+
add(term, d, true);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return [...found.values()]
|
|
155
|
+
.sort(
|
|
156
|
+
(a, b) =>
|
|
157
|
+
a.typos - b.typos ||
|
|
158
|
+
Number(a.prefix) - Number(b.prefix) ||
|
|
159
|
+
b.count - a.count ||
|
|
160
|
+
a.term.length - b.term.length ||
|
|
161
|
+
(a.term < b.term ? -1 : 1)
|
|
162
|
+
)
|
|
163
|
+
.slice(0, limit);
|
|
164
|
+
}
|