onionskin-cli 0.0.0-stage → 0.0.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.
- package/CHANGELOG.md +136 -0
- package/INSTALL.md +17 -0
- package/LICENSE +21 -0
- package/NOTICE +46 -0
- package/README.md +88 -2
- package/docs/contracts/batch.schema.json +839 -0
- package/docs/contracts/history.schema.json +126 -0
- package/docs/contracts/ops.schema.json +541 -0
- package/package.json +54 -4
- package/scripts/checker/check.py +411 -0
- package/scripts/checker/requirements.txt +5 -0
- package/scripts/install-check.mjs +9 -0
- package/scripts/requirements.txt +5 -0
- package/scripts/rewriter/apple/main.swift +135 -0
- package/scripts/rewriter/apple/onionskin-fm +0 -0
- package/scripts/rewriter/apple/onionskin-fm.json +6 -0
- package/skills/onionskin/SKILL.md +117 -0
- package/skills/onionskin/reference/batch.md +94 -0
- package/skills/onionskin/reference/formats.md +15 -0
- package/skills/onionskin/reference/ops.md +253 -0
- package/skills/onionskin/reference/troubleshooting.md +73 -0
- package/skills/onionskin/reference/voice-guide.md +86 -0
- package/skills/onionskin/voice.example.md +51 -0
- package/src/agent-detect.js +149 -0
- package/src/agent-events.js +159 -0
- package/src/agent-prompt.js +27 -0
- package/src/agent-runner.js +593 -0
- package/src/analyze.js +17 -0
- package/src/anchors.js +190 -0
- package/src/apply.js +387 -0
- package/src/auth.js +130 -0
- package/src/batch.js +322 -0
- package/src/check-worker.js +8 -0
- package/src/checker.js +293 -0
- package/src/cli.js +516 -0
- package/src/constants.js +20 -0
- package/src/diff.js +267 -0
- package/src/doctor.js +247 -0
- package/src/docx.js +438 -0
- package/src/fmt.js +30 -0
- package/src/fsutil.js +348 -0
- package/src/history-worker.js +16 -0
- package/src/history.js +576 -0
- package/src/lease.js +79 -0
- package/src/local.js +242 -0
- package/src/merge-pptx.js +198 -0
- package/src/merge-xlsx.js +453 -0
- package/src/merge.js +410 -0
- package/src/offload.js +34 -0
- package/src/ops/common.js +48 -0
- package/src/ops/docx-table.js +323 -0
- package/src/ops/docx.js +503 -0
- package/src/ops/engine.js +41 -0
- package/src/ops/pptx.js +336 -0
- package/src/ops/run.js +115 -0
- package/src/ops/runs.js +127 -0
- package/src/ops/schema.js +109 -0
- package/src/ops/xlsx.js +277 -0
- package/src/package.js +189 -0
- package/src/paths.js +64 -0
- package/src/person.js +66 -0
- package/src/pptx.js +194 -0
- package/src/question.js +47 -0
- package/src/quick-plan.js +107 -0
- package/src/quick.js +164 -0
- package/src/redline-check.js +251 -0
- package/src/redline-worker.js +29 -0
- package/src/redline.js +438 -0
- package/src/reply.js +410 -0
- package/src/review.js +274 -0
- package/src/rewrite-check.js +1014 -0
- package/src/rewriter-cleanup.js +105 -0
- package/src/rewriter-setup.js +104 -0
- package/src/rewriter.js +396 -0
- package/src/sanitize.js +42 -0
- package/src/sentence-span.js +25 -0
- package/src/server-entry.js +13 -0
- package/src/server.js +576 -0
- package/src/session.js +1910 -0
- package/src/settings.js +161 -0
- package/src/setup.js +160 -0
- package/src/stamp-words.js +10 -0
- package/src/stamps.js +59 -0
- package/src/style.js +110 -0
- package/src/suggest-reply.js +84 -0
- package/src/suggest.js +261 -0
- package/src/text.js +31 -0
- package/src/tracked-words.js +43 -0
- package/src/voice-guide.js +182 -0
- package/src/voice-learn.js +314 -0
- package/src/voice-sample-worker.js +95 -0
- package/src/voice-samples.js +329 -0
- package/src/voice-store.js +305 -0
- package/src/voice-worker.js +28 -0
- package/src/voice.js +297 -0
- package/src/xlsx.js +454 -0
- package/src/xml-tree.js +118 -0
- package/src/xml.js +127 -0
- package/src/zip-write.js +74 -0
- package/src/zip.js +88 -0
- package/vendor/engine/DocViewer-BZLXUcxJ.js +15137 -0
- package/vendor/engine/THIRD_PARTY_LICENSES.txt +79 -0
- package/vendor/engine/collab-BVynXMqc.js +2591 -0
- package/vendor/engine/collab.js +2 -0
- package/vendor/engine/core.js +4 -0
- package/vendor/engine/default-worker-DogNT09A.js +35625 -0
- package/vendor/engine/embed.js +107 -0
- package/vendor/engine/index.js +5 -0
- package/vendor/engine/office-engine.embed.js +184 -0
- package/vendor/engine/office-engine.worker.js +36030 -0
- package/vendor/engine/xlsx-ops-SYAv6V0f.js +939 -0
- package/vendor/engine-node/LICENSE +21 -0
- package/vendor/engine-node/engine-node.mjs +46618 -0
- package/vendor/engine.lock.json +26 -0
- package/web/dist/assets/index-CFISATkn.js +197 -0
- package/web/dist/assets/index-spXE4oTz.css +1 -0
- package/web/dist/index.html +14 -0
package/src/voice.js
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
// Learning on top of Writing preferences, stage 1: code only. Pure functions; nothing here reads or writes a file.
|
|
2
|
+
//
|
|
3
|
+
// What is learned is a suggestion, never a preference: `suggestions()` returns fixed trait ids from src/style.js and integers, and the
|
|
4
|
+
// person turns one on with a tap. No text from a document, an agent or an edit ever leaves this module except through `exampleOf()`,
|
|
5
|
+
// whose two sides are untrusted document-derived text for the person to look at (and, only if they tick it, export).
|
|
6
|
+
//
|
|
7
|
+
// Inputs are parsed paragraphs (readDocxParagraphs in src/docx.js) of three DOCX versions linked through the agent's output:
|
|
8
|
+
// pre (before the agent ran), post (what the agent wrote) and mine (what the person sent next).
|
|
9
|
+
import { docxFmt, matchParagraphs } from "./history.js";
|
|
10
|
+
import { tokenize } from "./diff.js";
|
|
11
|
+
import { fmtNote } from "./fmt.js";
|
|
12
|
+
import { normText, similarity, textHash } from "./text.js";
|
|
13
|
+
import { untrusted } from "./sanitize.js";
|
|
14
|
+
import { TRAITS } from "./style.js";
|
|
15
|
+
|
|
16
|
+
export const MAX_PAIRS = 50; // edit pairs taken from one Send
|
|
17
|
+
export const WINDOW = 8; // the last pairs the rules look at
|
|
18
|
+
export const HIT_WINDOW = 40; // the last comment hits the rules look at
|
|
19
|
+
export const LATER_AFTER = 4; // "Not now" holds until this many new pairs
|
|
20
|
+
export const MAX_SUGGESTIONS = 3;
|
|
21
|
+
export const EXAMPLE_CAP = 400; // characters per side of an example; longer is refused, never cut
|
|
22
|
+
/** The per-pair signals the evidence store keeps (booleans only). */
|
|
23
|
+
export const SIGNALS = ["short", "sentences", "exclaim", "restored", "fmt"];
|
|
24
|
+
/** Where a suggestion came from. */
|
|
25
|
+
export const SOURCES = ["edits", "comments"];
|
|
26
|
+
/** Traits a comment phrase can point at. */
|
|
27
|
+
export const COMMENT_TRAITS = ["relaxed", "short", "plain", "formal", "minimal"];
|
|
28
|
+
|
|
29
|
+
const TRAIT_IDS = TRAITS.map((t) => t.id);
|
|
30
|
+
const IS_WORD = /^[\p{L}\p{N}\p{M}]/u;
|
|
31
|
+
const words = (s) => tokenize(normText(s).toLowerCase()).filter((t) => IS_WORD.test(t));
|
|
32
|
+
const SENTENCE = /[^.!?。!?]*(?:[.!?。!?]+|$)/gu;
|
|
33
|
+
const sentenceWords = (s) => (normText(s).match(SENTENCE) || []).map((x) => words(x).length).filter((n) => n > 0);
|
|
34
|
+
const EXCLAIM = /[!!]/u;
|
|
35
|
+
|
|
36
|
+
const agentChanged = (a, b) => !a || a.hash !== b.hash || !!fmtNote(docxFmt(a), docxFmt(b));
|
|
37
|
+
const personChanged = (b, c) => !c || b.hash !== c.hash || !!fmtNote(docxFmt(b), docxFmt(c));
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The one-to-one rule for a post paragraph that `out` (post -> other) leaves unlinked. Unlinked post paragraphs are grouped by their linked
|
|
41
|
+
* neighbor before; the other side's unlinked, non-empty paragraphs between the same two linked neighbors belong to that gap. Returns a
|
|
42
|
+
* function of j: the other side's index when the gap is exactly one (non-empty) post paragraph for one other paragraph, -1 when the gap has
|
|
43
|
+
* no other paragraph, undefined when it cannot be told (two for one, neighbors out of order, a paragraph two gaps could claim).
|
|
44
|
+
*/
|
|
45
|
+
function oneToOne(post, out, back, other) {
|
|
46
|
+
const gapOf = new Int32Array(post.length);
|
|
47
|
+
const gaps = new Map(); // prev linked post index -> {post: [j], other: [k]}
|
|
48
|
+
let prev = -1;
|
|
49
|
+
for (let j = 0; j < post.length; j++) {
|
|
50
|
+
if (out[j] >= 0) prev = j;
|
|
51
|
+
else {
|
|
52
|
+
gapOf[j] = prev;
|
|
53
|
+
(gaps.get(prev) || gaps.set(prev, { post: [], other: [] }).get(prev)).post.push(j);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
for (const g of gaps.values()) {
|
|
57
|
+
const before = g.post[0] - 1;
|
|
58
|
+
let after = g.post[g.post.length - 1] + 1;
|
|
59
|
+
while (after < post.length && out[after] < 0) after++;
|
|
60
|
+
const lo = before >= 0 ? out[before] : -1;
|
|
61
|
+
const hi = after < post.length ? out[after] : other.length;
|
|
62
|
+
// a paragraph between the two linked neighbors, linked to nothing; neighbors out of order (a move) leave the gap ambiguous
|
|
63
|
+
if (hi < lo) g.ambiguous = true;
|
|
64
|
+
else for (let k = lo + 1; k < hi; k++) if (back[k] < 0 && normText(other[k].text)) g.other.push(k);
|
|
65
|
+
}
|
|
66
|
+
// a moved neighbor can make two gaps overlap: a paragraph two gaps could claim belongs to neither
|
|
67
|
+
const claims = new Map();
|
|
68
|
+
for (const g of gaps.values()) for (const k of g.other) claims.set(k, (claims.get(k) || 0) + 1);
|
|
69
|
+
for (const g of gaps.values()) if (g.other.some((k) => claims.get(k) > 1)) g.ambiguous = true;
|
|
70
|
+
return (j) => {
|
|
71
|
+
const g = gaps.get(gapOf[j]);
|
|
72
|
+
if (!g || g.ambiguous) return undefined;
|
|
73
|
+
const postSide = g.post.filter((x) => normText(post[x].text));
|
|
74
|
+
if (g.other.length === 0) return -1;
|
|
75
|
+
if (g.other.length === 1 && postSide.length === 1 && postSide[0] === j) return g.other[0];
|
|
76
|
+
return undefined;
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Pairs of the agent's text and the person's next version of it. A post paragraph counts when the agent added or changed it (text or
|
|
82
|
+
* formatting) against pre AND the person then changed or deleted it. Returns [{pre, agent, mine}] (paragraph objects; `pre` null for a
|
|
83
|
+
* paragraph the agent added or whose old version cannot be told, `mine` null for one the person deleted), at most MAX_PAIRS, in document
|
|
84
|
+
* order.
|
|
85
|
+
*
|
|
86
|
+
* matchParagraphs stops linking two versions below a similarity of 0.3 to 0.6, so a heavy rewrite reads as one removed and one added.
|
|
87
|
+
* That case is paired here only when it is exactly one-to-one between the same two linked neighbors, on both sides: pre -> post gives the
|
|
88
|
+
* old version (anything else leaves `pre` null), post -> mine gives the person's version; a gap with no paragraph left on the person's
|
|
89
|
+
* side is a delete; anything else (two for one, three for two) is skipped, never guessed.
|
|
90
|
+
*/
|
|
91
|
+
export function editPairs(pre, post, mine) {
|
|
92
|
+
const m1 = matchParagraphs(pre, post);
|
|
93
|
+
const m2 = matchParagraphs(post, mine);
|
|
94
|
+
const oldOf = oneToOne(post, m1.mapB, m1.mapA, pre);
|
|
95
|
+
const mineOf = oneToOne(post, m2.mapA, m2.mapB, mine);
|
|
96
|
+
const out = [];
|
|
97
|
+
for (let j = 0; j < post.length && out.length < MAX_PAIRS; j++) {
|
|
98
|
+
const b = post[j];
|
|
99
|
+
let i = m1.mapB[j];
|
|
100
|
+
if (i < 0) i = oldOf(j) ?? -1;
|
|
101
|
+
const a = i >= 0 ? pre[i] : null;
|
|
102
|
+
if (!a && !normText(b.text)) continue; // an empty paragraph the agent added is not text
|
|
103
|
+
if (!agentChanged(a, b)) continue;
|
|
104
|
+
let k = m2.mapA[j];
|
|
105
|
+
if (k < 0) k = mineOf(j); // -1: the person deleted it
|
|
106
|
+
if (k === undefined) continue;
|
|
107
|
+
const c = k >= 0 ? mine[k] : null;
|
|
108
|
+
if (!personChanged(b, c)) continue;
|
|
109
|
+
out.push({ pre: a, agent: b, mine: c && normText(c.text) ? c : null }); // emptied reads as deleted
|
|
110
|
+
}
|
|
111
|
+
return out;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** {own, total}: the person's words (lowercased, counted as a multiset) and how many of them are not in the agent's version. */
|
|
115
|
+
function ownCount(agent, mine) {
|
|
116
|
+
const m = words(mine);
|
|
117
|
+
const bag = new Map();
|
|
118
|
+
for (const w of words(agent)) bag.set(w, (bag.get(w) || 0) + 1);
|
|
119
|
+
let own = 0;
|
|
120
|
+
for (const w of m) {
|
|
121
|
+
const n = bag.get(w) || 0;
|
|
122
|
+
if (n) bag.set(w, n - 1);
|
|
123
|
+
else own++;
|
|
124
|
+
}
|
|
125
|
+
return { own, total: m.length };
|
|
126
|
+
}
|
|
127
|
+
/** Share of the person's words that are not in the agent's version: 0..1, 0 when the person wrote none. */
|
|
128
|
+
export function ownShare(agent, mine) {
|
|
129
|
+
const { own, total } = ownCount(agent, mine);
|
|
130
|
+
return total ? own / total : 0;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** The five booleans kept for one pair. Every rule is language-neutral: word and sentence counts, "!", hashes, formatting. */
|
|
134
|
+
export function pairSignals(p) {
|
|
135
|
+
const agent = p.agent.text;
|
|
136
|
+
const mine = p.mine ? p.mine.text : "";
|
|
137
|
+
const wa = words(agent).length;
|
|
138
|
+
const wm = words(mine).length;
|
|
139
|
+
const short = wa > 0 && (!p.mine || wm * 100 <= wa * 85);
|
|
140
|
+
const sa = sentenceWords(agent);
|
|
141
|
+
const sm = p.mine ? sentenceWords(mine) : [];
|
|
142
|
+
// average(mine) <= 0.8 * average(agent), as integers: sum(m) * len(a) * 10 <= sum(a) * len(m) * 8
|
|
143
|
+
const sum = (x) => x.reduce((t, n) => t + n, 0);
|
|
144
|
+
const sentences = sa.length >= 2 && sm.length >= 2 && sum(sm) * sa.length * 10 <= sum(sa) * sm.length * 8;
|
|
145
|
+
const exclaim = !!p.mine && EXCLAIM.test(agent) && !EXCLAIM.test(mine);
|
|
146
|
+
let restored = false;
|
|
147
|
+
if (p.pre && p.mine && p.pre.hash !== p.agent.hash) {
|
|
148
|
+
const sp = similarity(mine, p.pre.text);
|
|
149
|
+
restored = textHash(mine) === textHash(p.pre.text) || (sp >= 0.9 && sp >= similarity(agent, p.pre.text) + 0.1);
|
|
150
|
+
}
|
|
151
|
+
const fmt = !!(p.pre && p.mine) && !!fmtNote(docxFmt(p.pre), docxFmt(p.agent)) && !fmtNote(docxFmt(p.pre), docxFmt(p.mine));
|
|
152
|
+
return { short, sentences, exclaim, restored, fmt };
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// What untrusted() keeps but a reader cannot see: default-ignorable code points (tag characters, variation selectors, U+206A to U+206F,
|
|
156
|
+
// CGJ, soft hyphen, U+180E, the Hangul fillers), format characters, private-use, unassigned and lone surrogate code points.
|
|
157
|
+
const INVISIBLE = /[\p{Default_Ignorable_Code_Point}\p{Cf}\p{Co}\p{Cn}\p{Cs}]/u;
|
|
158
|
+
// A link, or text that common tools turn into one: a scheme, www., an email or a mention, an HTML entity, a domain with a path. Checked on
|
|
159
|
+
// the NFKC form, so full-width letters and punctuation count as their ASCII twins.
|
|
160
|
+
const LINK = /:\/\/|www\.|@|&#?[a-z0-9]+;|[\p{L}\p{N}-]+\.\p{L}{2,}\//iu;
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* One side of an example, sanitized with untrusted(), or null when it must be refused (never cleaned or cut): empty, over EXAMPLE_CAP
|
|
164
|
+
* characters, holding a character the person cannot see on the page (it could carry words they never read), or holding a link.
|
|
165
|
+
*/
|
|
166
|
+
export function exampleSide(text) {
|
|
167
|
+
const s = untrusted(text, Infinity);
|
|
168
|
+
if (!s || [...s].length > EXAMPLE_CAP || INVISIBLE.test(s) || LINK.test(s.normalize("NFKC"))) return null;
|
|
169
|
+
return s;
|
|
170
|
+
}
|
|
171
|
+
const mostlyOwn = (other, mine) => {
|
|
172
|
+
const { own, total } = ownCount(other, mine);
|
|
173
|
+
return own * 10 >= total * 8; // integers: exactly 0.8 passes
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
const RUN = 4; // words in a row that count as text taken from the document
|
|
177
|
+
// the runs of RUN words in a row; a side shorter than RUN is one run of its own
|
|
178
|
+
const runsOf = (w) => (w.length < RUN ? (w.length ? [w.join(" ")] : []) : Array.from({ length: w.length - RUN + 1 }, (_, i) => w.slice(i, i + RUN).join(" ")));
|
|
179
|
+
/**
|
|
180
|
+
* Every run of RUN words in a row (lowercased) that the given paragraphs hold, for exampleOf. In order, so a long paragraph full of common
|
|
181
|
+
* words does not count against the person's own sentence the way a bag of words would.
|
|
182
|
+
*/
|
|
183
|
+
export function documentRuns(paragraphs) {
|
|
184
|
+
const out = new Set();
|
|
185
|
+
for (const q of Array.isArray(paragraphs) ? paragraphs : []) for (const r of runsOf(words(untrusted(q?.text, Infinity)))) out.add(r);
|
|
186
|
+
return out;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* At least 80% of the person's words lie outside every run that `runs` holds. A side shorter than RUN words is refused when any run holds
|
|
190
|
+
* it. Ceiling: a short document paragraph inside a longer side, or document text with every fourth word changed, is not seen; the scan of
|
|
191
|
+
* all runs for a short side is linear in the document.
|
|
192
|
+
*/
|
|
193
|
+
function ownOfRuns(runs, mine) {
|
|
194
|
+
const m = words(mine);
|
|
195
|
+
if (m.length < RUN) {
|
|
196
|
+
const s = ` ${m.join(" ")} `;
|
|
197
|
+
for (const r of runs) if (` ${r} `.includes(s)) return false;
|
|
198
|
+
return true;
|
|
199
|
+
}
|
|
200
|
+
const taken = new Uint8Array(m.length);
|
|
201
|
+
runsOf(m).forEach((r, i) => {
|
|
202
|
+
if (runs.has(r)) taken.fill(1, i, i + RUN);
|
|
203
|
+
});
|
|
204
|
+
const t = taken.reduce((a, b) => a + b, 0);
|
|
205
|
+
return (m.length - t) * 10 >= m.length * 8;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* A before/after example for voice.md, or null. Only a rewrite that is mostly the person's own words: at least 80% of the person's words
|
|
210
|
+
* are not in the agent's version nor in the pre version (counted as a bag of words), and at least 80% lie outside every run of 4 words in a
|
|
211
|
+
* row that `runs` holds (documentRuns() of the pre and post documents), so document or agent text the person put back or moved is never
|
|
212
|
+
* shown as theirs; the person wrote 3 to 80 words and the agent at most 120; each side passes exampleSide() (at most 400 characters,
|
|
213
|
+
* refused never cut, nothing invisible, no link). Without `runs` (a Set) the document cannot be checked, so nothing is an example. Both
|
|
214
|
+
* sides are untrusted document-derived text.
|
|
215
|
+
*/
|
|
216
|
+
export function exampleOf(p, runs) {
|
|
217
|
+
if (!p.mine || !(runs instanceof Set)) return null;
|
|
218
|
+
const agent = exampleSide(p.agent.text);
|
|
219
|
+
const mine = exampleSide(p.mine.text);
|
|
220
|
+
if (agent == null || mine == null) return null;
|
|
221
|
+
const wm = words(mine).length;
|
|
222
|
+
if (wm < 3 || wm > 80 || words(agent).length > 120) return null;
|
|
223
|
+
if (!mostlyOwn(agent, mine)) return null;
|
|
224
|
+
if (p.pre && !mostlyOwn(untrusted(p.pre.text, Infinity), mine)) return null;
|
|
225
|
+
if (!ownOfRuns(runs, mine)) return null;
|
|
226
|
+
return { agent, mine };
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// English only, fixed, case-insensitive, whole words. The "less formal" family is matched first and blanked out, so it is never "formal".
|
|
230
|
+
const PHRASES = [
|
|
231
|
+
["relaxed", ["less formal", "more casual", "friendlier", "more relaxed"]],
|
|
232
|
+
["short", ["shorter", "shorten", "more concise", "too long", "trim"]],
|
|
233
|
+
["plain", ["simpler", "plainer", "plain words", "plain english", "less jargon"]],
|
|
234
|
+
["formal", ["more formal", "more professional"]],
|
|
235
|
+
["minimal", ["keep my wording", "don't rewrite", "do not rewrite", "only change"]],
|
|
236
|
+
];
|
|
237
|
+
const phraseRe = (ph) => new RegExp(`(?<![\\p{L}\\p{N}])${ph.replace(/[.*+?^${}()|[\]\\]/g, "\\$&").replace(/ /g, "\\s+")}(?![\\p{L}\\p{N}])`, "giu");
|
|
238
|
+
const PHRASE_RES = PHRASES.map(([trait, list]) => [trait, list.map(phraseRe)]);
|
|
239
|
+
|
|
240
|
+
/** Trait ids a comment the person typed asks for, each at most once, in COMMENT_TRAITS order. Only the ids are ever stored. */
|
|
241
|
+
export function commentHits(text) {
|
|
242
|
+
let s = normText(text).toLowerCase().replace(/[‘’ʼ]/g, "'");
|
|
243
|
+
const out = [];
|
|
244
|
+
for (const [trait, res] of PHRASE_RES) {
|
|
245
|
+
let hit = false;
|
|
246
|
+
for (const re of res) s = s.replace(re, () => ((hit = true), " "));
|
|
247
|
+
if (hit) out.push(trait);
|
|
248
|
+
}
|
|
249
|
+
return out;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const EDIT_RULES = [
|
|
253
|
+
// [trait, signal, at least, share of the window (percent, 0 = none)]
|
|
254
|
+
["short", "short", 4, 60],
|
|
255
|
+
["short_sentences", "sentences", 4, 60],
|
|
256
|
+
["no_exclaim", "exclaim", 3, 0],
|
|
257
|
+
["minimal", "restored", 3, 0],
|
|
258
|
+
["format", "fmt", 2, 0],
|
|
259
|
+
];
|
|
260
|
+
const COMMENT_MIN = 3;
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* What to offer the person: [{trait, n, m, source}], at most MAX_SUGGESTIONS, edits first. `trait` is a fixed id from src/style.js,
|
|
264
|
+
* `source` one of SOURCES, `n` and `m` integers (n of the last m edits, or n of the last m comment hits). `evidence` is the stored file
|
|
265
|
+
* and is not trusted: only `=== true` signals count, unknown hit traits are dropped, numbers are recounted here. Nothing is offered that
|
|
266
|
+
* is already on, was answered "never", or was answered "later" (the pair count at the time) fewer than LATER_AFTER new pairs ago.
|
|
267
|
+
*/
|
|
268
|
+
export function suggestions(evidence, style) {
|
|
269
|
+
const ev = evidence && typeof evidence === "object" ? evidence : {};
|
|
270
|
+
const pairs = Array.isArray(ev.pairs) ? ev.pairs.filter((p) => p && typeof p === "object") : [];
|
|
271
|
+
const hits = Array.isArray(ev.hits) ? ev.hits.filter((h) => h && typeof h === "object" && COMMENT_TRAITS.includes(h.trait)) : [];
|
|
272
|
+
const answers = ev.answers && typeof ev.answers === "object" && !Array.isArray(ev.answers) ? ev.answers : {};
|
|
273
|
+
const on = new Set(Array.isArray(style?.traits) ? style.traits : []);
|
|
274
|
+
const held = (trait) => {
|
|
275
|
+
const a = Object.hasOwn(answers, trait) ? answers[trait] : null;
|
|
276
|
+
if (!a || typeof a !== "object") return false;
|
|
277
|
+
if (a.never === true) return true;
|
|
278
|
+
return Number.isInteger(a.later) && pairs.length - a.later < LATER_AFTER;
|
|
279
|
+
};
|
|
280
|
+
const found = [];
|
|
281
|
+
const win = pairs.slice(-WINDOW);
|
|
282
|
+
for (const [trait, signal, min, share] of EDIT_RULES) {
|
|
283
|
+
const n = win.filter((p) => p[signal] === true).length;
|
|
284
|
+
if (n >= min && n * 100 >= share * win.length) found.push({ trait, n, m: win.length, source: "edits" });
|
|
285
|
+
}
|
|
286
|
+
const hwin = hits.slice(-HIT_WINDOW);
|
|
287
|
+
for (const trait of COMMENT_TRAITS) {
|
|
288
|
+
const n = hwin.filter((h) => h.trait === trait).length;
|
|
289
|
+
if (n >= COMMENT_MIN && !found.some((f) => f.trait === trait)) found.push({ trait, n, m: hwin.length, source: "comments" });
|
|
290
|
+
}
|
|
291
|
+
// asked for both a formal and a relaxed tone: the evidence disagrees, so neither is offered
|
|
292
|
+
const both = found.some((f) => f.trait === "formal") && found.some((f) => f.trait === "relaxed");
|
|
293
|
+
return found
|
|
294
|
+
.filter((f) => TRAIT_IDS.includes(f.trait) && !on.has(f.trait) && !held(f.trait) && !(both && (f.trait === "formal" || f.trait === "relaxed")))
|
|
295
|
+
.slice(0, MAX_SUGGESTIONS)
|
|
296
|
+
.map(({ trait, n, m, source }) => ({ trait, n, m, source }));
|
|
297
|
+
}
|