-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.js
1271 lines (1217 loc) · 36.7 KB
/
generator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
////
//// This file is very long.
//// It is used from the HTML page, or can be run by itself in Node.
//// It has a section at the end that only runs in Node.
//// The Node-only section reads from a hardcoded filepath, writes to a second
//// hardcoded filepath, and compares the output to a third hardcoded filepath.
////
//// Contents:
//// - Schema of output data
//// - Constants and helper functions
//// - Object of functions replacing the fields in the `wordsform` sheet of the velut Excel file
//// - Same object of functions, but memoised
//// - Functions for building the output Json
//// - Node-only code
////
////
//// Schema of output data:
////
const wordsSchema = {
Ord: 'int',
Word: 'string',
// Lemmata: 'string',
Length: 'int',
AllConsonants: 'string',
Uncompounded: 'string',
Phonetic: 'string',
Scansion: 'string',
ScansionWithElision: 'string',
IsFitForDactyl: 'int',
AllVowels: 'string',
SyllableCount: 'int',
Stress: 'int',
UltimaRhyme: 'string',
RhymeVowels: 'string',
PerfectRhyme: 'string',
RhymeConsonants: 'string',
// Ultima: 'string',
RhymeVowelsAndUltimaCoda: 'string',
EcclesPhonetic: 'string',
EcclesVowels: 'string',
EcclesRhymeVowels: 'string',
EcclesRhymeVowelsAndUltimaCoda: 'string',
EcclesPerfectRhyme: 'string',
EcclesSort: 'string',
LemmaCount: 'int',
// Lemma1: 'string',
// Lemma2: 'string',
// Lemma3: 'string',
// Lemma4: 'string',
// Lemma5: 'string',
LemmaArray: 'array',
IsLemma: 'int',
IsNonLemma: 'int',
// DuplicateWords: 'string',
// NewLemmata: 'string',
NoMacra: 'string',
NoMacraLowerCase: 'string',
AlphOrderNoMacra: 'string',
Sort: 'string',
// RepeatWord: 'string',
};
////
//// Constants and helper functions:
////
const phoneticExceptions = {
'Diphthong eu': [
'Aegeus',
'Andreus',
'Atreus',
'būleuta',
'būleutērion',
'Caeneus',
'Deucaliōn',
'Epimētheus',
'Leucōn',
'Leuconoē',
'Theseus',
'halipleumōn',
'Morpheus',
'Narseus',
'Nȳseus',
'Orpheus',
'Pēleus',
'Perseus',
'Phorōneus',
'Pīraeeus',
'Pittheus',
'proceleusmaticus',
'Promētheus',
'Prōteus',
'Seleucīa',
'Seleucus',
'Sinōpeus',
'toreuma',
'zeugma',
'Typhōeus',
'Rhoeteus',
'Patareus',
'Eurystheus',
'Lynceus',
'Tēreus',
'Phīneus',
'Leucippus',
'Erechtheus',
'Enīpeus',
],
'Vocalic initial i': [
'eō',
'Iālysius',
'Iālysus',
'iambus',
'Ianthē',
'ianthinus',
'Iāsōn',
'Iāsonius',
'Iāsonidēs',
'Ïēsus',
'Iobatēs',
'Iōnia',
'Iōnicus',
'iōnicus',
'Iōnius',
'iōta',
'iūlis',
'Iūlus',
'iūlus',
'iynx',
'Iambē',
'iaspis',
'iō',
],
};
// Eg 'rāia' => 'rājja' because the “i” is consonantal.
const replaceIntervocalicI = (text) => {
return `${text}`.replace(/(?<=[āēīōūȳ])i(?=[aeiouyāēīōūȳ])/gi, 'jj');
};
// Eg 'velut' => 'tulev'
const reverseString = (text) => {
return `${text}`.split('').reverse().join('');
};
// Defining a value then emptying the array gives us IntelliSense without TypeScript :)
const existingWords = [{ word: 'string', lemmata: ['string'] }].filter(
(_) => false
);
let allWordsOnlyWord = ['string'].filter((_) => false);
const addToWordsArray = (word, lemmata) => {
existingWords.push({ word, lemmata });
};
const clearWordsArray = () => {
existingWords.length = 0;
};
// Constant used when a field would be the empty string, such as the consonants in a word of all vowels.
const EMPTY = '∅';
// Syllable length symbols.
const SHORT = '⏑';
const LONG = '–';
// A dactylic hexameter fits the regex /–(–|⏑⏑)–(–|⏑⏑)–(–|⏑⏑)–(–|⏑⏑)–⏑⏑–[–⏑]/
// Another way of writing that is /(––|–⏑⏑){4}–⏑⏑–[–⏑]/
// `allValidHexameters` is all the possible variations.
// However, the last syllable is excluded, because IsFitForDactyl
// ignores the last syllable of the word it tests,
// but this would need to be included in the line.
const allValidHexameters = (() => {
const DACTYL = LONG + SHORT + SHORT;
const SPONDEE = LONG + LONG;
return [DACTYL, SPONDEE]
.flatMap((foot) => [foot + DACTYL, foot + SPONDEE])
.flatMap((foot) => [foot + DACTYL, foot + SPONDEE])
.flatMap((foot) => [foot + DACTYL, foot + SPONDEE])
.flatMap((foot) => [foot + DACTYL])
.flatMap((foot) => [foot + LONG]);
})();
const memoisedData = {};
const memoise = (func, functionName, word, lemmata) => {
if (memoisedData[word]?.[functionName] === undefined) {
const wordObject = { ...memoisedData[word] };
wordObject[functionName] = func(word, lemmata);
memoisedData[word] = wordObject;
}
return memoisedData[word][functionName];
};
const clearMemoisationCache = () => {
for (const key in memoisedData) {
delete memoisedData[key];
}
};
////
//// Functions replacing the fields in `wordsform` sheet:
////
const unmemoisedFuncs = {
Ord: (word, lemmata) => {
return existingWords.length + 1;
},
Word: (word, lemmata) => {
return word;
},
Lemmata: (word, lemmata) => {
return lemmata;
},
Length: (word, lemmata) => {
return word.length;
},
AllConsonants: (word, lemmata) => {
const replaced = f.NoMacraLowerCase(word).replace(/[aeiouy]/gi, '');
return replaced || '-';
},
Uncompounded: (word, lemmata) => {
if (['-ne', '-que', '-ve'].includes(word)) {
return EMPTY;
}
if (f.Lemma1(word, lemmata).endsWith('que')) {
return word;
}
if (!word.endsWith('ne') && !word.endsWith('que') && !word.endsWith('ve')) {
return word;
}
const wordMinusPossibleEnclitic = word.replace(/(ne|que|ve)$/, '');
if (allWordsOnlyWord.includes(wordMinusPossibleEnclitic)) {
return wordMinusPossibleEnclitic;
}
if (
['á', 'é', 'í', 'ó', 'ú', 'ý', 'ḗ', 'ü'].some((acute) =>
wordMinusPossibleEnclitic.includes(acute)
)
) {
const removeAcutes = (word) => {
return word
.replace(/á(?=[^aeiouyāēīōūȳ]*)$/, 'a')
.replace(/é(?=[^aeiouyāēīōūȳ]*)$/, 'e')
.replace(/í(?=[^aeiouyāēīōūȳ]*)$/, 'i')
.replace(/ó(?=[^aeiouyāēīōūȳ]*)$/, 'o')
.replace(/ú(?=[^aeiouyāēīōūȳ]*)$/, 'u')
.replace(/ý(?=[^aeiouyāēīōūȳ]*)$/, 'y')
.replace(/ḗ(?=[^aeiouyāēīōūȳ]*)$/, 'ē');
};
const wordMinusPossibleEncliticWithoutAccents = removeAcutes(
wordMinusPossibleEnclitic
).replaceAll('ü', 'u');
if (allWordsOnlyWord.includes(wordMinusPossibleEncliticWithoutAccents)) {
return wordMinusPossibleEncliticWithoutAccents;
}
}
return word;
},
Enclitic: (word, lemmata) => {
if (word.length === f.Uncompounded(word, lemmata).length) {
return '';
}
if (word.endsWith('que')) {
return 'que';
}
if (word.endsWith('ne')) {
return 'ne';
}
if (word.endsWith('ve')) {
return 've';
}
console.error(
'Uncompounded is different to Word but Word doesn’t end with an enclitic.'
);
},
UncompoundedPhonetic: (word, lemmata) => {
const getPhoneticBeforeGeneralSubstitutions = () => {
const uncompounded = f.Uncompounded(word, lemmata);
if (uncompounded === 'ai') {
return 'à';
}
if (uncompounded === 'ei') {
return 'è';
}
if (uncompounded === 'eia') {
return 'èa';
}
if (uncompounded === 'hei') {
return 'hè';
}
if (uncompounded === 'heia') {
return 'hèa';
}
if (uncompounded === 'hoc') {
return 'hocc';
}
if (uncompounded === 'oi') {
return 'ò';
}
if (uncompounded === 'oiei') {
return 'òè';
}
if (uncompounded === 'quoiās') {
return 'qòjās';
}
if (uncompounded === 'dehinc') {
return 'dènc';
}
if (['dein', 'deinde', 'proin', 'proindē'].includes(uncompounded)) {
return uncompounded.replace('dein', 'dèn').replace('proin', 'pròn');
}
if (f.Lemma1(word, lemmata) === 'praeeō') {
return uncompounded.replaceAll('praei', 'prài');
}
if (
f.Lemma1(word, lemmata).startsWith('cui') ||
f.Lemma1(word, lemmata).startsWith('quis') ||
f.Lemma1(word, lemmata).startsWith('quī') ||
f.Lemma1(word, lemmata) === 'aliquis' ||
f.Lemma1(word, lemmata) === 'ecquis' ||
f.Lemma1(word, lemmata) === 'nesciōquis' ||
f.Lemma1(word, lemmata) === 'ūnusquisque'
) {
return replaceIntervocalicI(uncompounded)
.replaceAll('cuiā', 'cùjā')
.replaceAll('cuiu', 'cùju')
.replaceAll('cui', 'cù');
}
if (uncompounded.includes('huius')) {
return replaceIntervocalicI(uncompounded).replaceAll('huius', 'hùjus');
}
if (
f.NoMacra(word, lemmata).includes('ngua') ||
f.NoMacra(word, lemmata).includes('ngue') ||
f.NoMacra(word, lemmata).includes('ngui') ||
f.NoMacra(word, lemmata).includes('nguo') ||
f.NoMacra(word, lemmata).includes('nguu')
) {
return uncompounded.replace('ngu', 'ngv');
}
if (
f.Lemma1(word, lemmata).includes('suād') ||
f.Lemma1(word, lemmata).includes('suās') ||
f.Lemma1(word, lemmata).includes('suāv')
) {
return uncompounded.replace('suā', 'svā');
}
if (word.startsWith('Eduard')) {
return uncompounded.replace('Eduard', 'edvard');
}
if (f.Lemma1(word, lemmata).toLowerCase().includes('suē')) {
return uncompounded
.replace('suē', 'svē')
.replace('Suē', 'Svē')
.replace('sue', 'sve')
.replace('sui', 'svi')
.replace('suī', 'svī')
.replace('suu', 'svu');
}
if (f.Lemma1(word, lemmata) === 'urgueō') {
return uncompounded.replaceAll('urgu', 'urgv');
}
if (
f.Lemma1(word, lemmata).endsWith('iaceō') ||
f.Lemma1(word, lemmata).endsWith('iectō') ||
f.Lemma1(word, lemmata).endsWith('iaciō') ||
f.Lemma1(word, lemmata).endsWith('iectus') ||
f.Lemma1(word, lemmata).endsWith('iectē') ||
[
'abiciō',
'adiciō',
'circumiciō',
'coiciō',
'coniciō',
'dēiciō',
'disiciō',
'ēiciō',
'iniciō',
'intericiō',
'obiciō',
'periciō',
'praeiciō',
'reiciō',
'subiciō',
'trāiciō',
'obex',
'subicēs',
].includes(f.Lemma1(word, lemmata))
) {
return uncompounded
.replace('coiē', 'còiē')
.replace('coie', 'còie')
.replace('iēc', 'jēc')
.replace('iec', 'jec')
.replace('iac', 'jac')
.replaceAll('bex', 'bjex')
.replaceAll('ic', 'jic')
.replaceAll('rej', 'rèj');
}
if (
uncompounded.startsWith('coniū') ||
uncompounded.startsWith('coniu')
) {
return uncompounded.replace('coni', 'conj');
}
if (
uncompounded.startsWith('disiū') ||
uncompounded.startsWith('disiu')
) {
return uncompounded.replace('disi', 'disj');
}
if (
f.Lemma1(word, lemmata) === 'iniugis' ||
f.Lemma1(word, lemmata) === 'biiugis' ||
f.Lemma1(word, lemmata) === 'biiugus' ||
f.Lemma1(word, lemmata) === 'subiugō'
) {
return uncompounded.replaceAll('iug', 'jug');
}
if (uncompounded.startsWith('adiu') || uncompounded.startsWith('adiū')) {
return uncompounded.replace('adi', 'adj');
}
if (uncompounded.startsWith('iniūr')) {
return uncompounded.replace('iniūr', 'injūr');
}
if (f.Lemma1(word, lemmata) === 'iūsiūrandum') {
return uncompounded.replaceAll('iū', 'jū');
}
if (f.Lemma1(word, lemmata) === 'periūrus') {
return uncompounded.replace('periūr', 'perjūr');
}
if (
/^i[aeiouyāēīōūȳ]/i.test(word) &&
!phoneticExceptions['Vocalic initial i'].includes(
f.Lemma1(word, lemmata)
) &&
uncompounded !== 'iīs'
) {
return uncompounded.replace(/^i/i, 'j');
}
if (
['magnus', 'magis', 'maiestās', 'maiōrēs'].includes(
f.Lemma1(word, lemmata)
) ||
(f.NoMacra(f.Lemma1(word, lemmata)) === 'aio' &&
['a', 'e', 'i', 'o', 'u', 'y'].includes(
f.NoMacra(word, lemmata).substring(2, 3)
))
) {
return uncompounded.replaceAll('ai', 'ajj');
}
if (['malus', 'male'].includes(f.Lemma1(word, lemmata))) {
return uncompounded.replaceAll('ei', 'èj');
}
if (
uncompounded.includes('eius')
// &&
// uncompounded.replace('eius', 'is') === f.Lemma1(word, lemmata)
) {
return uncompounded.replace('eius', 'èjus');
}
if (word.startsWith('-')) {
return '';
}
return uncompounded;
};
return `_${replaceIntervocalicI(
getPhoneticBeforeGeneralSubstitutions().toLowerCase()
)}_`
.replaceAll('am_', 'ã')
.replaceAll('em_', 'ẽ')
.replaceAll('im_', 'ĩ')
.replaceAll('om_', 'õ')
.replaceAll('um_', 'ũ')
.replaceAll('ym_', 'ỹ')
.replaceAll('qu', 'q')
.replaceAll('ds', 'ts')
.replaceAll('z', 'ds')
.replaceAll('x', 'cs')
.replaceAll('bs', 'ps')
.replaceAll('bt', 'pt')
.replaceAll('ch', 'χ')
.replaceAll('ph', 'φ')
.replaceAll('rh', 'r')
.replaceAll('th', 'θ')
.replaceAll('ae', 'à')
.replaceAll('au', 'â')
.replaceAll('oe', 'ò')
.replaceAll('ë', 'e')
.replaceAll('ï', 'i')
.replaceAll('ü', 'u')
.replaceAll('ṻ', 'ū')
.replaceAll('á', 'a')
.replaceAll('é', 'e')
.replaceAll('í', 'i')
.replaceAll('ó', 'o')
.replaceAll('ú', 'u')
.replaceAll('ý', 'y')
.replaceAll('ḗ', 'ē')
.replaceAll('āns', 'ãs')
.replaceAll('ēns', 'ẽs')
.replaceAll('īns', 'ĩs')
.replaceAll('ōns', 'õs')
.replaceAll('ūns', 'ũs')
.replaceAll('ȳns', 'ỹs')
.replaceAll('ānf', 'ãf')
.replaceAll('ēnf', 'ẽf')
.replaceAll('īnf', 'ĩf')
.replaceAll('ōnf', 'õf')
.replaceAll('ūnf', 'ũf')
.replaceAll('ȳnf', 'ỹf')
.replaceAll('lectiient', 'lectijent')
.replaceAll('ōsuestr', 'ōsvestr')
.replaceAll('reiciav', 'rejcjav')
.replaceAll('k', 'c')
.replaceAll(
'eu',
phoneticExceptions['Diphthong eu'].includes(f.Lemma1(word, lemmata)) > 0
? '€'
: 'eu'
)
.replaceAll('_eu', '_€')
.replaceAll('_€nd', 'eund')
.replaceAll('_€nt', 'eunt')
.replaceAll('eu_', '€')
.replaceAll('_', '');
},
EncliticPhonetic: (word, lemmata) => {
return f.Enclitic(word, lemmata).replace('qu', 'q');
},
Phonetic: (word, lemmata) => {
if (word === '') {
return '_';
}
return (
f.UncompoundedPhonetic(word, lemmata) + f.EncliticPhonetic(word, lemmata)
);
},
Scansion: (word, lemmata) => {
return (
f
.Phonetic(word, lemmata)
.replace(/[eiouy]/g, 'a')
.replace(/[ēīōūȳãẽĩõũỹàâ€èòùḗ]/g, 'ā')
.replace(/bl|cl|cr|dr|fr|fl|gl|gr|pr|pl|tr|θl|θr|φl|φr|χl|χr/g, 'br')
.replace(/br/g, 'b')
.replace(/h/g, '')
.replace(/c|d|f|g|j|k|l|m|n|p|q|r|s|t|v|φ|χ|θ/g, 'b')
.replace(/z/g, 'bb')
.replace(/abb/g, 'ā')
.replace(/b/g, '')
.replace(/a/g, SHORT)
.replace(/ā/g, LONG) || EMPTY
);
},
AllVowels: (word, lemmata) => {
const emptyIfNeeded =
f.Scansion(word, lemmata) === EMPTY || word.startsWith('-') ? EMPTY : '';
return (
emptyIfNeeded +
f.Phonetic(word, lemmata).replace(/[bcdfghjklmnpqrstvxzχφθ]/g, '')
);
},
SyllableCount: (word, lemmata) => {
return f.AllVowels(word, lemmata).replace(EMPTY, '').length;
},
// Returns the stress position: 3 for antepenult, 2 for penult, 1 for ultima, 0 for words of no syllables.
Stress: (word, lemmata) => {
// Eg ‘st’ has 0 syllables, so cannot have a stressed syllable.
if (f.SyllableCount(word, lemmata) === 0) {
return 0;
}
// Eg ‘-que’ is an enclitic, with the stress on the penult (the syllable before the enclitic!).
if (word.startsWith('-')) {
return 2;
}
// Monosyllables have only one syllable that could be stressed.
if (f.SyllableCount(word, lemmata) === 1) {
return 1;
}
// These other words are stressed on the ultima.
if (
[
'abhinc',
'adhūc',
'Antiās',
'Arpīnās',
'Asprēnās',
'Fīdēnās',
'illāc',
'illīc',
'illinc',
'illūc',
'istīc',
'Maecēnās',
'nostrās',
'posthāc',
'Samnīs',
'satin',
'Suffēnās',
'tantōn',
'viden',
'vidēn',
].includes(word)
) {
return 1;
}
// Some irregular imperatives have ultima stress.
if (
f.Phonetic(word, lemmata).endsWith('dīc') ||
f.Phonetic(word, lemmata).endsWith('dūc') ||
f.Phonetic(word, lemmata).endsWith('fac')
) {
return 1;
}
// Any other word of two syllables.
if (f.SyllableCount(word, lemmata) === 2) {
return 2;
}
// “-inde” behaves as an enclitic, moving stress to the antepenult.
if (['deïnde', 'exindē', 'perinde', 'proïndē', 'subinde'].includes(word)) {
return 3;
}
// Words with long penult are stressed on it.
if (
f.Scansion(word, lemmata)[f.SyllableCount(word, lemmata) - 2] === LONG
) {
return 2;
}
// Encliticized words have stress on the syllable before the enclitic (ie the penult).
if (word.length !== f.Uncompounded(word, lemmata).length) {
return 2;
}
// More encliticized words (not ending in the regular -que -ne -ve).
if (
[
'agedum',
'egomet',
'ibidem',
'meamet',
'satine',
'suamet',
'ubinam',
].includes(word)
) {
return 2;
}
// I use acutes to mark stress in words like ‘domínī’, to differentiate from homographs stressed on the antepenult.
// This won’t match Rhódane, which *is* stressed on the antepenult.
if (
/[áéíóúý][^aeiouyāēīōūȳ]*[aeiouyāēīōūȳ]?[^aeiouyāēīōūȳ]*$/i.test(
f.Uncompounded(word, lemmata)
)
) {
return 2;
}
// More contractions such as ‘imperī’ from ‘imperium’, where I haven’t used an acute.
if (
(f.Lemma1(word, lemmata).endsWith('ius') ||
f.Lemma1(word, lemmata).endsWith('ïus') ||
f.Lemma1(word, lemmata).endsWith('ium') ||
f.Lemma1(word, lemmata).endsWith('ius[prn]') ||
f.Lemma1(word, lemmata).endsWith('ius[adj]') ||
f.Lemma1(word, lemmata).endsWith('ius[n]') ||
f.Lemma1(word, lemmata).endsWith('ium[prn]') ||
f.Lemma1(word, lemmata).endsWith('ium[n]')) &&
word
.replaceAll('á', 'a')
.replaceAll('é', 'e')
.replaceAll('í', 'i')
.replaceAll('ó', 'o')
.replaceAll('ú', 'u')
.replaceAll('ý', 'y') ===
f
.Lemma1(word, lemmata)
.replace(/\[[^\]]+\]/, '')
.replace(/...$/, 'ī')
) {
return 2;
}
// All other words are stressed on the antepenult.
return 3;
},
UltimaRhyme: (word, lemmata) => {
if (f.SyllableCount(word, lemmata) === 0) {
return f.Phonetic(word, lemmata);
}
const ultimaVowel = f.AllVowels(word, lemmata).at(-1);
const lastIndex = `${f.Phonetic(word, lemmata)}`.lastIndexOf(ultimaVowel);
return `${f.Phonetic(word, lemmata)}`.substring(lastIndex);
},
RhymeVowels: (word, lemmata) => {
if (f.Scansion(word, lemmata) === EMPTY) {
return EMPTY;
}
return f
.AllVowels(word, lemmata)
.substring(f.SyllableCount(word, lemmata) - f.Stress(word, lemmata));
},
PerfectRhyme: (word, lemmata) => {
if (word.startsWith('-') || f.Stress(word, lemmata) === 0) {
return f.Phonetic(word, lemmata);
}
const rhymeVowels = f.RhymeVowels(word, lemmata).split('');
// Subtract syllables from the end until the rhyme-vowels are used up.
// Eg vocābulōrũ => vocābul
let wordMinusRhyme = rhymeVowels.reduceRight((substring, vowel) => {
return substring.substring(0, substring.lastIndexOf(vowel));
}, f.Phonetic(word, lemmata));
// Remove this from the initial word to give the syllables that were subtracted in the `reduceRight`.
// Eg vocābulōrũ - vocābul => ōrũ
return f.Phonetic(word, lemmata).replace(wordMinusRhyme, '');
},
RhymeConsonants: (word, lemmata) => {
return (f.PerfectRhyme(word, lemmata) + '.')
.replace(/[€āàâeēiīoōóòuūùyȳ]/g, 'a')
.replace(/[ãẽĩõũỹ]f/g, 'anf')
.replace(/[ãẽĩõũỹ]s/g, 'ans')
.replace(/[ãẽĩõũỹ]/g, 'am')
.replace('.', '');
},
Ultima: (word, lemmata) => {
if (f.Stress(word, lemmata) === 2) {
return '2 ' + f.UltimaRhyme(word, lemmata);
}
return f.UltimaRhyme(word, lemmata);
},
RhymeVowelsAndUltimaCoda: (word, lemmata) => {
if (f.Stress(word, lemmata) === 0) {
return f.UltimaRhyme(word, lemmata);
}
return (
f.RhymeVowels(word, lemmata) + f.UltimaRhyme(word, lemmata).substring(1)
);
},
EcclesPhonetic: (word, lemmata) => {
return `${f.Phonetic(word, lemmata)}.`
.replace(/ā/g, 'a')
.replace(/ē/g, 'e')
.replace(/ī/g, 'i')
.replace(/ō/g, 'o')
.replace(/ū/g, 'u')
.replace(/ȳ/g, 'y')
.replaceAll('ihi', 'iki')
.replaceAll('òè', 'ojej')
.replaceAll('à', 'e')
.replaceAll('è', 'ej')
.replaceAll('ò', 'e')
.replaceAll('ù', 'uj')
.replaceAll('y', 'i')
.replaceAll('ã.', 'am')
.replaceAll('ẽ.', 'em')
.replaceAll('ĩ.', 'im')
.replaceAll('õ.', 'om')
.replaceAll('ũ.', 'um')
.replaceAll('ỹ.', 'ym')
.replaceAll('ã', 'an')
.replaceAll('ẽ', 'en')
.replaceAll('ĩ', 'in')
.replaceAll('õ', 'on')
.replaceAll('ũ', 'un')
.replaceAll('ỹ', 'yn')
.replaceAll('χ', 'c')
.replaceAll('φ', 'f')
.replaceAll('θ', 't')
.replaceAll('h', '')
.replaceAll('.', '');
},
EcclesVowels: (word, lemmata) => {
const emptyIfNeeded =
f.Scansion(word, lemmata) === EMPTY || word.startsWith('-') ? EMPTY : '';
return (
emptyIfNeeded +
f.EcclesPhonetic(word, lemmata).replace(/[bcdfghjklmnpqrstvxz]/g, '')
);
},
EcclesRhymeVowels: (word, lemmata) => {
if (word === 'dehinc') {
return 'ei';
}
if (f.Scansion(word, lemmata) === EMPTY) {
return EMPTY;
}
return f
.EcclesVowels(word, lemmata)
.substring(f.SyllableCount(word, lemmata) - f.Stress(word, lemmata));
},
EcclesRhymeVowelsAndUltimaCoda: (word, lemmata) => {
if (f.Scansion(word, lemmata) === EMPTY) {
return EMPTY;
}
const phonetic = f.EcclesPhonetic(word, lemmata);
const rhymeVowels = f.EcclesRhymeVowels(word, lemmata);
const ultimaVowel = rhymeVowels.substring(rhymeVowels.length - 1);
const ultimaVowelIndex = phonetic.lastIndexOf(ultimaVowel);
return rhymeVowels + phonetic.substring(ultimaVowelIndex + 1);
},
EcclesPerfectRhyme: (word, lemmata) => {
if (word === 'dehinc') {
return 'einc';
}
if (word.startsWith('-') || f.Stress(word, lemmata) === 0) {
f.EcclesPhonetic(word, lemmata);
}
const rhymeVowels = f.EcclesRhymeVowels(word, lemmata).split('');
// Subtract syllables from the end until the rhyme-vowels are used up.
// Eg vocabulorum => vocabul
let wordMinusRhyme = rhymeVowels.reduceRight((substring, vowel) => {
return substring.substring(0, substring.lastIndexOf(vowel));
}, f.EcclesPhonetic(word, lemmata));
// Remove this from the initial word to give the syllables that were subtracted in the `reduceRight`.
// Eg vocabulorum - vocabul => orum
return f.EcclesPhonetic(word, lemmata).replace(wordMinusRhyme, '');
},
EcclesSort: (word, lemmata) => {
return (
(
(f.EcclesRhymeVowels(word, lemmata) === EMPTY
? ''
: f.EcclesRhymeVowels(word, lemmata)) +
'-' +
f
.EcclesPerfectRhyme(word, lemmata)
.replace(/[eiouyàâè€òùãẽĩõũỹ]/g, 'a') +
'-' +
reverseString(
f
.EcclesVowels(word, lemmata)
.substring(
0,
f.EcclesVowels(word, lemmata).length -
f.EcclesRhymeVowels(word, lemmata).length
)
) +
'-' +
reverseString(
f
.EcclesPhonetic(word, lemmata)
.substring(
0,
f.EcclesPhonetic(word, lemmata).length -
f.EcclesPerfectRhyme(word, lemmata).length
)
).replace(/[eiouyàâè€òùãẽĩõũỹ]/g, 'a') +
'-' +
word.toLowerCase()
)
.replaceAll('ā', 'azzzz')
.replaceAll('ē', 'ezzzz')
.replaceAll('ḗ', 'ezzzz')
.replaceAll('ī', 'izzzz')
.replaceAll('ō', 'ozzzz')
.replaceAll('ū', 'uzzzz')
.replaceAll('ȳ', 'yzzzz')
.replaceAll('ã', 'azzzzzz')
.replaceAll('ẽ', 'ezzzzzz')
.replaceAll('ĩ', 'izzzzzz')
.replaceAll('õ', 'ozzzzzz')
.replaceAll('ũ', 'uzzzzzz')
.replaceAll('ỹ', 'yzzzzzz')
.replaceAll('à', 'azzzzzzzz')
.replaceAll('â', 'azzzzzzzzzzzz')
.replaceAll('è', 'ezzzzzzzz')
.replaceAll('€', 'ezzzzzzzzzzzz')
.replaceAll('ò', 'ozzzzzzzz')
.replaceAll('ù', 'uzzzzzzzz') + (word === word.toLowerCase() ? '/' : '')
);
},
LemmaCount: (word, lemmata) => {
return f.LemmaArray(word, lemmata).length;
},
Lemma1: (word, lemmata) => {
return f.LemmaArray(word, lemmata)[0];
},
Lemma2: (word, lemmata) => {
return f.LemmaArray(word, lemmata)[1] || null;
},
Lemma3: (word, lemmata) => {
return f.LemmaArray(word, lemmata)[2] || null;
},
Lemma4: (word, lemmata) => {
return f.LemmaArray(word, lemmata)[3] || null;
},
Lemma5: (word, lemmata) => {
return f.LemmaArray(word, lemmata)[4] || null;
},
ScansionWithElision: (word, lemmata) => {
if (!'aeiouyāēīōūӯãẽĩõũỹàâé€òù'.includes(f.UltimaRhyme(word, lemmata))) {
return f.Scansion(word, lemmata);
}
if (f.SyllableCount(word, lemmata) === 1) {
return EMPTY;
}
return f
.Scansion(word, lemmata)
.substring(0, f.SyllableCount(word, lemmata) - 1);
},
IsFitForDactyl: (word, lemmata) => {
const scansionWithElision = f.ScansionWithElision(word, lemmata);
if (scansionWithElision === EMPTY) {
return 1;
}
const scansionWithoutElision = f.Scansion(word, lemmata);
const phonetic = f.Phonetic(word, lemmata);
const endsWithConsonant = scansionWithElision === scansionWithoutElision;
const endsWithShortVowelConsonant =
/[aeiouy].$/.test(phonetic) && endsWithConsonant;
// return endsWithShortVowelConsonant;
const scansionToFit = endsWithShortVowelConsonant
? scansionWithElision.substring(0, f.SyllableCount(word, lemmata) - 1)
: scansionWithElision;
if (
allValidHexameters.some((hexameter) => {
return hexameter.includes(scansionToFit);
})
) {
// return `IFFD true endsWithShortVowelConsonant ${endsWithShortVowelConsonant} Phonetic ${phonetic} Scansion ${scansionWithoutElision}`;
return 1;
}
// return `IFFD false endsWithShortVowelConsonant ${endsWithShortVowelConsonant} Phonetic ${phonetic} Scansion ${scansionWithoutElision}`;
return 0;
},
LemmaArray: (word, lemmata) => {
return lemmata.split(' ');
},
IsLemma: (word, lemmata) => {
return f.LemmaArray(word, lemmata).some((lemma) => {
return lemma.replace(/\[[^\]]+\]/, '') === word;
})
? 1
: 0;
},
IsNonLemma: (word, lemmata) => {
return f.LemmaArray(word, lemmata).some((lemma) => {
return lemma.replace(/\[[^\]]+\]/, '') !== word;
})
? 1
: 0;
},
DuplicateWords: (word, lemmata) => {
const duplicateFound = existingWords.find((record) => record.word === word);
if (duplicateFound) {
if (JSON.stringify(duplicateFound.lemmata) === JSON.stringify(lemmata)) {
console.error(
`Duplicate found for ${word} with matching lemmata — please delete the duplicate`
);
return word;
}
if (
lemmata.some(
(lemmaInNewRecord) =>
!duplicateFound.lemmata.includes(lemmaInNewRecord)
)
) {
console.error(
`Duplicate found for ${word} with new lemmata [${lemmata}] that are not in [${duplicateFound.lemmata}] — please merge two records`
);
return word;
}
console.error(
`Duplicate found for ${word} with lemmata [${lemmata}] that match existing lemmata [${duplicateFound.lemmata}] — please delete the duplicate`
);
return word;
}
return null;
},
NewLemmata: (word, lemmata) => {
// I’m not going to write something useful for this.
// In the Excel file, this column checks whether lemmata are missing from
// a sheet called `lemmata` — but that sheet cannot be replicated here.
return '';
},
NoMacra: (word, lemmata) => {
// Function from https://ricardometring.com/javascript-replace-special-characters
return word
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
.replace(/-/g, '');
},
NoMacraLowerCase: (word, lemmata) => {
return f.NoMacra(word).toLowerCase();
},
AlphOrderNoMacra: (word, lemmata) => {
return f.NoMacraLowerCase(word, lemmata).split('').sort().join('');
},
Sort: (word, lemmata) => {
return (
(
(f.RhymeVowels(word, lemmata) === EMPTY