OSV order and more

This commit is contained in:
lingdocs 2022-03-25 19:46:15 +05:00
parent 99cdda0f51
commit ba108a6b54
1 changed files with 68 additions and 47 deletions

View File

@ -9,7 +9,7 @@ import { removeBa } from "./vp-tools";
type Segment = { type Segment = {
isVerbHead?: boolean, isVerbHead?: boolean,
isOoHead?: boolean, isOoOrWaaHead?: boolean,
isVerbRest?: boolean, isVerbRest?: boolean,
isMiniPronoun?: boolean, isMiniPronoun?: boolean,
isKid?: boolean, isKid?: boolean,
@ -19,15 +19,16 @@ type Segment = {
ps: T.PsString[], ps: T.PsString[],
}; };
// TODO: make it an option to include O S V order
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 [] } {
const { head, rest } = VP.verb.ps; const verb = VP.verb.ps;
const { kids, NPs } = shrinkSegmentsAndGatherKids(VP, form); const { kids, NPs } = getSegmentsAndKids(VP, form);
return { return {
ps: compilePs({ ps: compilePs({
NPs, NPs,
kids, kids,
head, verb,
rest,
negative: VP.verb.negative, negative: VP.verb.negative,
}), }),
e: compileEnglish(VP), e: compileEnglish(VP),
@ -35,35 +36,32 @@ export function compileVP(VP: VPRendered, form: FormVersion): { ps: T.SingleOrLe
} }
type CompilePsInput = { type CompilePsInput = {
NPs: Segment[], NPs: Segment[][],
kids: Segment[], kids: Segment[],
head: T.PsString | undefined, verb: {
rest: T.SingleOrLengthOpts<T.PsString[]>, head: T.PsString | undefined,
rest: T.SingleOrLengthOpts<T.PsString[]>,
},
negative: boolean, negative: boolean,
} }
function compilePs({ NPs, kids, head, rest, negative }: CompilePsInput): T.SingleOrLengthOpts<T.PsString[]> { function compilePs({ NPs, kids, verb: { head, rest }, negative }: CompilePsInput): T.SingleOrLengthOpts<T.PsString[]> {
if ("long" in rest) { if ("long" in rest) {
return { return {
long: compilePs({ NPs, head, rest: rest.long, negative, kids }) as T.PsString[], long: compilePs({ NPs, verb: { head, rest: rest.long }, negative, kids }) as T.PsString[],
short: compilePs({ NPs, head, rest: rest.short, negative, kids }) as T.PsString[], short: compilePs({ NPs, verb: { head, rest: rest.short }, negative, kids }) as T.PsString[],
...rest.mini ? { ...rest.mini ? {
mini: compilePs({ NPs, head, rest: rest.mini, negative, kids }) as T.PsString[], mini: compilePs({ NPs, verb: { head, rest: rest.mini }, negative, kids }) as T.PsString[],
} : {}, } : {},
}; };
} }
const verbSegments = compileVerbWNegative(head, rest, negative) const verbWNegativeVersions = compileVerbWNegative(head, rest, negative);
const segments: Segment[] = [ return verbWNegativeVersions.flatMap((verbSegments) => (
...NPs, NPs.flatMap(NP => {
...verbSegments, const segments = putKidsInKidsSection([...NP, ...verbSegments], kids);
]; const withProperSpaces = addSpacesBetweenSegments(segments);
const segmentsWKids = putKidsInKidsSection( return combineSegments(withProperSpaces);
segments, })
kids, ));
);
// have all these pieces labelled
// add spaces
const segmentsWithSpaces = addSpacesBetweenSegments(segmentsWKids);
return combineSegments(segmentsWithSpaces);
} }
function addSpacesBetweenSegments(segments: Segment[]): (Segment | " " | "" | T.PsString)[] { function addSpacesBetweenSegments(segments: Segment[]): (Segment | " " | "" | T.PsString)[] {
@ -73,11 +71,11 @@ function addSpacesBetweenSegments(segments: Segment[]): (Segment | " " | "" | T.
const next = segments[i+1]; const next = segments[i+1];
o.push(current); o.push(current);
if (!next) break; if (!next) break;
if (next.isKidBetweenHeadAndRest || (next.isVerbRest && current.isKidBetweenHeadAndRest)) { if ((next.isKidBetweenHeadAndRest || next.isNu) || (next.isVerbRest && current.isKidBetweenHeadAndRest)) {
o.push({ o.push({
f: "-", f: "-",
p: ((current.isVerbHead && next.isMiniPronoun) p: ((current.isVerbHead && (next.isMiniPronoun || next.isNu))
|| (current.isOoHead && next.isBa)) ? "" : " ", // or if its waa head || (current.isOoOrWaaHead && next.isBa )) ? "" : " ", // or if its waa head
}); });
} else if (current.isVerbHead && next.isVerbRest) { } else if (current.isVerbHead && next.isVerbRest) {
o.push(""); o.push("");
@ -88,7 +86,7 @@ function addSpacesBetweenSegments(segments: Segment[]): (Segment | " " | "" | T.
return o; return o;
} }
function shrinkSegmentsAndGatherKids(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,
object: typeof VP.object === "object" ? VP.object.ps : undefined, object: typeof VP.object === "object" ? VP.object.ps : undefined,
@ -100,7 +98,6 @@ function shrinkSegmentsAndGatherKids(VP: VPRendered, form: FormVersion): { kids:
const toShrink = (!shrinkCanditate || typeof shrinkCanditate !== "object") const toShrink = (!shrinkCanditate || typeof shrinkCanditate !== "object")
? undefined ? undefined
: shrinkCanditate; : shrinkCanditate;
// TODO: big problem, the king removal doesn't work with grammatically transitive things
const king = main[VP.king]; const king = main[VP.king];
const showSubject = (VP.king === "subject" && !removeKing && king) || (VP.servant === "subject" && !shrinkServant); const showSubject = (VP.king === "subject" && !removeKing && king) || (VP.servant === "subject" && !shrinkServant);
const showObject = ( const showObject = (
@ -111,18 +108,30 @@ function shrinkSegmentsAndGatherKids(VP: VPRendered, form: FormVersion): { kids:
...VP.verb.hasBa ...VP.verb.hasBa
? [{ isBa: true, ps: [grammarUnits.baParticle] }] : [], ? [{ isBa: true, ps: [grammarUnits.baParticle] }] : [],
...toShrink ...toShrink
? [{ isMiniPronoun: true, ps: shrink(toShrink) }] : [], ? [shrink(toShrink)] : [],
].map(k => ({...k, isKid: true })), ].map(k => ({...k, isKid: true })),
NPs: [ NPs: [
...showSubject ? [{ ps: main.subject }] : [], [
...(showObject && main.object) ? [{ ps: main.object }] : [], ...showSubject ? [{ ps: main.subject }] : [],
...(showObject && main.object) ? [{ ps: main.object }] : [],
],
// TODO: make this an option to also include O S V order
// also show O S V if both are showing
// TODO: is in only in the past that you can do O S V?
...(VP.isPast && main.object && showObject && showSubject) ? [[
{ ps: main.object },
{ ps: main.subject },
]] : [],
], ],
} }
} }
function shrink(np: Rendered<NPSelection>): T.PsString[] { function shrink(np: Rendered<NPSelection>): Segment {
const [row, col] = getVerbBlockPosFromPerson(np.person); const [row, col] = getVerbBlockPosFromPerson(np.person);
return grammarUnits.pronouns.mini[row][col]; return {
isMiniPronoun: true,
ps: grammarUnits.pronouns.mini[row][col],
};
} }
function putKidsInKidsSection(segments: Segment[], kids: Segment[]): Segment[] { function putKidsInKidsSection(segments: Segment[], kids: Segment[]): Segment[] {
@ -137,7 +146,7 @@ function putKidsInKidsSection(segments: Segment[], kids: Segment[]): Segment[] {
]; ];
} }
function compileVerbWNegative(headRaw: T.PsString | undefined, restRaw: T.PsString[], negative: boolean): Segment[] { function compileVerbWNegative(headRaw: T.PsString | undefined, restRaw: T.PsString[], negative: boolean): Segment[][] {
const rest: Segment = { const rest: Segment = {
isVerbRest: true, isVerbRest: true,
ps: restRaw.map(removeBa), ps: restRaw.map(removeBa),
@ -147,31 +156,43 @@ function compileVerbWNegative(headRaw: T.PsString | undefined, restRaw: T.PsStri
: { : {
ps: [headRaw], ps: [headRaw],
isVerbHead: true, isVerbHead: true,
isOoHead: headRaw.p === "و" isOoOrWaaHead: (headRaw.p === "و" || headRaw.p === "وا"),
}; };
if (!negative) { if (!negative) {
return [ return [
...head ? [head] : [], [
rest, ...head ? [head] : [],
rest,
],
]; ];
} }
const nu: T.PsString = { p: "نه", f: "nú" }; const nu: T.PsString = { p: "نه", f: "nú" };
if (!head) { if (!head) {
return [ return [[
{ ps: [nu], isNu: true }, { ps: [nu], isNu: true },
{ {
...rest, ...rest,
ps: rest.ps.map(p => removeAccents(p)), ps: rest.ps.map(p => removeAccents(p)),
}, },
]; ]];
} }
return [ return [
...head ? [{ ...head, ps: head.ps.map(h =>removeAccents(h)) }] : [], [
{ ...head ? [{ ...head, ps: head.ps.map(h =>removeAccents(h)) }] : [],
...rest, {
isNu: true, ...rest,
ps: rest.ps.map(r => concatPsString(nu, " ", removeAccents(r))), isNu: true,
}, ps: rest.ps.map(r => concatPsString(nu, " ", removeAccents(r))),
},
],
...!head.isOoOrWaaHead ? [[
{ ps: [nu], isNu: true },
{ ...head, ps: head.ps.map(h =>removeAccents(h)) },
{
...rest,
ps: rest.ps.map(p => removeAccents(p)),
},
]] : [],
]; ];
} }