twl-generator 1.4.14 → 1.4.16
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/package.json +2 -2
- package/src/index.js +8 -4
- package/src/utils/twl-matcher.js +44 -7
- package/src/utils/usfm-alignment-remover.js +19 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "twl-generator",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.16",
|
|
4
4
|
"description": "Generate term-to-article lists from unfoldingWord en_tw archive for Bible books. Works in both Node.js (CLI) and React.js (browser) environments.",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"bin": {
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
"csv-stringify": "^6.5.0",
|
|
52
52
|
"en-inflectors": "^1.0.12",
|
|
53
53
|
"jszip": "^3.10.1",
|
|
54
|
-
"tsv-quote-converters": "^1.1.
|
|
54
|
+
"tsv-quote-converters": "^1.1.21",
|
|
55
55
|
"usfm-alignment-remover": "^0.1.6"
|
|
56
56
|
},
|
|
57
57
|
"peerDependencies": {
|
package/src/index.js
CHANGED
|
@@ -903,8 +903,10 @@ export async function generateTwlByBook(bookCode, options = {}) {
|
|
|
903
903
|
const chapterNums = Object.keys(versesByChapter).map(n => parseInt(n, 10)).sort((a, b) => a - b);
|
|
904
904
|
for (const c of chapterNums) {
|
|
905
905
|
const verses = versesByChapter[c] || {};
|
|
906
|
-
const verseNums = Object.keys(verses).map(n => parseInt(n, 10)).sort((a, b) => a - b);
|
|
907
|
-
|
|
906
|
+
const verseNums = Object.keys(verses).filter(k => k !== 'front').map(n => parseInt(n, 10)).sort((a, b) => a - b);
|
|
907
|
+
// Chapter front matter (\d) is emitted as `${c}:front`, ordered before verse 1.
|
|
908
|
+
const orderedKeys = verses.front ? ['front', ...verseNums] : verseNums;
|
|
909
|
+
for (const v of orderedKeys) {
|
|
908
910
|
const text = verses[v] || '';
|
|
909
911
|
const matches = scanVerseMatches(text, trie);
|
|
910
912
|
// Count occurrences per exact matchedText (case-sensitive)
|
|
@@ -922,8 +924,10 @@ export async function generateTwlByBook(bookCode, options = {}) {
|
|
|
922
924
|
else if (primaryArticle.startsWith('names/')) tag = 'name';
|
|
923
925
|
const twLink = primaryArticle ? `rc://*/tw/dict/bible/${primaryArticle}` : '';
|
|
924
926
|
|
|
925
|
-
// Variant of: only if beyond plural/-ed/-ing differences
|
|
926
|
-
|
|
927
|
+
// Variant of: only if beyond plural/-ed/-ing differences. Compare on the
|
|
928
|
+
// brace-free reading so a supplied-morpheme brace (e.g. "creature{s}")
|
|
929
|
+
// isn't itself counted as a difference from the term.
|
|
930
|
+
const variantOf = allowNoVariant(m.term, glq.replace(/[{}]/g, '')) ? '' : m.term;
|
|
927
931
|
// Disambiguation: list all candidate articles for this match
|
|
928
932
|
const disamb = (m.articles && m.articles.length > 1) ? `(${m.articles.join(', ')})` : '';
|
|
929
933
|
|
package/src/utils/twl-matcher.js
CHANGED
|
@@ -137,6 +137,15 @@ class PrefixTrie {
|
|
|
137
137
|
while (currentPos < searchText.length) {
|
|
138
138
|
const char = searchText[currentPos];
|
|
139
139
|
|
|
140
|
+
// Curly braces wrap "supplied" words/morphemes in the ULT (e.g.
|
|
141
|
+
// "creature{s}"). Match through them transparently (they never appear in
|
|
142
|
+
// a trie term) so "creature{s}" matches the term "creatures"; the braces
|
|
143
|
+
// are re-included when the matched span is extracted below.
|
|
144
|
+
if ((char === '{' || char === '}') && currentPos > startPos) {
|
|
145
|
+
currentPos++;
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
|
|
140
149
|
if (!node[char]) {
|
|
141
150
|
break; // No more matches possible
|
|
142
151
|
}
|
|
@@ -192,14 +201,42 @@ class PrefixTrie {
|
|
|
192
201
|
originalMatchedText = originalText.substring(extendedStartPos, extendedEndPos);
|
|
193
202
|
}
|
|
194
203
|
|
|
195
|
-
//
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
204
|
+
// Balance curly braces so a brace split across the match boundary keeps
|
|
205
|
+
// its partner. Matching "creature{s}" through the brace stops the span
|
|
206
|
+
// at "creature{s" (open '{' with no '}'); pull in the trailing '}' so
|
|
207
|
+
// OrigWords becomes "creature{s}". Symmetric for a leading '{'.
|
|
208
|
+
let open = 0, close = 0;
|
|
209
|
+
for (const ch of originalText.substring(extendedStartPos, extendedEndPos)) {
|
|
210
|
+
if (ch === '{') open++;
|
|
211
|
+
else if (ch === '}') close++;
|
|
212
|
+
}
|
|
213
|
+
while (open > close && extendedEndPos < originalText.length && originalText[extendedEndPos] === '}') {
|
|
214
|
+
extendedEndPos++; close++;
|
|
215
|
+
}
|
|
216
|
+
while (close > open && extendedStartPos > 0 && originalText[extendedStartPos - 1] === '{') {
|
|
217
|
+
extendedStartPos--; open++;
|
|
218
|
+
}
|
|
219
|
+
if (extendedStartPos < startPos || extendedEndPos > currentPos) {
|
|
220
|
+
originalMatchedText = originalText.substring(extendedStartPos, extendedEndPos);
|
|
221
|
+
}
|
|
199
222
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
223
|
+
// Check if this is a valid word boundary match (both start and end).
|
|
224
|
+
// Skip past any braces when locating the neighbouring character so a
|
|
225
|
+
// supplied-word brace does not act as a false word boundary: the
|
|
226
|
+
// brace-free reading ("creatures") must be treated as one word, so a
|
|
227
|
+
// bare "creature" before the "{s}" is correctly rejected.
|
|
228
|
+
let beforePos = extendedStartPos - 1;
|
|
229
|
+
while (beforePos >= 0 && (originalText[beforePos] === '{' || originalText[beforePos] === '}')) beforePos--;
|
|
230
|
+
let afterPos = extendedEndPos;
|
|
231
|
+
while (afterPos < originalText.length && (originalText[afterPos] === '{' || originalText[afterPos] === '}')) afterPos++;
|
|
232
|
+
|
|
233
|
+
const isStartBoundary = beforePos < 0 ||
|
|
234
|
+
/[\s\p{P}]/u.test(originalText[beforePos]) ||
|
|
235
|
+
!/[\w]/.test(originalText[beforePos]);
|
|
236
|
+
|
|
237
|
+
const isEndBoundary = afterPos >= originalText.length ||
|
|
238
|
+
/[\s\p{P}]/u.test(originalText[afterPos]) ||
|
|
239
|
+
!/[\w]/.test(originalText[afterPos]);
|
|
203
240
|
|
|
204
241
|
const isWordBoundary = isStartBoundary && isEndBoundary;
|
|
205
242
|
|
|
@@ -45,11 +45,18 @@ export const removeAllTagsExceptChapterVerse = (usfmContent) => {
|
|
|
45
45
|
cleanContent = cleanContent.replace(/ +\\v +/g, '\n\\v ');
|
|
46
46
|
cleanContent = cleanContent.replace(/ +\\c +/g, '\n\\c ');
|
|
47
47
|
cleanContent = cleanContent.replace(/ *(\\q\d*|\\p|\\ts\\\*) */g, ' ');
|
|
48
|
-
|
|
48
|
+
// Strip section headings (\s, \s1, \sr, \sp, etc.) but preserve \d (chapter
|
|
49
|
+
// descriptions / psalm superscriptions) so front-matter TWLs can be generated.
|
|
50
|
+
cleanContent = cleanContent.replace(/\\s.*?(\\|\n)/g, '$1');
|
|
49
51
|
cleanContent = cleanContent.replace(/ +/g, ' ');
|
|
50
52
|
cleanContent = cleanContent.replace(/^ +$/g, '');
|
|
51
53
|
cleanContent = cleanContent.replace(/\\f .*?\\f\*/g, ' ');
|
|
52
|
-
|
|
54
|
+
// NOTE: Curly braces ({ }) wrap "supplied" words/morphemes in the ULT (e.g.
|
|
55
|
+
// "creature{s}") that the matcher must see through but must NOT discard. They
|
|
56
|
+
// are preserved here so the matcher can match the brace-free reading
|
|
57
|
+
// ("creatures") yet retain the braces in OrigWords ("creature{s}"), which is
|
|
58
|
+
// what tsv-quote-converters needs to align the quote back to the original
|
|
59
|
+
// language. Brace-transparency lives in twl-matcher.js (findMatches).
|
|
53
60
|
|
|
54
61
|
// Remove all lines before the first \c marker, keeping the \c line
|
|
55
62
|
const lines = cleanContent.split('\n');
|
|
@@ -107,6 +114,16 @@ export function parseUsfmToVerses(usfm) {
|
|
|
107
114
|
if (!versesObj[currentChapter]) {
|
|
108
115
|
versesObj[currentChapter] = {};
|
|
109
116
|
}
|
|
117
|
+
// Capture chapter front matter (\d description / psalm superscription) so it
|
|
118
|
+
// can produce `<chapter>:front` TWL rows. Other pre-verse markers (\s, \q, \p)
|
|
119
|
+
// have already been stripped, leaving the \d text in the chapter head.
|
|
120
|
+
const frontMatch = text.match(/\\d\s+([^\\]*)/);
|
|
121
|
+
if (frontMatch) {
|
|
122
|
+
const frontText = frontMatch[1].replace(/\s+/g, ' ').trim();
|
|
123
|
+
if (frontText) {
|
|
124
|
+
versesObj[currentChapter].front = frontText;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
110
127
|
} else if (tag === 'v') {
|
|
111
128
|
if (!versesObj[currentChapter]) {
|
|
112
129
|
versesObj[currentChapter] = {};
|