implement inital suggestion application

This commit is contained in:
Chris Kruining 2025-02-18 14:15:17 +11:00
parent 544e974493
commit 759169159d
No known key found for this signature in database
GPG key ID: EB894A3560CCCAD2
5 changed files with 151 additions and 40 deletions

View file

@ -49,6 +49,8 @@ export function createSource(initalValue: string): Source {
};
}
const isMarker = (node: Node) => node.type === 'element' && Object.hasOwn((node as Element).properties, 'dataMarker')
function addErrors(): Transformer {
const wrapInMarker = (text: Text, type: string): Element => ({
type: 'element',
@ -67,6 +69,10 @@ function addErrors(): Transformer {
return;
}
if (isMarker(p)) {
return;
}
const errors = grammarChecker(n.value, 'en-GB');
if (errors.length === 0) {
@ -85,6 +91,10 @@ function addErrors(): Transformer {
return;
}
if (isMarker(p)) {
return;
}
const errors = spellChecker(n.value, 'en-GB');
if (errors.length === 0) {
@ -101,15 +111,13 @@ function addErrors(): Transformer {
}
function clearErrors(): Transformer {
const test = (n: Node) => n.type === 'element' && Object.hasOwn(n.properties, 'dataMarker');
return function (tree) {
visit(tree, test, (n, i, p: Element) => {
visit(tree, isMarker, (n, i, p: Element) => {
if (typeof i !== 'number' || p === undefined) {
return;
}
p.children.splice(i, 1, ...n.children);
p.children.splice(i, 1, ...(n as Element).children);
})
}
}
@ -122,8 +130,9 @@ function checker(regex: RegExp) {
return [];
let lastIndex = 0;
const threshold = .75//.99;
return Array.from<RegExpExecArray>(subject.matchAll(regex)).filter(() => Math.random() >= .99).flatMap<readonly [boolean, string]>(({ 0: match, index }) => {
return Array.from<RegExpExecArray>(subject.matchAll(regex)).filter(() => Math.random() >= threshold).flatMap<readonly [boolean, string]>(({ 0: match, index }) => {
const end = index + match.length;
const result = [
[false, subject.slice(lastIndex, index)],