textoneitor 0.0.0-stage → 1.2.1

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.
Files changed (77) hide show
  1. package/LICENSE +101 -0
  2. package/LICENSE-AGPL-3.0 +661 -0
  3. package/README.md +69 -2
  4. package/dist/api/index.d.ts +34 -0
  5. package/dist/api/index.js +98 -0
  6. package/dist/baseline/index.d.ts +20 -0
  7. package/dist/baseline/index.js +81 -0
  8. package/dist/bundle/dist/cli.mjs +29861 -0
  9. package/dist/bundle/package.json +6 -0
  10. package/dist/bundle/rulepack/rulepack.json +1496 -0
  11. package/dist/bundle/rules/situaciones.yml +27 -0
  12. package/dist/cli/benchmark.d.ts +89 -0
  13. package/dist/cli/benchmark.js +185 -0
  14. package/dist/cli/main.d.ts +2 -0
  15. package/dist/cli/main.js +357 -0
  16. package/dist/config/core.d.ts +55 -0
  17. package/dist/config/core.js +244 -0
  18. package/dist/config/glob.d.ts +2 -0
  19. package/dist/config/glob.js +72 -0
  20. package/dist/config/index.d.ts +8 -0
  21. package/dist/config/index.js +38 -0
  22. package/dist/config/situaciones.d.ts +17 -0
  23. package/dist/config/situaciones.js +43 -0
  24. package/dist/contracts/index.d.ts +238 -0
  25. package/dist/contracts/index.js +4 -0
  26. package/dist/document/index.d.ts +10 -0
  27. package/dist/document/index.js +68 -0
  28. package/dist/document/text.d.ts +22 -0
  29. package/dist/document/text.js +91 -0
  30. package/dist/document/tokenize.d.ts +4 -0
  31. package/dist/document/tokenize.js +59 -0
  32. package/dist/markdown/index.d.ts +7 -0
  33. package/dist/markdown/index.js +178 -0
  34. package/dist/paths.d.ts +4 -0
  35. package/dist/paths.js +14 -0
  36. package/dist/reporters/index.d.ts +29 -0
  37. package/dist/reporters/index.js +182 -0
  38. package/dist/rules/compiler/build.d.ts +1 -0
  39. package/dist/rules/compiler/build.js +46 -0
  40. package/dist/rules/compiler/index.d.ts +40 -0
  41. package/dist/rules/compiler/index.js +343 -0
  42. package/dist/rules/compiler/safety.d.ts +16 -0
  43. package/dist/rules/compiler/safety.js +124 -0
  44. package/dist/rules/detectors/index.d.ts +4 -0
  45. package/dist/rules/detectors/index.js +636 -0
  46. package/dist/rules/detectors/shared.d.ts +30 -0
  47. package/dist/rules/detectors/shared.js +87 -0
  48. package/dist/rules/runtime/index.d.ts +17 -0
  49. package/dist/rules/runtime/index.js +74 -0
  50. package/dist/runner/index.d.ts +16 -0
  51. package/dist/runner/index.js +171 -0
  52. package/dist/runner/lint-text.d.ts +19 -0
  53. package/dist/runner/lint-text.js +20 -0
  54. package/dist/scoring/index.d.ts +16 -0
  55. package/dist/scoring/index.js +41 -0
  56. package/dist/suppressions/index.d.ts +23 -0
  57. package/dist/suppressions/index.js +86 -0
  58. package/dist/web/engine.meta.json +1 -0
  59. package/dist/web/engine.mjs +11018 -0
  60. package/dist/web/index.d.ts +39 -0
  61. package/dist/web/index.js +37 -0
  62. package/dist/web/node-crypto-shim.d.ts +9 -0
  63. package/dist/web/node-crypto-shim.js +24 -0
  64. package/dist/web/sha256.d.ts +11 -0
  65. package/dist/web/sha256.js +89 -0
  66. package/package.json +88 -4
  67. package/rulepack/rulepack.json +1496 -0
  68. package/rules/profiles/academico.yml +4 -0
  69. package/rules/profiles/chat.yml +4 -0
  70. package/rules/profiles/correo.yml +3 -0
  71. package/rules/profiles/general.yml +2 -0
  72. package/rules/profiles/marketing.yml +4 -0
  73. package/rules/profiles/readme.yml +5 -0
  74. package/rules/profiles/redes.yml +6 -0
  75. package/rules/profiles/tecnico.yml +4 -0
  76. package/rules/situaciones.yml +27 -0
  77. package/schemas/rule.schema.json +90 -0
