Skip to content

Commit

Permalink
more on noun parser
Browse files Browse the repository at this point in the history
  • Loading branch information
adueck committed Aug 23, 2024
1 parent 1abc83e commit 191abc5
Show file tree
Hide file tree
Showing 15 changed files with 412 additions and 200 deletions.
41 changes: 35 additions & 6 deletions src/demo-components/ParserDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import { tokenizer } from "../lib/src/parsing/tokenizer";
// import { NPDisplay } from "../components/library";
// import EditableVP from "../components/src/vp-explorer/EditableVP";
// import { uncompleteVPSelection } from "../lib/src/phrase-building/vp-tools";
import { DictionaryAPI } from "../lib/src/dictionary/dictionary";
import { parseNoun } from "../lib/src/parsing/parse-noun-new";
import { JsonEditor } from "json-edit-react";
import { renderNounSelection } from "../lib/src/phrase-building/render-np";
import { NPBlock } from "../components/src/blocks/Block";
import { getEnglishFromRendered } from "../lib/src/phrase-building/np-tools";

const working = [
"limited demo vocab",
Expand Down Expand Up @@ -47,16 +49,18 @@ const examples = [
];

function ParserDemo({
// opts,
opts,
// entryFeeder,
dictionary,
}: {
opts: T.TextOptions;
entryFeeder: T.EntryFeeder;
dictionary: DictionaryAPI;
dictionary: T.DictionaryAPI;
}) {
const [text, setText] = useState<string>("");
const [result, setResult] = useState<any[]>([]);
const [result, setResult] = useState<
ReturnType<typeof parseNoun>[number]["body"][]
>([]);
// ReturnType<typeof parsePhrase>["success"]
const [errors, setErrors] = useState<string[]>([]);
function handleInput(value: string) {
Expand All @@ -66,8 +70,10 @@ function ParserDemo({
setErrors([]);
return;
}
const res = parseNoun(tokenizer(value), dictionary, undefined, []);
const success = res.filter((x) => !x.tokens.length).map((x) => x.body);
const res = parseNoun(tokenizer(value), dictionary, undefined);
const success: ReturnType<typeof parseNoun>[number]["body"][] = res
.filter((x) => !x.tokens.length)
.map((x) => x.body);
const errors = [
...new Set(res.flatMap(({ errors }) => errors.map((e) => e.message))),
];
Expand Down Expand Up @@ -135,6 +141,29 @@ function ParserDemo({
<div className="text-center">Did you mean:</div>
</>
)}
{result.map((r) => {
try {
const renderedNP: T.Rendered<T.NPSelection> = {
type: "NP",
selection: renderNounSelection(r.selection, r.inflected, "none"),
};
return (
<>
{r.inflected ? "INFLECTED" : "PLAIN"}
<NPBlock
opts={opts}
script="p"
english={getEnglishFromRendered(renderedNP)}
>
{renderedNP}
</NPBlock>
</>
);
} catch (e) {
console.error(e);
return <div>ERROR RENDERING</div>;
}
})}
<JsonEditor data={result} />
{/* {result.map((res) =>
"inflected" in res ? (
Expand Down
12 changes: 1 addition & 11 deletions src/lib/src/dictionary/dictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,7 @@ function specialPluralLookup(p: string): T.NounEntry[] {
.filter(tp.isNounEntry);
}

export type DictionaryAPI = {
initialize: () => ReturnType<typeof dictDb.initialize>;
update: () => ReturnType<typeof dictDb.updateDictionary>;
queryP: (p: string) => T.DictionaryEntry[];
adjLookup: (p: string) => T.AdjectiveEntry[];
nounLookup: (p: string) => T.NounEntry[];
otherLookup: (key: keyof T.DictionaryEntry, p: string) => T.DictionaryEntry[];
specialPluralLookup: (p: string) => T.NounEntry[];
};

export const dictionary: DictionaryAPI = {
export const dictionary: T.DictionaryAPI = {
initialize: async () => await dictDb.initialize(),
update: async () => await dictDb.updateDictionary(() => null),
queryP: memoizedQueryP,
Expand Down
3 changes: 1 addition & 2 deletions src/lib/src/parsing/mini-test-dictionary.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as T from "../../../types";
import type { DictionaryAPI } from "../dictionary/dictionary";
import { isAdjectiveEntry, isNounEntry } from "../type-predicates";
import { entries } from "../../../../vocab/mini-dict-entries";

Expand All @@ -26,7 +25,7 @@ function specialPluralLookup(p: string): T.NounEntry[] {
) as T.NounEntry[];
}

export const testDictionary: DictionaryAPI = {
export const testDictionary: T.DictionaryAPI = {
// @ts-expect-error we won't mock the initialization
initialize: async () => 0,
// @ts-expect-error not perfect mocking because won't need that
Expand Down
12 changes: 3 additions & 9 deletions src/lib/src/parsing/parse-adjective-new.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import * as T from "../../../types";
import { DictionaryAPI } from "../dictionary/dictionary";
import { fmapParseResult } from "../fp-ps";
import { makeAdjectiveSelection } from "../phrase-building/make-selections";
import * as tp from "../type-predicates";
import { parseInflectableWord } from "./parse-inflectable-word";

export function parseAdjective(
tokens: Readonly<T.Token[]>,
dictionary: DictionaryAPI
): T.ParseResult<{
inflection: (0 | 1 | 2)[];
gender: T.Gender[];
given: string;
selection: T.AdjectiveSelection;
}>[] {
dictionary: T.DictionaryAPI
): T.ParseResult<T.InflectableBaseParse<T.AdjectiveSelection>>[] {
if (tokens.length === 0) {
return [];
}
Expand All @@ -27,7 +21,7 @@ export function parseAdjective(
inflection: r.inflection,
gender: r.gender,
given: r.given,
selection: makeAdjectiveSelection(r.entry as T.AdjectiveEntry),
selection: makeAdjectiveSelection(r.selection as T.AdjectiveEntry),
}),
adjectives
);
Expand Down
7 changes: 1 addition & 6 deletions src/lib/src/parsing/parse-adjective.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ import { LookupFunction } from "./lookup";
export function parseAdjective(
tokens: Readonly<T.Token[]>,
lookup: LookupFunction
): T.ParseResult<{
inflection: (0 | 1 | 2)[];
gender: T.Gender[];
given: string;
selection: T.AdjectiveSelection;
}>[] {
): T.ParseResult<T.InflectableBaseParse<T.AdjectiveSelection>>[] {
const w: ReturnType<typeof parseAdjective> = [];
if (tokens.length === 0) {
return [];
Expand Down
100 changes: 100 additions & 0 deletions src/lib/src/parsing/parse-determiner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import * as T from "../../../types";
import { determiners } from "../../../types";
import * as tp from "../type-predicates";
import { returnParseResult } from "./utils";

export const parseDeterminer: T.Parser<
T.InflectableBaseParse<T.DeterminerSelection>
> = (
tokens: Readonly<T.Token[]>,
// eslint-disable-next-line
dictionary: T.DictionaryAPI

Check failure on line 11 in src/lib/src/parsing/parse-determiner.ts

View workflow job for this annotation

GitHub Actions / ci

'dictionary' is declared but its value is never read.
) => {
if (tokens.length === 0) {
return [];
}
const [first, ...rest] = tokens;
if (first.s.endsWith("و")) {
const determiner = determiners.find((d) => d.p === first.s.slice(0, -1));
if (!determiner) return [];
if (!isInflectingDet(determiner)) return [];
return returnParseResult(rest, {
inflection: [2],
gender: ["masc", "fem"],
given: first.s,
selection: {
type: "determiner",
determiner,
},
});
}
if (first.s.endsWith("ې")) {
const determinerExact = determiners.find((d) => d.p === first.s);
const determinerInflected = determiners.find(
(d) => d.p === first.s.slice(0, -1)
);
return [
...(determinerExact
? returnParseResult(rest, {
inflection: [0, 1, 2],
gender: ["masc", "fem"],
given: first.s,
selection: {
type: "determiner",
determiner: determinerExact,
},
} satisfies T.InflectableBaseParse<T.DeterminerSelection>)
: []),
...(determinerInflected && isInflectingDet(determinerInflected)
? returnParseResult(rest, {
inflection: [1] satisfies (0 | 1 | 2)[],
gender: ["fem"],
given: first.s,
selection: {
type: "determiner",
determiner: determinerInflected,
},
} satisfies T.InflectableBaseParse<T.DeterminerSelection>)
: []),
];
}
const exact: T.ParseResult<T.InflectableBaseParse<T.DeterminerSelection>>[] =
(() => {
const determiner = determiners.find((d) => d.p === first.s);
if (!determiner) return [];
const canInflect = isInflectingDet(determiner);
return returnParseResult(rest, {
inflection: canInflect ? [0, 1] : [0, 1, 2],
gender: canInflect ? ["masc"] : ["masc", "fem"],
given: first.s,
selection: {
type: "determiner",
determiner,
},
});
})();
const aEnding: T.ParseResult<
T.InflectableBaseParse<T.DeterminerSelection>
>[] = (() => {
if (first.s.endsWith("ه")) {
const determiner = determiners.find((d) => d.p === first.s.slice(0, -1));
if (!determiner) return [];
if (!isInflectingDet(determiner)) return [];
return returnParseResult(rest, {
inflection: [0],
gender: ["fem"],
given: first.s,
selection: {
type: "determiner",
determiner,
},
});
}
return [];
})();
return [...exact, ...aEnding];
};

function isInflectingDet(d: T.Determiner): boolean {
return tp.isPattern1Entry(d) && !("noInf" in d && !d.noInf);
}
Loading

0 comments on commit 191abc5

Please sign in to comment.