-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrammar.ts
More file actions
321 lines (303 loc) · 10.1 KB
/
Copy pathgrammar.ts
File metadata and controls
321 lines (303 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/**
* Grammar / spelling / style analysis powered by Harper.
*
* Harper is a Rust grammar checker compiled to WebAssembly — it runs in a
* dedicated worker (owned by `WorkerLinter`) so main-thread keystrokes
* stay buttery. The WASM payload is ~18 MB raw / ~7 MB gzipped, so the
* download is surfaced as visible progress on the first analysis tap.
*
* Once loaded, the linter is held in module-level state for the lifetime
* of the tab — subsequent scans don't re-download or re-instantiate.
*
* Nothing leaves the browser; everything (binary, dictionary, rules) is
* bundled at build time.
*/
import { useCallback, useEffect, useRef, useState } from "react";
import type { GrammarIssue, GrammarIssueKind, GrammarReport, ResumeData } from "../types.ts";
/** Strip markdown emphasis so the linter doesn't flag `**`/`*`/backticks as prose errors. */
function stripMarkdown(text: string): string {
return text
.replace(/\*\*([^*]+)\*\*/g, "$1")
.replace(/(^|[^\\])\*([^*]+)\*/g, "$1$2")
.replace(/`([^`]+)`/g, "$1");
}
/**
* Flatten the résumé into labelled prose segments. Only free-form writing
* fields are included — structured fields (names, employers, dates) would
* produce pure noise.
*/
function buildGrammarSegments(resume: ResumeData) {
const out: Array<{ id: string; label: string; text: string }> = [];
const summary = resume.profile.summary.trim();
if (summary) {
out.push({
id: "profile.summary",
label: "Professional summary",
text: stripMarkdown(summary),
});
}
resume.experience.forEach((role, i) => {
const roleLabel = role.title.trim() || role.company.trim() || `Role #${i + 1}`;
role.bullets.forEach((b, bi) => {
const trimmed = b.trim();
if (!trimmed) return;
out.push({
id: `experience.${i}.bullets.${bi}`,
label: `${roleLabel} · bullet ${bi + 1}`,
text: stripMarkdown(trimmed),
});
});
});
resume.projects.forEach((p, i) => {
const projLabel = p.name.trim() || `Project #${i + 1}`;
const desc = p.description.trim();
if (desc) {
out.push({
id: `projects.${i}.description`,
label: `${projLabel} · description`,
text: stripMarkdown(desc),
});
}
(p.roles ?? []).forEach((r, ri) => {
const trimmed = r.trim();
if (!trimmed) return;
out.push({
id: `projects.${i}.roles.${ri}`,
label: `${projLabel} · role ${ri + 1}`,
text: stripMarkdown(trimmed),
});
});
});
resume.awards.forEach((a, i) => {
const detail = a.detail.trim();
if (detail) {
out.push({
id: `awards.${i}.detail`,
label: `${a.title.trim() || `Award #${i + 1}`} · detail`,
text: stripMarkdown(detail),
});
}
});
return out;
}
/**
* Map Harper's rich lint kinds into the four buckets the ATS panel cares
* about. Anything Harper considers "Style" (passive voice, wordiness,
* clarity) lands in the style bucket; punctuation/agreement/tense land in
* grammar; spelling is its own; repetition piggybacks on grammar so
* "the the" isn't buried as style.
*/
function classify(rawKind: string): GrammarIssueKind {
const k = rawKind.toLowerCase();
if (k.includes("spell") || k.includes("misspell")) return "spelling";
if (k.includes("style") || k.includes("passive") || k.includes("word")) return "style";
if (k.includes("read") || k.includes("sentence") || k.includes("clarity")) return "readability";
return "grammar";
}
/** Count words in the segment for issue-density scoring. */
function countWords(text: string): number {
const trimmed = text.trim();
return trimmed ? trimmed.split(/\s+/).length : 0;
}
type HarperLinter = {
setup(): Promise<void>;
lint(
text: string,
options?: { language?: "plaintext" | "markdown" },
): Promise<
Array<{
get_problem_text(): string;
lint_kind_pretty(): string;
message(): string;
suggestions(): Array<{ get_replacement_text(): string }>;
}>
>;
importWords(words: string[]): Promise<void>;
};
/**
* Acronyms, product names, and newer tech/AI jargon that Harper's curated
* dictionary may still flag. Imported once per linter lifetime.
*/
const PERSONAL_DICTIONARY = [
"agentic",
"multimodal",
"observability",
"autoscaling",
"containerization",
"dockerized",
"kubernetes",
"microservices",
"microservice",
"serverless",
"roadmap",
"roadmaps",
"upskilling",
"reskilling",
"onboarding",
"offboarding",
"fullstack",
"webhook",
"webhooks",
"backend",
"frontend",
"MLOps",
"DevOps",
"GraphQL",
"Kubernetes",
"TypeScript",
"JavaScript",
"Postgres",
"PostgreSQL",
"MongoDB",
"SaaS",
"PaaS",
"IaaS",
"APIs",
"LLM",
"LLMs",
"RAG",
"OKRs",
"KPIs",
"NPS",
"ARR",
"MRR",
"GTM",
];
/**
* Fetch Harper's WASM with a streaming reader so callers can render
* byte-level progress, then hand it to a WorkerLinter as a blob URL.
* The blob is document-scoped and survives until the tab closes, so
* repeat scans reuse the cached linter instance.
*/
async function loadLinter(onProgress: (pct: number) => void): Promise<HarperLinter> {
// Importing `harper.js/binary` makes Vite fingerprint + emit the .wasm
// and sets `binary.url` to the resolved asset URL. We piggyback on that
// URL to do our own progress-tracked fetch.
const [{ binary }, { createBinaryModuleFromUrl, WorkerLinter, Dialect }] = await Promise.all([
import("harper.js/binary"),
import("harper.js"),
]);
const wasmUrl = binary.url.toString();
const resp = await fetch(wasmUrl);
if (!resp.ok || !resp.body) {
throw new Error(`Failed to fetch Harper WASM: ${resp.status}`);
}
const total = Number(resp.headers.get("content-length") ?? 0);
const reader = resp.body.getReader();
const chunks: Uint8Array[] = [];
let received = 0;
// Report initial activity so the UI leaves zero-state immediately even
// when the server doesn't send Content-Length.
onProgress(total ? 0 : 0.05);
for (;;) {
const { done, value } = await reader.read();
if (done) break;
if (!value) continue;
chunks.push(value);
received += value.length;
if (total) onProgress(Math.min(0.99, received / total));
}
onProgress(1);
const blob = new Blob(chunks as BlobPart[], { type: "application/wasm" });
const blobUrl = URL.createObjectURL(blob);
const customBinary = createBinaryModuleFromUrl(blobUrl);
const linter = new WorkerLinter({
binary: customBinary,
dialect: Dialect.American,
}) as unknown as HarperLinter;
await linter.setup();
await linter.importWords(PERSONAL_DICTIONARY);
return linter;
}
/**
* Filter out spelling hits on capitalised proper-noun-looking tokens.
* Résumés are full of names, employers, and products that neither Harper's
* curated dictionary nor our personal list will know about; flagging every
* one drowns real typos.
*/
function isProperNoun(actual: string): boolean {
return /^[A-Z]/.test(actual) && !/^[A-Z]+$/.test(actual);
}
export interface UseGrammarScan {
/** Latest completed report, or null if nothing has been scanned yet. */
report: GrammarReport | null;
/** True while a scan is in flight (includes first-time engine download). */
scanning: boolean;
/** True once the Harper WASM is loaded and the linter is ready. */
engineReady: boolean;
/** 0…1 download fraction of the Harper WASM. 0 before a scan is requested. */
engineProgress: number;
/** Trigger a fresh scan. First call also downloads + instantiates Harper. */
scan: () => void;
}
export function useGrammarScan(resume: ResumeData): UseGrammarScan {
const [report, setReport] = useState<GrammarReport | null>(null);
const [scanning, setScanning] = useState(false);
const [engineReady, setEngineReady] = useState(false);
const [engineProgress, setEngineProgress] = useState(0);
const linterRef = useRef<HarperLinter | null>(null);
const linterPromiseRef = useRef<Promise<HarperLinter> | null>(null);
const latestRequestRef = useRef(0);
const resumeRef = useRef(resume);
useEffect(() => {
resumeRef.current = resume;
}, [resume]);
const scan = useCallback(() => {
const requestId = ++latestRequestRef.current;
setScanning(true);
const ensureLinter = async (): Promise<HarperLinter> => {
if (linterRef.current) return linterRef.current;
if (!linterPromiseRef.current) {
linterPromiseRef.current = loadLinter((pct) => {
setEngineProgress(pct);
}).then((linter) => {
linterRef.current = linter;
setEngineReady(true);
return linter;
});
}
return linterPromiseRef.current;
};
void (async () => {
try {
const linter = await ensureLinter();
const segments = buildGrammarSegments(resumeRef.current);
const issues: GrammarIssue[] = [];
let wordsChecked = 0;
for (const seg of segments) {
wordsChecked += countWords(seg.text);
const lints = await linter.lint(seg.text, { language: "plaintext" });
for (const lint of lints) {
const rawKind = lint.lint_kind_pretty();
const kind = classify(rawKind);
const actual = lint.get_problem_text();
if (kind === "spelling" && isProperNoun(actual)) continue;
const suggestions = lint
.suggestions()
.map((s) => s.get_replacement_text())
.filter((s) => s.length > 0)
.slice(0, 3);
issues.push({
segmentId: seg.id,
segmentLabel: seg.label,
kind,
actual,
suggestions,
reason: lint.message(),
});
}
}
if (requestId !== latestRequestRef.current) return;
setReport({ issues, wordsChecked });
setScanning(false);
} catch {
if (requestId !== latestRequestRef.current) return;
// Engine failures degrade silently — the ATS score just omits the
// writing dimension rather than breaking the entire review.
setReport({ issues: [], wordsChecked: 0 });
setScanning(false);
}
})();
}, []);
return { report, scanning, engineReady, engineProgress, scan };
}