refactor with makeSegment

This commit is contained in:
lingdocs 2022-03-25 21:19:00 +05:00
parent ba108a6b54
commit e32f2cd6e7
1 changed files with 57 additions and 42 deletions

View File

@ -7,7 +7,7 @@ import {
} from "@lingdocs/pashto-inflector"; } from "@lingdocs/pashto-inflector";
import { removeBa } from "./vp-tools"; import { removeBa } from "./vp-tools";
type Segment = { type SegmentDescriptions = {
isVerbHead?: boolean, isVerbHead?: boolean,
isOoOrWaaHead?: boolean, isOoOrWaaHead?: boolean,
isVerbRest?: boolean, isVerbRest?: boolean,
@ -16,7 +16,11 @@ type Segment = {
isKidBetweenHeadAndRest?: boolean, isKidBetweenHeadAndRest?: boolean,
isNu?: boolean, isNu?: boolean,
isBa?: boolean, isBa?: boolean,
ps: T.PsString[], }
type SDT = keyof SegmentDescriptions;
type Segment = { ps: T.PsString[] } & SegmentDescriptions & {
adjust: (o: { ps?: T.PsString | T.PsString[], desc?: SDT[] }) => Segment,
}; };
// TODO: make it an option to include O S V order // TODO: make it an option to include O S V order
@ -106,21 +110,21 @@ function getSegmentsAndKids(VP: VPRendered, form: FormVersion): { kids: Segment[
return { return {
kids: [ kids: [
...VP.verb.hasBa ...VP.verb.hasBa
? [{ isBa: true, ps: [grammarUnits.baParticle] }] : [], ? [makeSegment(grammarUnits.baParticle, ["isBa"])] : [],
...toShrink ...toShrink
? [shrink(toShrink)] : [], ? [shrink(toShrink)] : [],
].map(k => ({...k, isKid: true })), ].map(k => ({...k, isKid: true })),
NPs: [ NPs: [
[ [
...showSubject ? [{ ps: main.subject }] : [], ...showSubject ? [makeSegment(main.subject)] : [],
...(showObject && main.object) ? [{ ps: main.object }] : [], ...(showObject && main.object) ? [makeSegment(main.object)] : [],
], ],
// TODO: make this an option to also include O S V order // TODO: make this an option to also include O S V order
// also show O S V if both are showing // also show O S V if both are showing
// TODO: is in only in the past that you can do O S V? // TODO: is in only in the past that you can do O S V?
...(VP.isPast && main.object && showObject && showSubject) ? [[ ...(VP.isPast && main.object && showObject && showSubject) ? [[
{ ps: main.object }, makeSegment(main.object),
{ ps: main.subject }, makeSegment(main.subject),
]] : [], ]] : [],
], ],
} }
@ -128,10 +132,7 @@ function getSegmentsAndKids(VP: VPRendered, form: FormVersion): { kids: Segment[
function shrink(np: Rendered<NPSelection>): Segment { function shrink(np: Rendered<NPSelection>): Segment {
const [row, col] = getVerbBlockPosFromPerson(np.person); const [row, col] = getVerbBlockPosFromPerson(np.person);
return { return makeSegment(grammarUnits.pronouns.mini[row][col], ["isMiniPronoun"]);
isMiniPronoun: true,
ps: grammarUnits.pronouns.mini[row][col],
};
} }
function putKidsInKidsSection(segments: Segment[], kids: Segment[]): Segment[] { function putKidsInKidsSection(segments: Segment[], kids: Segment[]): Segment[] {
@ -146,52 +147,66 @@ function putKidsInKidsSection(segments: Segment[], kids: Segment[]): Segment[] {
]; ];
} }
function compileVerbWNegative(headRaw: T.PsString | undefined, restRaw: T.PsString[], negative: boolean): Segment[][] { function makeSegment(
const rest: Segment = { ps: T.PsString | T.PsString[],
isVerbRest: true, options?: (keyof SegmentDescriptions)[],
ps: restRaw.map(removeBa), ): Segment {
}; return {
const head: Segment | undefined = !headRaw ps: Array.isArray(ps) ? ps : [ps],
? headRaw ...options && options.reduce((all, curr) => ({
: { ...all,
ps: [headRaw], [curr]: true,
isVerbHead: true, }), {}),
isOoOrWaaHead: (headRaw.p === "و" || headRaw.p === "وا"), adjust: function(o: { ps?: T.PsString | T.PsString[], desc?: SDT[] }): Segment {
return {
...this,
...o.ps ? { ps: Array.isArray(o.ps) ? o.ps : [o.ps] } : {},
...o.desc && o.desc.reduce((all, curr) => ({
...all,
[curr]: true,
}), {}),
}; };
},
}
}
function compileVerbWNegative(head: T.PsString | undefined, restRaw: T.PsString[], negative: boolean): Segment[][] {
const rest = makeSegment(restRaw.map(removeBa), ["isVerbRest"]);
const headSegment: Segment | undefined = !head
? head
: makeSegment(
head,
(head.p === "و" || head.p === "وا")
? ["isVerbHead", "isOoOrWaaHead"]
: ["isVerbHead"]
);
if (!negative) { if (!negative) {
return [ return [
[ [
...head ? [head] : [], ...headSegment ? [headSegment] : [],
rest, rest,
], ],
]; ];
} }
const nu: T.PsString = { p: "نه", f: "nú" }; const nu: T.PsString = { p: "نه", f: "nú" };
if (!head) { if (!headSegment) {
return [[ return [[
{ ps: [nu], isNu: true }, makeSegment(nu, ["isNu"]),
{ rest.adjust({ ps: rest.ps.map(p => removeAccents(p)) }),
...rest,
ps: rest.ps.map(p => removeAccents(p)),
},
]]; ]];
} }
return [ return [
[ [
...head ? [{ ...head, ps: head.ps.map(h =>removeAccents(h)) }] : [], ...headSegment ? [headSegment.adjust({ ps: headSegment.ps.map(h =>removeAccents(h)) })] : [],
{ rest.adjust({
...rest,
isNu: true,
ps: rest.ps.map(r => concatPsString(nu, " ", removeAccents(r))), ps: rest.ps.map(r => concatPsString(nu, " ", removeAccents(r))),
}, desc: ["isNu"],
}),
], ],
...!head.isOoOrWaaHead ? [[ ...!headSegment.isOoOrWaaHead ? [[
{ ps: [nu], isNu: true }, makeSegment(nu, ["isNu"]),
{ ...head, ps: head.ps.map(h =>removeAccents(h)) }, headSegment.adjust({ ps: headSegment.ps.map(h =>removeAccents(h)) }),
{ rest.adjust({ ps: rest.ps.map(p => removeAccents(p)) }),
...rest,
ps: rest.ps.map(p => removeAccents(p)),
},
]] : [], ]] : [],
]; ];
} }