refactor - cleaning 🧹
This commit is contained in:
parent
c7bd9e0665
commit
edca391118
|
@ -9,17 +9,13 @@ const nounAdjCollectionPath = path.join(wordsPath, "noun-adj-categories");
|
|||
const verbTsFiles = fs.readdirSync(verbCollectionPath);
|
||||
const nounAdjTsFiles = fs.readdirSync(nounAdjCollectionPath);
|
||||
|
||||
const allVerbTsS = [...new Set(verbTsFiles.reduce((arr, fileName) => {
|
||||
const TsS = require(path.join("..", verbCollectionPath, fileName))
|
||||
.filter((v, i, a) => a.findIndex(x => x.ts === v.ts) === i);
|
||||
return [...arr, ...TsS];
|
||||
}, []))];
|
||||
const allVerbTsS = verbTsFiles.flatMap(fileName => [
|
||||
...require(path.join("..", verbCollectionPath, fileName)).map(x => x.ts)
|
||||
]).filter((v, i, a) => a.findIndex(x => x === v) === i);
|
||||
|
||||
const allNounAdjTsS = [...new Set(nounAdjTsFiles.reduce((arr, fileName) => {
|
||||
const TsS = require(path.join("..", nounAdjCollectionPath, fileName))
|
||||
.filter((v, i, a) => a.findIndex(x => x.ts === v.ts) === i);
|
||||
return [...arr, ...TsS];
|
||||
}, []))];
|
||||
const allNounAdjTsS = nounAdjTsFiles.flatMap(fileName => [
|
||||
...require(path.join("..", nounAdjCollectionPath, fileName)).map(x => x.ts)
|
||||
]).filter((v, i, a) => a.findIndex(x => x === v) === i);
|
||||
|
||||
console.log("getting words from dictionary...");
|
||||
|
||||
|
@ -40,8 +36,8 @@ export default words;`;
|
|||
|
||||
function getVerbsFromTsS(entries) {
|
||||
const missingEc = [];
|
||||
const toReturn = allVerbTsS.map(item => {
|
||||
const entry = entries.find(x => item.ts === x.ts);
|
||||
const toReturn = allVerbTsS.map(ts => {
|
||||
const entry = entries.find(x => ts === x.ts);
|
||||
if (!entry) {
|
||||
console.log("couldn't find ts", ts);
|
||||
return undefined;
|
||||
|
@ -65,10 +61,10 @@ function getVerbsFromTsS(entries) {
|
|||
}
|
||||
|
||||
function getNounsAdjsFromTsS(entries) {
|
||||
const b = allNounAdjTsS.map(item => {
|
||||
const entry = entries.find(x => item.ts === x.ts);
|
||||
const b = allNounAdjTsS.map(ts => {
|
||||
const entry = entries.find(x => ts === x.ts);
|
||||
if (!entry) {
|
||||
console.log("couldn't find ts", item);
|
||||
console.log("couldn't find ts", ts);
|
||||
return undefined;
|
||||
}
|
||||
// const firstWord = entry.e.split(",")[0].split(";")[0].split("(")[0].trim();
|
||||
|
|
|
@ -1,101 +1,43 @@
|
|||
import React, { useState } from "react";
|
||||
import {
|
||||
Types as T,
|
||||
defaultTextOptions as opts,
|
||||
VerbTable,
|
||||
removeFVarients,
|
||||
getEnglishWord,
|
||||
} from "@lingdocs/pashto-inflector";
|
||||
import {
|
||||
isUnisexNoun,
|
||||
} from "../lib/type-predicates";
|
||||
import {
|
||||
equativeMachine,
|
||||
assembleEquativeOutput,
|
||||
} from "../lib/equative-machine";
|
||||
import { nouns, adjectives } from "../words/words";
|
||||
makeBlock,
|
||||
makeOptionLabel,
|
||||
} from "./explorer-helpers";
|
||||
import inputs from "./explorer-inputs";
|
||||
import { reducer, ExplorerReducerAction } from "./explorer-reducer";
|
||||
|
||||
function uniqueSort(arr: Adjective[]): Adjective[];
|
||||
function uniqueSort(arr: UnisexNoun[]): UnisexNoun[];
|
||||
function uniqueSort(arr: (Adjective | UnisexNoun)[]): (Adjective | UnisexNoun)[] {
|
||||
return arr
|
||||
.filter((e) => {
|
||||
try {
|
||||
for (let p = 0; p < 12; p++) {
|
||||
equativeMachine(p, e);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("equative generation failed", e);
|
||||
console.error(err);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
})
|
||||
.sort((a, b) => a.p.localeCompare(b.p));
|
||||
}
|
||||
|
||||
const unisexNouns = nouns.filter(x => isUnisexNoun(x)) as UnisexNoun[];
|
||||
|
||||
const inputs = {
|
||||
// TODO: instead - have them unique and sorted in the words.ts file
|
||||
adjectives: uniqueSort(adjectives),
|
||||
unisexNouns: unisexNouns.sort((a, b) => a.p.localeCompare(b.p)),
|
||||
};
|
||||
|
||||
function makeBlock(e: Adjective | UnisexNoun): T.VerbBlock {
|
||||
const makeP = (p: T.Person): T.ArrayOneOrMore<T.PsString> => {
|
||||
const b = assembleEquativeOutput(equativeMachine(p, e));
|
||||
return ("long" in b ? b.long : b) as T.ArrayOneOrMore<T.PsString>;
|
||||
};
|
||||
return [
|
||||
[makeP(0), makeP(6)],
|
||||
[makeP(1), makeP(7)],
|
||||
[makeP(2), makeP(8)],
|
||||
[makeP(3), makeP(9)],
|
||||
[makeP(4), makeP(10)],
|
||||
[makeP(5), makeP(11)],
|
||||
];
|
||||
}
|
||||
|
||||
type PredicateType = "adjectives" | "unisexNouns";
|
||||
export type PredicateType = "adjectives" | "unisexNouns";
|
||||
const predicateTypes: PredicateType[] = ["adjectives", "unisexNouns"];
|
||||
// type SubjectType = "pronouns" | "nouns";
|
||||
|
||||
// TODO: Plural nouns like shoode
|
||||
const defaultTs = 1527815306;
|
||||
// const defaultPe = inputs.adjectives.find(a => a.ts === defaultTs) || inputs.adjectives[0];
|
||||
|
||||
export type ExplorerState = {
|
||||
predicate: Adjective | UnisexNoun,
|
||||
predicateType: PredicateType,
|
||||
};
|
||||
const defaultState: ExplorerState = {
|
||||
predicate: inputs.adjectives.find(ps => ps.p === "ستړی") || inputs.adjectives[0],
|
||||
predicateType: "adjectives",
|
||||
};
|
||||
|
||||
function EquativeExplorer() {
|
||||
// TODO: Use sticky state
|
||||
const predicateTypes: PredicateType[] = ["adjectives", "unisexNouns"];
|
||||
// const subjectTypes: SubjectType[] = ["pronouns", "nouns"];
|
||||
const [predicate, setPredicate] = useState<number>(defaultTs);
|
||||
// const [subjectType, setSubjectType] = useState<SubjectType>("pronouns");
|
||||
const [predicateType, setPredicateType] = useState<PredicateType>("adjectives");
|
||||
|
||||
function makeOptionLabel(e: T.DictionaryEntry): string {
|
||||
const eng = getEnglishWord(e);
|
||||
const english = typeof eng === "string" ? eng : eng?.singular;
|
||||
return `${e.p} - ${removeFVarients(e.f)} (${english})`;
|
||||
const [state, unsafeSetState] = useState<ExplorerState>(defaultState);
|
||||
function dispatch(action: ExplorerReducerAction) {
|
||||
const newState = reducer(state, action);
|
||||
unsafeSetState(newState);
|
||||
}
|
||||
function handlePredicateSelect(e: React.ChangeEvent<HTMLSelectElement>) {
|
||||
setPredicate(parseInt(e.target.value));
|
||||
dispatch({ type: "setPredicate", payload: parseInt(e.target.value) });
|
||||
}
|
||||
function handlePredicateTypeSelect(e: React.ChangeEvent<HTMLInputElement>) {
|
||||
const pt = e.target.value as PredicateType;
|
||||
setPredicateType(pt);
|
||||
setPredicate(inputs[pt][0].ts);
|
||||
const payload = e.target.value as PredicateType;
|
||||
dispatch({ type: "setPredicateType", payload });
|
||||
}
|
||||
// function handleSubjectTypeSelect(e: React.ChangeEvent<HTMLInputElement>) {
|
||||
// const st = e.target.value as SubjectType;
|
||||
// setSubjectType(st);
|
||||
// // setPredicate(inputs[pt][0].ts);
|
||||
// }
|
||||
const predicatePile = inputs[predicateType] as T.DictionaryEntry[];
|
||||
const pe = predicatePile.find(p => p.ts === predicate) as UnisexNoun | Adjective;
|
||||
if (!pe) return <>
|
||||
ERROR!
|
||||
</>
|
||||
console.log("predicate in use", pe);
|
||||
return <>
|
||||
<div className="d-flex flex-row">
|
||||
{/* <div className="form-group">
|
||||
|
@ -138,14 +80,13 @@ function EquativeExplorer() {
|
|||
name="adjectivesPredicateRadio"
|
||||
id="adjectivesPredicateRadio"
|
||||
value={predicateTypes[0]}
|
||||
checked={predicateType === "adjectives"}
|
||||
checked={state.predicateType === "adjectives"}
|
||||
onChange={handlePredicateTypeSelect}
|
||||
/>
|
||||
<label className="form-check-label" htmlFor="adjectivesPredicateRadio">
|
||||
Adjectives
|
||||
</label>
|
||||
</div>
|
||||
{/* hiding this because it's a bit buggy still */}
|
||||
<div className="form-check mb-2">
|
||||
<input
|
||||
className="form-check-input"
|
||||
|
@ -153,7 +94,7 @@ function EquativeExplorer() {
|
|||
name="unisexNounsPredicateRadio"
|
||||
id="unisexNounsPredicateRadio"
|
||||
value={predicateTypes[1]}
|
||||
checked={predicateType === "unisexNouns"}
|
||||
checked={state.predicateType === "unisexNouns"}
|
||||
onChange={handlePredicateTypeSelect}
|
||||
/>
|
||||
<label className="form-check-label" htmlFor="unisexNounsPredicateRadio">
|
||||
|
@ -163,16 +104,16 @@ function EquativeExplorer() {
|
|||
<select
|
||||
className="form-control"
|
||||
id="predicate-select"
|
||||
value={predicate}
|
||||
value={state.predicate.ts}
|
||||
onChange={handlePredicateSelect}
|
||||
>
|
||||
{inputs[predicateType].map((e: Adjective | UnisexNoun) => (
|
||||
{inputs[state.predicateType].map(e => (
|
||||
<option key={e.ts+"s"} value={e.ts}>{makeOptionLabel(e)}</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<VerbTable textOptions={opts} block={makeBlock(pe)} />
|
||||
<VerbTable textOptions={opts} block={makeBlock(state.predicate)} />
|
||||
</>;
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
import {
|
||||
Types as T,
|
||||
removeFVarients,
|
||||
getEnglishWord,
|
||||
} from "@lingdocs/pashto-inflector";
|
||||
import {
|
||||
equativeMachine,
|
||||
assembleEquativeOutput,
|
||||
} from "../../lib/equative-machine";
|
||||
|
||||
export function sort(arr: Adjective[]): Adjective[];
|
||||
export function sort(arr: UnisexNoun[]): UnisexNoun[];
|
||||
export function sort(arr: (Adjective | UnisexNoun)[]): (Adjective | UnisexNoun)[] {
|
||||
return arr.sort((a, b) => a.p.localeCompare(b.p));
|
||||
}
|
||||
|
||||
export function makeBlock(e: Adjective | UnisexNoun): T.VerbBlock {
|
||||
const makeP = (p: T.Person): T.ArrayOneOrMore<T.PsString> => {
|
||||
const b = assembleEquativeOutput(equativeMachine(p, e));
|
||||
return ("long" in b ? b.long : b) as T.ArrayOneOrMore<T.PsString>;
|
||||
};
|
||||
return [
|
||||
[makeP(0), makeP(6)],
|
||||
[makeP(1), makeP(7)],
|
||||
[makeP(2), makeP(8)],
|
||||
[makeP(3), makeP(9)],
|
||||
[makeP(4), makeP(10)],
|
||||
[makeP(5), makeP(11)],
|
||||
];
|
||||
}
|
||||
|
||||
export function makeOptionLabel(e: T.DictionaryEntry): string {
|
||||
const eng = getEnglishWord(e);
|
||||
const english = typeof eng === "string" ? eng : eng?.singular;
|
||||
return `${e.p} - ${removeFVarients(e.f)} (${english})`;
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
import { nouns, adjectives } from "../../words/words";
|
||||
import {
|
||||
isUnisexNoun,
|
||||
} from "../../lib/type-predicates";
|
||||
import { sort } from "./explorer-helpers";
|
||||
|
||||
const unisexNouns = nouns.filter(x => isUnisexNoun(x)) as UnisexNoun[];
|
||||
|
||||
const inputs = {
|
||||
adjectives: sort(adjectives),
|
||||
unisexNouns: sort(unisexNouns),
|
||||
};
|
||||
|
||||
export default inputs;
|
|
@ -0,0 +1,26 @@
|
|||
import inputs from "./explorer-inputs";
|
||||
import { PredicateType, ExplorerState } from "./EquativeExplorer";
|
||||
|
||||
export type ExplorerReducerAction = {
|
||||
type: "setPredicateType", payload: PredicateType,
|
||||
} | {
|
||||
type: "setPredicate", payload: number,
|
||||
};
|
||||
|
||||
export function reducer(state: ExplorerState, action: ExplorerReducerAction): ExplorerState {
|
||||
if (action.type === "setPredicate") {
|
||||
const pile = inputs[state.predicateType] as (UnisexNoun | Adjective)[];
|
||||
const predicate = (pile.find(p => p.ts === action.payload) || pile[0]);
|
||||
return {
|
||||
...state,
|
||||
predicate,
|
||||
};
|
||||
}
|
||||
// if (action.type === "setPredicateType") {
|
||||
const predicate = inputs[action.payload][0];
|
||||
return {
|
||||
predicate,
|
||||
predicateType: action.payload,
|
||||
};
|
||||
// }
|
||||
}
|
|
@ -2,7 +2,7 @@
|
|||
title: Equative Explorer 🌎
|
||||
---
|
||||
|
||||
import EquativeExplorer from "../../components/EquativeExplorer";
|
||||
import EquativeExplorer from "../../components/equative-explorer/EquativeExplorer";
|
||||
|
||||
This is in progress... will be able to explore much more soon. 👷♂️
|
||||
|
||||
|
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue