From 3221bb1e89105dfb4de491c84b70386b3ee15e98 Mon Sep 17 00:00:00 2001 From: adueck Date: Sat, 5 Aug 2023 21:14:29 +0400 Subject: [PATCH] basic vp parsing starting --- src/lib/src/parsing/inflection-query.ts | 26 +++++++------- src/lib/src/parsing/parse-noun.test.ts | 24 ------------- src/lib/src/parsing/parse-vp.ts | 45 +++++++++++++++---------- src/lib/src/parsing/utils.ts | 31 +++++++++-------- 4 files changed, 56 insertions(+), 70 deletions(-) diff --git a/src/lib/src/parsing/inflection-query.ts b/src/lib/src/parsing/inflection-query.ts index a443f0d..88d52a1 100644 --- a/src/lib/src/parsing/inflection-query.ts +++ b/src/lib/src/parsing/inflection-query.ts @@ -1,5 +1,4 @@ import * as T from "../../../types"; -import { endsInConsonant } from "../p-text-helpers"; import { isPattern1Entry, isPattern2Entry, @@ -394,18 +393,19 @@ export function getInflectionQueries( predicate: isPattern1Entry, }, }); - if (noun) { - // bundled plural - queries.push({ - search: { p: s.slice(0, -1) }, - details: { - inflection: [0], - plural: true, - gender: ["masc"], - predicate: (e) => !isPattern5Entry(e) && endsInConsonant(e), - }, - }); - } + // TODO: Bundled plural only works when there is a quantifier in front of it ! + // if (noun) { + // // bundled plural + // queries.push({ + // search: { p: s.slice(0, -1) }, + // details: { + // inflection: [0], + // plural: true, + // gender: ["masc"], + // predicate: (e) => !isPattern5Entry(e) && endsInConsonant(e), + // }, + // }); + // } queries.push({ search: { infbp: s.slice(0, -1) }, details: { diff --git a/src/lib/src/parsing/parse-noun.test.ts b/src/lib/src/parsing/parse-noun.test.ts index dbc5e60..bbbef7e 100644 --- a/src/lib/src/parsing/parse-noun.test.ts +++ b/src/lib/src/parsing/parse-noun.test.ts @@ -130,13 +130,6 @@ const tests: { gender: "fem", }, }, - { - inflected: false, - selection: { - ...makeNounSelection(daktar, undefined), - number: "plural", - }, - }, ], }, { @@ -1370,23 +1363,6 @@ const tests: { }, ], }, - { - category: "bundled plurals", - cases: [ - { - input: "کوره", - output: [ - { - inflected: false, - selection: { - ...makeNounSelection(kor, undefined), - number: "plural", - }, - }, - ], - }, - ], - }, ]; describe("parsing nouns", () => { diff --git a/src/lib/src/parsing/parse-vp.ts b/src/lib/src/parsing/parse-vp.ts index 65d9e35..3c24e66 100644 --- a/src/lib/src/parsing/parse-vp.ts +++ b/src/lib/src/parsing/parse-vp.ts @@ -37,13 +37,17 @@ export function parseVP( return []; } // how to make this into a nice pipeline... 🤔 - const NP1 = parseNP(tokens, lookup); - const NP2 = bindParseResult(NP1, (tokens) => parseNP(tokens, lookup), true); + const NP1 = parseNP(tokens, lookup).filter(({ errors }) => !errors.length); + const NP2 = bindParseResult( + NP1, + (tokens) => parseNP(tokens, lookup), + true + ).filter(({ errors }) => !errors.length); const vb = bindParseResult( NP2, (tokens) => parseVerb(tokens, verbLookup), true - ); + ).filter(({ errors }) => !errors.length); // TODO: be able to bind mulitple vals return bindParseResult, T.VPSelectionComplete>( vb, @@ -51,28 +55,35 @@ export function parseVP( const w: T.ParseResult[] = []; NP1.forEach(({ body: np1 }) => { NP2.forEach(({ body: np2 }) => { + // NICE TODO: IF there's an error in any of the NPS, don't try + // to make the VPS - just show them that error + // for that we probably need a different type of [ [np1, np2], [np2, np1], ].forEach(([s, o]) => { const errors: T.ParseError[] = []; const subjPerson = getPersonFromNP(s.selection); - if (s.inflected) { - errors.push({ message: "subject should not be inflected" }); - } - if (o.selection.selection.type === "pronoun") { - if (!isThirdPerson(subjPerson) && !o.inflected) { - errors.push({ - message: - "1st or 2nd person object pronoun should be inflected", - }); - } - } else if (o.inflected) { - errors.push({ message: "object should not be inflected" }); - } if (getPersonFromNP(s.selection) !== v.person) { - errors.push({ message: "verb does not match subject" }); + errors.push({ + message: "verb does not match subject", + }); + } else { + if (s.inflected) { + errors.push({ message: "subject should not be inflected" }); + } + if (o.selection.selection.type === "pronoun") { + if (!isThirdPerson(subjPerson) && !o.inflected) { + errors.push({ + message: + "1st or 2nd person object pronoun should be inflected", + }); + } + } else if (o.inflected) { + errors.push({ message: "object should not be inflected" }); + } } + const blocks: T.VPSBlockComplete[] = [ { key: 1, diff --git a/src/lib/src/parsing/utils.ts b/src/lib/src/parsing/utils.ts index ad8fc33..59fc301 100644 --- a/src/lib/src/parsing/utils.ts +++ b/src/lib/src/parsing/utils.ts @@ -33,22 +33,21 @@ export function bindParseResult( }, ignorePrevious?: boolean ): T.ParseResult[] { - // const prev = ignorePrevious - // ? (() => { - // const resArr: T.ParseResult[] = []; - // previous.filter((item) => { - // var i = resArr.findIndex( - // (x) => x.tokens.length === item.tokens.length - // ); - // if (i <= -1) { - // resArr.push(item); - // } - // return null; - // }); - // return resArr; - // })() - // : previous; - const prev = previous; + const prev = ignorePrevious + ? (() => { + const resArr: T.ParseResult[] = []; + previous.filter((item) => { + var i = resArr.findIndex( + (x) => x.tokens.length === item.tokens.length + ); + if (i <= -1) { + resArr.push(item); + } + return null; + }); + return resArr; + })() + : previous; const nextPossibilities = prev.flatMap(({ tokens, body, errors }) => { const res = f(tokens, body); const { errors: errsPassed, next } = Array.isArray(res)