calliope-ts 0.0.2 → 0.0.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.
Files changed (160) hide show
  1. package/README.md +32 -20
  2. package/dist/caesura.d.ts +33 -0
  3. package/dist/caesura.d.ts.map +1 -0
  4. package/dist/caesura.js +202 -0
  5. package/dist/calliope/boundaries.d.ts +19 -0
  6. package/dist/calliope/boundaries.d.ts.map +1 -0
  7. package/dist/calliope/boundaries.js +182 -0
  8. package/dist/calliope/bracketing.d.ts +11 -0
  9. package/dist/calliope/bracketing.d.ts.map +1 -0
  10. package/dist/calliope/bracketing.js +416 -0
  11. package/dist/calliope/deps.d.ts +4 -0
  12. package/dist/calliope/deps.d.ts.map +1 -0
  13. package/dist/calliope/deps.js +181 -0
  14. package/dist/calliope/engine.d.ts +3 -0
  15. package/dist/calliope/engine.d.ts.map +1 -0
  16. package/dist/calliope/engine.js +71 -0
  17. package/dist/calliope/feats.d.ts +9 -0
  18. package/dist/calliope/feats.d.ts.map +1 -0
  19. package/dist/calliope/feats.js +45 -0
  20. package/dist/calliope/names.d.ts +7 -0
  21. package/dist/calliope/names.d.ts.map +1 -0
  22. package/dist/calliope/names.js +42 -0
  23. package/dist/calliope/postag.d.ts +8 -0
  24. package/dist/calliope/postag.d.ts.map +1 -0
  25. package/dist/calliope/postag.js +250 -0
  26. package/dist/calliope/prosodic.d.ts +3 -0
  27. package/dist/calliope/prosodic.d.ts.map +1 -0
  28. package/dist/calliope/prosodic.js +275 -0
  29. package/dist/calliope/relstress.d.ts +4 -0
  30. package/dist/calliope/relstress.d.ts.map +1 -0
  31. package/dist/calliope/relstress.js +688 -0
  32. package/dist/calliope/stressrules.d.ts +28 -0
  33. package/dist/calliope/stressrules.d.ts.map +1 -0
  34. package/dist/calliope/stressrules.js +147 -0
  35. package/dist/calliope/syntax.d.ts +38 -0
  36. package/dist/calliope/syntax.d.ts.map +1 -0
  37. package/dist/calliope/syntax.js +234 -0
  38. package/dist/calliope/udpos.d.ts +13 -0
  39. package/dist/calliope/udpos.d.ts.map +1 -0
  40. package/dist/calliope/udpos.js +156 -0
  41. package/dist/clio/caesura.d.ts +27 -0
  42. package/dist/clio/caesura.d.ts.map +1 -0
  43. package/dist/clio/caesura.js +148 -0
  44. package/dist/clio/depfix.d.ts +13 -0
  45. package/dist/clio/depfix.d.ts.map +1 -0
  46. package/dist/clio/depfix.js +84 -0
  47. package/dist/clio/display.d.ts +32 -0
  48. package/dist/clio/display.d.ts.map +1 -0
  49. package/dist/clio/display.js +976 -0
  50. package/dist/clio/engine.d.ts +3 -0
  51. package/dist/clio/engine.d.ts.map +1 -0
  52. package/dist/clio/engine.js +28 -0
  53. package/dist/clio/parser.d.ts +10 -0
  54. package/dist/clio/parser.d.ts.map +1 -0
  55. package/dist/clio/parser.js +696 -0
  56. package/dist/clio/phonological.d.ts +41 -0
  57. package/dist/clio/phonological.d.ts.map +1 -0
  58. package/dist/clio/phonological.js +788 -0
  59. package/dist/clio/phrasestress.d.ts +6 -0
  60. package/dist/clio/phrasestress.d.ts.map +1 -0
  61. package/dist/clio/phrasestress.js +106 -0
  62. package/dist/clio/pipeline.d.ts +11 -0
  63. package/dist/clio/pipeline.d.ts.map +1 -0
  64. package/dist/clio/pipeline.js +147 -0
  65. package/dist/clio/rhyme.d.ts +65 -0
  66. package/dist/clio/rhyme.d.ts.map +1 -0
  67. package/dist/clio/rhyme.js +761 -0
  68. package/dist/clio/scandroid.d.ts +17 -0
  69. package/dist/clio/scandroid.d.ts.map +1 -0
  70. package/dist/clio/scandroid.js +435 -0
  71. package/dist/clio/scansion.d.ts +46 -0
  72. package/dist/clio/scansion.d.ts.map +1 -0
  73. package/dist/clio/scansion.js +1086 -0
  74. package/dist/clio/semantics.d.ts +44 -0
  75. package/dist/clio/semantics.d.ts.map +1 -0
  76. package/dist/clio/semantics.js +139 -0
  77. package/dist/clio/stress.d.ts +83 -0
  78. package/dist/clio/stress.d.ts.map +1 -0
  79. package/dist/clio/stress.js +1737 -0
  80. package/dist/clio/tagfix.d.ts +6 -0
  81. package/dist/clio/tagfix.d.ts.map +1 -0
  82. package/dist/clio/tagfix.js +101 -0
  83. package/dist/display.d.ts +0 -6
  84. package/dist/display.d.ts.map +1 -1
  85. package/dist/display.js +322 -141
  86. package/dist/engine.d.ts +9 -0
  87. package/dist/engine.d.ts.map +1 -0
  88. package/dist/engine.js +12 -0
  89. package/dist/index.d.ts +4 -3
  90. package/dist/index.d.ts.map +1 -1
  91. package/dist/index.js +169 -26
  92. package/dist/parser.d.ts +3 -3
  93. package/dist/parser.d.ts.map +1 -1
  94. package/dist/parser.js +320 -563
  95. package/dist/phonological.d.ts +1 -1
  96. package/dist/phonological.d.ts.map +1 -1
  97. package/dist/phonological.js +73 -4
  98. package/dist/phrasestress.d.ts +6 -0
  99. package/dist/phrasestress.d.ts.map +1 -0
  100. package/dist/phrasestress.js +106 -0
  101. package/dist/rhyme.d.ts +40 -1
  102. package/dist/rhyme.d.ts.map +1 -1
  103. package/dist/rhyme.js +435 -7
  104. package/dist/scansion.d.ts +9 -0
  105. package/dist/scansion.d.ts.map +1 -1
  106. package/dist/scansion.js +145 -18
  107. package/dist/semantics.d.ts +44 -0
  108. package/dist/semantics.d.ts.map +1 -0
  109. package/dist/semantics.js +139 -0
  110. package/dist/stress.d.ts +127 -4
  111. package/dist/stress.d.ts.map +1 -1
  112. package/dist/stress.js +834 -61
  113. package/dist/types.d.ts +15 -0
  114. package/dist/types.d.ts.map +1 -1
  115. package/package.json +5 -3
  116. package/src/caesura.ts +201 -0
  117. package/src/calliope/boundaries.ts +190 -0
  118. package/src/calliope/bracketing.ts +390 -0
  119. package/src/calliope/deps.ts +160 -0
  120. package/src/calliope/engine.ts +77 -0
  121. package/src/calliope/feats.ts +46 -0
  122. package/src/calliope/names.ts +44 -0
  123. package/src/calliope/postag.ts +253 -0
  124. package/src/calliope/prosodic.ts +262 -0
  125. package/src/calliope/relstress.ts +645 -0
  126. package/src/calliope/stressrules.ts +147 -0
  127. package/src/calliope/syntax.ts +218 -0
  128. package/src/calliope/udpos.ts +152 -0
  129. package/src/calliope_src_contents.md +19049 -0
  130. package/src/clio/caesura.ts +145 -0
  131. package/src/clio/depfix.ts +88 -0
  132. package/src/clio/display.ts +1042 -0
  133. package/src/clio/engine.ts +38 -0
  134. package/src/clio/parser.ts +845 -0
  135. package/src/clio/phonological.ts +849 -0
  136. package/src/clio/phrasestress.ts +108 -0
  137. package/src/clio/pipeline.ts +154 -0
  138. package/src/clio/rhyme.ts +740 -0
  139. package/src/clio/scandroid.ts +434 -0
  140. package/src/clio/scansion.ts +1130 -0
  141. package/src/clio/semantics.ts +134 -0
  142. package/src/clio/stress.ts +1731 -0
  143. package/src/clio/tagfix.ts +104 -0
  144. package/src/display.ts +321 -137
  145. package/src/engine.ts +22 -0
  146. package/src/index.ts +176 -32
  147. package/src/parser.ts +346 -682
  148. package/src/phonological.ts +72 -4
  149. package/src/phrasestress.ts +108 -0
  150. package/src/rhyme.ts +428 -8
  151. package/src/scansion.ts +136 -15
  152. package/src/semantics.ts +134 -0
  153. package/src/stress.ts +794 -64
  154. package/src/types.ts +39 -4
  155. package/tests/DataForHayesLinesOnly.txt +364 -0
  156. package/tests/DataForHayesStressSymbolsRevised.txt +728 -0
  157. package/tests/basic.test.ts +443 -37
  158. package/tests/bench-hayes.mjs +72 -0
  159. package/tests/wagner-stress.test.ts +188 -0
  160. package/vitest.config.ts +0 -15
@@ -0,0 +1,108 @@
1
+ // phrasestress.ts — McAleese's Phrase-Stress phase (previously SKIPPED).
2
+ //
3
+ // The pipeline used to map lexical stress straight to relative stress (plus a
4
+ // single "+1 on the rightmost content word"), leaving `ClsWord.phraseStress`
5
+ // a vestigial field stuck at 0. This module restores the genuine phase that
6
+ // sits BETWEEN lexical and relative stress in McAleese's procedure (thesis
7
+ // Appendix A; Chomsky & Halle's Nuclear + Compound Stress Rules):
8
+ //
9
+ // 1. every word starts at 1;
10
+ // 2. the Compound Stress Rule pins a compound's subordinate element at the
11
+ // floor while its principal stays in the ramp;
12
+ // 3. the recursive Nuclear Stress Rule ramps the principal stress of each
13
+ // successive *stressed* word left-to-right, so prominence rises to the
14
+ // phrase's nuclear (rightmost) peak.
15
+ //
16
+ // For the right-branching structure of English declaratives the recursive NSR
17
+ // reduces to a monotone ramp over the stressed words — function words and
18
+ // compound-subordinates pinned at 1. NOTE the convention: here 1 is the FLOOR
19
+ // and the ramp climbs RIGHTWARD to the nuclear peak (the "If hairs…" example
20
+ // below). McAleese's "Mary ate sweet ice cream" worked example uses the
21
+ // OPPOSITE (SPE) numbering — 1 = PRIMARY/strongest, weakened outward — so his
22
+ // ICE cream (primary on "ice") is reproduced here as ice=PEAK, cream=floor
23
+ // (the compound's HEAD "cream" is the subordinate, pinned at 1):
24
+ //
25
+ // "Mary ate sweet ice cream" -> Mary2 ate3 sweet4 ice5 cream1 (ICE cream: head subordinate)
26
+ // "If hairs be wires, black wires grow on her head" -> 1 2 3 4 5 6 7 1 1 8
27
+ //
28
+ // (Marked / left-branching structures are approximated by the same ramp; a
29
+ // constituent-tree-driven refinement over `sentence.nodes` is a flagged
30
+ // follow-up. This module reads only POS / content flags and surface order,
31
+ // and writes only the previously-unused phraseStress field — purely additive.)
32
+
33
+ import { ClsWord } from '../types.js';
34
+ import { isPunctuation } from './parser.js';
35
+ import { compoundStressSide } from './stress.js';
36
+
37
+ /** Sentence-final punctuation resets the nuclear ramp (the NSR is
38
+ * sentence-bounded). A comma / colon / dash does NOT — the ramp runs across
39
+ * an internal intonational break (cf. "If hairs…, black wires…" → 1234…5,678). */
40
+ const SENTENCE_FINAL = new Set(['.', '!', '?', '…']);
41
+
42
+ /** Common-/proper-noun POS tags, gating which compounds floor an element. */
43
+ const NOUN_TAGS = new Set(['NN', 'NNS', 'NNP', 'NNPS']);
44
+
45
+ /**
46
+ * Proclitic POS tags pinned at the phrase-stress floor: the NSR ramp applies to
47
+ * *stressed words* (nouns, adjectives, adverbs, and VERBS — including the
48
+ * copula/auxiliary "be" and modals, which bear lexical stress even though they
49
+ * reduce in the contour), while pure clitics carry none. Verbs are NOT here,
50
+ * so McAleese's "if hairs BE wires…" keeps `be` in the ramp (=3). A word the
51
+ * earlier pipeline promoted to content (a phrasal-verb particle, a focus
52
+ * demonstrative — `isContent === true`) is un-pinned and ramps.
53
+ */
54
+ const PINNED_POS = new Set([
55
+ 'IN', 'TO', 'DT', 'CC', 'PRP', 'PRP$', 'WP', 'WP$', 'WDT', 'EX', 'POS',
56
+ ]);
57
+
58
+ /**
59
+ * Populate `word.phraseStress` for every word of a parsed sentence.
60
+ */
61
+ export function computePhraseStress(words: ClsWord[]): void {
62
+ const subordinate = compoundSubordinates(words);
63
+ let ramp = 1; // running nuclear ramp; the floor for pinned words is 1
64
+ for (const w of words) {
65
+ if (isPunctuation(w.lexicalClass)) {
66
+ w.phraseStress = 0;
67
+ if (SENTENCE_FINAL.has(w.word)) ramp = 1;
68
+ continue;
69
+ }
70
+ // Pinned at the floor: a proclitic function word bears no nuclear ramp, and
71
+ // a compound's subordinate element is demoted by the Compound Stress Rule.
72
+ const isClitic = PINNED_POS.has(w.lexicalClass) && !w.isContent;
73
+ if (isClitic || subordinate.has(w)) {
74
+ w.phraseStress = 1;
75
+ continue;
76
+ }
77
+ w.phraseStress = ++ramp;
78
+ }
79
+ }
80
+
81
+ /**
82
+ * Identify the subordinate (pinned) element of each surface-adjacent compound,
83
+ * delegating the direction to `compoundStressSide` (the shared rule, so phrase
84
+ * stress and the lexical compound pass cannot disagree).
85
+ * - fore-stress ('left') → the HEAD (right) element is subordinate: SEA·shore,
86
+ * ICE cream, KITCHen table — the Compound Stress Rule default for N+N;
87
+ * - right-stress ('right') → the MODIFIER (left) is subordinate: apple PIE,
88
+ * and proper-name pairs (New YORK);
89
+ * - Adjective+noun ("sweet cream") is phrasal, not a compound (`side` is
90
+ * 'right' there too, so the modifier "sweet" is pinned and "cream" ramps —
91
+ * i.e. end-stress, as desired).
92
+ */
93
+ function compoundSubordinates(words: ClsWord[]): Set<ClsWord> {
94
+ const subs = new Set<ClsWord>();
95
+ for (let i = 0; i + 1 < words.length; i++) {
96
+ const a = words[i], b = words[i + 1];
97
+ if (isPunctuation(a.lexicalClass) || isPunctuation(b.lexicalClass)) continue;
98
+ if (!a.isContent || !b.isContent) continue;
99
+ const side = compoundStressSide(a.word, a.lexicalClass, b.word, b.lexicalClass);
100
+ // Floor an element only for a genuine N+N compound; an Adj+N ("sweet ice",
101
+ // "red car") is phrasal — its end-stress emerges from the ramp itself, so we
102
+ // pin nothing and let the adjective ramp (McAleese: sweet=4, above cream).
103
+ const bothNouns = NOUN_TAGS.has(a.lexicalClass) && NOUN_TAGS.has(b.lexicalClass);
104
+ if (side === 'left') subs.add(b); // fore-stress compound: head subordinate (ICE cream)
105
+ else if (side === 'right' && bothNouns) subs.add(a); // right-stress N+N: modifier subordinate (apple PIE)
106
+ }
107
+ return subs;
108
+ }
@@ -0,0 +1,154 @@
1
+ // clio/pipeline.ts — the FROZEN Clio analysis pipeline (the "control group").
2
+ //
3
+ // This is a verbatim snapshot of index.ts's orchestration (processLine +
4
+ // applyContinuityRename + analyzeStanzas/analyzeText/analyzeReadingDocument) as it
5
+ // stood at the structure-first rebuild, wired EXCLUSIVELY to clio-local frozen
6
+ // modules (clio/parser, clio/scansion, clio/rhyme, clio/scandroid, clio/phonological,
7
+ // clio/display) and hard-wired to clioEngine. Nothing here imports the live
8
+ // top-level pipeline, so changes to the regular (Calliope) pipeline — including the
9
+ // shared scorer — CANNOT affect Clio's output. Clio is grounded in this tree alone.
10
+ //
11
+ // DO NOT evolve this file or any clio/* module with the Calliope rebuild.
12
+
13
+ import { parseDocument } from './parser.js';
14
+ import { renderHierarchy, renderKeyStresses } from './phonological.js';
15
+ import {
16
+ extractKeyStresses, scoreMeters, applyStanzaConsensus, applyRhythmLayer, applyMetricalityLayer,
17
+ } from './scansion.js';
18
+ import { applyRhymeAndForm } from './rhyme.js';
19
+ import { scandroidCorralWeird, scandroidMaximizeNormal, stressToMarks } from './scandroid.js';
20
+ import { type ReadingStanza } from './display.js';
21
+ import { clioEngine } from './engine.js';
22
+ import type {
23
+ ClsSentence, MetreName, IntonationalUnit, StressLevel, ScansionResult, LineResult,
24
+ } from '../types.js';
25
+
26
+ export type { ReadingStanza } from './display.js';
27
+ // Frozen Clio renderers + parse entry, so the CLI can render Clio output entirely
28
+ // from the clio/ tree (live display.ts changes cannot reach Clio).
29
+ export { renderUnifiedDisplay, renderReadingView, renderFullLegend } from './display.js';
30
+ export { parseDocument, isPunctuation } from './parser.js';
31
+ export { renderHierarchy, renderKeyStresses } from './phonological.js';
32
+ export { clioEngine } from './engine.js';
33
+
34
+ /** Scan one verse line (which may parse into several grammatical sentences). */
35
+ function processLine(sents: ClsSentence[]): LineResult | null {
36
+ if (sents.length === 0) return null;
37
+
38
+ const iusPerSent: IntonationalUnit[][] = [];
39
+ for (const sent of sents) {
40
+ iusPerSent.push(clioEngine.analyzeSentence(sent));
41
+ }
42
+
43
+ const words = sents.flatMap(s => s.words);
44
+ const ius = iusPerSent.flat();
45
+ let merged: ClsSentence;
46
+ if (sents.length === 1) {
47
+ merged = sents[0];
48
+ } else {
49
+ words.forEach((w, i) => { w.index = i + 1; });
50
+ merged = {
51
+ index: sents[0].index, nodes: null,
52
+ dependencies: sents.flatMap(s => s.dependencies), words, xml: '',
53
+ };
54
+ }
55
+
56
+ const keyStresses = extractKeyStresses(ius, words);
57
+ const phonoDetail = scoreMeters(keyStresses, words, ius);
58
+ phonoDetail.all = renderHierarchy(ius, words);
59
+ phonoDetail.keyStresses = renderKeyStresses(ius, words, keyStresses);
60
+
61
+ const stressPattern: StressLevel[] = words.flatMap(w => w.syllables.map(s => s.relativeStress ?? 'w'));
62
+ const marks = stressToMarks(stressPattern);
63
+ const actualFeet = phonoDetail.footCount > 0 ? phonoDetail.footCount : 5;
64
+ const corral = scandroidCorralWeird(marks, actualFeet);
65
+ const max = scandroidMaximizeNormal(marks, actualFeet);
66
+
67
+ const corralResult: ScansionResult | undefined = corral.footlist.length
68
+ ? { meter: 'iambic', scansion: corral.footlist.map(f => f.replace(/[()]/g, '')).join(' | '),
69
+ certainty: 0, weightScore: 0, maxPossibleWeight: 0, algorithm: 'Scandroid Corral the Weird' }
70
+ : undefined;
71
+ const maxResult: ScansionResult | undefined = max.footlist.length
72
+ ? { meter: 'iambic', scansion: max.footlist.map(f => f.replace(/[()]/g, '')).join(' | '),
73
+ certainty: 0, weightScore: 0, maxPossibleWeight: 0, algorithm: 'Scandroid Maximise the Normal' }
74
+ : undefined;
75
+
76
+ return {
77
+ sentence: merged, phonologicalHierarchy: ius, keyStresses,
78
+ phonologicalScansion: phonoDetail, scandroidCorral: corralResult, scandroidMaximise: maxResult,
79
+ };
80
+ }
81
+
82
+ function applyContinuityRename(results: LineResult[]): void {
83
+ const noted = results.filter(r => r.phonologicalScansion.rhythmNote).length;
84
+ if (results.length > 0 && noted >= results.length / 2) return;
85
+ for (const res of results) {
86
+ const d = res.phonologicalScansion;
87
+ if (!d.consensusMeter) continue;
88
+ const family = d.consensusMeter.split(' ')[0] as MetreName;
89
+ const forced = scoreMeters(res.keyStresses, res.sentence.words, res.phonologicalHierarchy, family);
90
+ if (!forced || forced.meterName === 'free verse' || forced.footCount <= 0) continue;
91
+ d.standaloneMeter = d.meter;
92
+ d.meter = forced.meter; d.meterName = forced.meterName; d.footCount = forced.footCount;
93
+ d.scansion = forced.scansion; d.certainty = forced.certainty; d.summary = forced.summary;
94
+ d.consensusMeter = undefined;
95
+ }
96
+ }
97
+
98
+ export function analyzeStanzasClio(text: string): LineResult[][] {
99
+ const stanzas = text.split(/\n\s*\n/);
100
+ const results: LineResult[][] = [];
101
+ for (const stanza of stanzas) {
102
+ const lines = stanza.split('\n').filter(l => l.trim() !== '');
103
+ const stanzaResults: LineResult[] = [];
104
+ for (const line of lines) {
105
+ const doc = parseDocument(line);
106
+ const res = processLine(doc.sentences);
107
+ if (res) stanzaResults.push(res);
108
+ }
109
+ applyStanzaConsensus(stanzaResults.map(r => r.phonologicalScansion));
110
+ applyRhythmLayer(stanzaResults.map(r => r.phonologicalScansion));
111
+ applyContinuityRename(stanzaResults);
112
+ results.push(stanzaResults);
113
+ }
114
+ if (results.length > 1) {
115
+ const all = results.flat();
116
+ applyStanzaConsensus(all.map(r => r.phonologicalScansion));
117
+ applyContinuityRename(all);
118
+ for (const st of results) applyRhythmLayer(st.map(r => r.phonologicalScansion));
119
+ }
120
+ applyMetricalityLayer(results.flatMap(st => st.map(r => r.phonologicalScansion)));
121
+ applyRhymeAndForm(results);
122
+ return results;
123
+ }
124
+
125
+ export function analyzeTextClio(text: string): LineResult[] {
126
+ return analyzeStanzasClio(text).flat();
127
+ }
128
+
129
+ export function analyzeReadingDocumentClio(text: string): ReadingStanza[] {
130
+ const stanzas = text.split(/\n\s*\n/);
131
+ const out: ReadingStanza[] = [];
132
+ for (const stanza of stanzas) {
133
+ const rawLines = stanza.split('\n').filter(l => l.trim() !== '');
134
+ if (rawLines.length === 0) continue;
135
+ const lines = rawLines.map(raw => {
136
+ const doc = parseDocument(raw);
137
+ const res = processLine(doc.sentences);
138
+ return { raw, results: res ? [res] : [] };
139
+ });
140
+ applyStanzaConsensus(lines.flatMap(l => l.results.map(r => r.phonologicalScansion)));
141
+ applyRhythmLayer(lines.flatMap(l => l.results.map(r => r.phonologicalScansion)));
142
+ applyContinuityRename(lines.flatMap(l => l.results));
143
+ out.push({ lines });
144
+ }
145
+ if (out.length > 1) {
146
+ const all = out.flatMap(st => st.lines.flatMap(l => l.results));
147
+ applyStanzaConsensus(all.map(r => r.phonologicalScansion));
148
+ applyContinuityRename(all);
149
+ for (const st of out) applyRhythmLayer(st.lines.flatMap(l => l.results.map(r => r.phonologicalScansion)));
150
+ }
151
+ applyMetricalityLayer(out.flatMap(st => st.lines.flatMap(l => l.results.map(r => r.phonologicalScansion))));
152
+ applyRhymeAndForm(out.map(st => st.lines.flatMap(l => l.results)));
153
+ return out;
154
+ }