towards refactor
This commit is contained in:
parent
2d17daf81f
commit
8843a7f106
|
@ -15,41 +15,120 @@ import {
|
|||
equativeMachine,
|
||||
assembleEquativeOutput,
|
||||
SubjectInput,
|
||||
PredicateInput,
|
||||
isParticipleInput,
|
||||
ParticipleInput,
|
||||
} from "../../lib/equative-machine";
|
||||
import { isNoun, isPluralEntry, isUnisexNoun } from "../../lib/type-predicates";
|
||||
import { isPluralEntry, isUnisexNoun, isAdjective, isSingularEntry } from "../../lib/type-predicates";
|
||||
|
||||
export function chooseLength<O>(o: T.SingleOrLengthOpts<O>, length: "short" | "long"): O {
|
||||
return ("long" in o) ? o[length] : o;
|
||||
}
|
||||
|
||||
function SingleItemDisplay({ state }: { state: ExplorerState }) {
|
||||
if (state.subjectType === "pronouns") {
|
||||
if (state.subject.type === "pronouns") {
|
||||
return <div>ERROR: Wrong display being used</div>;
|
||||
}
|
||||
const entry = state.subjectsSelected[state.subjectType];
|
||||
// @ts-ignore - TODO: safer and for use with unisex nouns
|
||||
const subjInput: SubjectInput = isNoun(entry) ? {
|
||||
entry,
|
||||
plural: isPluralEntry(entry) ? true : state.subjectsSelected.info.plural,
|
||||
...isUnisexNoun(entry) ? {
|
||||
gender: state.subjectsSelected.info.gender,
|
||||
} : {},
|
||||
} : entry;
|
||||
try {
|
||||
const subjInput = makeSubjectInput(state.subject[state.subject.type], state);
|
||||
const predInput = makePredicateInput(state.predicate[state.predicate.type], state);
|
||||
const block = assembleEquativeOutput(
|
||||
equativeMachine(subjInput, predInput, state.tense)
|
||||
);
|
||||
return <div>
|
||||
<VerbTable textOptions={opts} block={chooseLength(block, state.length)} />
|
||||
</div>;
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return <div>Error making equative sentence</div>
|
||||
}
|
||||
}
|
||||
|
||||
const block = assembleEquativeOutput(
|
||||
equativeMachine(subjInput, state.predicatesSelected[state.predicateType], state.tense)
|
||||
);
|
||||
return <div>
|
||||
<VerbTable textOptions={opts} block={chooseLength(block, state.length)} />
|
||||
</div>;
|
||||
function makeSubjectInput(entry: Noun | ParticipleInput | UnisexNoun, state: ExplorerState): SubjectInput {
|
||||
if (isParticipleInput(entry)) {
|
||||
return entry;
|
||||
}
|
||||
const isUnisex = isUnisexNoun(entry);
|
||||
if (isUnisex && isSingularEntry(entry)) {
|
||||
return {
|
||||
...state.subject.info,
|
||||
entry,
|
||||
};
|
||||
}
|
||||
if (isUnisex && isPluralEntry(entry)) {
|
||||
return {
|
||||
...state.subject.info,
|
||||
plural: true,
|
||||
entry,
|
||||
};
|
||||
}
|
||||
if (isUnisex) {
|
||||
throw new Error("improper unisex noun");
|
||||
}
|
||||
if (isPluralEntry(entry)) {
|
||||
return {
|
||||
plural: true,
|
||||
entry,
|
||||
}
|
||||
}
|
||||
if (isSingularEntry(entry)) {
|
||||
return {
|
||||
entry,
|
||||
plural: state.subject.info.plural,
|
||||
};
|
||||
}
|
||||
throw new Error("unable to make subject input from entry");
|
||||
}
|
||||
|
||||
function makePredicateInput(entry: Noun | ParticipleInput | UnisexNoun | Adjective, state: ExplorerState): PredicateInput {
|
||||
if (isParticipleInput(entry) || isAdjective(entry)) {
|
||||
return entry;
|
||||
}
|
||||
const isUnisex = isUnisexNoun(entry);
|
||||
if (isUnisex && state.subject.type === "pronouns") {
|
||||
return entry;
|
||||
}
|
||||
if (isUnisex && isSingularEntry(entry)) {
|
||||
return {
|
||||
...state.predicate.info,
|
||||
entry,
|
||||
};
|
||||
}
|
||||
if (isUnisex && isPluralEntry(entry)) {
|
||||
return {
|
||||
...state.predicate.info,
|
||||
plural: true,
|
||||
entry,
|
||||
};
|
||||
}
|
||||
if (isUnisex) {
|
||||
throw new Error("improper unisex noun");
|
||||
}
|
||||
if (isPluralEntry(entry)) {
|
||||
return {
|
||||
plural: true,
|
||||
entry,
|
||||
}
|
||||
}
|
||||
if (isSingularEntry(entry)) {
|
||||
return {
|
||||
entry,
|
||||
plural: state.predicate.info.plural,
|
||||
};
|
||||
}
|
||||
throw new Error("unable to make predicate input from entry");
|
||||
}
|
||||
|
||||
function PronounBlockDisplay({ state }: { state: ExplorerState }) {
|
||||
const block = makeBlockWPronouns(state.predicatesSelected[state.predicateType], state.tense);
|
||||
return <VerbTable
|
||||
textOptions={opts}
|
||||
block={chooseLength(block, state.length)}
|
||||
/>;
|
||||
const pred = state.predicate[state.predicate.type];
|
||||
if (!isParticipleInput(pred) && (isAdjective(pred) || isUnisexNoun(pred))) {
|
||||
const block = makeBlockWPronouns(pred, state.tense);
|
||||
return <VerbTable
|
||||
textOptions={opts}
|
||||
block={chooseLength(block, state.length)}
|
||||
/>;
|
||||
}
|
||||
return <div>Invalid combination</div>
|
||||
}
|
||||
|
||||
function EquativeDisplay({ state, dispatch }: { state: ExplorerState, dispatch: (action: ExplorerReducerAction) => void }) {
|
||||
|
@ -65,7 +144,7 @@ function EquativeDisplay({ state, dispatch }: { state: ExplorerState, dispatch:
|
|||
handleChange={(p) => dispatch({ type: "setLength", payload: p as "long" | "short" })}
|
||||
/>
|
||||
</div>}
|
||||
{state.subjectType === "pronouns"
|
||||
{state.subject.type === "pronouns"
|
||||
? <PronounBlockDisplay state={state} />
|
||||
: <SingleItemDisplay state={state} />
|
||||
}
|
||||
|
|
|
@ -22,11 +22,19 @@ import EquativeDisplay from "./EquativeDisplay";
|
|||
const defaultState: ExplorerState = {
|
||||
tense: "present",
|
||||
length: "short",
|
||||
predicatesSelected: {
|
||||
predicate: {
|
||||
type: "adjective",
|
||||
adjective: defaultAdjective,
|
||||
unisexNoun: defaultUnisexNoun,
|
||||
participle: defaultParticiple,
|
||||
noun: defaultNoun,
|
||||
info: {
|
||||
plural: false,
|
||||
gender: "masc",
|
||||
},
|
||||
},
|
||||
subjectsSelected: {
|
||||
subject: {
|
||||
type: "pronouns",
|
||||
noun: defaultNoun,
|
||||
participle: defaultParticiple,
|
||||
unisexNoun: defaultUnisexNoun,
|
||||
|
@ -35,8 +43,6 @@ const defaultState: ExplorerState = {
|
|||
gender: "masc",
|
||||
},
|
||||
},
|
||||
predicateType: "adjective",
|
||||
subjectType: "pronouns",
|
||||
};
|
||||
|
||||
function EquativeExplorer() {
|
||||
|
@ -46,8 +52,10 @@ function EquativeExplorer() {
|
|||
unsafeSetState(newState);
|
||||
}
|
||||
return <>
|
||||
<TenseSelector state={state} dispatch={dispatch} />
|
||||
<div className="row">
|
||||
<div className="col-sm">
|
||||
<TenseSelector state={state} dispatch={dispatch} />
|
||||
</div>
|
||||
<div className="col">
|
||||
<SubjectSelector state={state} dispatch={dispatch} />
|
||||
</div>
|
||||
|
|
|
@ -3,13 +3,13 @@ import { ExplorerState, ExplorerReducerAction } from "./explorer-types";
|
|||
|
||||
export function reducer(state: ExplorerState, action: ExplorerReducerAction): ExplorerState {
|
||||
if (action.type === "setPredicate") {
|
||||
const pile = inputs[state.predicateType] as (UnisexNoun | Adjective)[];
|
||||
const pile = inputs[state.predicate.type] as (UnisexNoun | Adjective)[];
|
||||
const predicate = (pile.find(p => p.ts === action.payload) || pile[0]);
|
||||
return {
|
||||
...state,
|
||||
predicatesSelected: {
|
||||
...state.predicatesSelected,
|
||||
[state.predicateType]: predicate,
|
||||
predicate: {
|
||||
...state.predicate,
|
||||
[state.predicate.type]: predicate,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
@ -17,37 +17,46 @@ export function reducer(state: ExplorerState, action: ExplorerReducerAction): Ex
|
|||
const predicateType = action.payload;
|
||||
return {
|
||||
...state,
|
||||
predicateType: (predicateType === "unisexNoun" && state.subjectType === "noun") ? "adjective" : predicateType,
|
||||
predicate: {
|
||||
...state.predicate,
|
||||
type: (predicateType === "unisexNoun" && state.subject.type === "noun") ? "adjective" : predicateType,
|
||||
},
|
||||
};
|
||||
}
|
||||
if (action.type === "setSubjectType") {
|
||||
const subjectType = action.payload;
|
||||
return {
|
||||
...state,
|
||||
predicateType: state.predicateType === "unisexNoun" ? "adjective" : state.predicateType,
|
||||
subjectType,
|
||||
predicate: {
|
||||
...state.predicate,
|
||||
type: state.predicate.type === "unisexNoun" ? "adjective" : state.predicate.type,
|
||||
},
|
||||
subject: {
|
||||
...state.subject,
|
||||
type: subjectType,
|
||||
}
|
||||
};
|
||||
}
|
||||
if (action.type === "setSubject") {
|
||||
if (state.subjectType === "pronouns") return state;
|
||||
const pile = inputs[state.subjectType];
|
||||
if (state.subject.type === "pronouns") return state;
|
||||
const pile = inputs[state.subject.type];
|
||||
// @ts-ignore
|
||||
const subject = (pile.find(p => p.ts === action.payload) || pile[0]);
|
||||
return {
|
||||
...state,
|
||||
subjectsSelected: {
|
||||
...state.subjectsSelected,
|
||||
[state.subjectType]: subject,
|
||||
subject: {
|
||||
...state.subject,
|
||||
[state.subject.type]: subject,
|
||||
},
|
||||
};
|
||||
}
|
||||
if (action.type === "setSubjectPlural") {
|
||||
return {
|
||||
...state,
|
||||
subjectsSelected: {
|
||||
...state.subjectsSelected,
|
||||
subject: {
|
||||
...state.subject,
|
||||
info: {
|
||||
...state.subjectsSelected.info,
|
||||
...state.subject.info,
|
||||
plural: action.payload,
|
||||
},
|
||||
},
|
||||
|
@ -56,10 +65,10 @@ export function reducer(state: ExplorerState, action: ExplorerReducerAction): Ex
|
|||
if (action.type === "setSubjectGender") {
|
||||
return {
|
||||
...state,
|
||||
subjectsSelected: {
|
||||
...state.subjectsSelected,
|
||||
subject: {
|
||||
...state.subject,
|
||||
info: {
|
||||
...state.subjectsSelected.info,
|
||||
...state.subject.info,
|
||||
gender: action.payload,
|
||||
},
|
||||
},
|
||||
|
|
|
@ -30,17 +30,17 @@ export function SubjectSelector({ state, dispatch }: {
|
|||
dispatch({ type: "setSubject", payload: parseInt(value) });
|
||||
}
|
||||
const pluralNounSelected = (
|
||||
state.subjectType === "noun" && isPluralEntry(state.subjectsSelected.noun)
|
||||
state.subject.type === "noun" && isPluralEntry(state.subject.noun)
|
||||
);
|
||||
const options = state.subjectType === "pronouns"
|
||||
const options = state.subject.type === "pronouns"
|
||||
? []
|
||||
: inputs[state.subjectType].map(e => ({
|
||||
: inputs[state.subject.type].map(e => ({
|
||||
value: e.ts.toString(),
|
||||
label: makeOptionLabel(e),
|
||||
}));
|
||||
const subject = state.subjectType === "pronouns"
|
||||
const subject = state.subject.type === "pronouns"
|
||||
? undefined
|
||||
: state.subjectsSelected[state.subjectType];
|
||||
: state.subject[state.subject.type];
|
||||
return <div className="form-group">
|
||||
<label htmlFor="predicate-select"><h5 className="mb-0">Subject:</h5></label>
|
||||
<div className="form-check">
|
||||
|
@ -50,7 +50,7 @@ export function SubjectSelector({ state, dispatch }: {
|
|||
name="pronounsSubjectRadio"
|
||||
id="pronounsSubjectRadio"
|
||||
value="pronouns"
|
||||
checked={state.subjectType === "pronouns"}
|
||||
checked={state.subject.type === "pronouns"}
|
||||
onChange={onTypeSelect}
|
||||
/>
|
||||
<label className="form-check-label" htmlFor="adjectivesPredicateRadio">
|
||||
|
@ -64,7 +64,7 @@ export function SubjectSelector({ state, dispatch }: {
|
|||
name="nounsSubjectRadio"
|
||||
id="nounsSubjectRadio"
|
||||
value="noun"
|
||||
checked={state.subjectType === "noun"}
|
||||
checked={state.subject.type === "noun"}
|
||||
onChange={onTypeSelect}
|
||||
/>
|
||||
<label className="form-check-label" htmlFor="unisexNounsPredicateRadio">
|
||||
|
@ -78,7 +78,7 @@ export function SubjectSelector({ state, dispatch }: {
|
|||
name="unisexNounsSubjectRadio"
|
||||
id="unisexNounsSubjectRadio"
|
||||
value="unisexNoun"
|
||||
checked={state.subjectType === "unisexNoun"}
|
||||
checked={state.subject.type === "unisexNoun"}
|
||||
onChange={onTypeSelect}
|
||||
/>
|
||||
<label className="form-check-label" htmlFor="unisexNounsPredicateRadio">
|
||||
|
@ -92,14 +92,14 @@ export function SubjectSelector({ state, dispatch }: {
|
|||
name="participlesSubjectRadio"
|
||||
id="participlesSubjectRadio"
|
||||
value="participle"
|
||||
checked={state.subjectType === "participle"}
|
||||
checked={state.subject.type === "participle"}
|
||||
onChange={onTypeSelect}
|
||||
/>
|
||||
<label className="form-check-label" htmlFor="unisexNounsPredicateRadio">
|
||||
Participles
|
||||
</label>
|
||||
</div>
|
||||
{state.subjectType !== "pronouns" &&
|
||||
{state.subject.type !== "pronouns" &&
|
||||
<>
|
||||
<Select
|
||||
value={subject?.ts.toString()}
|
||||
|
@ -116,27 +116,27 @@ export function SubjectSelector({ state, dispatch }: {
|
|||
<ButtonSelect
|
||||
small
|
||||
options={[
|
||||
...(state.subjectType === "unisexNoun" || (state.subjectType === "participle") || (isMascNoun(state.subjectsSelected[state.subjectType])))
|
||||
...(state.subject.type === "unisexNoun" || (state.subject.type === "participle") || (isMascNoun(state.subject[state.subject.type])))
|
||||
? [{ label: "Masc.", value: "masc" }] : [],
|
||||
...(state.subjectType === "unisexNoun" || ((state.subjectType !== "participle") && isFemNoun(state.subjectsSelected[state.subjectType])))
|
||||
...(state.subject.type === "unisexNoun" || ((state.subject.type !== "participle") && isFemNoun(state.subject[state.subject.type])))
|
||||
? [{ label: "Fem.", value: "fem" }] : [],
|
||||
]}
|
||||
value={state.subjectType === "noun"
|
||||
? (isMascNoun(state.subjectsSelected[state.subjectType]) ? "masc" : "fem")
|
||||
: state.subjectType === "participle"
|
||||
value={state.subject.type === "noun"
|
||||
? (isMascNoun(state.subject[state.subject.type]) ? "masc" : "fem")
|
||||
: state.subject.type === "participle"
|
||||
? "masc"
|
||||
: state.subjectsSelected.info.gender}
|
||||
handleChange={state.subjectType === "noun" ? p => null : (p) => dispatch({ type: "setSubjectGender", payload: p as T.Gender })}
|
||||
: state.subject.info.gender}
|
||||
handleChange={state.subject.type === "noun" ? p => null : (p) => dispatch({ type: "setSubjectGender", payload: p as T.Gender })}
|
||||
/>
|
||||
</div>
|
||||
<div className="ml-2">
|
||||
<ButtonSelect
|
||||
small
|
||||
options={[
|
||||
...(!pluralNounSelected && state.subjectType !== "participle") ? [{ label: "Singular", value: "singular" }] : [],
|
||||
...(!pluralNounSelected && state.subject.type !== "participle") ? [{ label: "Singular", value: "singular" }] : [],
|
||||
{ label: "Plural", value: "plural" },
|
||||
]}
|
||||
value={(state.subjectsSelected.info.plural || pluralNounSelected || state.subjectType === "participle") ? "plural" : "singular"}
|
||||
value={(state.subject.info.plural || pluralNounSelected || state.subject.type === "participle") ? "plural" : "singular"}
|
||||
handleChange={(p) => dispatch({ type: "setSubjectPlural", payload: p === "plural" ? true : false })}
|
||||
/>
|
||||
</div>
|
||||
|
@ -157,11 +157,11 @@ export function PredicateSelector({ state, dispatch }: {
|
|||
function onPredicateSelect({ value }: any) {
|
||||
dispatch({ type: "setPredicate", payload: parseInt(value) });
|
||||
}
|
||||
const options = inputs[state.predicateType].map(e => ({
|
||||
const options = inputs[state.predicate.type].map(e => ({
|
||||
value: `${e.ts}`,
|
||||
label: makeOptionLabel(e),
|
||||
}));
|
||||
const predicate = state.predicatesSelected[state.predicateType];
|
||||
const predicate = state.predicate[state.predicate.type];
|
||||
return <div>
|
||||
<label htmlFor="predicate-select"><h5 className="mb-0">Predicate:</h5></label>
|
||||
<div className="form-check">
|
||||
|
@ -171,7 +171,7 @@ export function PredicateSelector({ state, dispatch }: {
|
|||
name="adjectivesPredicateRadio"
|
||||
id="adjectivesPredicateRadio"
|
||||
value="adjective"
|
||||
checked={state.predicateType === "adjective"}
|
||||
checked={state.predicate.type === "adjective"}
|
||||
onChange={onTypeSelect}
|
||||
/>
|
||||
<label className="form-check-label" htmlFor="adjectivesPredicateRadio">
|
||||
|
@ -185,9 +185,9 @@ export function PredicateSelector({ state, dispatch }: {
|
|||
name="unisexNounsPredicateRadio"
|
||||
id="unisexNounsPredicateRadio"
|
||||
value="unisexNoun"
|
||||
checked={state.predicateType === "unisexNoun"}
|
||||
checked={state.predicate.type === "unisexNoun"}
|
||||
onChange={onTypeSelect}
|
||||
disabled={state.subjectType !== "pronouns"}
|
||||
disabled={state.subject.type !== "pronouns"}
|
||||
/>
|
||||
<label className="form-check-label" htmlFor="unisexNounsPredicateRadio">
|
||||
Unisex Nouns
|
||||
|
|
|
@ -1,22 +1,21 @@
|
|||
import { Types as T } from "@lingdocs/pashto-inflector";
|
||||
import { ParticipleInput } from "../../lib/equative-machine";
|
||||
|
||||
export type PredicateType = keyof PredicatesSelected;
|
||||
export type SubjectType = "noun" | "pronouns" | "participle" | "unisexNoun";
|
||||
export type PredicateType = "adjective" | "noun" | "unisexNoun" | "participle";
|
||||
export type SubjectType = "pronouns" | "noun" | "unisexNoun" | "participle";
|
||||
|
||||
export type ExplorerState = {
|
||||
tense: EquativeTense,
|
||||
length: "short" | "long",
|
||||
subjectType: SubjectType,
|
||||
subjectsSelected: SubjectSelected,
|
||||
predicateType: PredicateType,
|
||||
predicatesSelected: PredicatesSelected,
|
||||
subject: SubjectEntityInfo,
|
||||
predicate: PredicateEntityInfo,
|
||||
};
|
||||
type PredicatesSelected = {
|
||||
adjective: Adjective,
|
||||
unisexNoun: UnisexNoun,
|
||||
};
|
||||
type SubjectSelected = {
|
||||
|
||||
export type SubjectEntityInfo = EntitiyInfo & { type: SubjectType };
|
||||
|
||||
export type PredicateEntityInfo = EntitiyInfo & { type: PredicateType, adjective: Adjective };
|
||||
|
||||
type EntitiyInfo = {
|
||||
noun: Noun,
|
||||
participle: ParticipleInput,
|
||||
unisexNoun: UnisexNoun,
|
||||
|
|
Loading…
Reference in New Issue