This commit is contained in:
adueck 2023-07-23 17:17:38 +04:00
parent 0c2948721b
commit 0846fee749
1 changed files with 115 additions and 103 deletions

View File

@ -16,9 +16,15 @@ import { makePsString, removeFVarients } from "./accent-and-ps-utils";
* @param s * @param s
*/ */
export function accentOnFront(s: T.PsString): T.PsString; export function accentOnFront(s: T.PsString): T.PsString;
export function accentOnFront(s: T.LengthOptions<T.PsString>): T.LengthOptions<T.PsString>; export function accentOnFront(
export function accentOnFront(s: T.SingleOrLengthOpts<T.PsString>): T.SingleOrLengthOpts<T.PsString>; s: T.LengthOptions<T.PsString>
export function accentOnFront(s: T.SingleOrLengthOpts<T.PsString>): T.SingleOrLengthOpts<T.PsString> { ): T.LengthOptions<T.PsString>;
export function accentOnFront(
s: T.SingleOrLengthOpts<T.PsString>
): T.SingleOrLengthOpts<T.PsString>;
export function accentOnFront(
s: T.SingleOrLengthOpts<T.PsString>
): T.SingleOrLengthOpts<T.PsString> {
if ("long" in s) { if ("long" in s) {
return { return {
short: accentOnFront(s.short), short: accentOnFront(s.short),
@ -40,13 +46,11 @@ export function accentPastParticiple(s: T.PsString): T.PsString {
// check for accent placing in words like wáayuley and azmóyuley // check for accent placing in words like wáayuley and azmóyuley
const accentFallsOnThirdLast = (syls: string[]) => { const accentFallsOnThirdLast = (syls: string[]) => {
if (syls.length < 3) return false; if (syls.length < 3) return false;
const secondLast = syls[syls.length-2]; const secondLast = syls[syls.length - 2];
const thirdLast = syls[syls.length-3]; const thirdLast = syls[syls.length - 3];
const lastLetterOfThirdLast = thirdLast.slice(-1); const lastLetterOfThirdLast = thirdLast.slice(-1);
return ( return secondLast === "ul" && lastLetterOfThirdLast === "y";
(secondLast === "ul") && (lastLetterOfThirdLast === "y") };
);
}
// remove all accents // remove all accents
const accentsRemoved = removeAccents(s.f); const accentsRemoved = removeAccents(s.f);
// split up the syllables (preserving the spaces) // split up the syllables (preserving the spaces)
@ -58,7 +62,11 @@ export function accentPastParticiple(s: T.PsString): T.PsString {
} }
export function splitUpSyllables(f: string): string[] { export function splitUpSyllables(f: string): string[] {
return f.match(/ |([^a|á|e|é|i|í|o|ó|u|ú| ]*(aa|áa|a|á|ey|éy|ee|ée|e|é|oo|óo|o|ó|i|í|u|ú)[^a|á|e|é|i|í|o|ó|u|ú| ]*)/ig) || [] as string[]; return (
f.match(
/ |([^a|á|e|é|i|í|o|ó|u|ú| ]*(aa|áa|a|á|ey|éy|ee|ée|e|é|oo|óo|o|ó|i|í|u|ú)[^a|á|e|é|i|í|o|ó|u|ú| ]*)/gi
) || ([] as string[])
);
} }
export function countSyllables(f: T.PsString | string): number { export function countSyllables(f: T.PsString | string): number {
@ -72,14 +80,17 @@ export function countSyllables(f: T.PsString | string): number {
* @param syls - an array of syllables in phonetic strings without accents (including spaces as extra items) * @param syls - an array of syllables in phonetic strings without accents (including spaces as extra items)
* @param n - the number of syllables from the end to put the accent * @param n - the number of syllables from the end to put the accent
*/ */
export function accentFSylsOnNFromEnd(syls: string[] | string, n: number): string { export function accentFSylsOnNFromEnd(
syls: string[] | string,
n: number
): string {
if (typeof syls === "string") { if (typeof syls === "string") {
return accentFSylsOnNFromEnd(splitUpSyllables(syls), n); return accentFSylsOnNFromEnd(splitUpSyllables(syls), n);
} }
return [ return [
...syls.slice(0, syls.length-(n+1)), // before accent ...syls.slice(0, syls.length - (n + 1)), // before accent
accentLetter(syls[syls.length-(n+1)]), // syllable to be accented accentLetter(syls[syls.length - (n + 1)]), // syllable to be accented
...(n !== 0) ? syls.slice(syls.length-n) : [], // after syllable to be accented ...(n !== 0 ? syls.slice(syls.length - n) : []), // after syllable to be accented
].join(""); ].join("");
} }
@ -89,14 +100,12 @@ export function accentOnNFromEnd(ps: T.PsString, n: number): T.PsString {
// TODO: enable this and fix the tests it breaks!!! // TODO: enable this and fix the tests it breaks!!!
// don't add accent if only one syllable // don't add accent if only one syllable
// if (fSyls.length === 1) return makePsString(ps.p, fNoAccents); // if (fSyls.length === 1) return makePsString(ps.p, fNoAccents);
return makePsString( return makePsString(ps.p, accentFSylsOnNFromEnd(fSyls, n));
ps.p,
accentFSylsOnNFromEnd(fSyls, n),
);
} }
const accentReplacer = [ const accentReplacer = [
{ vowel: "a", accented: "á" }, { vowel: "a", accented: "á" },
{ vowel: "ă", accented: "á" },
{ vowel: "e", accented: "é" }, { vowel: "e", accented: "é" },
{ vowel: "i", accented: "í" }, { vowel: "i", accented: "í" },
{ vowel: "o", accented: "ó" }, { vowel: "o", accented: "ó" },
@ -105,7 +114,7 @@ const accentReplacer = [
]; ];
export function accentLetter(s: string): string { export function accentLetter(s: string): string {
return s.replace(/a|e|i|o|u|U/, (match) => { return s.replace(/a|e|i|o|u|U/, (match) => {
const r = accentReplacer.find((x) => x.vowel === match); const r = accentReplacer.find((x) => x.vowel === match);
/* istanbul ignore next */ /* istanbul ignore next */
return r?.accented || ""; return r?.accented || "";
@ -119,28 +128,31 @@ export function accentPsSyllable(ps: T.PsString): T.PsString {
}; };
} }
export function removeAccentsWLength(
s: T.SingleOrLengthOpts<T.PsString[]>
export function removeAccentsWLength(s: T.SingleOrLengthOpts<T.PsString[]>): T.SingleOrLengthOpts<T.PsString[]> { ): T.SingleOrLengthOpts<T.PsString[]> {
if ("long" in s) { if ("long" in s) {
return { return {
long: removeAccentsWLength(s.long) as T.PsString[], long: removeAccentsWLength(s.long) as T.PsString[],
short: removeAccentsWLength(s.short) as T.PsString[], short: removeAccentsWLength(s.short) as T.PsString[],
...s.mini ? { ...(s.mini
? {
mini: removeAccentsWLength(s.mini) as T.PsString[], mini: removeAccentsWLength(s.mini) as T.PsString[],
} : {}, }
: {}),
}; };
} }
return removeAccents(s); return removeAccents(s);
} }
export function removeAccents(s: T.PsString): T.PsString; export function removeAccents(s: T.PsString): T.PsString;
export function removeAccents(s: string): string; export function removeAccents(s: string): string;
export function removeAccents(s: T.PsString[]): T.PsString[]; export function removeAccents(s: T.PsString[]): T.PsString[];
export function removeAccents(s: T.PsString | string | T.PsString[]): T.PsString | string | T.PsString[] { export function removeAccents(
s: T.PsString | string | T.PsString[]
): T.PsString | string | T.PsString[] {
if (Array.isArray(s)) { if (Array.isArray(s)) {
return s.map(t => removeAccents(t)); return s.map((t) => removeAccents(t));
} }
if (typeof s !== "string") { if (typeof s !== "string") {
return { return {