verb section order checking

This commit is contained in:
adueck 2023-09-08 13:41:15 -05:00
parent 0b5eb452e1
commit 9168265043
4 changed files with 80 additions and 71 deletions

View File

@ -8,7 +8,13 @@ import { parseNP } from "./parse-np";
import { parsePastPart } from "./parse-past-part"; import { parsePastPart } from "./parse-past-part";
import { parsePH } from "./parse-ph"; import { parsePH } from "./parse-ph";
import { parseVerb } from "./parse-verb"; import { parseVerb } from "./parse-verb";
import { bindParseResult, returnParseResult } from "./utils"; import {
bindParseResult,
returnParseResult,
isParsedVBE,
isParsedVBP,
startsVerbSection,
} from "./utils";
export function parseBlocks( export function parseBlocks(
tokens: Readonly<T.Token[]>, tokens: Readonly<T.Token[]>,
@ -22,19 +28,13 @@ export function parseBlocks(
if (tokens.length === 0) { if (tokens.length === 0) {
return returnParseResult(tokens, { blocks, kids }); return returnParseResult(tokens, { blocks, kids });
} }
const inPreVerbSection = blocks.every( const inVerbSection = blocks.some(startsVerbSection);
(b) =>
b.type !== "PH" &&
b.type !== "VB" &&
b.type !== "welded" &&
b.type !== "negative"
);
const prevPh: T.ParsedPH | undefined = blocks.find( const prevPh: T.ParsedPH | undefined = blocks.find(
(b): b is T.ParsedPH => b.type === "PH" (b): b is T.ParsedPH => b.type === "PH"
); );
const allBlocks: T.ParseResult<T.ParsedBlock | T.ParsedKidsSection>[] = [ const allBlocks: T.ParseResult<T.ParsedBlock | T.ParsedKidsSection>[] = [
...(inPreVerbSection ...(!inVerbSection
? [...parseAP(tokens, lookup), ...parseNP(tokens, lookup)] ? [...parseAP(tokens, lookup), ...parseNP(tokens, lookup)]
: []), : []),
// ensure at most one of each PH, VBE, VBP // ensure at most one of each PH, VBE, VBP
@ -108,17 +108,3 @@ function getPhFromVerb(v: T.VerbEntry, base: "root" | "stem"): string {
} }
return "و"; return "و";
} }
function isParsedVBP(b: T.ParsedBlock): boolean {
return (
(b.type === "VB" || b.type === "welded") &&
(b.info.type === "ability" || b.info.type === "ppart")
);
}
function isParsedVBE(b: T.ParsedBlock): boolean {
return (
(b.type === "VB" || b.type === "welded") &&
(b.info.type === "verb" || b.info.type === "equative")
);
}

View File

@ -22,9 +22,7 @@ export function parsePhrase(s: T.Token[]): {
...parseVP(s, lookup), ...parseVP(s, lookup),
]; ];
console.log({ res });
const success = res.filter((x) => !x.tokens.length).map((x) => x.body); const success = res.filter((x) => !x.tokens.length).map((x) => x.body);
console.log({ success });
return { return {
success, success,
errors: [ errors: [

View File

@ -1,5 +1,14 @@
import * as T from "../../../types"; import * as T from "../../../types";
import { bindParseResult, returnParseResult } from "./utils"; import {
bindParseResult,
isNeg,
isNonOoPh,
isPH,
isParsedVBE,
isParsedVBP,
returnParseResult,
startsVerbSection,
} from "./utils";
import { import {
makeObjectSelectionComplete, makeObjectSelectionComplete,
makeSubjectSelectionComplete, makeSubjectSelectionComplete,
@ -14,7 +23,7 @@ import { makePronounSelection } from "../phrase-building/make-selections";
import { isFirstOrSecondPersPronoun } from "../phrase-building/render-vp"; import { isFirstOrSecondPersPronoun } from "../phrase-building/render-vp";
import { LookupFunction } from "./lookup"; import { LookupFunction } from "./lookup";
import { personToGenNum } from "../misc-helpers"; import { personToGenNum } from "../misc-helpers";
import { equals } from "rambda"; import { equals, zip } from "rambda";
// to hide equatives type-doubling issue // to hide equatives type-doubling issue
// TODO: word query for kawul/kedul/stat/dyn // TODO: word query for kawul/kedul/stat/dyn
@ -80,20 +89,13 @@ function getTenses(
const negative = negIndex !== -1; const negative = negIndex !== -1;
const phIndex = blocks.findIndex((x) => x.type === "PH"); const phIndex = blocks.findIndex((x) => x.type === "PH");
const vbeIndex = blocks.findIndex((x) => x.type === "VB"); const vbeIndex = blocks.findIndex((x) => x.type === "VB");
const verbSection = blocks.findIndex(startsVerbSection);
const ph = phIndex !== -1 ? (blocks[phIndex] as T.ParsedPH) : undefined; const ph = phIndex !== -1 ? (blocks[phIndex] as T.ParsedPH) : undefined;
const verb = vbeIndex !== -1 ? (blocks[vbeIndex] as T.ParsedVBE) : undefined; const verb = vbeIndex !== -1 ? (blocks[vbeIndex] as T.ParsedVBE) : undefined;
if (!verb || verb.type !== "VB") { if (!verb || verb.type !== "VB") {
return []; return [];
} }
if ( if (!verbSectionOK(blocks.slice(verbSection))) {
!negativeInPlace({
neg: negIndex,
v: vbeIndex,
phIndex: phIndex,
ph,
kids: hasKids,
})
) {
return []; return [];
} }
if (verb.info.type === "verb") { if (verb.info.type === "verb") {
@ -895,41 +897,25 @@ function getTenseFromRootsStems(
} }
} }
// possible neg setups function verbSectionOK(blocks: T.ParsedBlock[]): boolean {
// NEG VBE const possibilites = [
// PH NEG VBE [isParsedVBE],
// NEG (Non-و PH) VBE [isNeg, isParsedVBE],
[isPH, isParsedVBE],
// (PH) NEG VBE VBP [isPH, isNeg, isParsedVBE],
// (PH) VBP NEG VBE [isNeg, isNonOoPh, isParsedVBE],
// (with non-و negative things?) [isParsedVBP, isParsedVBE],
[isNeg, isParsedVBE, isParsedVBP],
function negativeInPlace({ [isParsedVBP, isNeg, isParsedVBE],
neg, // could be more clever with optional isPH here
v, [isPH, isParsedVBP, isParsedVBE],
phIndex, [isPH, isNeg, isParsedVBE, isParsedVBP],
ph, [isPH, isParsedVBP, isNeg, isParsedVBE],
kids, ];
}: { return possibilites.some(
neg: number; (ps) =>
v: number; ps.length === blocks.length && zip(ps, blocks).every(([p, b]) => p(b))
phIndex: number; );
ph: T.ParsedPH | undefined;
kids: boolean;
}): boolean {
if (neg === -1) {
return true;
}
if (ph) {
if (!kids && !["و", "وا"].includes(ph.s) && neg === phIndex - 1) {
return true;
}
return neg === phIndex + 1;
}
if (neg < v - 1) {
return false;
}
return true;
} }
function pronounConflictInBlocks(blocks: T.VPSBlockComplete[]): boolean { function pronounConflictInBlocks(blocks: T.VPSBlockComplete[]): boolean {

View File

@ -133,3 +133,42 @@ export function getPeople(
? people.filter((p) => p > 5) ? people.filter((p) => p > 5)
: people; : people;
} }
export function isPH(b: T.ParsedBlock): b is T.ParsedPH {
return b.type === "PH";
}
export function isNeg(b: T.ParsedBlock): b is T.NegativeBlock {
return b.type === "negative";
}
export function isOoPh(b: T.ParsedBlock): b is T.ParsedPH {
return b.type === "PH" && ["و", "وا"].includes(b.s);
}
export function isNonOoPh(b: T.ParsedBlock): b is T.ParsedPH {
return b.type === "PH" && !["و", "وا"].includes(b.s);
}
export function isParsedVBP(b: T.ParsedBlock): b is T.ParsedVBP {
return (
(b.type === "VB" || b.type === "welded") &&
(b.info.type === "ability" || b.info.type === "ppart")
);
}
export function isParsedVBE(b: T.ParsedBlock): b is T.ParsedVBE {
return (
(b.type === "VB" || b.type === "welded") &&
(b.info.type === "verb" || b.info.type === "equative")
);
}
export function startsVerbSection(b: T.ParsedBlock): boolean {
return (
b.type === "PH" ||
b.type === "VB" ||
b.type === "welded" ||
b.type === "negative"
);
}