fix inflection pattern

This commit is contained in:
adueck 2023-08-18 21:28:14 +04:00
parent 16757d1e49
commit 1da82dc4da
1 changed files with 221 additions and 173 deletions

View File

@ -16,17 +16,18 @@ const chevStyle = {
color: "#ccc", color: "#ccc",
}; };
const nounsAdjs = nounsAdjsUnsafe const nounsAdjs = nounsAdjsUnsafe.filter(
.filter(x => tp.isNounEntry(x) || tp.isAdjectiveEntry(x)) as (T.NounEntry | T.AdjectiveEntry)[]; (x) => tp.isNounEntry(x) || tp.isAdjectiveEntry(x)
) as (T.NounEntry | T.AdjectiveEntry)[];
function InflectionDemo({ opts }: { function InflectionDemo({ opts }: { opts: T.TextOptions }) {
opts: T.TextOptions,
}) {
const [pattern, setPattern] = useState<T.InflectionPattern | "all">("all"); const [pattern, setPattern] = useState<T.InflectionPattern | "all">("all");
const [word, setWord] = useState<T.NounEntry | T.AdjectiveEntry | undefined>(undefined) const [word, setWord] = useState<T.NounEntry | T.AdjectiveEntry | undefined>(
undefined
);
const patterns: { const patterns: {
value: T.InflectionPattern | "all", value: T.InflectionPattern | "all";
label: JSX.Element | string, label: JSX.Element | string;
}[] = [ }[] = [
{ {
value: "all", value: "all",
@ -35,27 +36,47 @@ function InflectionDemo({ opts }: {
{ {
value: 0, value: 0,
label: "no inflection", label: "no inflection",
}, { },
{
value: 1, value: 1,
label: "basic", label: "basic",
}, { },
{
value: 2, value: 2,
label: <>unstressed <InlinePs opts={opts}>{{ p: "ی", f: "ey" }}</InlinePs></>, label: (
}, { <>
unstressed <InlinePs opts={opts}>{{ p: "ی", f: "ay" }}</InlinePs>
</>
),
},
{
value: 3, value: 3,
label: <>stressed <InlinePs opts={opts}>{{ p: "ی", f: "éy" }}</InlinePs></>, label: (
}, { <>
stressed <InlinePs opts={opts}>{{ p: "ی", f: "áy" }}</InlinePs>
</>
),
},
{
value: 4, value: 4,
label: '"Pashtoon" pattern', label: '"Pashtoon" pattern',
}, { },
{
value: 5, value: 5,
label: "short squish", label: "short squish",
}, { },
{
value: 6, value: 6,
label: <>fem. inan. <InlinePs opts={opts}>{{ p: "ي", f: "ee" }}</InlinePs></>, label: (
<>
fem. inan. <InlinePs opts={opts}>{{ p: "ي", f: "ee" }}</InlinePs>
</>
),
}, },
]; ];
const entries = (nounsAdjs as (T.NounEntry | T.AdjectiveEntry)[]).filter(tp.isPattern(pattern)) const entries = (nounsAdjs as (T.NounEntry | T.AdjectiveEntry)[]).filter(
tp.isPattern(pattern)
);
function handlePatternChange(e: React.ChangeEvent<HTMLInputElement>) { function handlePatternChange(e: React.ChangeEvent<HTMLInputElement>) {
const v = e.target.value; const v = e.target.value;
const value = v === "all" ? v : (Number(v) as T.InflectionPattern); const value = v === "all" ? v : (Number(v) as T.InflectionPattern);
@ -70,9 +91,8 @@ function InflectionDemo({ opts }: {
setWord(entries[0]); setWord(entries[0]);
return; return;
} }
const oldIndex = entries.findIndex(e => e.ts === word.ts); const oldIndex = entries.findIndex((e) => e.ts === word.ts);
const newIndex = entries.length === (oldIndex + 1) const newIndex = entries.length === oldIndex + 1 ? 0 : oldIndex + 1;
? 0 : (oldIndex + 1);
setWord(entries[newIndex]); setWord(entries[newIndex]);
} }
function wordBack() { function wordBack() {
@ -80,9 +100,8 @@ function InflectionDemo({ opts }: {
setWord(entries[entries.length - 1]); setWord(entries[entries.length - 1]);
return; return;
} }
const oldIndex = entries.findIndex(e => e.ts === word.ts); const oldIndex = entries.findIndex((e) => e.ts === word.ts);
const newIndex = oldIndex === 0 const newIndex = oldIndex === 0 ? entries.length - 1 : oldIndex + 1;
? (entries.length - 1) : (oldIndex + 1);
setWord(entries[newIndex]); setWord(entries[newIndex]);
} }
const inf = ((): T.InflectorOutput | false => { const inf = ((): T.InflectorOutput | false => {
@ -94,18 +113,29 @@ function InflectionDemo({ opts }: {
return false; return false;
} }
})(); })();
return <div style={{ paddingBottom: "20px" }}> return (
<div style={{ paddingBottom: "20px" }}>
<p> <p>
Produces the inflections and plural forms (Pashto and Arabic plurals where applicable) for words Produces the inflections and plural forms (Pashto and Arabic plurals
where applicable) for words
</p> </p>
<div className="d-block mx-auto card" style={{ maxWidth: "700px", background: "var(--closer)"}}> <div
className="d-block mx-auto card"
style={{ maxWidth: "700px", background: "var(--closer)" }}
>
<div className="card-body"> <div className="card-body">
<div className="row"> <div className="row">
<div className="col-sm-6"> <div className="col-sm-6">
<div> <div>
<h6 className="my-2">Filter words by <a href="https://grammar.lingdocs.com/inflection/inflection-patterns/">inflection pattern</a>:</h6> <h6 className="my-2">
Filter words by{" "}
<a href="https://grammar.lingdocs.com/inflection/inflection-patterns/">
inflection pattern
</a>
:
</h6>
<div> <div>
{patterns.map(({ value, label}) => ( {patterns.map(({ value, label }) => (
<div key={value} className="form-check"> <div key={value} className="form-check">
<input <input
className="form-check-input" className="form-check-input"
@ -115,9 +145,7 @@ function InflectionDemo({ opts }: {
value={value} value={value}
onChange={handlePatternChange} onChange={handlePatternChange}
/> />
<label className="form-check-label"> <label className="form-check-label">{label}</label>
{label}
</label>
</div> </div>
))} ))}
</div> </div>
@ -157,28 +185,48 @@ function InflectionDemo({ opts }: {
</div> </div>
</div> </div>
</div> </div>
{inf ? <div className="mt-3"> {inf ? (
{inf.inflections && word && (() => { <div className="mt-3">
{inf.inflections &&
word &&
(() => {
const pattern = getInflectionPattern(word); const pattern = getInflectionPattern(word);
return <div> return (
<a href={`https://grammar.lingdocs.com/inflection/inflection-patterns/${inflectionSubUrl(pattern)}`} rel="noreferrer" target="_blank"> <div>
<div className="badge bg-light mb-2">Inflection pattern {HumanReadableInflectionPattern(pattern, opts)} <a
href={`https://grammar.lingdocs.com/inflection/inflection-patterns/${inflectionSubUrl(
pattern
)}`}
rel="noreferrer"
target="_blank"
>
<div className="badge bg-light mb-2">
Inflection pattern{" "}
{HumanReadableInflectionPattern(pattern, opts)}
</div> </div>
</a> </a>
<InflectionsTable inf={inf.inflections} textOptions={opts} /> <InflectionsTable inf={inf.inflections} textOptions={opts} />
</div>; </div>
);
})()} })()}
{"plural" in inf && inf.plural !== undefined && <div> {"plural" in inf && inf.plural !== undefined && (
<div>
<h5>Plural</h5> <h5>Plural</h5>
<InflectionsTable inf={inf.plural} textOptions={opts} /> <InflectionsTable inf={inf.plural} textOptions={opts} />
</div>} </div>
{"arabicPlural" in inf && inf.arabicPlural !== undefined && <div> )}
{"arabicPlural" in inf && inf.arabicPlural !== undefined && (
<div>
<h5>Arabic Plural</h5> <h5>Arabic Plural</h5>
<InflectionsTable inf={inf.arabicPlural} textOptions={opts} /> <InflectionsTable inf={inf.arabicPlural} textOptions={opts} />
</div>} </div>
</div> : <div> )}
</div>} </div>
</div>; ) : (
<div></div>
)}
</div>
);
} }
function inflectionSubUrl(pattern: T.InflectionPattern): string { function inflectionSubUrl(pattern: T.InflectionPattern): string {
@ -194,8 +242,8 @@ function inflectionSubUrl(pattern: T.InflectionPattern): string {
? "#4-words-with-the-pashtoon-pattern" ? "#4-words-with-the-pashtoon-pattern"
: pattern === 5 : pattern === 5
? "#5-shorter-words-that-squish" ? "#5-shorter-words-that-squish"
// : pattern === 6 : // : pattern === 6
: "#6-inanimate-feminine-nouns-ending-in-ي---ee" "#6-inanimate-feminine-nouns-ending-in-ي---ee";
} }
export default InflectionDemo; export default InflectionDemo;