calliope-ts 0.0.4 → 0.1.0
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/README.md +69 -3
- package/dist/calliope/bracketing.d.ts.map +1 -1
- package/dist/calliope/bracketing.js +59 -5
- package/dist/calliope/deps.d.ts +5 -1
- package/dist/calliope/deps.d.ts.map +1 -1
- package/dist/calliope/deps.js +53 -3
- package/dist/calliope/engine.d.ts.map +1 -1
- package/dist/calliope/engine.js +6 -1
- package/dist/calliope/relstress.d.ts.map +1 -1
- package/dist/calliope/relstress.js +411 -15
- package/dist/calliope/syntax.d.ts +33 -0
- package/dist/calliope/syntax.d.ts.map +1 -1
- package/dist/calliope/syntax.js +199 -0
- package/dist/data/en_common_words.json +1 -0
- package/dist/display.d.ts +15 -1
- package/dist/display.d.ts.map +1 -1
- package/dist/display.js +112 -10
- package/dist/fabbhalle.d.ts +54 -0
- package/dist/fabbhalle.d.ts.map +1 -0
- package/dist/fabbhalle.js +404 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +86 -33
- package/dist/phonological.d.ts.map +1 -1
- package/dist/phonological.js +7 -1
- package/dist/rewriteEn.d.ts +14 -0
- package/dist/rewriteEn.d.ts.map +1 -0
- package/dist/rewriteEn.js +392 -0
- package/dist/rhyme.d.ts +35 -3
- package/dist/rhyme.d.ts.map +1 -1
- package/dist/rhyme.js +176 -11
- package/dist/russian/accentuator.d.ts +31 -0
- package/dist/russian/accentuator.d.ts.map +1 -0
- package/dist/russian/accentuator.js +546 -0
- package/dist/russian/collocations.d.ts +7 -0
- package/dist/russian/collocations.d.ts.map +1 -0
- package/dist/russian/collocations.js +38 -0
- package/dist/russian/compounds.d.ts +6 -0
- package/dist/russian/compounds.d.ts.map +1 -0
- package/dist/russian/compounds.js +81 -0
- package/dist/russian/download.d.ts +23 -0
- package/dist/russian/download.d.ts.map +1 -0
- package/dist/russian/download.js +117 -0
- package/dist/russian/engine.d.ts +6 -0
- package/dist/russian/engine.d.ts.map +1 -0
- package/dist/russian/engine.js +1424 -0
- package/dist/russian/parser.d.ts +8 -0
- package/dist/russian/parser.d.ts.map +1 -0
- package/dist/russian/parser.js +92 -0
- package/dist/russian/paths.d.ts +55 -0
- package/dist/russian/paths.d.ts.map +1 -0
- package/dist/russian/paths.js +114 -0
- package/dist/russian/rewrite.d.ts +6 -0
- package/dist/russian/rewrite.d.ts.map +1 -0
- package/dist/russian/rewrite.js +400 -0
- package/dist/russian/rhyme.d.ts +18 -0
- package/dist/russian/rhyme.d.ts.map +1 -0
- package/dist/russian/rhyme.js +230 -0
- package/dist/russian/syllabifier.d.ts +14 -0
- package/dist/russian/syllabifier.d.ts.map +1 -0
- package/dist/russian/syllabifier.js +475 -0
- package/dist/russian/types.d.ts +94 -0
- package/dist/russian/types.d.ts.map +1 -0
- package/dist/russian/types.js +27 -0
- package/dist/scandroidNative/constants.d.ts +16 -0
- package/dist/scandroidNative/constants.d.ts.map +1 -0
- package/dist/scandroidNative/constants.js +60 -0
- package/dist/scandroidNative/deduceParameters.d.ts +16 -0
- package/dist/scandroidNative/deduceParameters.d.ts.map +1 -0
- package/dist/scandroidNative/deduceParameters.js +69 -0
- package/dist/scandroidNative/dictionary-data.d.ts +2 -0
- package/dist/scandroidNative/dictionary-data.d.ts.map +1 -0
- package/dist/scandroidNative/dictionary-data.js +744 -0
- package/dist/scandroidNative/dictionary.d.ts +12 -0
- package/dist/scandroidNative/dictionary.d.ts.map +1 -0
- package/dist/scandroidNative/dictionary.js +50 -0
- package/dist/scandroidNative/engine.d.ts +41 -0
- package/dist/scandroidNative/engine.d.ts.map +1 -0
- package/dist/scandroidNative/engine.js +119 -0
- package/dist/scandroidNative/machine.d.ts +93 -0
- package/dist/scandroidNative/machine.d.ts.map +1 -0
- package/dist/scandroidNative/machine.js +795 -0
- package/dist/scandroidNative/positioner.d.ts +32 -0
- package/dist/scandroidNative/positioner.d.ts.map +1 -0
- package/dist/scandroidNative/positioner.js +208 -0
- package/dist/scandroidNative/syllabizer.d.ts +3 -0
- package/dist/scandroidNative/syllabizer.d.ts.map +1 -0
- package/dist/scandroidNative/syllabizer.js +215 -0
- package/dist/scandroidNative/utilities.d.ts +37 -0
- package/dist/scandroidNative/utilities.d.ts.map +1 -0
- package/dist/scandroidNative/utilities.js +86 -0
- package/dist/scansion.d.ts.map +1 -1
- package/dist/scansion.js +118 -14
- package/dist/stress.d.ts +1 -0
- package/dist/stress.d.ts.map +1 -1
- package/dist/stress.js +502 -42
- package/dist/types.d.ts +3 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +27 -6
- package/tools/fetch-russian-data.mjs +30 -0
- package/webapp/public/app.js +2418 -0
- package/webapp/public/assets/GithubLogo.png +0 -0
- package/webapp/public/assets/NPM2.png +0 -0
- package/webapp/public/assets/fonts/MurenaBold-webfont.woff2 +0 -0
- package/webapp/public/assets/fonts/MurenaItalic-webfont.woff2 +0 -0
- package/webapp/public/assets/fonts/MurenaRegular-webfont.woff2 +0 -0
- package/webapp/public/assets/fonts/Myra_4F_Caps_Bold-webfont.woff2 +0 -0
- package/webapp/public/assets/fonts/SinkinSans-700Bold.otf +0 -0
- package/webapp/public/assets/fonts/butterflies.ttf +0 -0
- package/webapp/public/assets/fonts/midcase_blackline-webfont.woff2 +0 -0
- package/webapp/public/assets/fonts/neueral-bold-webfont.woff2 +0 -0
- package/webapp/public/assets/fonts/neueral-regular-webfont.woff2 +0 -0
- package/webapp/public/assets/fonts/popelka-webfont.woff2 +0 -0
- package/webapp/public/assets/fonts/vremenagrotesk-webfont.woff +0 -0
- package/webapp/public/assets/fonts/wicky_javick_bold-webfont.woff2 +0 -0
- package/webapp/public/assets/logos/Logo_Var_10_Celtic_Knot_SVG.svg +116 -0
- package/webapp/public/assets/logos/Logo_Var_11_Graph_SVG.svg +313 -0
- package/webapp/public/assets/logos/Logo_Var_12_Limacon_Knot_SVG.svg +3942 -0
- package/webapp/public/assets/logos/Logo_Var_13_Crescents_SVG.svg +1030 -0
- package/webapp/public/assets/logos/Logo_Var_1_Spiral_SVG.svg +1491 -0
- package/webapp/public/assets/logos/Logo_Var_2_Tunnel_SVG.svg +12 -0
- package/webapp/public/assets/logos/Logo_Var_3_Triskelion_SVG.svg +10 -0
- package/webapp/public/assets/logos/Logo_Var_4_Quinquetra_PNG.png +0 -0
- package/webapp/public/assets/logos/Logo_Var_5_Penrose_Pentagram_SVG.svg +37 -0
- package/webapp/public/assets/logos/Logo_Var_6_Triskelion2_SVG.svg +10 -0
- package/webapp/public/assets/logos/Logo_Var_7_SixfoldSpirals_SVG.svg +20 -0
- package/webapp/public/assets/logos/Logo_Var_8_Radial_Labyrinth_SVG.svg +163 -0
- package/webapp/public/assets/logos/Logo_Var_9_Radial_Labyrinth_2_SVG.svg +106 -0
- package/webapp/public/index.html +316 -0
- package/webapp/public/styles.css +1114 -0
- package/webapp/server.mjs +680 -0
- package/src/caesura.ts +0 -201
- package/src/calliope/boundaries.ts +0 -190
- package/src/calliope/bracketing.ts +0 -390
- package/src/calliope/deps.ts +0 -160
- package/src/calliope/engine.ts +0 -77
- package/src/calliope/feats.ts +0 -46
- package/src/calliope/names.ts +0 -44
- package/src/calliope/postag.ts +0 -253
- package/src/calliope/prosodic.ts +0 -262
- package/src/calliope/relstress.ts +0 -645
- package/src/calliope/stressrules.ts +0 -147
- package/src/calliope/syntax.ts +0 -218
- package/src/calliope/udpos.ts +0 -152
- package/src/calliope_src_contents.md +0 -19049
- package/src/clio/caesura.ts +0 -145
- package/src/clio/depfix.ts +0 -88
- package/src/clio/display.ts +0 -1042
- package/src/clio/engine.ts +0 -38
- package/src/clio/parser.ts +0 -845
- package/src/clio/phonological.ts +0 -849
- package/src/clio/phrasestress.ts +0 -108
- package/src/clio/pipeline.ts +0 -154
- package/src/clio/rhyme.ts +0 -740
- package/src/clio/scandroid.ts +0 -434
- package/src/clio/scansion.ts +0 -1130
- package/src/clio/semantics.ts +0 -134
- package/src/clio/stress.ts +0 -1731
- package/src/clio/tagfix.ts +0 -104
- package/src/depfix.ts +0 -88
- package/src/display.ts +0 -1138
- package/src/engine.ts +0 -22
- package/src/index.ts +0 -690
- package/src/parser.ts +0 -501
- package/src/phonological.ts +0 -917
- package/src/phrasestress.ts +0 -108
- package/src/rhyme.ts +0 -748
- package/src/scandroid.ts +0 -434
- package/src/scansion.ts +0 -1174
- package/src/semantics.ts +0 -134
- package/src/stress.ts +0 -2111
- package/src/tagfix.ts +0 -104
- package/src/types.ts +0 -265
- package/tests/DataForHayesLinesOnly.txt +0 -364
- package/tests/DataForHayesStressSymbolsRevised.txt +0 -728
- package/tests/basic.test.ts +0 -1102
- package/tests/bench-hayes.mjs +0 -72
- package/tests/epg64.meter.train.txt +0 -8139
- package/tests/litlab-sample-2016.txt +0 -1738
- package/tests/prosodic.meter.train.txt +0 -13192
- package/tests/wagner-stress.test.ts +0 -188
- package/tsconfig.json +0 -29
- package/vitest.config.d.ts +0 -2
- package/vitest.config.js +0 -14
package/src/parser.ts
DELETED
|
@@ -1,501 +0,0 @@
|
|
|
1
|
-
// parser.ts — Syntactic dependency parser powered by UDPipe (English GUM model),
|
|
2
|
-
// producing a ClsDocument with a full dependency graph and a phrase‑structure node
|
|
3
|
-
// tree in the Universal Dependencies format used in McAleese's Calliope.
|
|
4
|
-
//
|
|
5
|
-
// HISTORY: this module previously ran a staged FinNLP pipeline
|
|
6
|
-
// (en-norm → lexed → en-pos → en-parse) with hand-written tag/dep correction
|
|
7
|
-
// layers (tagfix.ts / depfix.ts) to patch en-parse's systematic errors. It now
|
|
8
|
-
// delegates tokenisation, POS tagging, and dependency parsing to UDPipe via the
|
|
9
|
-
// `udpipe-node` package (a pure-WASM build — no native binary, no subprocess),
|
|
10
|
-
// which is a far more accurate parser. UDPipe's output maps cleanly onto the
|
|
11
|
-
// existing data model:
|
|
12
|
-
// • XPOS column is Penn Treebank → ClsWord.lexicalClass (unchanged downstream)
|
|
13
|
-
// • DEPREL column is Universal Dependencies → ClsDependency.dependentType
|
|
14
|
-
// The correction layers are therefore no longer applied on this path.
|
|
15
|
-
|
|
16
|
-
import { createUDPipe } from 'udpipe-node/wasm';
|
|
17
|
-
import type { UDSentence, UDWord } from 'udpipe-node';
|
|
18
|
-
import { correctUDPipePos } from './calliope/postag.js';
|
|
19
|
-
import { pennTagOf } from './calliope/udpos.js';
|
|
20
|
-
import {
|
|
21
|
-
ClsDocument,
|
|
22
|
-
ClsSentence,
|
|
23
|
-
ClsWord,
|
|
24
|
-
ClsDependency,
|
|
25
|
-
ClsNode,
|
|
26
|
-
} from './types.js';
|
|
27
|
-
|
|
28
|
-
// ── UDPipe instance (lazy singleton) ────────────────────────────────
|
|
29
|
-
// The "./wasm" entry point pre-initialises the WASM runtime via top-level await,
|
|
30
|
-
// so by the time this module is imported the engine is ready and construction /
|
|
31
|
-
// parsing are fully synchronous — `parseDocument` keeps its synchronous contract.
|
|
32
|
-
let _nlp: ReturnType<typeof createUDPipe> | null = null;
|
|
33
|
-
function nlp(): ReturnType<typeof createUDPipe> {
|
|
34
|
-
// CALLIOPE_UDPIPE_MODEL lets us swap the UDPipe model (EWT / GUM / LinES /
|
|
35
|
-
// ParTUT) for auditing — different treebanks tag XPOS quite differently, so the
|
|
36
|
-
// model choice materially affects the parse the phonological pipeline consumes.
|
|
37
|
-
// Unset → the bundled GUM model.
|
|
38
|
-
const modelPath = process.env.CALLIOPE_UDPIPE_MODEL || undefined;
|
|
39
|
-
return (_nlp ??= createUDPipe({ defaultInputMode: 'presegmented', modelPath }));
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// ── POS / punctuation classification (unchanged) ─────────────────────
|
|
43
|
-
|
|
44
|
-
const CONTENT_POS = new Set([
|
|
45
|
-
'NN', 'NNS', 'NNP', 'NNPS',
|
|
46
|
-
'JJ', 'JJR', 'JJS',
|
|
47
|
-
'VB', 'VBD', 'VBG', 'VBN', 'VBP', 'VBZ',
|
|
48
|
-
'RB', 'RBR', 'RBS',
|
|
49
|
-
'CD', // cardinal numbers (content‑like)
|
|
50
|
-
]);
|
|
51
|
-
|
|
52
|
-
/** Punctuation POS tags that should not be syllabified. */
|
|
53
|
-
const PUNCT_TAGS = new Set([
|
|
54
|
-
',', '.', ':', ';', '!', '?',
|
|
55
|
-
'-LRB-', '-RRB-', '``', "''",
|
|
56
|
-
'--', '...', '"', "'",
|
|
57
|
-
'(', ')', '[', ']', '{', '}',
|
|
58
|
-
]);
|
|
59
|
-
|
|
60
|
-
export function isPunctuation(tag: string): boolean {
|
|
61
|
-
return PUNCT_TAGS.has(tag);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Quotation-mark tags. Quotes are tokens (never syllabified) but NOT prosodic
|
|
66
|
-
* breaks: a quoted word inside a clause is read in one breath — no intonational
|
|
67
|
-
* boundary, no caesura.
|
|
68
|
-
*/
|
|
69
|
-
const QUOTE_TAGS = new Set(['``', "''", '"', "'"]);
|
|
70
|
-
|
|
71
|
-
export function isQuoteTag(tag: string): boolean {
|
|
72
|
-
return QUOTE_TAGS.has(tag);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function isContentWord(tag: string): boolean {
|
|
76
|
-
return CONTENT_POS.has(tag);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Lowercase the first alphabetic character of every line. Kept available but
|
|
81
|
-
* NOT called by default — empirically net-negative with UDPipe (see parseDocument).
|
|
82
|
-
*/
|
|
83
|
-
export function lowerLineInitials(text: string): string {
|
|
84
|
-
return text
|
|
85
|
-
.split('\n')
|
|
86
|
-
.map((line) => line.replace(/[A-Za-z]/, (c) => c.toLowerCase()))
|
|
87
|
-
.join('\n');
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// Archaic / Early-Modern English forms the UD model (trained on modern text)
|
|
91
|
-
// systematically mis-tags. This is a closed lexicon of forms that are virtually
|
|
92
|
-
// never modern words, so an unconditional retag is safe domain adaptation for a
|
|
93
|
-
// verse tool (it replaces the role the old en-pos correction layer played for
|
|
94
|
-
// these tokens). Surface forms are matched lowercased, sans apostrophes.
|
|
95
|
-
const ARCHAIC_POS: Record<string, string> = {
|
|
96
|
-
thy: 'PRP$', thine: 'PRP$',
|
|
97
|
-
thee: 'PRP', thou: 'PRP', ye: 'PRP',
|
|
98
|
-
hath: 'VBZ', doth: 'VBZ', saith: 'VBZ',
|
|
99
|
-
hast: 'VBP', dost: 'VBP', wilt: 'MD',
|
|
100
|
-
shalt: 'MD', canst: 'MD', wouldst: 'MD', shouldst: 'MD', couldst: 'MD',
|
|
101
|
-
hadst: 'VBD', didst: 'VBD', wast: 'VBD', wert: 'VBD',
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
// ── Dash handling (unchanged) ────────────────────────────────────────
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Standalone en/em/figure/bar/minus dashes (or a run of 2+ hyphens) are prosodic
|
|
108
|
-
* breaks (a dash caesura), not stress-bearing tokens. Re-tagged to the Penn dash
|
|
109
|
-
* class ':' so they drop out of syllabification & scansion and mark a pause.
|
|
110
|
-
*/
|
|
111
|
-
const DASH_GLYPH_RE = /^(?:[‒–—―−]+|-{2,})$/;
|
|
112
|
-
function isDashGlyph(word: string): boolean {
|
|
113
|
-
return DASH_GLYPH_RE.test(word);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const DASH_CLASS = '‒–—―−';
|
|
117
|
-
const DASH_GLYPHS_RE = new RegExp(`[${DASH_CLASS}]`, 'g');
|
|
118
|
-
const DASH_PAREN_RE = new RegExp(`([${DASH_CLASS}])([^${DASH_CLASS}]*?[.!?][^${DASH_CLASS}]*?)([${DASH_CLASS}])`, 'g');
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Normalize dash *usages* to colon-class clause-breaks BEFORE parsing. A dash is
|
|
122
|
-
* an ι (intonational-unit) boundary — a stronger pause than a comma. We fold
|
|
123
|
-
* every dash usage into a canonical glyph, neutralise sentence-final punctuation
|
|
124
|
-
* inside a dash-delimited parenthetical (so the line stays one sentence), then
|
|
125
|
-
* rewrite the dashes to a colon-class break (which prosodic.ts reads as an ι
|
|
126
|
-
* boundary). Unspaced hyphen compounds ("torch-flames") are left intact.
|
|
127
|
-
*/
|
|
128
|
-
function normalizeDashesToClauseBreaks(text: string): string {
|
|
129
|
-
text = text.replace(/(^|\s)-+(?=\s|$)/g, '$1–');
|
|
130
|
-
text = text.replace(/-{2,}/g, '–');
|
|
131
|
-
text = text.replace(DASH_PAREN_RE, (_m, a, inner, b) => a + inner.replace(/[.!?]+/g, ',') + b);
|
|
132
|
-
text = text.replace(DASH_GLYPHS_RE, ' : ');
|
|
133
|
-
text = text.replace(/(?:\s*:\s*){2,}/g, ' : ')
|
|
134
|
-
.replace(/\s+:/g, ' :')
|
|
135
|
-
.replace(/:(\S)/g, ': $1')
|
|
136
|
-
.replace(/^\s*:\s*/, '')
|
|
137
|
-
.replace(/\s{2,}/g, ' ')
|
|
138
|
-
.trim();
|
|
139
|
-
return text;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
// ── Clitic / contraction re‑merge (UDPipe-specific) ──────────────────
|
|
143
|
-
// UDPipe tokenises contractions and elisions on the apostrophe boundary, e.g.
|
|
144
|
-
// it's → it + 's don't → do + n't we'll → we + 'll
|
|
145
|
-
// th'expense → th' + expense 'Tis → ' + Tis fix'd → fix + 'd
|
|
146
|
-
// For scansion a contraction must be ONE orthographic word (one syllable count,
|
|
147
|
-
// one stress domain). We re-merge using UDPipe's SpaceAfter flag (which marks
|
|
148
|
-
// tokens that were contiguous in the source) plus the apostrophe shape:
|
|
149
|
-
// • a LEFT clitic (apostrophe-initial, or n't) merges into the previous word,
|
|
150
|
-
// EXCEPT the possessive 's (XPOS=POS), which stays split (as it always has);
|
|
151
|
-
// • a RIGHT proclitic (a short apostrophe-final piece like "th'", or a bare
|
|
152
|
-
// leading apostrophe before an aphaeresis like 'tis/'twas) merges into the
|
|
153
|
-
// next word.
|
|
154
|
-
|
|
155
|
-
const APOS = /['’]/;
|
|
156
|
-
const LEFT_CLITIC_RE = /^['’]([a-z]+)?$|^n['’]?t$/i; // 's 've 'll 'd 're 'm n't
|
|
157
|
-
const RIGHT_PROCLITIC_RE = /^[a-z]{1,3}['’]$/i; // th' o' d' ne'
|
|
158
|
-
const APHAERESIS = new Set(['tis', 'twas', 'twere', 'twill', 'twould', 'gainst', 'neath', 'tween', 'twixt', 'til', 'cause', 'em', 'round', 'bout']);
|
|
159
|
-
|
|
160
|
-
interface Cluster {
|
|
161
|
-
tokens: UDWord[];
|
|
162
|
-
repr: UDWord; // the token that carries the syntactic role / POS
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/** Group UDPipe words into orthographic clusters, re-merging clitics. */
|
|
166
|
-
function clusterWords(uds: UDWord[]): { clusters: Cluster[]; idToCluster: Map<number, number> } {
|
|
167
|
-
const idToCluster = new Map<number, number>();
|
|
168
|
-
const clusters: Cluster[] = [];
|
|
169
|
-
|
|
170
|
-
for (let i = 0; i < uds.length; i++) {
|
|
171
|
-
const w = uds[i];
|
|
172
|
-
const prev = uds[i - 1];
|
|
173
|
-
const contiguous = prev ? prev.spaceAfter === false : false;
|
|
174
|
-
const isLeftClitic =
|
|
175
|
-
contiguous && w.xpos !== 'POS' && LEFT_CLITIC_RE.test(w.form);
|
|
176
|
-
|
|
177
|
-
if (isLeftClitic && clusters.length > 0) {
|
|
178
|
-
clusters[clusters.length - 1].tokens.push(w);
|
|
179
|
-
idToCluster.set(w.id, clusters.length - 1);
|
|
180
|
-
continue;
|
|
181
|
-
}
|
|
182
|
-
clusters.push({ tokens: [w], repr: w });
|
|
183
|
-
idToCluster.set(w.id, clusters.length - 1);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
// Right-merge pass: a cluster that is a lone proclitic (th') or a bare leading
|
|
187
|
-
// apostrophe before an aphaeresis ('tis) folds into the following cluster.
|
|
188
|
-
const merged: Cluster[] = [];
|
|
189
|
-
for (let c = 0; c < clusters.length; c++) {
|
|
190
|
-
const cl = clusters[c];
|
|
191
|
-
const next = clusters[c + 1];
|
|
192
|
-
const onlyTok = cl.tokens.length === 1 ? cl.tokens[0] : null;
|
|
193
|
-
const contiguous = onlyTok ? onlyTok.spaceAfter === false : false;
|
|
194
|
-
const nextWord = next?.repr;
|
|
195
|
-
|
|
196
|
-
const isProclitic =
|
|
197
|
-
!!onlyTok && contiguous && !!nextWord &&
|
|
198
|
-
(RIGHT_PROCLITIC_RE.test(onlyTok.form) ||
|
|
199
|
-
(/^['’]$/.test(onlyTok.form) && APHAERESIS.has(nextWord.form.toLowerCase())));
|
|
200
|
-
|
|
201
|
-
if (isProclitic && next) {
|
|
202
|
-
next.tokens.unshift(onlyTok!); // prepend proclitic
|
|
203
|
-
for (const t of cl.tokens) idToCluster.set(t.id, merged.length); // re-point to next cluster's eventual index
|
|
204
|
-
// The next cluster will be pushed next iteration; fix its index mapping then.
|
|
205
|
-
// Mark by leaving cl out (skip pushing it).
|
|
206
|
-
// Re-point all of next's tokens to current merged length too:
|
|
207
|
-
continue;
|
|
208
|
-
}
|
|
209
|
-
merged.push(cl);
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
// Rebuild idToCluster cleanly against the merged list (indices shifted by right-merges).
|
|
213
|
-
idToCluster.clear();
|
|
214
|
-
for (let c = 0; c < merged.length; c++) {
|
|
215
|
-
for (const t of merged[c].tokens) idToCluster.set(t.id, c);
|
|
216
|
-
// representative = first token that is neither a left-clitic nor a proclitic
|
|
217
|
-
merged[c].repr =
|
|
218
|
-
merged[c].tokens.find(
|
|
219
|
-
(t) => !(t.xpos !== 'POS' && LEFT_CLITIC_RE.test(t.form)) && !RIGHT_PROCLITIC_RE.test(t.form) && !/^['’]$/.test(t.form),
|
|
220
|
-
) ?? merged[c].tokens[0];
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
return { clusters: merged, idToCluster };
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
// Dependency labels are passed through to `ClsDependency.dependentType` as RAW
|
|
227
|
-
// Universal Dependencies relations (obl, nsubj:pass, compound, nmod:poss, …) —
|
|
228
|
-
// they are deliberately NOT folded into the old Stanford names. The canonical
|
|
229
|
-
// normaliser `calliope/deps.ts` maps every UD relation onto the engine's Scenario
|
|
230
|
-
// label space (canonicalRel), so new UD tags are accommodated there, not hidden
|
|
231
|
-
// here.
|
|
232
|
-
|
|
233
|
-
// ── Public API ───────────────────────────────────────────────────────
|
|
234
|
-
|
|
235
|
-
export function parseDocument(text: string): ClsDocument {
|
|
236
|
-
// Normalise curly/typographic apostrophes to straight ' so contractions and
|
|
237
|
-
// elisions tokenise identically regardless of glyph.
|
|
238
|
-
text = text.replace(/[‘’ʼ′]/g, "'");
|
|
239
|
-
// Collapse runs of sentence-final punctuation (ellipsis, "!!") to a single mark.
|
|
240
|
-
text = text.replace(/([.!?])\1+/g, '$1');
|
|
241
|
-
// Dashes → colon-class clause-breaks (see helper above).
|
|
242
|
-
text = normalizeDashesToClauseBreaks(text);
|
|
243
|
-
|
|
244
|
-
// NOTE on line-initial caps: lowering the first letter of each line before
|
|
245
|
-
// tagging (the role the old `normalizeCaps` played) was tested and is NET
|
|
246
|
-
// NEGATIVE with UDPipe — it recovers cases like "Nap"/"Gap" (UH→NN) but a
|
|
247
|
-
// line-initial capital often HELPS UDPipe's parse (e.g. "Through Eden took…"
|
|
248
|
-
// parses "Eden" as nsubj when capitalised, obl when lowercased), so it changes
|
|
249
|
-
// more scansions than it fixes. Left disabled; see lowerLineInitials() below.
|
|
250
|
-
|
|
251
|
-
const udSentences: UDSentence[] = nlp().parse(text, { inputMode: 'presegmented' });
|
|
252
|
-
|
|
253
|
-
const sentences: ClsSentence[] = [];
|
|
254
|
-
let absoluteOffset = 0;
|
|
255
|
-
|
|
256
|
-
udSentences.forEach((ud, si) => {
|
|
257
|
-
const { clusters, idToCluster } = clusterWords(ud.words);
|
|
258
|
-
|
|
259
|
-
// ---- 1. Build ClsWord array ----
|
|
260
|
-
const words: ClsWord[] = clusters.map((cl, i) => {
|
|
261
|
-
// Preserve the ORIGINAL case of the surface form (UDPipe keeps it); only
|
|
262
|
-
// lowercase a private lookup key for the archaic-lexicon / dash checks.
|
|
263
|
-
// Lowercasing `word` itself lost every proper-noun capital ("pakistan",
|
|
264
|
-
// "marcel proust") in the display and projection; downstream stress/name
|
|
265
|
-
// lookups all lowercase internally, so case in `word` is display-only.
|
|
266
|
-
const surfaceRaw = cl.tokens.map((t) => t.form).join('');
|
|
267
|
-
const surface = surfaceRaw.toLowerCase();
|
|
268
|
-
// Penn tag: use the raw XPOS when it already is Penn (EWT/GUM), else derive
|
|
269
|
-
// it from UPOS+FEATS (LinES/ParTUT emit non-Penn XPOS the pipeline can't read).
|
|
270
|
-
const rawTag = pennTagOf(cl.repr);
|
|
271
|
-
const archaic = ARCHAIC_POS[surface.replace(/['’]/g, '')];
|
|
272
|
-
const tag = isDashGlyph(surfaceRaw) ? ':' : (archaic ?? rawTag);
|
|
273
|
-
return {
|
|
274
|
-
index: i + 1,
|
|
275
|
-
lexicalClass: tag,
|
|
276
|
-
lexicalDetails: cl.repr.feats,
|
|
277
|
-
lexicalPlural: tag === 'NNS' || tag === 'NNPS',
|
|
278
|
-
position: '',
|
|
279
|
-
word: surfaceRaw,
|
|
280
|
-
absoluteIndex: absoluteOffset + i,
|
|
281
|
-
isContent: isContentWord(tag),
|
|
282
|
-
syllables: [],
|
|
283
|
-
phraseStress: 0,
|
|
284
|
-
dependency: undefined,
|
|
285
|
-
node: undefined,
|
|
286
|
-
};
|
|
287
|
-
});
|
|
288
|
-
|
|
289
|
-
// Sentence-initial de-capitalisation (mirrors en-norm.normalizeCaps in the
|
|
290
|
-
// pre-UD path): lower the first letter of the sentence's first word UNLESS it
|
|
291
|
-
// is a proper noun, so "The"→"the" and "I"→"i" (an orthographic capital forced
|
|
292
|
-
// by line position carries no lexical signal) while mid-line proper nouns
|
|
293
|
-
// ("Marcel Proust", "Pakistan") keep their caps for display/projection.
|
|
294
|
-
for (const w of words) {
|
|
295
|
-
if (isPunctuation(w.lexicalClass)) continue;
|
|
296
|
-
if (!/^(NNP|NNPS)$/.test(w.lexicalClass) && /^[A-Z]/.test(w.word)) {
|
|
297
|
-
w.displayWord = w.word; // keep the original surface for reports/phonopoetics
|
|
298
|
-
w.word = w.word[0].toLowerCase() + w.word.slice(1);
|
|
299
|
-
}
|
|
300
|
-
break;
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
// ---- 2. Build ClsDependency array ----
|
|
304
|
-
const dependencies: ClsDependency[] = [];
|
|
305
|
-
clusters.forEach((cl, depIdx) => {
|
|
306
|
-
const r = cl.repr;
|
|
307
|
-
let govIdx: number | undefined;
|
|
308
|
-
if (r.head === 0) {
|
|
309
|
-
govIdx = undefined; // attaches to root
|
|
310
|
-
} else {
|
|
311
|
-
const g = idToCluster.get(r.head);
|
|
312
|
-
// If the representative's head fell inside its own cluster (e.g. a copula
|
|
313
|
-
// clitic), follow that clitic's head out of the cluster.
|
|
314
|
-
if (g === depIdx) {
|
|
315
|
-
const external = cl.tokens
|
|
316
|
-
.map((t) => idToCluster.get(t.head))
|
|
317
|
-
.find((gi) => gi !== undefined && gi !== depIdx);
|
|
318
|
-
govIdx = external;
|
|
319
|
-
} else {
|
|
320
|
-
govIdx = g;
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
const depWord = words[depIdx];
|
|
325
|
-
const govWord = govIdx !== undefined ? words[govIdx] : null;
|
|
326
|
-
|
|
327
|
-
dependencies.push({
|
|
328
|
-
index: depIdx + 1,
|
|
329
|
-
governorIndex: govIdx !== undefined ? govIdx + 1 : 0,
|
|
330
|
-
dependentIndex: depIdx + 1,
|
|
331
|
-
dependentType: govIdx === undefined ? 'root' : r.deprel,
|
|
332
|
-
governorName: govWord ? govWord.word : 'ROOT',
|
|
333
|
-
dependentName: depWord.word,
|
|
334
|
-
governor: govWord as unknown as ClsWord,
|
|
335
|
-
dependent: depWord,
|
|
336
|
-
});
|
|
337
|
-
});
|
|
338
|
-
|
|
339
|
-
// Ensure a ROOT dependency exists.
|
|
340
|
-
if (!dependencies.some((d) => d.governorIndex === 0) && words.length > 0) {
|
|
341
|
-
dependencies.push({
|
|
342
|
-
index: 0,
|
|
343
|
-
governorIndex: 0,
|
|
344
|
-
dependentIndex: 1,
|
|
345
|
-
dependentType: 'root',
|
|
346
|
-
governorName: 'ROOT',
|
|
347
|
-
dependentName: words[0].word,
|
|
348
|
-
governor: null as unknown as ClsWord,
|
|
349
|
-
dependent: words[0],
|
|
350
|
-
});
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
// Back‑reference: each word stores the dependency edge where it is dependent.
|
|
354
|
-
words.forEach((w) => {
|
|
355
|
-
w.dependency = dependencies.find((d) => d.dependent === w);
|
|
356
|
-
});
|
|
357
|
-
|
|
358
|
-
// UDPipe XPOS correction (the role en-pos + tagfix.ts played pre-UD): fix the
|
|
359
|
-
// systematic mis-tags UDPipe makes on terse, decontextualised verse via
|
|
360
|
-
// en-lexicon cross-check. Runs HERE (in the parser, after deps are attached)
|
|
361
|
-
// so direct `parseDocument` consumers — and every engine — see corrected tags;
|
|
362
|
-
// rule (3) needs the dependency back-references just set above.
|
|
363
|
-
correctUDPipePos({ index: si + 1, nodes: null, dependencies, words, xml: '' });
|
|
364
|
-
|
|
365
|
-
// ---- 3. Build phrase‑structure node tree from the dependency graph ----
|
|
366
|
-
const rootNode = buildDepNodeTree(words, dependencies);
|
|
367
|
-
const wordNodeMap = new Map<number, ClsNode>();
|
|
368
|
-
collectWordNodes(rootNode, wordNodeMap);
|
|
369
|
-
words.forEach((w) => {
|
|
370
|
-
w.node = wordNodeMap.get(w.index);
|
|
371
|
-
});
|
|
372
|
-
|
|
373
|
-
sentences.push({
|
|
374
|
-
index: si + 1,
|
|
375
|
-
nodes: rootNode,
|
|
376
|
-
dependencies,
|
|
377
|
-
words,
|
|
378
|
-
xml: '',
|
|
379
|
-
});
|
|
380
|
-
|
|
381
|
-
absoluteOffset += words.length;
|
|
382
|
-
});
|
|
383
|
-
|
|
384
|
-
return { sentences, xml: '' };
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
// ── Dependency → constituency projection ─────────────────────────────
|
|
388
|
-
// phonological.ts groups clitic groups into phonological phrases by finding the
|
|
389
|
-
// smallest phrase node containing them, so it needs a properly nested, position-
|
|
390
|
-
// ordered constituency tree. We synthesise one by projection: each head plus its
|
|
391
|
-
// dependent subtrees forms a phrase, labelled by the head's POS family.
|
|
392
|
-
|
|
393
|
-
function phraseType(tag: string): string {
|
|
394
|
-
if (/^(NN|NNS|NNP|NNPS|PRP|PRP\$|DT|CD|WP|WDT|EX)$/.test(tag)) return 'NP';
|
|
395
|
-
if (/^(VB|VBD|VBG|VBN|VBP|VBZ|MD)$/.test(tag)) return 'VP';
|
|
396
|
-
if (/^(IN|TO)$/.test(tag)) return 'PP';
|
|
397
|
-
if (/^(JJ|JJR|JJS)$/.test(tag)) return 'ADJP';
|
|
398
|
-
if (/^(RB|RBR|RBS|WRB)$/.test(tag)) return 'ADVP';
|
|
399
|
-
return 'XP';
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
function buildDepNodeTree(words: ClsWord[], deps: ClsDependency[]): ClsNode {
|
|
403
|
-
const sq: ClsNode = { index: '1', nodeName: 'SQ', parent: null, contains: [] };
|
|
404
|
-
if (words.length === 0) return sq;
|
|
405
|
-
|
|
406
|
-
// children[g] = list of dependent word-indices (1-based) governed by g (1-based);
|
|
407
|
-
// roots are governed by 0.
|
|
408
|
-
const children = new Map<number, number[]>();
|
|
409
|
-
for (const d of deps) {
|
|
410
|
-
if (d.dependentIndex < 1 || d.dependentIndex > words.length) continue;
|
|
411
|
-
const g = d.governorIndex;
|
|
412
|
-
if (!children.has(g)) children.set(g, []);
|
|
413
|
-
children.get(g)!.push(d.dependentIndex);
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
const build = (wordIdx: number, parent: ClsNode): ClsNode => {
|
|
417
|
-
const word = words[wordIdx - 1];
|
|
418
|
-
const kids = (children.get(wordIdx) ?? []).filter((k) => k !== wordIdx);
|
|
419
|
-
if (kids.length === 0) {
|
|
420
|
-
const leaf = createWordLeaf(word);
|
|
421
|
-
leaf.parent = parent;
|
|
422
|
-
return leaf;
|
|
423
|
-
}
|
|
424
|
-
const node: ClsNode = {
|
|
425
|
-
index: `ph_${wordIdx}`,
|
|
426
|
-
nodeName: phraseType(word.lexicalClass),
|
|
427
|
-
parent,
|
|
428
|
-
contains: [],
|
|
429
|
-
};
|
|
430
|
-
// Order head + dependents by surface position for a projective tree.
|
|
431
|
-
const ordered = [...kids, wordIdx].sort((a, b) => a - b);
|
|
432
|
-
for (const idx of ordered) {
|
|
433
|
-
if (idx === wordIdx) {
|
|
434
|
-
const leaf = createWordLeaf(word);
|
|
435
|
-
leaf.parent = node;
|
|
436
|
-
node.contains.push(leaf);
|
|
437
|
-
} else {
|
|
438
|
-
node.contains.push(build(idx, node));
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
return node;
|
|
442
|
-
};
|
|
443
|
-
|
|
444
|
-
const roots = (children.get(0) ?? []).sort((a, b) => a - b);
|
|
445
|
-
if (roots.length === 0) {
|
|
446
|
-
// No explicit root: attach all words as leaves under SQ.
|
|
447
|
-
for (const w of words) {
|
|
448
|
-
const leaf = createWordLeaf(w);
|
|
449
|
-
leaf.parent = sq;
|
|
450
|
-
sq.contains.push(leaf);
|
|
451
|
-
}
|
|
452
|
-
return sq;
|
|
453
|
-
}
|
|
454
|
-
for (const r of roots) {
|
|
455
|
-
const child = build(r, sq);
|
|
456
|
-
sq.contains.push(child);
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
// Attach any orphan words (rare) directly under SQ.
|
|
460
|
-
const attached = new Set<number>();
|
|
461
|
-
collectAttachedWordIndices(sq, attached);
|
|
462
|
-
for (const w of words) {
|
|
463
|
-
if (!attached.has(w.index)) {
|
|
464
|
-
const leaf = createWordLeaf(w);
|
|
465
|
-
leaf.parent = sq;
|
|
466
|
-
sq.contains.push(leaf);
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
return sq;
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
// ── Leaf / traversal helpers (unchanged) ─────────────────────────────
|
|
473
|
-
|
|
474
|
-
function createWordLeaf(word: ClsWord): ClsNode {
|
|
475
|
-
return {
|
|
476
|
-
index: `w${word.index}`,
|
|
477
|
-
nodeName: word.index.toString(),
|
|
478
|
-
parent: null,
|
|
479
|
-
contains: [word],
|
|
480
|
-
};
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
function collectWordNodes(node: ClsNode, map: Map<number, ClsNode>): void {
|
|
484
|
-
for (const child of node.contains) {
|
|
485
|
-
if (child instanceof Object && 'word' in (child as any)) {
|
|
486
|
-
map.set((child as ClsWord).index, node);
|
|
487
|
-
} else if (child instanceof Object && 'index' in (child as any)) {
|
|
488
|
-
collectWordNodes(child as ClsNode, map);
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
function collectAttachedWordIndices(node: ClsNode, set: Set<number>): void {
|
|
494
|
-
for (const child of node.contains) {
|
|
495
|
-
if (child instanceof Object && 'word' in (child as any)) {
|
|
496
|
-
set.add((child as ClsWord).index);
|
|
497
|
-
} else if (child instanceof Object && 'index' in (child as any)) {
|
|
498
|
-
collectAttachedWordIndices(child as ClsNode, set);
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
}
|