phonetics update

This commit is contained in:
adueck 2023-07-28 10:25:39 +04:00
parent ddf967ae63
commit c82fc04821
3 changed files with 262 additions and 212 deletions

View File

@ -20,28 +20,32 @@ const amount = 8;
const timeLimit = 300; const timeLimit = 300;
type Question = { type Question = {
entry: T.NounEntry | T.AdjectiveEntry, entry: T.NounEntry | T.AdjectiveEntry;
inflections: T.Inflections, inflections: T.Inflections;
}; };
type InfFormContent = { type InfFormContent = {
masc: [string, string, string], masc: [string, string, string];
fem: [string, string, string], fem: [string, string, string];
}; };
export default function InflectionsWriting({ inChapter, id, link, level }: { export default function InflectionsWriting({
inChapter: boolean, inChapter,
id: string, id,
link: string, link,
level: T.InflectionPattern, level,
}: {
inChapter: boolean;
id: string;
link: string;
level: T.InflectionPattern;
}) { }) {
const wordPool = makePool( const wordPool = makePool(
[...nouns, ...adjectives] [...nouns, ...adjectives].filter((x) => {
.filter(x => {
if (isAdverbEntry(x)) return false; if (isAdverbEntry(x)) return false;
const infs = inflectWord(x); const infs = inflectWord(x);
if (!infs || !infs.inflections) return false; if (!infs || !infs.inflections) return false;
return (getInflectionPattern(x) === level); return getInflectionPattern(x) === level;
}) })
); );
@ -55,41 +59,54 @@ export default function InflectionsWriting({ inChapter, id, link, level }: {
entry: word, entry: word,
inflections: r.inflections, inflections: r.inflections,
}; };
}; }
function Display({ question, callback }: QuestionDisplayProps<Question>) { function Display({ question, callback }: QuestionDisplayProps<Question>) {
function handleAnswer(inf: InfFormContent) { function handleAnswer(inf: InfFormContent) {
callback(infAnswerCorrect(inf, question.inflections)); callback(infAnswerCorrect(inf, question.inflections));
} }
return <div> return (
<div>
<div className="mb-2" style={{ maxWidth: "300px", margin: "0 auto" }}> <div className="mb-2" style={{ maxWidth: "300px", margin: "0 auto" }}>
<Examples opts={opts}>{[ <Examples opts={opts}>
{[
{ {
p: firstVariation(question.entry.p), p: firstVariation(question.entry.p),
f: firstVariation(question.entry.f), f: firstVariation(question.entry.f),
e: `${firstVariation(question.entry.e)} - ${question.entry.c}`, e: `${firstVariation(question.entry.e)} - ${question.entry.c}`,
} },
]}</Examples> ]}
</Examples>
</div> </div>
<InflectionTableForm <InflectionTableForm
onSubmit={handleAnswer} onSubmit={handleAnswer}
question={question} question={question}
genders={isUnisexSet(question.inflections) genders={
isUnisexSet(question.inflections)
? "unisex" ? "unisex"
: "masc" in question.inflections : "masc" in question.inflections
? "masc" ? "masc"
: "fem"} : "fem"
}
/> />
</div> </div>
);
} }
function Instructions() { function Instructions() {
return <div> return (
<p className="lead">Complete the inflections for the <strong>{HumanReadableInflectionPattern(level, opts)}</strong> pattern word</p> <div>
<p className="lead">
Complete the inflections for the{" "}
<strong>{HumanReadableInflectionPattern(level, opts)}</strong> pattern
word
</p>
</div> </div>
);
} }
return <GameCore return (
<GameCore
inChapter={inChapter} inChapter={inChapter}
studyLink={link} studyLink={link}
getQuestion={getQuestion} getQuestion={getQuestion}
@ -100,14 +117,22 @@ export default function InflectionsWriting({ inChapter, id, link, level }: {
amount={amount} amount={amount}
Instructions={Instructions} Instructions={Instructions}
/> />
}; );
}
function InflectionTableForm({ onSubmit, genders, question }: { function InflectionTableForm({
onSubmit: (i: InfFormContent) => void, onSubmit,
genders: T.Gender | "unisex", genders,
question: Question, question,
}: {
onSubmit: (i: InfFormContent) => void;
genders: T.Gender | "unisex";
question: Question;
}) { }) {
const [inf, setInf] = useState<InfFormContent>({ fem: ["", "", ""], masc: ["", "", ""] }); const [inf, setInf] = useState<InfFormContent>({
fem: ["", "", ""],
masc: ["", "", ""],
});
const mascInputRef = useRef<HTMLInputElement>(null); const mascInputRef = useRef<HTMLInputElement>(null);
const femInputRef = useRef<HTMLInputElement>(null); const femInputRef = useRef<HTMLInputElement>(null);
useEffect(() => { useEffect(() => {
@ -120,29 +145,39 @@ function InflectionTableForm({ onSubmit, genders, question }: {
setInf({ fem: ["", "", ""], masc: ["", "", ""] }); setInf({ fem: ["", "", ""], masc: ["", "", ""] });
} }
function handleInfInput(gender: T.Gender, inflection: number) { function handleInfInput(gender: T.Gender, inflection: number) {
return ({ target: { value }}: ChangeEvent<HTMLInputElement>) => { return ({ target: { value } }: ChangeEvent<HTMLInputElement>) => {
inf[gender][inflection] = value; inf[gender][inflection] = value;
setInf({...inf}); setInf({ ...inf });
}; };
} }
function handleSubmit(e: FormEvent<HTMLFormElement>) { function handleSubmit(e: FormEvent<HTMLFormElement>) {
e.preventDefault(); e.preventDefault();
onSubmit(inf); onSubmit(inf);
} }
return <form onSubmit={handleSubmit}> return (
<form onSubmit={handleSubmit}>
<table className="table" style={{ tableLayout: "fixed" }}> <table className="table" style={{ tableLayout: "fixed" }}>
<thead> <thead>
<tr> <tr>
<th scope="col" style={{ width: "3.5rem" }}></th> <th scope="col" style={{ width: "3.5rem" }}></th>
{genders !== "fem" && <th scope="col" style={{ maxWidth: "10rem", textAlign: "left" }}>Masculine</th>} {genders !== "fem" && (
{genders !== "masc" && <th scope="col" style={{ maxWidth: "10rem", textAlign: "left" }}>Feminine</th>} <th scope="col" style={{ maxWidth: "10rem", textAlign: "left" }}>
Masculine
</th>
)}
{genders !== "masc" && (
<th scope="col" style={{ maxWidth: "10rem", textAlign: "left" }}>
Feminine
</th>
)}
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{["Plain", "1st", "2nd"].map((title, i) => ( {["Plain", "1st", "2nd"].map((title, i) => (
<tr key={title}> <tr key={title}>
<th scope="row">{title}</th> <th scope="row">{title}</th>
{genders !== "fem" && <td> {genders !== "fem" && (
<td>
<input <input
ref={i === 0 ? mascInputRef : undefined} ref={i === 0 ? mascInputRef : undefined}
type="text" type="text"
@ -155,8 +190,10 @@ function InflectionTableForm({ onSubmit, genders, question }: {
onChange={handleInfInput("masc", i)} onChange={handleInfInput("masc", i)}
style={{ maxWidth: "12rem" }} style={{ maxWidth: "12rem" }}
/> />
</td>} </td>
{genders !== "masc" && <td> )}
{genders !== "masc" && (
<td>
<input <input
ref={i === 0 ? femInputRef : undefined} ref={i === 0 ? femInputRef : undefined}
type="text" type="text"
@ -169,39 +206,54 @@ function InflectionTableForm({ onSubmit, genders, question }: {
onChange={handleInfInput("fem", i)} onChange={handleInfInput("fem", i)}
style={{ maxWidth: "12rem" }} style={{ maxWidth: "12rem" }}
/> />
</td>} </td>
)}
</tr> </tr>
))} ))}
</tbody> </tbody>
</table> </table>
<div className="text-center"> <div className="text-center">
<button type="button" className="btn btn-secondary mx-3" onClick={handleClearInf}>Clear</button> <button
<button type="submit" className="btn btn-primary mx-3">Submit</button> type="button"
className="btn btn-secondary mx-3"
onClick={handleClearInf}
>
Clear
</button>
<button type="submit" className="btn btn-primary mx-3">
Submit
</button>
</div> </div>
</form>; </form>
);
} }
function DisplayCorrectAnswer({ question }: { question: Question }): JSX.Element { function DisplayCorrectAnswer({
return <div> question,
}: {
question: Question;
}): JSX.Element {
return (
<div>
<InflectionsTable <InflectionsTable
inf={question.inflections} inf={question.inflections}
textOptions={opts} textOptions={opts}
hideTitle hideTitle
/> />
</div>; </div>
);
} }
function infAnswerCorrect(answer: InfFormContent, inf: T.Inflections): boolean { function infAnswerCorrect(answer: InfFormContent, inf: T.Inflections): boolean {
function genInfCorrect(gender: T.Gender): boolean { function genInfCorrect(gender: T.Gender): boolean {
// @ts-ignore // @ts-ignore
const genInf = inf[gender] as T.InflectionSet; const genInf = inf[gender] as T.InflectionSet;
return genInf.every((x, i) => ( return genInf.every((x, i) =>
x.some(ps => comparePs(answer[gender][i], ps)) x.some((ps) => comparePs(answer[gender][i], [ps]))
)); );
} }
if (isUnisexSet(inf)) { if (isUnisexSet(inf)) {
return genInfCorrect("masc") && genInfCorrect("fem"); return genInfCorrect("masc") && genInfCorrect("fem");
} }
return genInfCorrect("masc" in inf ? "masc": "fem"); return genInfCorrect("masc" in inf ? "masc" : "fem");
} }

View File

@ -414,8 +414,8 @@ function addUserAnswer(
function addBa(x: T.PsString) { function addBa(x: T.PsString) {
if (!a.withBa) return x; if (!a.withBa) return x;
return { return {
p: x.p.replace(kidsBlank.p, baParticle.p), p: x.p.replace(kidsBlank.p, baParticle.p + " "),
f: x.f.replace(kidsBlank.f, baParticle.f), f: x.f.replace(kidsBlank.f, baParticle.f + " "),
}; };
} }
function addAnswer(x: T.PsString): T.PsString { function addAnswer(x: T.PsString): T.PsString {

View File

@ -9,9 +9,7 @@ import {
import { removeAShort } from "./misc-helpers"; import { removeAShort } from "./misc-helpers";
export function getPercentageDone(current: number, total: number): number { export function getPercentageDone(current: number, total: number): number {
return Math.round( return Math.round((current / (total + 1)) * 100);
(current / (total + 1)) * 100
);
} }
/** /**
@ -28,7 +26,10 @@ export function compareF(input: string, answer: string): boolean {
return inp === (hasAccents(inp) ? ans : removeAccents(ans)); return inp === (hasAccents(inp) ? ans : removeAccents(ans));
} }
export function comparePs(inputRaw: string, answer: T.SingleOrLengthOpts<T.PsString | T.PsString[]>): boolean { export function comparePs(
inputRaw: string,
answer: T.SingleOrLengthOpts<T.PsString[]>
): boolean {
function cleanSpaces(s: string): string { function cleanSpaces(s: string): string {
return s.replace(/\s+/g, " "); return s.replace(/\s+/g, " ");
} }
@ -36,11 +37,8 @@ export function comparePs(inputRaw: string, answer: T.SingleOrLengthOpts<T.PsStr
if ("long" in answer) { if ("long" in answer) {
return comparePs(input, flattenLengths(answer)); return comparePs(input, flattenLengths(answer));
} }
if (Array.isArray(answer)) { return answer.some((a) => {
return answer.some(a => comparePs(input, a)); const stand = standardizePhonetics(standardizePashto(input)).trim();
} return stand === cleanSpaces(a.p) || compareF(stand, cleanSpaces(a.f));
const stand = standardizePhonetics( });
standardizePashto(input)
).trim();
return stand === cleanSpaces(answer.p) || compareF(stand, cleanSpaces(answer.f));
} }