clean up compile vp

This commit is contained in:
lingdocs 2022-03-25 22:27:07 +05:00
parent c8c2132bbf
commit 874fb731f1
1 changed files with 66 additions and 54 deletions

View File

@ -7,30 +7,21 @@ import {
} from "@lingdocs/pashto-inflector";
import { removeBa } from "./vp-tools";
type SegmentDescriptions = {
isVerbHead?: boolean,
isOoOrWaaHead?: boolean,
isVerbRest?: boolean,
isMiniPronoun?: boolean,
isKid?: boolean,
isKidBetweenHeadAndRest?: boolean,
isNu?: boolean,
isBa?: boolean,
}
// TODO: make it an option to include O S V order
// TODO: make it an option to include O S V order ?? or is that just always in past tense
// TODO: tu ba laaR nu she hyphens all messed up
export function compileVP(VP: VPRendered, form: FormVersion): { ps: T.SingleOrLengthOpts<T.PsString[]>, e?: string [] } {
export function compileVP(VP: VPRendered, form: FormVersion): { ps: T.SingleOrLengthOpts<T.PsString[]>, e?: string [] };
export function compileVP(VP: VPRendered, form: FormVersion, combineLengths: true): { ps: T.PsString[], e?: string [] };
export function compileVP(VP: VPRendered, form: FormVersion, combineLengths?: true): { ps: T.SingleOrLengthOpts<T.PsString[]>, e?: string [] } {
const verb = VP.verb.ps;
const { kids, NPs } = getSegmentsAndKids(VP, form);
const psResult = compilePs({
NPs,
kids,
verb,
negative: VP.verb.negative,
});
return {
ps: compilePs({
NPs,
kids,
verb,
negative: VP.verb.negative,
}),
ps: combineLengths ? flattenLengths(psResult) : psResult,
e: compileEnglish(VP),
};
}
@ -55,42 +46,23 @@ function compilePs({ NPs, kids, verb: { head, rest }, negative }: CompilePsInput
};
}
const verbWNegativeVersions = compileVerbWNegative(head, rest, negative);
// put together all the different possible permutations based on:
// potential different versions of where the nu goes
return verbWNegativeVersions.flatMap((verbSegments) => (
// potential reordering of NPs
// potential reordering of NPs
NPs.flatMap(NP => {
// put in kids
// for each permutation of the possible ordering of NPs and Verb + nu
// 1. put in kids in the kids section
const segments = putKidsInKidsSection([...NP, ...verbSegments], kids);
// space out the words properly
// 2. space out the words properly
const withProperSpaces = addSpacesBetweenSegments(segments);
// throw it all together into a PsString
// 3. throw it all together into a PsString for each permutation
return combineSegments(withProperSpaces);
})
));
}
function addSpacesBetweenSegments(segments: Segment[]): (Segment | " " | "" | T.PsString)[] {
const o: (Segment | " " | "" | T.PsString)[] = [];
for (let i = 0; i < segments.length; i++) {
const current = segments[i];
const next = segments[i+1];
o.push(current);
if (!next) break;
if ((next.isKidBetweenHeadAndRest || next.isNu) || (next.isVerbRest && current.isKidBetweenHeadAndRest)) {
o.push({
f: "-",
p: ((current.isVerbHead && (next.isMiniPronoun || next.isNu))
|| (current.isOoOrWaaHead && next.isBa )) ? "" : " ", // or if its waa head
});
} else if (current.isVerbHead && next.isVerbRest) {
o.push("");
} else {
o.push(" ");
}
}
return o;
}
function getSegmentsAndKids(VP: VPRendered, form: FormVersion): { kids: Segment[], NPs: Segment[][] } {
const main = {
subject: VP.subject.ps,
@ -113,7 +85,7 @@ function getSegmentsAndKids(VP: VPRendered, form: FormVersion): { kids: Segment[
...VP.verb.hasBa
? [makeSegment(grammarUnits.baParticle, ["isBa", "isKid"])] : [],
...toShrink
? [shrink(toShrink)] : [],
? [shrinkNP(toShrink)] : [],
],
NPs: [
[
@ -131,11 +103,6 @@ function getSegmentsAndKids(VP: VPRendered, form: FormVersion): { kids: Segment[
}
}
function shrink(np: Rendered<NPSelection>): Segment {
const [row, col] = getVerbBlockPosFromPerson(np.person);
return makeSegment(grammarUnits.pronouns.mini[row][col], ["isKid", "isMiniPronoun"]);
}
function putKidsInKidsSection(segments: Segment[], kids: Segment[]): Segment[] {
const first = segments[0];
const rest = segments.slice(1);
@ -186,6 +153,33 @@ function compileVerbWNegative(head: T.PsString | undefined, restRaw: T.PsString[
];
}
function shrinkNP(np: Rendered<NPSelection>): Segment {
const [row, col] = getVerbBlockPosFromPerson(np.person);
return makeSegment(grammarUnits.pronouns.mini[row][col], ["isKid", "isMiniPronoun"]);
}
function addSpacesBetweenSegments(segments: Segment[]): (Segment | " " | "" | T.PsString)[] {
const o: (Segment | " " | "" | T.PsString)[] = [];
for (let i = 0; i < segments.length; i++) {
const current = segments[i];
const next = segments[i+1];
o.push(current);
if (!next) break;
if ((next.isKidBetweenHeadAndRest || next.isNu) || (next.isVerbRest && current.isKidBetweenHeadAndRest)) {
o.push({
f: "-",
p: ((current.isVerbHead && (next.isMiniPronoun || next.isNu))
|| (current.isOoOrWaaHead && next.isBa )) ? "" : " ", // or if its waa head
});
} else if (current.isVerbHead && next.isVerbRest) {
o.push("");
} else {
o.push(" ");
}
}
return o;
}
function compileEnglish(VP: VPRendered): string[] | undefined {
function insertEWords(e: string, { subject, object }: { subject: string, object?: string }): string {
return e.replace("$SUBJ", subject).replace("$OBJ", object || "");
@ -201,6 +195,17 @@ function compileEnglish(VP: VPRendered): string[] | undefined {
: undefined;
}
type SegmentDescriptions = {
isVerbHead?: boolean,
isOoOrWaaHead?: boolean,
isVerbRest?: boolean,
isMiniPronoun?: boolean,
isKid?: boolean,
isKidBetweenHeadAndRest?: boolean,
isNu?: boolean,
isBa?: boolean,
}
type SDT = keyof SegmentDescriptions;
type Segment = { ps: T.PsString[] } & SegmentDescriptions & {
adjust: (o: { ps?: T.PsString | T.PsString[] | ((ps: T.PsString) => T.PsString), desc?: SDT[] }) => Segment,
@ -216,7 +221,7 @@ function makeSegment(
...all,
[curr]: true,
}), {}),
adjust: function(o: { ps?: T.PsString | T.PsString[] | ((ps: T.PsString) => T.PsString), desc?: SDT[] }): Segment {
adjust: function(o): Segment {
return {
...this,
...o.ps ? {
@ -232,7 +237,7 @@ function makeSegment(
}), {}),
};
},
}
};
}
function combineSegments(loe: (Segment | " " | "" | T.PsString)[]): T.PsString[] {
@ -250,4 +255,11 @@ function combineSegments(loe: (Segment | " " | "" | T.PsString)[]): T.PsString[]
: [concatPsString(first, r)]
)
);
}
function flattenLengths(r: T.SingleOrLengthOpts<T.PsString[]>): T.PsString[] {
if ("long" in r) {
return Object.values(r).flat();
}
return r;
}