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 verbTsFiles = fs.readdirSync(verbCollectionPath);
|
||||||
const nounAdjTsFiles = fs.readdirSync(nounAdjCollectionPath);
|
const nounAdjTsFiles = fs.readdirSync(nounAdjCollectionPath);
|
||||||
|
|
||||||
const allVerbTsS = [...new Set(verbTsFiles.reduce((arr, fileName) => {
|
const allVerbTsS = verbTsFiles.flatMap(fileName => [
|
||||||
const TsS = require(path.join("..", verbCollectionPath, fileName))
|
...require(path.join("..", verbCollectionPath, fileName)).map(x => x.ts)
|
||||||
.filter((v, i, a) => a.findIndex(x => x.ts === v.ts) === i);
|
]).filter((v, i, a) => a.findIndex(x => x === v) === i);
|
||||||
return [...arr, ...TsS];
|
|
||||||
}, []))];
|
|
||||||
|
|
||||||
const allNounAdjTsS = [...new Set(nounAdjTsFiles.reduce((arr, fileName) => {
|
const allNounAdjTsS = nounAdjTsFiles.flatMap(fileName => [
|
||||||
const TsS = require(path.join("..", nounAdjCollectionPath, fileName))
|
...require(path.join("..", nounAdjCollectionPath, fileName)).map(x => x.ts)
|
||||||
.filter((v, i, a) => a.findIndex(x => x.ts === v.ts) === i);
|
]).filter((v, i, a) => a.findIndex(x => x === v) === i);
|
||||||
return [...arr, ...TsS];
|
|
||||||
}, []))];
|
|
||||||
|
|
||||||
console.log("getting words from dictionary...");
|
console.log("getting words from dictionary...");
|
||||||
|
|
||||||
|
@ -40,8 +36,8 @@ export default words;`;
|
||||||
|
|
||||||
function getVerbsFromTsS(entries) {
|
function getVerbsFromTsS(entries) {
|
||||||
const missingEc = [];
|
const missingEc = [];
|
||||||
const toReturn = allVerbTsS.map(item => {
|
const toReturn = allVerbTsS.map(ts => {
|
||||||
const entry = entries.find(x => item.ts === x.ts);
|
const entry = entries.find(x => ts === x.ts);
|
||||||
if (!entry) {
|
if (!entry) {
|
||||||
console.log("couldn't find ts", ts);
|
console.log("couldn't find ts", ts);
|
||||||
return undefined;
|
return undefined;
|
||||||
|
@ -65,10 +61,10 @@ function getVerbsFromTsS(entries) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function getNounsAdjsFromTsS(entries) {
|
function getNounsAdjsFromTsS(entries) {
|
||||||
const b = allNounAdjTsS.map(item => {
|
const b = allNounAdjTsS.map(ts => {
|
||||||
const entry = entries.find(x => item.ts === x.ts);
|
const entry = entries.find(x => ts === x.ts);
|
||||||
if (!entry) {
|
if (!entry) {
|
||||||
console.log("couldn't find ts", item);
|
console.log("couldn't find ts", ts);
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
// const firstWord = entry.e.split(",")[0].split(";")[0].split("(")[0].trim();
|
// const firstWord = entry.e.split(",")[0].split(";")[0].split("(")[0].trim();
|
||||||
|
|
|
@ -1,101 +1,43 @@
|
||||||
import React, { useState } from "react";
|
import React, { useState } from "react";
|
||||||
import {
|
import {
|
||||||
Types as T,
|
|
||||||
defaultTextOptions as opts,
|
defaultTextOptions as opts,
|
||||||
VerbTable,
|
VerbTable,
|
||||||
removeFVarients,
|
|
||||||
getEnglishWord,
|
|
||||||
} from "@lingdocs/pashto-inflector";
|
} from "@lingdocs/pashto-inflector";
|
||||||
import {
|
import {
|
||||||
isUnisexNoun,
|
makeBlock,
|
||||||
} from "../lib/type-predicates";
|
makeOptionLabel,
|
||||||
import {
|
} from "./explorer-helpers";
|
||||||
equativeMachine,
|
import inputs from "./explorer-inputs";
|
||||||
assembleEquativeOutput,
|
import { reducer, ExplorerReducerAction } from "./explorer-reducer";
|
||||||
} from "../lib/equative-machine";
|
|
||||||
import { nouns, adjectives } from "../words/words";
|
|
||||||
|
|
||||||
function uniqueSort(arr: Adjective[]): Adjective[];
|
export type PredicateType = "adjectives" | "unisexNouns";
|
||||||
function uniqueSort(arr: UnisexNoun[]): UnisexNoun[];
|
const predicateTypes: PredicateType[] = ["adjectives", "unisexNouns"];
|
||||||
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";
|
|
||||||
// type SubjectType = "pronouns" | "nouns";
|
// type SubjectType = "pronouns" | "nouns";
|
||||||
|
|
||||||
// TODO: Plural nouns like shoode
|
// 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() {
|
function EquativeExplorer() {
|
||||||
// TODO: Use sticky state
|
const [state, unsafeSetState] = useState<ExplorerState>(defaultState);
|
||||||
const predicateTypes: PredicateType[] = ["adjectives", "unisexNouns"];
|
function dispatch(action: ExplorerReducerAction) {
|
||||||
// const subjectTypes: SubjectType[] = ["pronouns", "nouns"];
|
const newState = reducer(state, action);
|
||||||
const [predicate, setPredicate] = useState<number>(defaultTs);
|
unsafeSetState(newState);
|
||||||
// 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})`;
|
|
||||||
}
|
}
|
||||||
function handlePredicateSelect(e: React.ChangeEvent<HTMLSelectElement>) {
|
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>) {
|
function handlePredicateTypeSelect(e: React.ChangeEvent<HTMLInputElement>) {
|
||||||
const pt = e.target.value as PredicateType;
|
const payload = e.target.value as PredicateType;
|
||||||
setPredicateType(pt);
|
dispatch({ type: "setPredicateType", payload });
|
||||||
setPredicate(inputs[pt][0].ts);
|
|
||||||
}
|
}
|
||||||
// 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 <>
|
return <>
|
||||||
<div className="d-flex flex-row">
|
<div className="d-flex flex-row">
|
||||||
{/* <div className="form-group">
|
{/* <div className="form-group">
|
||||||
|
@ -138,14 +80,13 @@ function EquativeExplorer() {
|
||||||
name="adjectivesPredicateRadio"
|
name="adjectivesPredicateRadio"
|
||||||
id="adjectivesPredicateRadio"
|
id="adjectivesPredicateRadio"
|
||||||
value={predicateTypes[0]}
|
value={predicateTypes[0]}
|
||||||
checked={predicateType === "adjectives"}
|
checked={state.predicateType === "adjectives"}
|
||||||
onChange={handlePredicateTypeSelect}
|
onChange={handlePredicateTypeSelect}
|
||||||
/>
|
/>
|
||||||
<label className="form-check-label" htmlFor="adjectivesPredicateRadio">
|
<label className="form-check-label" htmlFor="adjectivesPredicateRadio">
|
||||||
Adjectives
|
Adjectives
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
{/* hiding this because it's a bit buggy still */}
|
|
||||||
<div className="form-check mb-2">
|
<div className="form-check mb-2">
|
||||||
<input
|
<input
|
||||||
className="form-check-input"
|
className="form-check-input"
|
||||||
|
@ -153,7 +94,7 @@ function EquativeExplorer() {
|
||||||
name="unisexNounsPredicateRadio"
|
name="unisexNounsPredicateRadio"
|
||||||
id="unisexNounsPredicateRadio"
|
id="unisexNounsPredicateRadio"
|
||||||
value={predicateTypes[1]}
|
value={predicateTypes[1]}
|
||||||
checked={predicateType === "unisexNouns"}
|
checked={state.predicateType === "unisexNouns"}
|
||||||
onChange={handlePredicateTypeSelect}
|
onChange={handlePredicateTypeSelect}
|
||||||
/>
|
/>
|
||||||
<label className="form-check-label" htmlFor="unisexNounsPredicateRadio">
|
<label className="form-check-label" htmlFor="unisexNounsPredicateRadio">
|
||||||
|
@ -163,16 +104,16 @@ function EquativeExplorer() {
|
||||||
<select
|
<select
|
||||||
className="form-control"
|
className="form-control"
|
||||||
id="predicate-select"
|
id="predicate-select"
|
||||||
value={predicate}
|
value={state.predicate.ts}
|
||||||
onChange={handlePredicateSelect}
|
onChange={handlePredicateSelect}
|
||||||
>
|
>
|
||||||
{inputs[predicateType].map((e: Adjective | UnisexNoun) => (
|
{inputs[state.predicateType].map(e => (
|
||||||
<option key={e.ts+"s"} value={e.ts}>{makeOptionLabel(e)}</option>
|
<option key={e.ts+"s"} value={e.ts}>{makeOptionLabel(e)}</option>
|
||||||
))}
|
))}
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
</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 🌎
|
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. 👷♂️
|
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