clean up compile vp
This commit is contained in:
parent
c8c2132bbf
commit
874fb731f1
|
@ -7,30 +7,21 @@ import {
|
||||||
} from "@lingdocs/pashto-inflector";
|
} from "@lingdocs/pashto-inflector";
|
||||||
import { removeBa } from "./vp-tools";
|
import { removeBa } from "./vp-tools";
|
||||||
|
|
||||||
type SegmentDescriptions = {
|
// TODO: make it an option to include O S V order ?? or is that just always in past tense
|
||||||
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: tu ba laaR nu she hyphens all messed up
|
// 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 verb = VP.verb.ps;
|
||||||
const { kids, NPs } = getSegmentsAndKids(VP, form);
|
const { kids, NPs } = getSegmentsAndKids(VP, form);
|
||||||
return {
|
const psResult = compilePs({
|
||||||
ps: compilePs({
|
|
||||||
NPs,
|
NPs,
|
||||||
kids,
|
kids,
|
||||||
verb,
|
verb,
|
||||||
negative: VP.verb.negative,
|
negative: VP.verb.negative,
|
||||||
}),
|
});
|
||||||
|
return {
|
||||||
|
ps: combineLengths ? flattenLengths(psResult) : psResult,
|
||||||
e: compileEnglish(VP),
|
e: compileEnglish(VP),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -55,42 +46,23 @@ function compilePs({ NPs, kids, verb: { head, rest }, negative }: CompilePsInput
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
const verbWNegativeVersions = compileVerbWNegative(head, rest, negative);
|
const verbWNegativeVersions = compileVerbWNegative(head, rest, negative);
|
||||||
|
|
||||||
|
// put together all the different possible permutations based on:
|
||||||
// potential different versions of where the nu goes
|
// potential different versions of where the nu goes
|
||||||
return verbWNegativeVersions.flatMap((verbSegments) => (
|
return verbWNegativeVersions.flatMap((verbSegments) => (
|
||||||
// potential reordering of NPs
|
// potential reordering of NPs
|
||||||
NPs.flatMap(NP => {
|
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);
|
const segments = putKidsInKidsSection([...NP, ...verbSegments], kids);
|
||||||
// space out the words properly
|
// 2. space out the words properly
|
||||||
const withProperSpaces = addSpacesBetweenSegments(segments);
|
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);
|
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[][] } {
|
function getSegmentsAndKids(VP: VPRendered, form: FormVersion): { kids: Segment[], NPs: Segment[][] } {
|
||||||
const main = {
|
const main = {
|
||||||
subject: VP.subject.ps,
|
subject: VP.subject.ps,
|
||||||
|
@ -113,7 +85,7 @@ function getSegmentsAndKids(VP: VPRendered, form: FormVersion): { kids: Segment[
|
||||||
...VP.verb.hasBa
|
...VP.verb.hasBa
|
||||||
? [makeSegment(grammarUnits.baParticle, ["isBa", "isKid"])] : [],
|
? [makeSegment(grammarUnits.baParticle, ["isBa", "isKid"])] : [],
|
||||||
...toShrink
|
...toShrink
|
||||||
? [shrink(toShrink)] : [],
|
? [shrinkNP(toShrink)] : [],
|
||||||
],
|
],
|
||||||
NPs: [
|
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[] {
|
function putKidsInKidsSection(segments: Segment[], kids: Segment[]): Segment[] {
|
||||||
const first = segments[0];
|
const first = segments[0];
|
||||||
const rest = segments.slice(1);
|
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 compileEnglish(VP: VPRendered): string[] | undefined {
|
||||||
function insertEWords(e: string, { subject, object }: { subject: string, object?: string }): string {
|
function insertEWords(e: string, { subject, object }: { subject: string, object?: string }): string {
|
||||||
return e.replace("$SUBJ", subject).replace("$OBJ", object || "");
|
return e.replace("$SUBJ", subject).replace("$OBJ", object || "");
|
||||||
|
@ -201,6 +195,17 @@ function compileEnglish(VP: VPRendered): string[] | undefined {
|
||||||
: 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 SDT = keyof SegmentDescriptions;
|
||||||
type Segment = { ps: T.PsString[] } & SegmentDescriptions & {
|
type Segment = { ps: T.PsString[] } & SegmentDescriptions & {
|
||||||
adjust: (o: { ps?: T.PsString | T.PsString[] | ((ps: T.PsString) => T.PsString), desc?: SDT[] }) => Segment,
|
adjust: (o: { ps?: T.PsString | T.PsString[] | ((ps: T.PsString) => T.PsString), desc?: SDT[] }) => Segment,
|
||||||
|
@ -216,7 +221,7 @@ function makeSegment(
|
||||||
...all,
|
...all,
|
||||||
[curr]: true,
|
[curr]: true,
|
||||||
}), {}),
|
}), {}),
|
||||||
adjust: function(o: { ps?: T.PsString | T.PsString[] | ((ps: T.PsString) => T.PsString), desc?: SDT[] }): Segment {
|
adjust: function(o): Segment {
|
||||||
return {
|
return {
|
||||||
...this,
|
...this,
|
||||||
...o.ps ? {
|
...o.ps ? {
|
||||||
|
@ -232,7 +237,7 @@ function makeSegment(
|
||||||
}), {}),
|
}), {}),
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function combineSegments(loe: (Segment | " " | "" | T.PsString)[]): T.PsString[] {
|
function combineSegments(loe: (Segment | " " | "" | T.PsString)[]): T.PsString[] {
|
||||||
|
@ -251,3 +256,10 @@ function combineSegments(loe: (Segment | " " | "" | T.PsString)[]): T.PsString[]
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function flattenLengths(r: T.SingleOrLengthOpts<T.PsString[]>): T.PsString[] {
|
||||||
|
if ("long" in r) {
|
||||||
|
return Object.values(r).flat();
|
||||||
|
}
|
||||||
|
return r;
|
||||||
|
}
|
Loading…
Reference in New Issue