From 5d41d953a06304dc5053a0682a20f3f88a180064 Mon Sep 17 00:00:00 2001 From: Bill D Date: Sun, 16 May 2021 18:00:05 +0300 Subject: [PATCH] working with special du behaviour --- src/lib/diacritics-helpers.test.ts | 36 +++++++++++++++++++++++++----- src/lib/diacritics-helpers.ts | 11 ++++----- src/lib/diacritics.test.ts | 32 +++++++++++++------------- src/lib/diacritics.ts | 20 +++++++++++------ 4 files changed, 62 insertions(+), 37 deletions(-) diff --git a/src/lib/diacritics-helpers.test.ts b/src/lib/diacritics-helpers.test.ts index 4651a0f..65c9052 100644 --- a/src/lib/diacritics-helpers.test.ts +++ b/src/lib/diacritics-helpers.test.ts @@ -2,7 +2,7 @@ import { splitFIntoPhonemes, last, addP, - prev2Chars, + lastNonWhitespace, advanceP, reverseP, overwriteP, @@ -97,8 +97,32 @@ test("addP should work", () => { }); }); -test("prev2Chars should work", () => { - expect(prev2Chars("تورن")).toBe("رن"); - expect(prev2Chars("وست .. ")).toBe("ست"); - expect(prev2Chars("دَ ... ")).toBe("دَ"); -}); \ No newline at end of file +test("lastNonWhiteSpace should work", () => { + expect(lastNonWhitespace("تورن")).toBe("ن"); + expect(lastNonWhitespace("وست .. ")).toBe("ت"); + expect(lastNonWhitespace("د ... ")).toBe("د"); +}); + +test("reverseP should work", () => { + expect(reverseP({ + pIn: "کور", + pOut: "تور ", + })).toEqual({ + pIn: " کور", + pOut: "تور", + }); + expect(reverseP({ + pIn: "کور", + pOut: "تور ... ", + })).toEqual({ + pIn: " ... کور", + pOut: "تور", + }); + expect(reverseP({ + pIn: "کور", + pOut: "تور . ", + })).toEqual({ + pIn: " . کور", + pOut: "تور", + }); +}) \ No newline at end of file diff --git a/src/lib/diacritics-helpers.ts b/src/lib/diacritics-helpers.ts index f29d284..3824f4d 100644 --- a/src/lib/diacritics-helpers.ts +++ b/src/lib/diacritics-helpers.ts @@ -353,19 +353,16 @@ export const overwriteP = (toWrite: string) => (state: DiacriticsAccumulator): D }; /** - * returns the last two character in a string that was not a space or a dote + * returns the last letter before any whitespace (" " / ".") * * @param s * @returns */ -export function prev2Chars(s: string): string { - // console.log("looking at pOut", s); +export function lastNonWhitespace(s: string): string { const reversed = [...s].reverse(); - // console.log(reversed.join("-")); const lastIndex = reversed.findIndex((c) => ![" ", "."].includes(c)); - const last2 = reversed[lastIndex + 1] + reversed[lastIndex]; - // console.log("last2", last2); - return last2; + const penultimateChar = reversed[lastIndex]; + return penultimateChar; } export function getCurrentNext(state: DiacriticsAccumulator): { current: string, next: string} { diff --git a/src/lib/diacritics.test.ts b/src/lib/diacritics.test.ts index e0d6773..4d03af6 100644 --- a/src/lib/diacritics.test.ts +++ b/src/lib/diacritics.test.ts @@ -587,13 +587,13 @@ const diacriticsSections: { }, out: "د" + zwarakey + " لاس", }, - // { - // in: { - // p: "د ... په شان", - // f: "du ... pu shaan", - // }, - // out: "د" + zwarakey + "... پهٔ شان", - // }, + { + in: { + p: "د ... په شان", + f: "du ... pu shaan", + }, + out: "د" + zwarakey + " ... پهٔ شان", + }, ], }, ]; @@ -601,16 +601,14 @@ const diacriticsSections: { diacriticsSections.forEach((section) => { describe(section.describe, () => { section.tests.forEach((t) => { - if (section.describe === "special behaviour with د") { - if (t.out) { - test(`diacritics should work for ${t.in.p} - ${t.in.f}`, () => { - expect(addDiacritics(t.in)).toEqual({ p: t.out, f: t.in.f }); - }); - } else { - expect(() => { - expect(addDiacritics(t.in)).toThrowError(); - }); - } + if (t.out) { + test(`diacritics should work for ${t.in.p} - ${t.in.f}`, () => { + expect(addDiacritics(t.in)).toEqual({ p: t.out, f: t.in.f }); + }); + } else { + expect(() => { + expect(addDiacritics(t.in)).toThrowError(); + }); } }); }); diff --git a/src/lib/diacritics.ts b/src/lib/diacritics.ts index 108b138..a67f33b 100644 --- a/src/lib/diacritics.ts +++ b/src/lib/diacritics.ts @@ -21,7 +21,7 @@ import { wasla, daggerAlif, fathahan, - prev2Chars, + lastNonWhitespace, addP, last, advanceP, @@ -75,7 +75,10 @@ function processPhoneme( // console.log("space coming up", acc.pIn[0] === " "); // console.log("state", acc); // Prep state - const state = acc.pIn[0] === " " + // TODO: CLEANER function jump to next char + const state = acc.pIn.slice(0, 5) === " ... " + ? advanceP(acc, 5) + : acc.pIn[0] === " " ? advanceP(acc) : acc; // console.log("AFTER SPACE PREP", phoneme); @@ -132,10 +135,10 @@ function processPhoneme( addP(zer), )(state) : (phs === PhonemeStatus.EndOfDuParticle) ? - (console.log("here"), pipe( + pipe( reverseP, addP(zwarakey), - )(state)) + )(state) : // phs === PhonemeState.ShortVowel pipe( @@ -177,9 +180,12 @@ function stateInfo({ state, i, phonemes, phoneme }: { if (isBeginningOfWord && (phonemeInfo.beginningMatches?.includes(currentPLetter) || phonemeInfo.matches?.includes(currentPLetter))) { return PhonemeStatus.LeadingConsonantOrShortVowel; } - console.log(phoneme, phonemes, prev2Chars(state.pOut)) - if (isBeginningOfWord && phoneme === "u" && prevPLetter === " " && prev2Chars(state.pOut) === ("د" + zwarakey)) { - // console.log("du here", phoneme, phonemes); + // console.log("------"); + // console.log("phoneme", phoneme); + // console.log("state", state); + // console.log("prevPLetter is space", prevPLetter === " "); + // console.log("------"); + if (isBeginningOfWord && phoneme === "u" && prevPLetter === " " && lastNonWhitespace(state.pOut) === "د") { return PhonemeStatus.EndOfDuParticle } if (!isBeginningOfWord && phoneme === "aa" && currentPLetter === "و" && nextPLetter === "ا") {