hebrew-transliteration
Version:
a package for transliterating Hebrew
390 lines (389 loc) • 16.1 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var rules_exports = {};
__export(rules_exports, {
sylRules: () => sylRules,
wordRules: () => wordRules
});
module.exports = __toCommonJS(rules_exports);
var import_cluster = require("havarotjs/cluster");
var import_syllable = require("havarotjs/syllable");
var import_regularExpressions = require("havarotjs/utils/regularExpressions");
var import_word = require("havarotjs/word");
var import_hebCharsTrans = require("./hebCharsTrans.js");
const taamim = /[\u{0591}-\u{05AF}\u{05BD}\u{05BF}]/gu;
const addStressMarker = (text, syl, schema) => {
var _a, _b;
if (!schema.STRESS_MARKER || !text) {
return text;
}
if (!syl.isAccented) {
return text;
}
const exclude = (_b = (_a = schema.STRESS_MARKER) == null ? void 0 : _a.exclude) != null ? _b : "never";
if (exclude === "single" && !syl.prev && !syl.next) {
return text;
}
if (exclude === "final" && !syl.next) {
return text;
}
const location = schema.STRESS_MARKER.location;
const mark = schema.STRESS_MARKER.mark;
if (text.includes(mark)) {
return text;
}
if (location === "before-syllable") {
const isDoubled = syl.clusters.map((c) => isDageshChazaq(c, schema)).includes(true);
if (isDoubled) {
const [first, ...rest] = text.split("");
return `${first}${mark}${rest.join("")}`;
}
return `${mark}${text}`;
}
if (location === "after-syllable") {
return `${text}${mark}`;
}
const vowels = [
schema.PATAH,
schema.HATAF_PATAH,
schema.QAMATS,
schema.HATAF_QAMATS,
schema.SEGOL,
schema.HATAF_SEGOL,
schema.TSERE,
schema.HIRIQ,
schema.HOLAM,
schema.QAMATS_QATAN,
schema.QUBUTS,
schema.QAMATS_HE,
schema.SEGOL_HE,
schema.TSERE_HE,
schema.HIRIQ_YOD,
schema.TSERE_YOD,
schema.SEGOL_YOD,
schema.HOLAM_VAV,
schema.SHUREQ
].filter((v) => typeof v === "string").sort((a, b) => b.length - a.length);
const vowelRgx = new RegExp(`${vowels.join("|")}`);
const match = text.match(vowelRgx);
if (location === "before-vowel") {
return (match == null ? void 0 : match.length) ? text.replace(match[0], `${mark}${match[0]}`) : text;
}
return (match == null ? void 0 : match.length) ? text.replace(match[0], `${match[0]}${mark}`) : text;
};
const copySyllable = (newText, old) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
const newClusters = newText.split(import_regularExpressions.clusterSplitGroup).map((clusterString) => new import_cluster.Cluster(clusterString, true));
const oldClusters = old.clusters;
if (newClusters.length === oldClusters.length) {
newClusters.forEach((c, i) => {
var _a2, _b2, _c2, _d2;
return c.prev = (_b2 = (_a2 = oldClusters[i]) == null ? void 0 : _a2.prev) != null ? _b2 : null, c.next = (_d2 = (_c2 = oldClusters[i]) == null ? void 0 : _c2.next) != null ? _d2 : null;
});
} else {
for (let i = 0; i < newClusters.length; i++) {
const c = newClusters[i];
if (((_a = oldClusters[i]) == null ? void 0 : _a.text[0]) === (c == null ? void 0 : c.text[0])) {
c.prev = (_c = (_b = oldClusters[i]) == null ? void 0 : _b.prev) != null ? _c : null;
c.next = (_e = (_d = oldClusters[i]) == null ? void 0 : _d.next) != null ? _e : null;
} else {
c.prev = (_g = (_f = oldClusters[i]) == null ? void 0 : _f.prev) != null ? _g : null;
c.next = (_i = (_h = oldClusters[i + 1]) == null ? void 0 : _h.next) != null ? _i : null;
i++;
}
}
}
const newSyl = new import_syllable.Syllable(newClusters, {
isClosed: old.isClosed,
isAccented: old.isAccented,
isFinal: old.isFinal
});
newClusters.forEach((c) => c.syllable = newSyl);
newSyl.prev = old.prev;
newSyl.next = old.next;
newSyl.word = old.word;
return newSyl;
};
const getDageshChazaqVal = (input, schema, isChazaq) => {
var _a;
if (!isChazaq) {
return input;
}
const dagesh = schema["DAGESH_CHAZAQ"];
const syllableSeparator = (_a = schema["SYLLABLE_SEPARATOR"]) != null ? _a : "";
if (typeof dagesh === "boolean") {
return input + syllableSeparator + input;
}
return input + dagesh;
};
const getDivineName = (str, schema) => {
const begn = str[0];
const end = str[str.length - 1];
const divineName = schema.DIVINE_NAME_ELOHIM && /\u{05B4}/u.test(str) ? schema.DIVINE_NAME_ELOHIM : schema.DIVINE_NAME;
return `${import_regularExpressions.hebChars.test(begn) ? "" : begn}${divineName}${import_regularExpressions.hebChars.test(end) ? "" : end}`;
};
const isDageshChazaq = (cluster, schema) => {
var _a, _b, _c, _d, _e, _f, _g;
if (!schema.DAGESH_CHAZAQ) {
return false;
}
if (cluster.isShureq) {
return false;
}
if (!/\u{05BC}/u.test(cluster.text)) {
return false;
}
const prevCluster = (_a = cluster.prev) == null ? void 0 : _a.value;
if (prevCluster && prevCluster.hasSheva) {
return false;
}
const prevWord = (_d = (_c = (_b = cluster.syllable) == null ? void 0 : _b.word) == null ? void 0 : _c.prev) == null ? void 0 : _d.value;
if (prevWord && (prevWord == null ? void 0 : prevWord.isInConstruct) && !prevWord.syllables[prevWord.syllables.length - 1].isClosed) {
return true;
}
const prevSyllable = (_e = cluster.syllable) == null ? void 0 : _e.prev;
if (!prevSyllable) {
return false;
}
const prevCoda = (_f = prevSyllable.value) == null ? void 0 : _f.codaWithGemination;
if (!prevCoda) {
return false;
}
return prevCoda === ((_g = cluster.syllable) == null ? void 0 : _g.onset);
};
const joinSyllableChars = (syl, sylChars, schema) => {
var _a;
const isInConstruct = (_a = syl.word) == null ? void 0 : _a.isInConstruct;
if (isInConstruct) {
return sylChars.map(mapChars(schema)).join("");
}
if (!syl.isAccented) {
return sylChars.map(mapChars(schema)).join("");
}
const isOnlyPunctuation = syl.clusters.map((c) => c.isPunctuation).every((c) => c);
if (isOnlyPunctuation) {
return sylChars.map(mapChars(schema)).join("");
}
return sylChars.map(mapChars(schema)).join("");
};
const mapChars = (schema) => (input) => {
return [...input].map((char) => char in import_hebCharsTrans.transliterateMap ? schema[import_hebCharsTrans.transliterateMap[char]] : char).join("");
};
const materFeatures = (syl, schema) => {
const mater = syl.clusters.filter((c) => c.isMater)[0];
const prev = mater.prev instanceof import_cluster.Cluster ? mater.prev : null;
const materText = mater.text;
const prevText = ((prev == null ? void 0 : prev.text) || "").replace(taamim, "");
let noMaterText = syl.clusters.filter((c) => !c.isMater).map((c) => cluterRules(c, schema)).join("");
const hasMaqaf = mater.text.includes("\u05BE");
noMaterText = hasMaqaf ? noMaterText.concat("\u05BE") : noMaterText;
if (/י/.test(materText)) {
if (/\u{05B4}/u.test(prevText)) {
return replaceWithRegex(noMaterText, /\u{05B4}/u, schema.HIRIQ_YOD);
}
if (/\u{05B5}/u.test(prevText)) {
return replaceWithRegex(noMaterText, /\u{05B5}/u, schema.TSERE_YOD);
}
if (/\u{05B6}/u.test(prevText)) {
return replaceWithRegex(noMaterText, /\u{05B6}/u, schema.SEGOL_YOD);
}
}
if (/ו/u.test(materText)) {
if (/\u{05B9}/u.test(prevText)) {
return replaceWithRegex(noMaterText, /\u{05B9}/u, schema.HOLAM_VAV);
}
}
if (/ה/.test(materText)) {
if (/\u{05B8}/u.test(prevText)) {
return replaceWithRegex(noMaterText, /\u{05B8}/u, schema.QAMATS_HE);
}
}
return materText;
};
const removeTaamim = (input) => input.replace(taamim, "");
const replaceAndTransliterate = (input, regex, replaceValue, schema) => {
const sylSeq = replaceWithRegex(input, regex, replaceValue);
return [...sylSeq].map(mapChars(schema)).join("");
};
const replaceWithRegex = (input, regex, replaceValue) => input.replace(regex, replaceValue);
const cluterRules = (cluster, schema) => {
var _a, _b;
let clusterText = removeTaamim(cluster.text);
const clusterFeatures = (_a = schema.ADDITIONAL_FEATURES) == null ? void 0 : _a.filter((seq) => seq.FEATURE === "cluster");
if (clusterFeatures) {
for (const feature of clusterFeatures) {
const heb = new RegExp(feature.HEBREW, "u");
if (feature.FEATURE === "cluster" && heb.test(clusterText)) {
const transliteration = feature.TRANSLITERATION;
const passThrough = (_b = feature.PASS_THROUGH) != null ? _b : true;
if (typeof transliteration === "string") {
return replaceAndTransliterate(clusterText, heb, transliteration, schema);
}
if (!passThrough) {
return transliteration(cluster, feature.HEBREW, schema);
}
clusterText = transliteration(cluster, feature.HEBREW, schema);
}
}
}
const syl = cluster.syllable;
clusterText = cluster.hasSheva && (syl == null ? void 0 : syl.isClosed) ? clusterText.replace(/\u{05B0}/u, "") : clusterText;
if (/ה\u{05BC}$/mu.test(clusterText)) {
return replaceWithRegex(clusterText, /ה\u{05BC}/u, schema.HE);
}
if ((syl == null ? void 0 : syl.isFinal) && !(syl == null ? void 0 : syl.isClosed)) {
const furtiveChet = /\u{05D7}\u{05B7}$/mu;
if (furtiveChet.test(clusterText)) {
return replaceWithRegex(clusterText, furtiveChet, "\u05B7\u05D7");
}
const furtiveAyin = /\u{05E2}\u{05B7}$/mu;
if (furtiveAyin.test(clusterText)) {
return replaceWithRegex(clusterText, furtiveAyin, "\u05B7\u05E2");
}
const furtiveHe = /\u{05D4}\u{05BC}\u{05B7}$/mu;
if (furtiveHe.test(clusterText)) {
return replaceWithRegex(clusterText, furtiveHe, "\u05B7\u05D4\u05BC");
}
}
const isDageshChazq = isDageshChazaq(cluster, schema);
if (schema.BET_DAGESH && /ב\u{05BC}/u.test(clusterText)) {
return replaceWithRegex(clusterText, /ב\u{05BC}/u, getDageshChazaqVal(schema.BET_DAGESH, schema, isDageshChazq));
}
if (schema.GIMEL_DAGESH && /ג\u{05BC}/u.test(clusterText)) {
return replaceWithRegex(clusterText, /ג\u{05BC}/u, getDageshChazaqVal(schema.GIMEL_DAGESH, schema, isDageshChazq));
}
if (schema.DALET_DAGESH && /ד\u{05BC}/u.test(clusterText)) {
return replaceWithRegex(clusterText, /ד\u{05BC}/u, getDageshChazaqVal(schema.DALET_DAGESH, schema, isDageshChazq));
}
if (schema.KAF_DAGESH && /כ\u{05BC}/u.test(clusterText)) {
return replaceWithRegex(clusterText, /כ\u{05BC}/u, getDageshChazaqVal(schema.KAF_DAGESH, schema, isDageshChazq));
}
if (schema.KAF_DAGESH && /ך\u{05BC}/u.test(clusterText)) {
return replaceWithRegex(clusterText, /ך\u{05BC}/u, getDageshChazaqVal(schema.KAF_DAGESH, schema, isDageshChazq));
}
if (schema.PE_DAGESH && /פ\u{05BC}/u.test(clusterText)) {
return replaceWithRegex(clusterText, /פ\u{05BC}/u, getDageshChazaqVal(schema.PE_DAGESH, schema, isDageshChazq));
}
if (schema.TAV_DAGESH && /ת\u{05BC}/u.test(clusterText)) {
return replaceWithRegex(clusterText, /ת\u{05BC}/u, getDageshChazaqVal(schema.TAV_DAGESH, schema, isDageshChazq));
}
if (/ש\u{05C1}/u.test(clusterText)) {
return replaceWithRegex(clusterText, /ש\u{05C1}/u, getDageshChazaqVal(schema.SHIN, schema, isDageshChazq));
}
if (/ש\u{05C2}/u.test(clusterText)) {
return replaceWithRegex(clusterText, /ש\u{05C2}/u, getDageshChazaqVal(schema.SIN, schema, isDageshChazq));
}
if (isDageshChazq) {
const consonant = cluster.chars[0].text;
const consonantDagesh = new RegExp(consonant + "\u05BC", "u");
return replaceWithRegex(clusterText, consonantDagesh, getDageshChazaqVal(consonant, schema, isDageshChazq));
}
if (cluster.isShureq) {
return clusterText.replace("\u05D5\u05BC", schema.SHUREQ);
}
return clusterText;
};
const sylRules = (syl, schema) => {
var _a, _b;
const baseSyllableText = syl.text.replace(taamim, "");
const syllableFeatures = (_a = schema.ADDITIONAL_FEATURES) == null ? void 0 : _a.filter((seq) => seq.FEATURE === "syllable");
if (syllableFeatures) {
for (const feature of syllableFeatures) {
const heb = new RegExp(feature.HEBREW, "u");
if (feature.FEATURE === "syllable" && heb.test(baseSyllableText)) {
const transliteration = feature.TRANSLITERATION;
const passThrough = (_b = feature.PASS_THROUGH) != null ? _b : true;
if (typeof transliteration === "string") {
return replaceAndTransliterate(baseSyllableText, heb, transliteration, schema);
}
if (!passThrough) {
return transliteration(syl, feature.HEBREW, schema);
}
const newText = transliteration(syl, feature.HEBREW, schema);
if (newText !== baseSyllableText) {
syl = copySyllable(newText, syl);
}
}
}
}
const hasMater = syl.clusters.map((c) => c.isMater).includes(true);
if (hasMater) {
const materSyl = materFeatures(syl, schema);
const text2 = joinSyllableChars(syl, [...materSyl], schema);
return addStressMarker(text2, syl, schema);
}
const clusters = syl.clusters.map((cluster) => cluterRules(cluster, schema));
const joined = joinSyllableChars(syl, clusters, schema).replace(taamim, "");
const mSSuffix = /\u{05B8}\u{05D9}\u{05D5}/u;
if (syl.isFinal && mSSuffix.test(baseSyllableText)) {
const text2 = joined.replace(schema["QAMATS"] + schema["YOD"] + schema["VAV"], schema.MS_SUFX);
return addStressMarker(text2, syl, schema);
}
if (schema.SEGOL_HE && /\u{05B6}\u{05D4}/u.test(baseSyllableText)) {
const text2 = joined.replace(schema["SEGOL"] + schema["HE"], schema.SEGOL_HE);
return addStressMarker(text2, syl, schema);
}
if (schema.TSERE_HE && /\u{05B5}\u{05D4}/u.test(baseSyllableText)) {
const text2 = joined.replace(schema["TSERE"] + schema["HE"], schema.TSERE_HE);
return addStressMarker(text2, syl, schema);
}
if (schema.PATAH_HE && /\u{05B7}\u{05D4}/u.test(baseSyllableText)) {
const text2 = joined.replace(schema["PATAH"] + schema["HE"], schema.PATAH_HE);
return addStressMarker(text2, syl, schema);
}
const text = joined.replace(taamim, "");
return addStressMarker(text, syl, schema);
};
const wordRules = (word, schema) => {
var _a, _b;
if (word.isDivineName) {
return getDivineName(word.text, schema);
}
if (word.hasDivineName) {
return `${sylRules(word.syllables[0], schema)}-${getDivineName(word.text, schema)}`;
}
if (word.isNotHebrew) {
return word.text;
}
const wordFeatures = (_a = schema.ADDITIONAL_FEATURES) == null ? void 0 : _a.filter((seq) => seq.FEATURE === "word");
if (wordFeatures) {
const text = word.text.replace(taamim, "");
for (const feature of wordFeatures) {
const heb = new RegExp(feature.HEBREW, "u");
if (heb.test(text)) {
const transliteration = feature.TRANSLITERATION;
const passThrough = (_b = feature.PASS_THROUGH) != null ? _b : true;
if (typeof transliteration === "string") {
return replaceAndTransliterate(text, heb, transliteration, schema);
}
if (!passThrough) {
return transliteration(word, feature.HEBREW, schema);
}
return new import_word.Word(transliteration(word, feature.HEBREW, schema), schema);
}
}
return word;
}
return word;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
sylRules,
wordRules
});
//# sourceMappingURL=rules.js.map