both Pakistani spelling varients

This commit is contained in:
lingdocs 2021-07-24 18:43:53 +03:00
parent beb24bc820
commit a85670a99f
7 changed files with 89 additions and 36 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@lingdocs/pashto-inflector",
"version": "0.8.5",
"version": "0.9.0",
"author": "lingdocs.com",
"description": "A Pashto inflection and verb conjugation engine, inculding React components for displaying Pashto text, inflections, and conjugations",
"homepage": "https://verbs.lingdocs.com",

View File

@ -24,7 +24,7 @@ import {
} from "react-bootstrap";
import * as T from "./types";
import defualtTextOptions from "./lib/default-text-options";
const textOptionsLocalStorageName = "textOptions2";
type VerbType = "simple" | "stative compound" | "dynamic compound";
const verbTypes: VerbType[] = [
"simple",
@ -66,7 +66,7 @@ function App() {
const regularIrregular = localStorage.getItem("regularIrregular") as "regular" | "irregular";
const transitivitiyShowing = localStorage.getItem("transitivityShowing") as undefined | T.Transitivity;
const theme = localStorage.getItem("theme");
const textOptionst = localStorage.getItem("textOptions");
const textOptionst = localStorage.getItem(textOptionsLocalStorageName);
if (regularIrregular) {
setRegularIrregular(regularIrregular);
}
@ -95,7 +95,7 @@ function App() {
localStorage.setItem("regularIrregular", regularIrregular);
localStorage.setItem("verbTypeShowing", verbTypeShowing);
localStorage.setItem("transitivityShowing", transitivityShowing);
localStorage.setItem("textOptions", JSON.stringify(textOptions));
localStorage.setItem(textOptionsLocalStorageName, JSON.stringify(textOptions));
localStorage.setItem("theme", theme);
});
@ -305,14 +305,15 @@ function App() {
<h6>Pashto Spelling</h6>
<ButtonSelect
options={[
{ label: "🇦🇫 Afghan", value: "Afghan" },
{ label: "🇵🇰 Pakistani", value: "Pakistani" },
{ label: "Afghan", value: "Afghan" },
{ label: "Pakistani ي", value: "Pakistani ي" },
{ label: "Pakistani ی", value: "Pakistani ی" },
]}
value={textOptions.spelling}
handleChange={(p) => {
setTextOptions({
...textOptions,
spelling: p as "Afghan" | "Pakistani",
spelling: p as T.Spelling,
});
}}
/>

View File

@ -7,7 +7,7 @@
*/
import {
convertAfToPkSpelling,
convertSpelling,
} from "../lib/convert-spelling";
import {
phoneticsToDiacritics
@ -23,9 +23,7 @@ const Pashto = ({ opts, children: text }: {
const p = opts.diacritics
? (phoneticsToDiacritics(ps.p, ps.f) || ps.p)
: ps.p;
return opts.spelling === "Afghan"
? p
: convertAfToPkSpelling(p);
return convertSpelling(p, opts.spelling);
}
const style = opts.pTextSize === "normal"
? undefined

View File

@ -7,11 +7,11 @@
*/
import {
convertAfToPkSpelling,
convertPkToAfSpelling,
convertSpelling,
revertSpelling,
} from "./convert-spelling";
const pairs = [
const pairsWPakistaniUndotted = [
["سړی", "سړے"],
["موسیٰ", "موسیٰ"],
["فرمايي", "فرمائی"],
@ -27,14 +27,44 @@ const pairs = [
["ضمائر", "ضمائر"],
];
pairs.forEach((pair) => {
test(`${pair[0]} should be converted to ${pair[1]} in Pakistani spelling`, () => {
const converted = convertAfToPkSpelling(pair[0]);
const pairsWPakistaniDotted = [
["سړی", "سړے"],
["موسیٰ", "موسیٰ"],
["فرمايي", "فرمائي"],
["چای", "چائ"],
["زوی", "زوئ"],
["ښويېدل", "ښوئېدل"],
["ويي", "وئي"],
["دوستي", "دوستي"],
["هييت", "هييت"],
["ښيي", "ښيي"],
["ستاينه", "ستائينه"],
["فرمايل", "فرمائيل"],
["ضمائر", "ضمائر"],
];
pairsWPakistaniDotted.forEach((pair) => {
test(`${pair[0]} should be converted to ${pair[1]} in Pakistani ي spelling`, () => {
const converted = convertSpelling(pair[0], "Pakistani ي");
expect(converted).toBe(pair[1]);
});
test(`${pair[1]} should be reverted to ${pair[0]} in Pakistani ي spelling`, () => {
const reverted = revertSpelling(pair[1], "Pakistani ي");
expect(reverted).toBe(pair[0]);
});
});
test(`${pair[1]} should be converted to ${pair[0]} in Afghan spelling`, () => {
const converted = convertPkToAfSpelling(pair[1]);
pairsWPakistaniUndotted.forEach((pair) => {
test(`${pair[0]} should be converted to ${pair[1]} in Pakistani ی spelling`, () => {
const converted = convertSpelling(pair[0], "Pakistani ی");
expect(converted).toBe(pair[1]);
});
test(`${pair[0]} should stay the same`, () => {
const converted = convertSpelling(pair[0], "Afghan");
expect(converted).toBe(pair[0]);
});
test(`${pair[1]} should be reverted to ${pair[0]} in Pakistani ی spelling`, () => {
const reverted = revertSpelling(pair[1], "Pakistani ی");
expect(reverted).toBe(pair[0]);
});
});

View File

@ -6,28 +6,50 @@
*
*/
export function convertAfToPkSpelling(input: string): string {
const converted = input
.replace(/ای(?![\u0621-\u065f\u0670-\u06d3\u06d5])/g, "ائ")
import * as T from "../types";
/**
* takes a string of standard Afghan Pashto text and puts it into the same or a different spelling system
*
* @param input
* @param spelling
* @returns
*/
export function convertSpelling(input: string, spelling: T.Spelling): string {
if (spelling === "Afghan") {
return input;
}
return input.replace(/ای(?![\u0621-\u065f\u0670-\u06d3\u06d5])/g, "ائ")
.replace(/وی(?![\u0621-\u065f\u0670-\u06d3\u06d5])/g, "وئ")
.replace(/ی(?![\u0621-\u065f\u0670-\u06d3\u06d5])/g, "ے")
.replace(/ي(?![\u0621-\u065f\u0670-\u06d3\u06d5])/g, "ی")
.replace(/ي(?![\u0621-\u065f\u0670-\u06d3\u06d5])/g, (spelling === "Pakistani ی")
? "ی"
: "ي")
.replace(/(?:ای|اي)(?=ي|ی|ې)/g, "ائ")
.replace(/(?:وی|وي)(?=ي|ی|ې)/g, "وئ")
.replace(/(?:ای|اي)(?=[\u0621-\u065f\u0670-\u06d3\u06d5])/g, "ائي")
.replace(/(?:وی|وي)(?=[\u0621-\u065f\u0670-\u06d3\u06d5])/g, "وئي");
return converted;
}
export function convertPkToAfSpelling(input: string): string {
const converted = input
.replace(/ی(?![\u0621-\u065f\u0670-\u06d3\u06d5])/g, "ي")
/**
* Takes a string of a given spelling system and puts it into standardAfghan Pashto text
*
* @param input
* @param spelling
*/
export function revertSpelling(input: string, spelling: T.Spelling): string {
if (spelling === "Afghan") {
return input;
}
return input
.replace(new RegExp(`${spelling === "Pakistani ی"
? "ی"
: "ي"}(?![\u0621-\u065f\u0670-\u06d3\u06d5])`, "g"), "ي")
.replace(/ے(?![\u0621-\u065f\u0670-\u06d3\u06d5])/g, "ی")
.replace(/ائ(?![\u0621-\u065f\u0670-\u06d3\u06d5])/g, "ای")
.replace(/وئ(?![\u0621-\u065f\u0670-\u06d3\u06d5])/g, "وی")
.replace(/(?:ائی|ائي)(?=[\u0621-\u065f\u0670-\u06d3\u06d5])/g, "اي")
.replace(/(?:وئی|وئي)(?=[\u0621-\u065f\u0670-\u06d3\u06d5])/g, "وي")
.replace(/ائ(?=ي|ی|ې)/g, "اي")
.replace(/وئ(?=ي|ی|ې)/g, "وي");
return converted;
.replace(/وئ(?=ي|ی|ې)/g, "وي");;
}

View File

@ -37,8 +37,8 @@ import {
import { standardizePashto } from "./lib/standardize-pashto";
import { phoneticsToDiacritics } from "./lib/phonetics-to-diacritics";
import {
convertAfToPkSpelling,
convertPkToAfSpelling,
convertSpelling,
revertSpelling,
} from "./lib/convert-spelling";
import {
dictionaryEntryBooleanFields,
@ -87,8 +87,8 @@ export {
makePsString,
removeFVariants,
standardizePashto,
convertAfToPkSpelling,
convertPkToAfSpelling,
convertSpelling,
revertSpelling,
validateEntry,
isNounAdjOrVerb,
simplifyPhonetics,

View File

@ -128,11 +128,13 @@ export type DictionaryEntryError = {
erroneousFields: DictionaryEntryField[],
}
export type Spelling = "Afghan" | "Pakistani ی" | "Pakistani ي";
export type TextOptions = {
pTextSize: "normal" | "larger" | "largest";
phonetics: "lingdocs" | "ipa" | "alalc" | "none";
dialect: "standard" | "peshawer" | "southern";
spelling: "Afghan" | "Pakistani";
spelling: Spelling;
diacritics: boolean;
}