The prompt in lower case, words corrected, the selection's name as ` `.
(prompt: string, names: readonly string[])
| 86 | |
| 87 | /** The prompt in lower case, words corrected, the selection's name as `<name>`. */ |
| 88 | function normalize(prompt: string, names: readonly string[]): string { |
| 89 | const own = names.flatMap(name => [name, name.split(/[./:]/).filter(Boolean).pop() ?? name]).map(name => name.toLowerCase()).filter(Boolean); |
| 90 | const text = prompt.toLowerCase().replace(/[’`"]/g, "'").replace(/\bwhat'?s\b/g, 'what is').replace(/'/g, ' ') |
| 91 | .replace(/[?!,;:()]+/g, ' ').replace(/\.+(?=\s|$)/g, ' '); |
| 92 | return text.split(/\s+/).filter(Boolean).map(word => { |
| 93 | if (own.includes(word) || own.includes(word.split(/[.:]/).pop()!)) return '<name>'; |
| 94 | const spelled = SPELLED[word] ?? word; |
| 95 | if (OWN.has(spelled)) return spelled; |
| 96 | if (own.some(name => name.length >= 4 && withinEdits(word, name, typoBudget(name)))) return '<name>'; |
| 97 | if (spelled.length < 4) return spelled; |
| 98 | return VOCABULARY.find(candidate => withinEdits(spelled, candidate, 1)) |
| 99 | ?? VIEW_VOCABULARY.find(candidate => withinEdits(spelled, candidate, candidate.length >= 9 ? 2 : 1)) ?? spelled; |
| 100 | }).join(' '); |
| 101 | } |
| 102 | |
| 103 | /** "general" for a short general question about the selection or the open file, "detail" for the |
| 104 | * same question asking for detail, undefined for any other prompt. `names` are the selection's. */ |
no test coverage detected