@@ -0,0 +1,636 @@
1
+ import { buildTermRegExp, execAll, isExcepted, STOPWORDS, tokensByBlock } from "./shared.js";
2
+ /* ------------------------------------------------------------------ */
3
+ /* regex: patrones sobre el texto de cada bloque. */
4
+ /* params: { patterns: string[], flags?: string, anchor?: "block_start" | "block_end" | "last_block" | "first_block" } */
5
+ /* ------------------------------------------------------------------ */
6
+ const regex = (rule, doc) => {
7
+ const p = rule.params;
8
+ const flags = p.flags ?? "giu";
9
+ const res = p.patterns.map((s) => new RegExp(s, flags.includes("g") ? flags : flags + "g"));
10
+ const out = [];
11
+ doc.blocks.forEach((b, bi) => {
12
+ if (p.kinds && !p.kinds.includes(b.kind))
13
+ return;
14
+ if (p.anchor === "last_block" && bi !== doc.blocks.length - 1)
15
+ return;
16
+ if (p.anchor === "first_block" && bi !== 0)
17
+ return;
18
+ for (const re of res) {
19
+ for (const m of execAll(re, b.text)) {
20
+ if (p.anchor === "block_start" && m.index !== 0)
21
+ continue;
22
+ if (p.anchor === "block_end" && m.index + m.length !== b.text.length)
23
+ continue;
24
+ if (isExcepted(rule, b.text, m.index, m.index + m.length))
25
+ continue;
26
+ out.push({ blockIndex: bi, start: m.index, end: m.index + m.length, data: { term: m.match[0] } });
27
+ }
28
+ }
29
+ });
30
+ return out;
31
+ };
32
+ /* ------------------------------------------------------------------ */
33
+ /* lexicon: términos literales (o re:) con límites de palabra. */
34
+ /* params: { terms: string[], kinds?: string[] } */
35
+ /* ------------------------------------------------------------------ */
36
+ const lexicon = (rule, doc) => {
37
+ const p = rule.params;
38
+ const re = buildTermRegExp(p.terms);
39
+ if (!re)
40
+ return [];
41
+ const out = [];
42
+ doc.blocks.forEach((b, bi) => {
43
+ if (p.kinds && !p.kinds.includes(b.kind))
44
+ return;
45
+ for (const m of execAll(re, b.text)) {
46
+ if (isExcepted(rule, b.text, m.index, m.index + m.length))
47
+ continue;
48
+ out.push({ blockIndex: bi, start: m.index, end: m.index + m.length, data: { term: m.match[0] } });
49
+ }
50
+ });
51
+ return out;
52
+ };
53
+ /* ------------------------------------------------------------------ */
54
+ /* sequence: pasos en orden dentro de una frase, con hueco máximo. */
55
+ /* params: { steps: string[][] (alternativas literales o re: por paso), max_gap: number (palabras) } */
56
+ /* ------------------------------------------------------------------ */
57
+ const sequence = (rule, doc) => {
58
+ const p = rule.params;
59
+ const stepRes = p.steps.map((alts) => buildTermRegExp(alts));
60
+ if (stepRes.some((r) => r === null))
61
+ return [];
62
+ const out = [];
63
+ for (const s of doc.sentences) {
64
+ const b = doc.blocks[s.blockIndex];
65
+ let cursor = s.start;
66
+ let first = -1;
67
+ let last = -1;
68
+ let ok = true;
69
+ for (let i = 0; i < stepRes.length; i++) {
70
+ const re = new RegExp(stepRes[i].source, "iug");
71
+ re.lastIndex = cursor;
72
+ let found = null;
73
+ let m;
74
+ while ((m = re.exec(b.text)) !== null) {
75
+ if (m.index + m[0].length > s.end)
76
+ break;
77
+ if (i === 0) {
78
+ found = m;
79
+ break;
80
+ }
81
+ const gapText = b.text.slice(last, m.index);
82
+ const gapWords = (gapText.match(/[\p{L}\p{N}]+/gu) ?? []).length;
83
+ if (gapWords <= p.max_gap) {
84
+ found = m;
85
+ break;
86
+ }
87
+ // Demasiado lejos: probamos un inicio posterior no tiene sentido; abortamos.
88
+ break;
89
+ }
90
+ if (!found) {
91
+ ok = false;
92
+ break;
93
+ }
94
+ if (i === 0)
95
+ first = found.index;
96
+ last = found.index + found[0].length;
97
+ cursor = last;
98
+ }
99
+ if (ok && first >= 0 && !isExcepted(rule, b.text, first, last)) {
100
+ out.push({ blockIndex: s.blockIndex, start: first, end: last, data: { term: b.text.slice(first, last) } });
101
+ }
102
+ }
103
+ return out;
104
+ };
105
+ /* ------------------------------------------------------------------ */
106
+ /* density: frecuencia de un léxico/patrón en una ventana. */
107
+ /* params: { terms: string[], window: "block"|"document", unit?: "match"|"block_start"|"sentence",
108
+ /* min_count: number, per_1000?: number, ratio?: number, min_words?: number, kinds?: string[] } */
109
+ /* ------------------------------------------------------------------ */
110
+ const density = (rule, doc) => {
111
+ const p = rule.params;
112
+ const re = buildTermRegExp(p.terms);
113
+ if (!re)
114
+ return [];
115
+ const unit = p.unit ?? "match";
116
+ const out = [];
117
+ const tb = tokensByBlock(doc);
118
+ const countBlock = (b, bi) => {
119
+ let count = 0;
120
+ let first = null;
121
+ if (unit === "block_start") {
122
+ const r = new RegExp(re.source, "iu");
123
+ const m = r.exec(b.text);
124
+ if (m && m.index === 0) {
125
+ count = 1;
126
+ first = [0, m[0].length];
127
+ }
128
+ return { count, first, units: 1 };
129
+ }
130
+ if (unit === "sentence") {
131
+ const sents = doc.sentences.filter((s) => s.blockIndex === bi);
132
+ for (const s of sents) {
133
+ const r = new RegExp(re.source, "iu");
134
+ const m = r.exec(s.text);
135
+ if (m) {
136
+ count++;
137
+ if (!first)
138
+ first = [s.start + m.index, s.start + m.index + m[0].length];
139
+ }
140
+ }
141
+ return { count, first, units: sents.length };
142
+ }
143
+ for (const m of execAll(re, b.text)) {
144
+ if (isExcepted(rule, b.text, m.index, m.index + m.length))
145
+ continue;
146
+ count++;
147
+ if (!first)
148
+ first = [m.index, m.index + m.length];
149
+ }
150
+ const words = tb[bi].filter((i) => doc.tokens[i].isWord).length;
151
+ return { count, first, units: words };
152
+ };
153
+ if (p.window === "block") {
154
+ doc.blocks.forEach((b, bi) => {
155
+ if (p.kinds && !p.kinds.includes(b.kind))
156
+ return;
157
+ const { count, first, units } = countBlock(b, bi);
158
+ if (count < p.min_count || !first)
159
+ return;
160
+ if (p.min_words && units < p.min_words)
161
+ return;
162
+ if (p.per_1000 !== undefined && units > 0 && (count * 1000) / units < p.per_1000)
163
+ return;
164
+ if (p.ratio !== undefined && units > 0 && count / units < p.ratio)
165
+ return;
166
+ out.push({ blockIndex: bi, start: first[0], end: first[1], data: { count, per_1000: units ? Math.round((count * 1000) / units) : 0 } });
167
+ });
168
+ return out;
169
+ }
170
+ // window: document
171
+ let total = 0;
172
+ let units = 0;
173
+ let first = null;
174
+ doc.blocks.forEach((b, bi) => {
175
+ if (p.kinds && !p.kinds.includes(b.kind))
176
+ return;
177
+ const r = countBlock(b, bi);
178
+ total += r.count;
179
+ units += r.units;
180
+ if (!first && r.first)
181
+ first = { blockIndex: bi, start: r.first[0], end: r.first[1] };
182
+ });
183
+ if (unit === "match")
184
+ units = doc.eligibleWords;
185
+ if (total < p.min_count || !first)
186
+ return [];
187
+ if (p.min_words && doc.eligibleWords < p.min_words)
188
+ return [];
189
+ const per1000 = units > 0 ? (total * 1000) / units : 0;
190
+ if (p.per_1000 !== undefined && per1000 < p.per_1000)
191
+ return [];
192
+ if (p.ratio !== undefined && units > 0 && total / units < p.ratio)
193
+ return [];
194
+ const f = first;
195
+ f.data = { count: total, per_1000: Math.round(per1000), ratio: units ? Math.round((100 * total) / units) : 0 };
196
+ return [f];
197
+ };
198
+ /* ------------------------------------------------------------------ */
199
+ /* repetition: inicios de frase/párrafo repetidos o n-gramas repetidos. */
200
+ /* params: { unit: "sentence_start"|"block_start"|"ngram", n: number, min_repeats: number, consecutive?: boolean } */
201
+ /* ------------------------------------------------------------------ */
202
+ const repetition = (rule, doc) => {
203
+ const p = rule.params;
204
+ const out = [];
205
+ const tb = tokensByBlock(doc);
206
+ if (p.unit === "ngram") {
207
+ const seen = new Map();
208
+ // Recorrido lineal: frases y tokens están ordenados por bloque y posición.
209
+ let si = 0;
210
+ doc.blocks.forEach((_b, bi) => {
211
+ const words = tb[bi].map((i) => doc.tokens[i]).filter((t) => t.isWord);
212
+ let wi = 0;
213
+ while (si < doc.sentences.length && doc.sentences[si].blockIndex < bi)
214
+ si++;
215
+ for (; si < doc.sentences.length && doc.sentences[si].blockIndex === bi; si++) {
216
+ const s = doc.sentences[si];
217
+ while (wi < words.length && words[wi].start < s.start)
218
+ wi++;
219
+ const from = wi;
220
+ while (wi < words.length && words[wi].end <= s.end)
221
+ wi++;
222
+ for (let i = from; i + p.n <= wi; i++) {
223
+ let stops = 0;
224
+ let key = "";
225
+ for (let k = 0; k < p.n; k++) {
226
+ const t = words[i + k];
227
+ if (STOPWORDS.has(t.lower))
228
+ stops++;
229
+ key += (k ? " " : "") + t.lower;
230
+ }
231
+ if (stops >= p.n - 1)
232
+ continue; // al menos dos palabras plenas
233
+ if (STOPWORDS.has(words[i].lower) && STOPWORDS.has(words[i + p.n - 1].lower))
234
+ continue;
235
+ let arr = seen.get(key);
236
+ if (!arr)
237
+ seen.set(key, (arr = []));
238
+ arr.push({ blockIndex: bi, start: words[i].start, end: words[i + p.n - 1].end });
239
+ }
240
+ }
241
+ });
242
+ for (const [key, arr] of seen) {
243
+ if (arr.length < p.min_repeats)
244
+ continue;
245
+ // Evitar solapamientos con n-gramas más largos ya notificados: nos quedamos con la primera ocurrencia.
246
+ const a = arr[0];
247
+ out.push({ blockIndex: a.blockIndex, start: a.start, end: a.end, data: { term: key, count: arr.length } });
248
+ }
249
+ return dedupeOverlaps(out);
250
+ }
251
+ // sentence_start / block_start
252
+ const units = [];
253
+ if (p.unit === "sentence_start") {
254
+ for (const s of doc.sentences) {
255
+ const words = (s.text.match(/[\p{L}\p{M}\p{N}]+/gu) ?? []).slice(0, p.n).map((w) => w.toLowerCase());
256
+ if (words.length < p.n)
257
+ continue;
258
+ units.push({ blockIndex: s.blockIndex, start: s.start, end: s.end, key: words.join(" ") });
259
+ }
260
+ }
261
+ else {
262
+ doc.blocks.forEach((b, bi) => {
263
+ const words = (b.text.match(/[\p{L}\p{M}\p{N}]+/gu) ?? []).slice(0, p.n).map((w) => w.toLowerCase());
264
+ if (words.length < p.n)
265
+ return;
266
+ units.push({ blockIndex: bi, start: 0, end: Math.min(b.text.length, 80), key: words.join(" ") });
267
+ });
268
+ }
269
+ if (p.consecutive !== false) {
270
+ let run = [];
271
+ const flush = () => {
272
+ if (run.length >= p.min_repeats) {
273
+ const u = run[0];
274
+ out.push({ blockIndex: u.blockIndex, start: u.start, end: u.end, data: { term: u.key, count: run.length } });
275
+ }
276
+ run = [];
277
+ };
278
+ for (const u of units) {
279
+ if (run.length && run[run.length - 1].key === u.key)
280
+ run.push(u);
281
+ else {
282
+ flush();
283
+ run = [u];
284
+ }
285
+ }
286
+ flush();
287
+ }
288
+ else {
289
+ const groups = new Map();
290
+ for (const u of units)
291
+ groups.set(u.key, [...(groups.get(u.key) ?? []), u]);
292
+ for (const [key, g] of groups) {
293
+ if (g.length >= p.min_repeats && !key.split(" ").every((w) => STOPWORDS.has(w))) {
294
+ const u = g[0];
295
+ out.push({ blockIndex: u.blockIndex, start: u.start, end: u.end, data: { term: key, count: g.length } });
296
+ }
297
+ }
298
+ }
299
+ return out;
300
+ };
301
+ function dedupeOverlaps(ms) {
302
+ ms.sort((a, b) => a.blockIndex - b.blockIndex || a.start - b.start || b.end - a.end);
303
+ const out = [];
304
+ for (const m of ms) {
305
+ const last = out[out.length - 1];
306
+ if (last && last.blockIndex === m.blockIndex && m.start < last.end)
307
+ continue;
308
+ out.push(m);
309
+ }
310
+ return out;
311
+ }
312
+ /* ------------------------------------------------------------------ */
313
+ /* structure: comprobaciones estructurales con nombre. */
314
+ /* params: { kind: "triad"|"heading_colon"|"heading_title_case"|"sentence_length_cv"|"list_bold_lead"|"list_uniform_start"|"paragraph_bold_lead"|"sentence_length_alternation", ... } */
315
+ /* ------------------------------------------------------------------ */
316
+ const structure = (rule, doc) => {
317
+ const p = rule.params;
318
+ const out = [];
319
+ switch (p.kind) {
320
+ case "triad": {
321
+ // "X, Y y Z" con tres palabras plenas de la misma clase aparente (misma terminación o todas no-stopword).
322
+ const minPer = p.min_per_document ?? 1;
323
+ const found = [];
324
+ doc.blocks.forEach((b, bi) => {
325
+ for (const m of triadCandidates(b.text)) {
326
+ const ws = m.words;
327
+ if (ws.some((w) => STOPWORDS.has(w.toLowerCase())))
328
+ continue;
329
+ if (ws.some((w) => /^\p{Lu}/u.test(w)))
330
+ continue; // nombres propios
331
+ if (!sameClass(ws))
332
+ continue;
333
+ // La regla describe nombres o adjetivos: los infinitivos forman enumeraciones funcionales legítimas.
334
+ if (ws.every((w) => /(?:ar|er|ir)(?:lo|la|los|las|le|les|se|me|te|nos|os)?$/i.test(w)))
335
+ continue;
336
+ // No extraer una falsa tríada de los tres últimos miembros de una enumeración más larga.
337
+ const before = b.text.slice(0, m.index);
338
+ if (/\p{L}{3,},\s*$/u.test(before))
339
+ continue;
340
+ if (isExcepted(rule, b.text, m.index, m.index + m.length))
341
+ continue;
342
+ found.push({ blockIndex: bi, start: m.index, end: m.index + m.length, data: { term: b.text.slice(m.index, m.index + m.length) } });
343
+ }
344
+ });
345
+ if (found.length >= minPer)
346
+ out.push(...found);
347
+ return out;
348
+ }
349
+ case "heading_colon": {
350
+ doc.blocks.forEach((b, bi) => {
351
+ if (b.kind !== "heading")
352
+ return;
353
+ const m = /^[^:]{3,80}:\s+\S/u.exec(b.text);
354
+ if (m && !/https?:/i.test(b.text))
355
+ out.push({ blockIndex: bi, start: 0, end: b.text.length, data: { term: b.text } });
356
+ });
357
+ return out;
358
+ }
359
+ case "heading_title_case": {
360
+ const minWords = p.min_words ?? 4;
361
+ doc.blocks.forEach((b, bi) => {
362
+ if (b.kind !== "heading")
363
+ return;
364
+ const words = b.text.match(/[\p{L}\p{M}]+/gu) ?? [];
365
+ if (words.length < minWords)
366
+ return;
367
+ const content = words.filter((w) => !STOPWORDS.has(w.toLowerCase()));
368
+ if (content.length < minWords)
369
+ return;
370
+ const caps = content.filter((w) => /^\p{Lu}/u.test(w));
371
+ // Todo en mayúsculas (siglas) no cuenta.
372
+ if (content.every((w) => w === w.toUpperCase()))
373
+ return;
374
+ if (caps.length === content.length)
375
+ out.push({ blockIndex: bi, start: 0, end: b.text.length, data: { term: b.text } });
376
+ });
377
+ return out;
378
+ }
379
+ case "sentence_length_cv": {
380
+ const minSentences = p.min_sentences ?? 10;
381
+ const maxCv = p.max_cv ?? 0.3;
382
+ const lens = doc.sentences
383
+ .filter((s) => doc.blocks[s.blockIndex].kind !== "heading")
384
+ .map((s) => (s.text.match(/[\p{L}\p{M}\p{N}]+/gu) ?? []).length)
385
+ .filter((n) => n >= 3);
386
+ if (lens.length < minSentences)
387
+ return out;
388
+ const mean = lens.reduce((a, b) => a + b, 0) / lens.length;
389
+ const sd = Math.sqrt(lens.reduce((a, b) => a + (b - mean) ** 2, 0) / lens.length);
390
+ const cv = mean > 0 ? sd / mean : 0;
391
+ if (cv < maxCv) {
392
+ const s = doc.sentences[0];
393
+ out.push({ blockIndex: s.blockIndex, start: s.start, end: s.end, data: { cv: Math.round(cv * 100) / 100, count: lens.length } });
394
+ }
395
+ return out;
396
+ }
397
+ case "list_bold_lead": {
398
+ const min = p.min_consecutive ?? 3;
399
+ let run = [];
400
+ const flush = () => {
401
+ if (run.length >= min) {
402
+ const bi = run[0];
403
+ out.push({ blockIndex: bi, start: 0, end: doc.blocks[bi].text.length, data: { count: run.length } });
404
+ }
405
+ run = [];
406
+ };
407
+ doc.blocks.forEach((b, bi) => {
408
+ if (b.kind === "list_item" && b.startsWithStrong)
409
+ run.push(bi);
410
+ else if (b.kind === "list_item")
411
+ flush();
412
+ else
413
+ flush();
414
+ });
415
+ flush();
416
+ return out;
417
+ }
418
+ case "sentence_length_alternation": {
419
+ // Proporción de frases en las que la longitud cambia de signo respecto a la anterior.
420
+ // Cerca de 1 = larga, corta, larga, corta: cadencia de plantilla, no de persona.
421
+ const minSentences = p.min_sentences ?? 8;
422
+ const minRate = p.min_alternation ?? 0.85;
423
+ const minCv = p.min_cv ?? 0;
424
+ const lens = doc.sentences
425
+ .filter((s) => doc.blocks[s.blockIndex].kind !== "heading")
426
+ .map((s) => (s.text.match(/[\p{L}\p{M}\p{N}]+/gu) ?? []).length)
427
+ .filter((n) => n >= 3);
428
+ if (lens.length < minSentences)
429
+ return out;
430
+ // Un texto plano oscila ±1 palabra y alternaría al 100 % sin ser un metrónomo:
431
+ // se exige que además las frases varíen de verdad (mismo coeficiente que ritmo-plano).
432
+ if (minCv > 0) {
433
+ const mean = lens.reduce((a, b) => a + b, 0) / lens.length;
434
+ const cv = mean === 0 ? 0 : Math.sqrt(lens.reduce((a, b) => a + (b - mean) ** 2, 0) / lens.length) / mean;
435
+ if (cv < minCv)
436
+ return out;
437
+ }
438
+ let changes = 0;
439
+ let pairs = 0;
440
+ for (let i = 1; i < lens.length - 1; i++) {
441
+ const a = Math.sign(lens[i] - lens[i - 1]);
442
+ const b = Math.sign(lens[i + 1] - lens[i]);
443
+ if (a === 0 || b === 0)
444
+ continue;
445
+ pairs++;
446
+ if (a !== b)
447
+ changes++;
448
+ }
449
+ if (pairs === 0)
450
+ return out;
451
+ const rate = changes / pairs;
452
+ if (rate >= minRate) {
453
+ const s = doc.sentences[0];
454
+ out.push({ blockIndex: s.blockIndex, start: s.start, end: s.end, data: { rate: Math.round(rate * 100), count: lens.length } });
455
+ }
456
+ return out;
457
+ }
458
+ case "paragraph_bold_lead": {
459
+ // Párrafos que abren con negrita a modo de titular. Un hallazgo por documento, en el primero.
460
+ const min = p.min_count ?? 2;
461
+ const hits = [];
462
+ doc.blocks.forEach((b, bi) => {
463
+ if (b.kind === "paragraph" && b.startsWithStrong)
464
+ hits.push(bi);
465
+ });
466
+ if (hits.length >= min) {
467
+ const bi = hits[0];
468
+ out.push({ blockIndex: bi, start: 0, end: doc.blocks[bi].text.length, data: { count: hits.length } });
469
+ }
470
+ return out;
471
+ }
472
+ case "list_uniform_start": {
473
+ const min = p.min_consecutive ?? 4;
474
+ let run = [];
475
+ const flush = () => {
476
+ if (run.length >= min && !STOPWORDS.has(run[0].key)) {
477
+ const bi = run[0].bi;
478
+ out.push({ blockIndex: bi, start: 0, end: doc.blocks[bi].text.length, data: { count: run.length, term: run[0].key } });
479
+ }
480
+ run = [];
481
+ };
482
+ doc.blocks.forEach((b, bi) => {
483
+ if (b.kind !== "list_item")
484
+ return flush();
485
+ const w = /^[\p{L}\p{M}]+/u.exec(b.text)?.[0]?.toLowerCase();
486
+ if (!w)
487
+ return flush();
488
+ if (run.length && run[run.length - 1].key !== w)
489
+ flush();
490
+ run.push({ bi, key: w });
491
+ });
492
+ flush();
493
+ return out;
494
+ }
495
+ default:
496
+ return out;
497
+ }
498
+ };
499
+ const TRIAD_TAIL = /,\s+([\p{L}\p{M}]{4,})\s+(?:y|e|o|u)\s+([\p{L}\p{M}]{4,})(?![\p{L}\p{M}])/uy;
500
+ const LETTER = /[\p{L}\p{M}]/u;
501
+ /**
502
+ * Candidatas a tríada, «X, Y y Z» con tres palabras de cuatro letras o más. Da lo mismo que recorrer el texto con
503
+ * /(?<![\p{L}\p{M}])([\p{L}\p{M}]{4,}),\s+([\p{L}\p{M}]{4,})\s+(?:y|e|o|u)\s+([\p{L}\p{M}]{4,})(?![\p{L}\p{M}])/gu,
504
+ * pero solo mira alrededor de las comas, que es mucho más barato en textos largos.
505
+ */
506
+ function triadCandidates(text) {
507
+ const out = [];
508
+ let lastEnd = 0;
509
+ let c = -1;
510
+ while ((c = text.indexOf(",", c + 1)) !== -1) {
511
+ TRIAD_TAIL.lastIndex = c;
512
+ const m = TRIAD_TAIL.exec(text);
513
+ if (!m)
514
+ continue;
515
+ // Inicio de la palabra que precede a la coma, retrocediendo por puntos de código.
516
+ let s = c;
517
+ while (s > 0) {
518
+ let k = s - 1;
519
+ const lo = text.charCodeAt(k);
520
+ if (lo >= 0xdc00 && lo <= 0xdfff && k > 0) {
521
+ const hi = text.charCodeAt(k - 1);
522
+ if (hi >= 0xd800 && hi <= 0xdbff)
523
+ k--;
524
+ }
525
+ if (!LETTER.test(text.slice(k, s)))
526
+ break;
527
+ s = k;
528
+ }
529
+ if (s < lastEnd)
530
+ continue; // esa palabra ya cerraba la candidata anterior
531
+ const first = text.slice(s, c);
532
+ if ([...first].length < 4)
533
+ continue;
534
+ const end = c + m[0].length;
535
+ out.push({ index: s, length: end - s, words: [first, m[1], m[2]] });
536
+ lastEnd = end;
537
+ c = end - 1;
538
+ }
539
+ return out;
540
+ }
541
+ function sameClass(ws) {
542
+ const suf = (w) => {
543
+ const l = w.toLowerCase();
544
+ if (/(mente)$/.test(l))
545
+ return "mente";
546
+ if (/(ción|sión|dad|tad|eza|ura|ismo|anza|encia|ancia)$/.test(l))
547
+ return "nombre";
548
+ if (/(ar|er|ir)$/.test(l))
549
+ return "verbo";
550
+ if (/(ndo)$/.test(l))
551
+ return "gerundio";
552
+ if (/(oso|osa|osos|osas|ivo|iva|ivos|ivas|able|ables|ible|ibles|al|ales|ante|antes|ente|entes|ado|ada|ados|adas|ido|ida|idos|idas|ico|ica|icos|icas|dor|dora|dores|doras|ar|ares)$/.test(l))
553
+ return "adj";
554
+ return "otro";
555
+ };
556
+ const s = ws.map(suf);
557
+ if (s[0] === s[1] && s[1] === s[2] && s[0] !== "otro")
558
+ return true;
559
+ // Misma terminación vocálica de género (claro, conciso y directo).
560
+ const last = ws.map((w) => w.toLowerCase().slice(-1));
561
+ return ws.every((w) => w.length >= 5) && (last.every((c) => c === "o") || last.every((c) => c === "a"));
562
+ }
563
+ /* ------------------------------------------------------------------ */
564
+ /* cooccurrence: dos (o más) grupos presentes en la misma ventana. */
565
+ /* params: { groups: string[][], window: "sentence"|"block"|"document", ordered?: boolean } */
566
+ /* ------------------------------------------------------------------ */
567
+ const cooccurrence = (rule, doc) => {
568
+ const p = rule.params;
569
+ const res = p.groups.map((g) => buildTermRegExp(g));
570
+ if (res.some((r) => r === null))
571
+ return [];
572
+ const out = [];
573
+ const findIn = (text, from, to, re) => {
574
+ for (const m of execAll(re, text)) {
575
+ if (m.index >= from && m.index + m.length <= to) {
576
+ if (p.anchor === "block_start" && m.index !== from)
577
+ continue;
578
+ return m;
579
+ }
580
+ }
581
+ return null;
582
+ };
583
+ if (p.window === "document") {
584
+ const hits = [];
585
+ for (const re of res) {
586
+ let h = null;
587
+ for (let bi = 0; bi < doc.blocks.length && !h; bi++) {
588
+ const b = doc.blocks[bi];
589
+ const m = findIn(b.text, 0, b.text.length, re);
590
+ if (m)
591
+ h = { blockIndex: bi, start: m.index, end: m.index + m.length };
592
+ }
593
+ if (!h)
594
+ return [];
595
+ hits.push(h);
596
+ }
597
+ if (p.ordered) {
598
+ for (let i = 1; i < hits.length; i++) {
599
+ const a = hits[i - 1];
600
+ const b = hits[i];
601
+ if (b.blockIndex < a.blockIndex || (b.blockIndex === a.blockIndex && b.start < a.start))
602
+ return [];
603
+ }
604
+ }
605
+ const h = hits[0];
606
+ return [{ blockIndex: h.blockIndex, start: h.start, end: h.end, data: { count: hits.length } }];
607
+ }
608
+ const windows = p.window === "sentence"
609
+ ? doc.sentences.map((s) => ({ bi: s.blockIndex, from: s.start, to: s.end }))
610
+ : doc.blocks.map((b, bi) => ({ bi, from: 0, to: b.text.length }));
611
+ for (const w of windows) {
612
+ const b = doc.blocks[w.bi];
613
+ let cursor = w.from;
614
+ let first = null;
615
+ let last = 0;
616
+ let ok = true;
617
+ for (const re of res) {
618
+ const m = findIn(b.text, p.ordered ? cursor : w.from, w.to, re);
619
+ if (!m) {
620
+ ok = false;
621
+ break;
622
+ }
623
+ if (!first)
624
+ first = m;
625
+ last = Math.max(last, m.index + m.length);
626
+ cursor = m.index + m.length;
627
+ }
628
+ if (ok && first) {
629
+ if (isExcepted(rule, b.text, first.index, last))
630
+ continue;
631
+ out.push({ blockIndex: w.bi, start: first.index, end: last, data: { term: b.text.slice(first.index, last) } });
632
+ }
633
+ }
634
+ return out;
635
+ };
636
+ export const DETECTORS = { regex, lexicon, sequence, density, repetition, structure, cooccurrence };
@@ -0,0 +1,30 @@
1
+ import type { CompiledRule, Document, TextBlock } from "../../contracts/index.js";
2
+ /** Coincidencia bruta dentro de un bloque (índices sobre `block.text`). */
3
+ export interface RawMatch {
4
+ blockIndex: number;
5
+ start: number;
6
+ end: number;
7
+ /** Datos para interpolar en el mensaje: {count}, {ratio}, {term}… */
8
+ data?: Record<string, string | number>;
9
+ }
10
+ export type Detector = (rule: CompiledRule, doc: Document) => RawMatch[];
11
+ /** Palabras funcionales frecuentes; se excluyen de tríadas y n-gramas. */
12
+ export declare const STOPWORDS: Set<string>;
13
+ /** Escapa texto para usarlo literal en una RegExp. */
14
+ export declare function escapeRe(s: string): string;
15
+ /**
16
+ * Construye una única RegExp a partir de una lista de términos o expresiones.
17
+ * Los términos literales admiten espacios (se convierten en `\s+`) y se delimitan por límites de palabra Unicode.
18
+ * Los términos que empiezan por `re:` se insertan como expresión regular (ya validada por el compilador).
19
+ */
20
+ export declare function buildTermRegExp(terms: string[], flags?: string): RegExp | null;
21
+ export declare function wordsIn(block: TextBlock, doc: Document, blockIndex: number): number;
22
+ /** Devuelve los índices de tokens por bloque (cache barata por documento). */
23
+ export declare function tokensByBlock(doc: Document): number[][];
24
+ /** Comprueba si una coincidencia cae dentro de una excepción (patrón sobre una ventana de contexto). */
25
+ export declare function isExcepted(rule: CompiledRule, text: string, start: number, end: number): boolean;
26
+ export declare function execAll(re: RegExp, text: string): {
27
+ index: number;
28
+ length: number;
29
+ match: RegExpExecArray;
30
+ }[];