more on phrase builder

This commit is contained in:
lingdocs 2022-03-15 17:29:14 +04:00
parent 30e5697452
commit a000731a4a
7 changed files with 331 additions and 21 deletions

View File

@ -31,7 +31,6 @@ function makeVerbSelection(verb: VerbEntry, oldVerbSelection?: VerbSelection): V
: verbType === "trans" : verbType === "trans"
? getTransObjFromOldVerbSelection() ? getTransObjFromOldVerbSelection()
: "none"; : "none";
console.log({ verbType, object });
return { return {
type: "verb", type: "verb",
verb, verb,
@ -52,7 +51,6 @@ function VerbPicker({ onChange, verb, verbs }: { verbs: VerbEntry[], verb: VerbS
} }
function onTenseSelect({ value }: { label: string, value: "present" | "subjunctive" }) { function onTenseSelect({ value }: { label: string, value: "present" | "subjunctive" }) {
if (verb) { if (verb) {
console.log("changing to", value)
onChange({ onChange({
...verb, ...verb,
tense: value, tense: value,

View File

@ -52,15 +52,15 @@ function NPNounPicker({ onChange, noun, nouns }: { nouns: NounEntry[], noun: Nou
{noun.changeNumber ? <ButtonSelect {noun.changeNumber ? <ButtonSelect
small small
options={[ options={[
{ label: "Sing.", value: "sing" }, { label: "Sing.", value: "singular" },
{ label: "Plur.", value: "plur" }, { label: "Plur.", value: "plural" },
]} ]}
value={noun.number} value={noun.number}
handleChange={(n) => { handleChange={(n) => {
if (!noun.changeNumber) return; if (!noun.changeNumber) return;
onChange(noun.changeNumber(n)); onChange(noun.changeNumber(n));
}} }}
/> : noun.number === "sing" ? "Sing." : "Plur."} /> : noun.number === "singular" ? "Sing." : "Plur."}
</div> </div>
</div>} </div>}
</div>; </div>;

View File

@ -1,7 +1,4 @@
import { import {
isNounEntry,
isAdjectiveEntry,
isAdverbEntry,
isPluralNounEntry, isPluralNounEntry,
isMascNounEntry, isMascNounEntry,
isUnisexNounEntry, isUnisexNounEntry,
@ -14,6 +11,7 @@ import {
import { import {
getEnglishWord, getEnglishWord,
removeFVarients, removeFVarients,
Types as T,
} from "@lingdocs/pashto-inflector"; } from "@lingdocs/pashto-inflector";
export const zIndexProps = { export const zIndexProps = {
@ -48,22 +46,22 @@ export function makeSelectOption(e: VerbEntry | NounEntry | AdjectiveEntry | Loc
} }
export function makeNounSelection(entry: NounEntry): NounSelection { export function makeNounSelection(entry: NounEntry): NounSelection {
const number = isPluralNounEntry(entry) ? "plur" : "sing"; const number = isPluralNounEntry(entry) ? "plural" : "singular";
return { return {
type: "noun", type: "noun",
entry, entry,
gender: isMascNounEntry(entry) ? "masc" : "fem", gender: isMascNounEntry(entry) ? "masc" : "fem",
number, number,
...isUnisexNounEntry(entry) ? { ...isUnisexNounEntry(entry) ? {
changeGender: function(gender: "masc" | "fem"): NounSelection { changeGender: function(gender: T.Gender): NounSelection {
return { return {
...this, ...this,
gender, gender,
}; };
}, },
} : {}, } : {},
...number === "sing" ? { ...number === "singular" ? {
changeNumber: function(number: "plur" | "sing"): NounSelection { changeNumber: function(number: NounNumber): NounSelection {
return { return {
...this, ...this,
number, number,

View File

@ -1,9 +1,22 @@
import { useState } from "react"; import { useState } from "react";
import NPPicker from "../np-picker/NPPicker"; import NPPicker from "../np-picker/NPPicker";
import VerbPicker from "../VerbPicker"; import VerbPicker from "../VerbPicker";
import VPDisplay from "./VPDisplay";
import ObjectDisplay from "./ObjectDisplay"; import ObjectDisplay from "./ObjectDisplay";
import { verbs } from "../../words/words"; import { verbs } from "../../words/words";
function verbPhraseComplete({ subject, verb }: { subject: NPSelection | undefined, verb: VerbSelection | undefined }): VPSelection | undefined {
if (!subject) return undefined;
if (!verb) return undefined;
if (verb.object === undefined) return undefined;
return {
type: "VPSelection",
subject,
object: verb.object,
verb,
};
}
export function PhraseBuilder() { export function PhraseBuilder() {
const [subject, setSubject] = useState<NPSelection | undefined>(undefined); const [subject, setSubject] = useState<NPSelection | undefined>(undefined);
const [verb, setVerb] = useState<VerbSelection | undefined>(undefined); const [verb, setVerb] = useState<VerbSelection | undefined>(undefined);
@ -15,7 +28,7 @@ export function PhraseBuilder() {
object, object,
}); });
} }
console.log({ subject, verb }); const verbPhrase: VPSelection | undefined = verbPhraseComplete({ subject, verb });
return <div> return <div>
<div className="d-flex flex-row justify-content-between"> <div className="d-flex flex-row justify-content-between">
<div className="mr-2"> <div className="mr-2">
@ -31,6 +44,9 @@ export function PhraseBuilder() {
<VerbPicker verbs={verbs} verb={verb} onChange={setVerb} /> <VerbPicker verbs={verbs} verb={verb} onChange={setVerb} />
</div> </div>
</div> </div>
{verbPhrase && <div>
<VPDisplay VP={verbPhrase} />
</div>}
</div> </div>
} }

View File

@ -0,0 +1,20 @@
import { renderVP, compileVP } from "../../lib/eval-vp";
import {
InlinePs,
defaultTextOptions as opts,
} from "@lingdocs/pashto-inflector";
function VPDisplay({ VP }: { VP: VPSelection }) {
const result = compileVP(renderVP(VP));
const rPs = "long" in result.ps ? result.ps.long : result.ps;
return <div className="text-center mt-2">
{rPs.map((r, i) => <div key={i}>
<InlinePs opts={opts}>{r}</InlinePs>
</div>)}
{result.e && <div className="text-muted">
{result.e.map((e, i) => <div key={i}>{e}</div>)}
</div>}
</div>
}
export default VPDisplay;

256
src/lib/eval-vp.ts Normal file
View File

@ -0,0 +1,256 @@
import {
Types as T,
getVerbBlockPosFromPerson,
grammarUnits,
getEnglishWord,
inflectWord,
parseEc,
conjugateVerb,
concatPsString,
} from "@lingdocs/pashto-inflector";
import {
psStringFromEntry,
} from "./text-tools";
export function renderVP(VP: VPSelection): VPRendered {
// TODO: Will be based on past tense etc
const king = "subject";
// const servant = VP.object ? "object" : undefined;
const verbPerson = getPersonFromNP(VP[king]);
const subjectPerson = getPersonFromNP(VP.subject);
const subject: Rendered<NPSelection> = {
...VP.subject,
inflected: false,
// TODO: possibility for inflecting
...textOfNP(VP.subject, false, false),
};
const inflectObject = isFirstOrSecondPersPronoun(VP.object);
const object: "none" | Rendered<NPSelection> | T.Person.ThirdPlurMale = typeof VP.object === "object"
? {
...VP.subject,
inflected: inflectObject,
// TODO: possibility for inflecting
...textOfNP(VP.object, inflectObject, true),
} : VP.object;
// TODO: error handle this?
const conjugations = conjugateVerb(VP.verb.verb.entry, VP.verb.verb.complement);
// TODO: option to manually select these
const conj = "grammaticallyTransitive" in conjugations
? conjugations.grammaticallyTransitive
: "stative" in conjugations
? conjugations.stative
: conjugations;
return {
type: "VPRendered",
subject,
object,
verb: {
...VP.verb,
person: verbPerson,
ps: getPsVerbConjugation(conj, VP.verb.tense, verbPerson),
e: getEnglishVerbConjugation({
subjectPerson,
object: VP.object,
v: parseEc(VP.verb.verb.entry.ec || ""),
ep: VP.verb.verb.entry.ep,
tense: VP.verb.tense,
n: false,
}),
},
};
}
export function compileVP(VP: VPRendered | VPSelection): { ps: T.SingleOrLengthOpts<T.PsString[]>, e?: string [] } {
if (VP.type === "VPSelection") {
return compileVP(renderVP(VP));
}
const vPs = "long" in VP.verb.ps ? VP.verb.ps.long : VP.verb.ps;
// const vE = VP.verb.e;
const obj = typeof VP.object === "object" ? VP.object : undefined;
const ps = VP.subject.ps.flatMap(s => (
// TODO: fix double space thing
obj ? obj.ps.flatMap(o => (
vPs.flatMap(v => (
concatPsString(s, " ", o, " ", v)
))
)) : vPs.flatMap(v => (
concatPsString(s, " ", v)
))
));
return { ps };
}
function isFirstOrSecondPersPronoun(o: "none" | NPSelection | T.Person.ThirdPlurMale): boolean {
if (typeof o !== "object") return false;
if (o.type !== "pronoun") return false;
return [0,1,2,3,6,7,8,9].includes(o.person);
}
function getPsVerbConjugation(conj: T.VerbConjugation, tense: "present" | "subjunctive", person: T.Person): T.SingleOrLengthOpts<T.PsString[]> {
const f = conj[tense === "present" ? "imperfective" : "perfective"].nonImperative;
// TODO: ability to grab the correct part of matrix
const block = "mascSing" in f
? f.mascSing
: f;
function grabFromBlock(b: T.VerbBlock, [row, col]: [ row: number, col: number ]): T.PsString[] {
return b[row][col];
}
const pos = getVerbBlockPosFromPerson(person);
if ("long" in block) {
return {
long: grabFromBlock(block.long, pos),
short: grabFromBlock(block.short, pos),
...block.mini ? {
mini: grabFromBlock(block.mini, pos),
} : {},
};
}
return grabFromBlock(block, pos);
}
function getEnglishVerbConjugation({ subjectPerson, object, ep, v, tense, n }: {
subjectPerson: T.Person,
object: "none" | NPSelection | T.Person.ThirdPlurMale,
v: T.EnglishVerbConjugationEc,
ep: string | undefined,
tense: "present" | "subjunctive",
n: boolean,
}): string[] {
function engEquative(tense: "past" | "present", s: T.Person): string {
const [row, col] = getVerbBlockPosFromPerson(s);
return grammarUnits.englishEquative[tense][row][col];
}
function engPresC(s: T.Person, ec: T.EnglishVerbConjugationEc | [string, string]): string {
function isThirdPersonSing(p: T.Person): boolean {
return (
p === T.Person.ThirdSingMale ||
p === T.Person.ThirdSingFemale
);
}
return isThirdPersonSing(s) ? ec[1] : ec[0];
}
function isToBe(v: T.EnglishVerbConjugationEc): boolean {
return (v[2] === "being");
}
const builders: Record<
string,
(s: T.Person, v: T.EnglishVerbConjugationEc, n: boolean) => string[]
> = {
present: (s: T.Person, v: T.EnglishVerbConjugationEc, n: boolean) => ([
`$SUBJ ${isToBe(v)
? `${engEquative("present", s)}${n ? " not" : ""}`
: `${n ? engPresC(s, ["don't", "doesn't"]) : ""} ${n ? v[0] : engPresC(s, v)}`}`,
`$SUBJ ${engEquative("present", s)}${n ? " not" : ""} ${v[2]}`,
]),
subjunctive: (s: T.Person, v: T.EnglishVerbConjugationEc, n: boolean) => ([
`that $SUBJ ${n ? " won't" : " will"} ${isToBe(v) ? "be" : v[0]}`,
`should $SUBJ ${n ? " not" : ""} ${isToBe(v) ? "be" : v[0]}`,
]),
};
const base = builders[tense](subjectPerson, v, n);
return base.map(b => `${b}${typeof object === "object" ? " $OBJ" : ""}${ep ? ` ${ep}` : ""}`);
}
function getPersonFromNP(np: NPSelection | T.Person.ThirdPlurMale): T.Person {
if (typeof np === "number") return np;
if (np.type === "participle") {
return T.Person.ThirdPlurMale;
}
if (np.type === "pronoun") {
return np.person;
}
return np.number === "plural"
? (np.gender === "masc" ? T.Person.ThirdPlurMale : T.Person.ThirdPlurFemale)
: (np.gender === "masc" ? T.Person.ThirdSingMale : T.Person.ThirdSingFemale);
}
function textOfNP(np: NPSelection, inflected: boolean, englishInflected: boolean): { ps: T.PsString[], e: string } {
if (np.type === "participle") {
// TODO: Implement ability to inflect participles
return textOfParticiple(np, inflected);
}
if (np.type === "pronoun") {
return textOfPronoun(np, inflected, englishInflected);
}
// TODO: Implement ability to inflect nouns
return textOfNoun(np, inflected);
}
function textOfParticiple({ verb: { entry }}: ParticipleSelection, inflected: boolean): { ps: T.PsString[], e: string } {
// TODO: ability to inflect participles
return {
ps: [psStringFromEntry(entry)],
e: getEnglishParticiple(entry),
};
}
function textOfPronoun(p: PronounSelection, inflected: boolean, englishInflected: boolean): { ps: T.PsString[], e: string } {
// TODO: Will need to handle inflecting and inflecting english pronouns etc.
const [row, col] = getVerbBlockPosFromPerson(p.person);
return {
ps: grammarUnits.pronouns[p.distance][inflected ? "inflected" : "plain"][row][col],
e: grammarUnits.persons[p.person].label[englishInflected ? "object" : "subject"],
};
}
function getEnglishFromNoun(entry: T.DictionaryEntry, number: NounNumber): string {
const articles = {
singular: "(a/the)",
plural: "(the)",
};
const article = articles[number];
function addArticle(s: string) {
return `${article} ${s}`;
}
const e = getEnglishWord(entry);
if (!e) throw new Error(`unable to get english from subject ${entry.f} - ${entry.ts}`);
if (typeof e === "string") return ` ${e}`;
if (number === "plural") return addArticle(e.plural);
if (!e.singular || e.singular === undefined) {
throw new Error(`unable to get english from subject ${entry.f} - ${entry.ts}`);
}
return addArticle(e.singular);
}
function textOfNoun(n: NounSelection, inflected: boolean): { ps: T.PsString[], e: string } {
const english = getEnglishFromNoun(n.entry, n.number);
const pashto = ((): T.PsString[] => {
const infs = inflectWord(n.entry);
const ps = n.number === "singular"
? getInf(infs, "inflections", n.gender, false)
: [
...getInf(infs, "plural", n.gender, true),
...getInf(infs, "arabicPlural", n.gender, true),
...getInf(infs, "inflections", n.gender, true),
];
return ps.length > 0
? ps
: [psStringFromEntry(n.entry)];
})();
return { ps: pashto, e: english };
}
function getInf(infs: T.InflectorOutput, t: "plural" | "arabicPlural" | "inflections", gender: T.Gender, plural: boolean): T.PsString[] {
// @ts-ignore
if (infs && t in infs && infs[t] !== undefined && gender in infs[t] && infs[t][gender] !== undefined) {
// @ts-ignore
const iset = infs[t][gender] as T.InflectionSet;
const ipick = iset[(t === "inflections" && plural) ? 1 : 0];
return ipick;
}
return [];
}
export function getEnglishParticiple(entry: T.DictionaryEntry): string {
if (!entry.ec) {
console.log("errored participle");
console.log(entry);
throw new Error("no english information for participle");
}
const ec = parseEc(entry.ec);
const participle = ec[2];
return (entry.ep)
? `${participle} ${entry.ep}`
: participle;
}

38
src/types/gen-g.d.ts vendored
View File

@ -4,12 +4,22 @@
// type FemPlurNounEntry = ""; // can change nothing // type FemPlurNounEntry = ""; // can change nothing
// type UnisexNounEntry = ""; // can change number or gender // type UnisexNounEntry = ""; // can change number or gender
type VP = {
type VPSelection = {
type: "VPSelection",
subject: NPSelection, subject: NPSelection,
object: NPSelection, object: Exclude<VerbObject, undefined>,
verb: VerbSelection, verb: Exclude<VerbSelection, "object">,
}; };
// TODO: make this Rendered<VPSelection> with recursive Rendered<>
type VPRendered = {
type: "VPRendered",
subject: Rendered<NPSelection>,
object: "none" | Rendered<NPSelection> | import("@lingdocs/pashto-inflector").Types.Person.ThirdPlurMale,
verb: VerbRendered,
}
type VerbSelection = { type VerbSelection = {
type: "verb", type: "verb",
verb: VerbEntry, verb: VerbEntry,
@ -17,6 +27,14 @@ type VerbSelection = {
object: VerbObject, object: VerbObject,
}; };
type VerbRendered = Omit<VerbSelection, "object"> & {
ps: import("@lingdocs/pashto-inflector").Types.SingleOrLengthOpts<
import("@lingdocs/pashto-inflector").Types.PsString[]
>,
person: import("@lingdocs/pashto-inflector").Types.Person,
e?: string[],
};
type VerbObject = // intransitive verb type VerbObject = // intransitive verb
"none" | "none" |
// transitive verb - object not selected yet // transitive verb - object not selected yet
@ -34,16 +52,16 @@ type NPType = "noun" | "pronoun" | "participle";
type NounSelection = { type NounSelection = {
type: "noun", type: "noun",
entry: NounEntry, entry: NounEntry,
gender: "masc" | "fem", gender: import("@lingdocs/pashto-inflector").Types.Gender,
number: "sing" | "plur", number: NounNumber,
// TODO: Implement // TODO: Implement
// adjectives: [], // adjectives: [],
// TODO: Implement // TODO: Implement
// possesor: NPSelection | undefined, // possesor: NPSelection | undefined,
/* method only present if it's possible to change gender */ /* method only present if it's possible to change gender */
changeGender?: (gender: "masc" | "fem") => NounSelection, changeGender?: (gender: import("@lingdocs/pashto-inflector").Types.Gender) => NounSelection,
/* method only present if it's possible to change number */ /* method only present if it's possible to change number */
changeNumber?: (number: "sing" | "plur") => NounSelection, changeNumber?: (number: NounNumber) => NounSelection,
}; };
// take an argument for subject/object in rendering English // take an argument for subject/object in rendering English
@ -68,7 +86,11 @@ type Rendered<T extends NPSelection> = ReplaceKey<
Omit<T, "changeGender" | "changeNumber" | "changeDistance">, Omit<T, "changeGender" | "changeNumber" | "changeDistance">,
"e", "e",
string string
> & { inflected: boolean }; > & {
ps: import("@lingdocs/pashto-inflector").Types.PsString[],
e?: string,
inflected: boolean,
};
// TODO: recursive changing this down into the possesor etc. // TODO: recursive changing this down into the possesor etc.