dravoice 0.1.2 → 0.1.4

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.
@@ -1,59 +1,65 @@
1
- import { evidenceTypes } from "./evidence.js";
2
- import { transitionLabel } from "./discourse.js";
3
- import { topItems } from "../text-utils.js";
4
-
5
- export function analyzeRhetoricalShape(documents) {
6
- const documentMoves = documents.map((document) => document.sentences.map((sentence) => moveFor(sentence.text)));
7
- const sentenceMoves = documentMoves.flat();
8
- const openingMoves = documents.flatMap((document) => document.sentences.slice(0, 3).map((sentence) => moveFor(sentence.text)));
9
- const bigrams = [];
10
- const trigrams = [];
11
- const openingMovePatterns = [];
12
- for (const moves of documentMoves) {
13
- if (moves.length > 0) {
14
- openingMovePatterns.push(moves.slice(0, 3).join(" -> "));
15
- }
16
- for (let index = 0; index < moves.length - 1; index += 1) {
17
- bigrams.push(`${moves[index]} -> ${moves[index + 1]}`);
18
- }
19
- for (let index = 0; index < moves.length - 2; index += 1) {
20
- trigrams.push(`${moves[index]} -> ${moves[index + 1]} -> ${moves[index + 2]}`);
21
- }
22
- }
23
-
24
- return {
25
- family: "rhetoricalShape",
26
- confidence: sentenceMoves.length >= 12 ? "medium" : "low",
27
- features: {
28
- moveRates: topItems(sentenceMoves, 12),
29
- openingMoves: openingMoves.slice(0, 9),
30
- openingMovePatterns: topItems(openingMovePatterns, 8),
31
- moveBigrams: topItems(bigrams, 12),
32
- moveTrigrams: topItems(trigrams, 12),
33
- commonSequences: topItems(bigrams, 12),
34
- },
35
- examples: openingMoves.slice(0, 5),
36
- warnings: sentenceMoves.length < 12 ? ["Rhetorical-shape confidence is limited because the corpus has fewer than 12 sentences."] : [],
37
- revisionHandles: ["Compare the draft's opening and move sequence against observed source patterns."],
38
- };
39
- }
40
-
41
- export function moveFor(text) {
42
- if (evidenceTypes(text).length > 0) {
43
- return "scene-or-evidence";
44
- }
45
- const transition = transitionLabel(text);
46
- if (transition === "contrast") {
47
- return "turn";
48
- }
49
- if (transition === "causal" || transition === "conclusion") {
50
- return "implication";
51
- }
52
- if (/\b(should|must|rule|lesson|start|fix|avoid|keep)\b/i.test(text)) {
53
- return "advice";
54
- }
55
- if (/\b(always|never|everyone|everything|important|best|better|worse|should|must|all|none|every)\b/i.test(text)) {
56
- return "abstract-claim";
57
- }
58
- return "statement";
59
- }
1
+ import { evidenceTypes } from "./evidence.js";
2
+ import { transitionLabel } from "./discourse.js";
3
+ import { splitSentences, topItems } from "../text-utils.js";
4
+
5
+ export function analyzeRhetoricalShape(documents) {
6
+ const documentMoves = documents.map((document) => document.sentences.map((sentence) => moveFor(sentence.text)));
7
+ const sentenceMoves = documentMoves.flat();
8
+ const openingMoves = documents.flatMap((document) => document.sentences.slice(0, 3).map((sentence) => moveFor(sentence.text)));
9
+ const paragraphMovePatterns = documents.flatMap((document) =>
10
+ document.paragraphs
11
+ .map((paragraph) => splitSentences(paragraph.text).map((sentence) => moveFor(sentence)).join(" -> "))
12
+ .filter(Boolean)
13
+ );
14
+ const bigrams = [];
15
+ const trigrams = [];
16
+ const openingMovePatterns = [];
17
+ for (const moves of documentMoves) {
18
+ if (moves.length > 0) {
19
+ openingMovePatterns.push(moves.slice(0, 3).join(" -> "));
20
+ }
21
+ for (let index = 0; index < moves.length - 1; index += 1) {
22
+ bigrams.push(`${moves[index]} -> ${moves[index + 1]}`);
23
+ }
24
+ for (let index = 0; index < moves.length - 2; index += 1) {
25
+ trigrams.push(`${moves[index]} -> ${moves[index + 1]} -> ${moves[index + 2]}`);
26
+ }
27
+ }
28
+
29
+ return {
30
+ family: "rhetoricalShape",
31
+ confidence: sentenceMoves.length >= 12 ? "medium" : "low",
32
+ features: {
33
+ moveRates: topItems(sentenceMoves, 12),
34
+ openingMoves: openingMoves.slice(0, 9),
35
+ openingMovePatterns: topItems(openingMovePatterns, 8),
36
+ paragraphMovePatterns: topItems(paragraphMovePatterns, 12),
37
+ moveBigrams: topItems(bigrams, 12),
38
+ moveTrigrams: topItems(trigrams, 12),
39
+ commonSequences: topItems(bigrams, 12),
40
+ },
41
+ examples: openingMoves.slice(0, 5),
42
+ warnings: sentenceMoves.length < 12 ? ["Rhetorical-shape confidence is limited because the corpus has fewer than 12 sentences."] : [],
43
+ revisionHandles: ["Compare the draft's opening and move sequence against observed source patterns."],
44
+ };
45
+ }
46
+
47
+ export function moveFor(text) {
48
+ if (evidenceTypes(text).length > 0) {
49
+ return "scene-or-evidence";
50
+ }
51
+ const transition = transitionLabel(text);
52
+ if (transition === "contrast") {
53
+ return "turn";
54
+ }
55
+ if (transition === "causal" || transition === "conclusion") {
56
+ return "implication";
57
+ }
58
+ if (/\b(should|must|rule|lesson|start|fix|avoid|keep)\b/i.test(text)) {
59
+ return "advice";
60
+ }
61
+ if (/\b(always|never|everyone|everything|important|best|better|worse|should|must|all|none|every)\b/i.test(text)) {
62
+ return "abstract-claim";
63
+ }
64
+ return "statement";
65
+ }
@@ -1,47 +1,39 @@
1
- import { distribution, rate } from "../text-utils.js";
2
-
3
- export function analyzeRhythm(documents) {
4
- const sentences = documents.flatMap((document) => document.sentences);
5
- const paragraphs = documents.flatMap((document) => document.paragraphs);
6
- const blocks = documents.flatMap((document) => document.blocks);
7
-
8
- return {
9
- family: "rhythm",
10
- confidence: confidenceFor(sentences.length),
11
- features: {
12
- documentCount: documents.length,
13
- sentenceCount: sentences.length,
14
- paragraphCount: paragraphs.length,
15
- sentenceWords: distribution(sentences.map((sentence) => sentence.tokens.length)),
16
- paragraphWords: distribution(paragraphs.map((paragraph) => paragraph.text.split(/\s+/).filter(Boolean).length)),
17
- paragraphSentences: distribution(paragraphs.map((paragraph) => countParagraphSentences(sentences, paragraph))),
18
- headingDensity: rate(documents.reduce((sum, document) => sum + document.headings.length, 0), sentences.length, 2),
19
- listDensity: rate(blocks.filter((block) => block.type === "list").length, paragraphs.length, 2),
20
- quoteDensity: rate(blocks.filter((block) => block.type === "quote").length, paragraphs.length, 2),
21
- },
22
- examples: [
23
- `sentenceWords.median: ${distribution(sentences.map((sentence) => sentence.tokens.length)).median}`,
24
- `paragraphWords.median: ${distribution(paragraphs.map((paragraph) => paragraph.text.split(/\s+/).filter(Boolean).length)).median}`,
25
- ],
26
- warnings: sentences.length < 12 ? ["Rhythm confidence is limited because the corpus has fewer than 12 usable sentences."] : [],
27
- revisionHandles: ["Compare sentence and paragraph pacing against the learned range."],
28
- };
29
- }
30
-
31
- function countParagraphSentences(sentences, paragraph) {
32
- return Math.max(1, sentences.filter((sentence) =>
33
- sentence.line >= paragraph.line &&
34
- sentence.line < paragraph.line + 8 &&
35
- sentence.headingId === paragraph.headingId
36
- ).length);
37
- }
38
-
39
- function confidenceFor(sentenceCount) {
40
- if (sentenceCount >= 30) {
41
- return "high";
42
- }
43
- if (sentenceCount >= 8) {
44
- return "medium";
45
- }
46
- return "low";
47
- }
1
+ import { distribution, rate, splitSentences } from "../text-utils.js";
2
+
3
+ export function analyzeRhythm(documents) {
4
+ const sentences = documents.flatMap((document) => document.sentences);
5
+ const paragraphs = documents.flatMap((document) => document.paragraphs);
6
+ const blocks = documents.flatMap((document) => document.blocks);
7
+
8
+ return {
9
+ family: "rhythm",
10
+ confidence: confidenceFor(sentences.length),
11
+ features: {
12
+ documentCount: documents.length,
13
+ sentenceCount: sentences.length,
14
+ paragraphCount: paragraphs.length,
15
+ sentenceWords: distribution(sentences.map((sentence) => sentence.tokens.length)),
16
+ paragraphWords: distribution(paragraphs.map((paragraph) => paragraph.text.split(/\s+/).filter(Boolean).length)),
17
+ paragraphSentences: distribution(paragraphs.map((paragraph) => Math.max(1, splitSentences(paragraph.text).length))),
18
+ headingDensity: rate(documents.reduce((sum, document) => sum + document.headings.length, 0), sentences.length, 2),
19
+ listDensity: rate(blocks.filter((block) => block.type === "list").length, paragraphs.length, 2),
20
+ quoteDensity: rate(blocks.filter((block) => block.type === "quote").length, paragraphs.length, 2),
21
+ },
22
+ examples: [
23
+ `sentenceWords.median: ${distribution(sentences.map((sentence) => sentence.tokens.length)).median}`,
24
+ `paragraphWords.median: ${distribution(paragraphs.map((paragraph) => paragraph.text.split(/\s+/).filter(Boolean).length)).median}`,
25
+ ],
26
+ warnings: sentences.length < 12 ? ["Rhythm confidence is limited because the corpus has fewer than 12 usable sentences."] : [],
27
+ revisionHandles: ["Compare sentence and paragraph pacing against the learned range."],
28
+ };
29
+ }
30
+
31
+ function confidenceFor(sentenceCount) {
32
+ if (sentenceCount >= 30) {
33
+ return "high";
34
+ }
35
+ if (sentenceCount >= 8) {
36
+ return "medium";
37
+ }
38
+ return "low";
39
+ }
@@ -1,24 +1,41 @@
1
- import { distribution, rate } from "../text-utils.js";
2
- import { moveFor } from "./rhetorical-shape.js";
3
-
4
- export function analyzeStructure(documents) {
5
- const sectionLengths = documents.flatMap((document) =>
6
- document.sections.map((section) => section.blocks.reduce((sum, block) => sum + block.lines.join(" ").split(/\s+/).filter(Boolean).length, 0))
7
- );
8
- const openingMoves = documents.flatMap((document) => document.sentences.slice(0, 2).map((sentence) => moveFor(sentence.text)));
9
-
10
- return {
11
- family: "structure",
12
- confidence: documents.length >= 3 ? "medium" : "low",
13
- features: {
14
- sectionWords: distribution(sectionLengths),
15
- headingCount: distribution(documents.map((document) => document.headings.length)),
16
- openingMoves,
17
- listDocumentRate: rate(documents.filter((document) => document.blocks.some((block) => block.type === "list")).length, documents.length, 2),
18
- quoteDocumentRate: rate(documents.filter((document) => document.blocks.some((block) => block.type === "quote")).length, documents.length, 2),
19
- },
20
- examples: openingMoves.slice(0, 5),
21
- warnings: documents.length < 3 ? ["Structure confidence is limited because the corpus has fewer than 3 documents."] : [],
22
- revisionHandles: ["Compare headings, list/quote use, section size, and opening structure."],
23
- };
24
- }
1
+ import { distribution, rate, topItems } from "../text-utils.js";
2
+ import { moveFor } from "./rhetorical-shape.js";
3
+
4
+ export function analyzeStructure(documents) {
5
+ const sectionLengths = documents.flatMap((document) =>
6
+ document.sections.map((section) => section.blocks.reduce((sum, block) => sum + block.lines.join(" ").split(/\s+/).filter(Boolean).length, 0))
7
+ );
8
+ const openingMoves = documents.flatMap((document) => document.sentences.slice(0, 2).map((sentence) => moveFor(sentence.text)));
9
+ const sectionOrderPatterns = documents
10
+ .map((document) => document.headings.map((heading) => `h${heading.depth}`).join(" -> "))
11
+ .filter(Boolean);
12
+ const listPlacementPatterns = documents.flatMap((document) =>
13
+ document.blocks
14
+ .filter((block) => block.type === "list")
15
+ .map((block) => `h${block.headingDepth || 0}:list`)
16
+ );
17
+ const quotePlacementPatterns = documents.flatMap((document) =>
18
+ document.blocks
19
+ .filter((block) => block.type === "quote")
20
+ .map((block) => `h${block.headingDepth || 0}:quote`)
21
+ );
22
+
23
+ return {
24
+ family: "structure",
25
+ confidence: documents.length >= 3 ? "medium" : "low",
26
+ features: {
27
+ sectionWords: distribution(sectionLengths),
28
+ headingCount: distribution(documents.map((document) => document.headings.length)),
29
+ maxHeadingDepth: distribution(documents.map((document) => Math.max(0, ...document.headings.map((heading) => heading.depth)))),
30
+ sectionOrderPatterns: topItems(sectionOrderPatterns, 12),
31
+ listPlacementPatterns: topItems(listPlacementPatterns, 12),
32
+ quotePlacementPatterns: topItems(quotePlacementPatterns, 12),
33
+ openingMoves,
34
+ listDocumentRate: rate(documents.filter((document) => document.blocks.some((block) => block.type === "list")).length, documents.length, 2),
35
+ quoteDocumentRate: rate(documents.filter((document) => document.blocks.some((block) => block.type === "quote")).length, documents.length, 2),
36
+ },
37
+ examples: openingMoves.slice(0, 5),
38
+ warnings: documents.length < 3 ? ["Structure confidence is limited because the corpus has fewer than 3 documents."] : [],
39
+ revisionHandles: ["Compare headings, list/quote use, section size, and opening structure."],
40
+ };
41
+ }