better spacing

This commit is contained in:
lingdocs 2022-03-25 17:18:11 +05:00
parent 9afe378660
commit 99cdda0f51
1 changed files with 21 additions and 22 deletions

View File

@ -12,6 +12,8 @@ type Segment = {
isOoHead?: boolean, isOoHead?: boolean,
isVerbRest?: boolean, isVerbRest?: boolean,
isMiniPronoun?: boolean, isMiniPronoun?: boolean,
isKid?: boolean,
isKidBetweenHeadAndRest?: boolean,
isNu?: boolean, isNu?: boolean,
isBa?: boolean, isBa?: boolean,
ps: T.PsString[], ps: T.PsString[],
@ -64,21 +66,19 @@ function compilePs({ NPs, kids, head, rest, negative }: CompilePsInput): T.Singl
return combineSegments(segmentsWithSpaces); return combineSegments(segmentsWithSpaces);
} }
function addSpacesBetweenSegments(segments: Segment[]): (Segment | " " | "" | "-")[] { function addSpacesBetweenSegments(segments: Segment[]): (Segment | " " | "" | T.PsString)[] {
const o: (Segment | " " | "" | "-")[] = []; const o: (Segment | " " | "" | T.PsString)[] = [];
for (let i = 0; i < segments.length; i++) { for (let i = 0; i < segments.length; i++) {
const current = segments[i]; const current = segments[i];
const next = segments[i+1]; const next = segments[i+1];
o.push(current); o.push(current);
if (!next) break; if (!next) break;
if (current.isVerbHead && if (next.isKidBetweenHeadAndRest || (next.isVerbRest && current.isKidBetweenHeadAndRest)) {
( o.push({
(next.isMiniPronoun || next.isNu) f: "-",
|| p: ((current.isVerbHead && next.isMiniPronoun)
(current.isOoHead && next.isBa) || (current.isOoHead && next.isBa)) ? "" : " ", // or if its waa head
) });
) {
o.push("-");
} else if (current.isVerbHead && next.isVerbRest) { } else if (current.isVerbHead && next.isVerbRest) {
o.push(""); o.push("");
} else { } else {
@ -112,7 +112,7 @@ function shrinkSegmentsAndGatherKids(VP: VPRendered, form: FormVersion): { kids:
? [{ isBa: true, ps: [grammarUnits.baParticle] }] : [], ? [{ isBa: true, ps: [grammarUnits.baParticle] }] : [],
...toShrink ...toShrink
? [{ isMiniPronoun: true, ps: shrink(toShrink) }] : [], ? [{ isMiniPronoun: true, ps: shrink(toShrink) }] : [],
], ].map(k => ({...k, isKid: true })),
NPs: [ NPs: [
...showSubject ? [{ ps: main.subject }] : [], ...showSubject ? [{ ps: main.subject }] : [],
...(showObject && main.object) ? [{ ps: main.object }] : [], ...(showObject && main.object) ? [{ ps: main.object }] : [],
@ -130,7 +130,9 @@ function putKidsInKidsSection(segments: Segment[], kids: Segment[]): Segment[] {
const rest = segments.slice(1); const rest = segments.slice(1);
return [ return [
first, first,
...kids, ...(first.isVerbHead && rest[0] && rest[0].isVerbRest)
? kids.map(k => ({ ...k, isKidBetweenHeadAndRest: true }))
: kids,
...rest, ...rest,
]; ];
} }
@ -173,24 +175,21 @@ function compileVerbWNegative(headRaw: T.PsString | undefined, restRaw: T.PsStri
]; ];
} }
function combineSegments(loe: (Segment | " " | "" | "-")[]): T.PsString[] { function combineSegments(loe: (Segment | " " | "" | T.PsString)[]): T.PsString[] {
const first = loe[0]; const first = loe[0];
const rest = loe.slice(1); const rest = loe.slice(1);
if (!rest.length) { if (!rest.length) {
if (typeof first === "string") { if (typeof first === "string" || !("ps" in first)) {
throw new Error("can't end with a spacer"); throw new Error("can't end with a spacer");
} }
return first.ps; return first.ps;
} }
function spaceOrDash(s: "" | " " | "-"): "" | " " | T.PsString { return combineSegments(rest).flatMap(r => (
return s === "-" ? { p: "", f: "-" } : s; (typeof first === "object" && "ps" in first)
} ? first.ps.map(f => concatPsString(f, r))
return combineSegments(rest).flatMap((r) => ( : [concatPsString(first, r)]
typeof first === "string"
? [concatPsString(spaceOrDash(first), r)]
: first.ps.map(f => concatPsString(f, r)
) )
)); );
} }
function compileEnglish(VP: VPRendered): string[] | undefined { function compileEnglish(VP: VPRendered): string[] | undefined {