pronoun clash protection

This commit is contained in:
lingdocs 2022-03-15 19:35:54 +04:00
parent 5d632f095e
commit a8bd1a337f
4 changed files with 55 additions and 8 deletions

View File

@ -10,7 +10,7 @@ import { nouns, verbs } from "../../words/words";
const npTypes: NPType[] = ["noun", "pronoun", "participle"]; const npTypes: NPType[] = ["noun", "pronoun", "participle"];
function NPPicker({ np, onChange }: { onChange: (nps: NPSelection | undefined) => void, np: NPSelection | undefined }) { function NPPicker({ np, onChange, counterPart }: { onChange: (nps: NPSelection | undefined) => void, np: NPSelection | undefined, counterPart: NPSelection | VerbObject | undefined }) {
// eslint-disable-next-line // eslint-disable-next-line
const [npType, setNpType] = useState<NPType | undefined>(np ? np.type : undefined); const [npType, setNpType] = useState<NPType | undefined>(np ? np.type : undefined);
function handleClear() { function handleClear() {
@ -19,7 +19,9 @@ function NPPicker({ np, onChange }: { onChange: (nps: NPSelection | undefined) =
} }
function handleNPTypeChange(ntp: NPType) { function handleNPTypeChange(ntp: NPType) {
if (ntp === "pronoun") { if (ntp === "pronoun") {
const person = randomPerson(); const person = ((typeof counterPart === "object") && (counterPart.type === "pronoun"))
? randomPerson({ counterPart: counterPart.person })
: randomPerson();
const pronoun: PronounSelection = { const pronoun: PronounSelection = {
type: "pronoun", type: "pronoun",
person, person,

View File

@ -1,9 +1,9 @@
import NPPicker from "../np-picker/NPPicker"; import NPPicker from "../np-picker/NPPicker";
function ObjectDisplay({ object, onChange }: { object: Exclude<VerbObject, "none">, onChange: (o: NPSelection | undefined) => void }) { function ObjectDisplay({ object, onChange, counterPart }: { object: Exclude<VerbObject, "none">, onChange: (o: NPSelection | undefined) => void, counterPart: NPSelection | undefined }) {
return (typeof object === "number") return (typeof object === "number")
? <div className="text-muted">Unspoken 3rd Pers. Masc. Plur.</div> ? <div className="text-muted">Unspoken 3rd Pers. Masc. Plur.</div>
: <NPPicker np={object} onChange={onChange} />; : <NPPicker np={object} counterPart={counterPart} onChange={onChange} />;
} }
export default ObjectDisplay; export default ObjectDisplay;

View File

@ -4,6 +4,9 @@ import VerbPicker from "../VerbPicker";
import VPDisplay from "./VPDisplay"; import VPDisplay from "./VPDisplay";
import ObjectDisplay from "./ObjectDisplay"; import ObjectDisplay from "./ObjectDisplay";
import { verbs } from "../../words/words"; import { verbs } from "../../words/words";
import {
isInvalidSubjObjCombo,
} from "../../lib/np-tools";
function verbPhraseComplete({ subject, verb }: { subject: NPSelection | undefined, verb: VerbSelection | undefined }): VPSelection | undefined { function verbPhraseComplete({ subject, verb }: { subject: NPSelection | undefined, verb: VerbSelection | undefined }): VPSelection | undefined {
if (!subject) return undefined; if (!subject) return undefined;
@ -20,9 +23,33 @@ function verbPhraseComplete({ subject, verb }: { subject: NPSelection | undefine
export function PhraseBuilder() { export function PhraseBuilder() {
const [subject, setSubject] = useState<NPSelection | undefined>(undefined); const [subject, setSubject] = useState<NPSelection | undefined>(undefined);
const [verb, setVerb] = useState<VerbSelection | undefined>(undefined); const [verb, setVerb] = useState<VerbSelection | undefined>(undefined);
function handleSubjectChange(subject: NPSelection | undefined) {
const objPronoun = (typeof verb?.object === "object" && verb.object.type === "pronoun")
? verb.object.person
: undefined;
if (subject?.type === "pronoun" && objPronoun && isInvalidSubjObjCombo(subject.person, objPronoun)) {
alert("That combination of pronouns is not allowed");
return;
// let newP = 0;
// do {
// newP = randomPerson();
// } while (isInvalidSubjObjCombo(newP, object.person));
// return setSubject({ ...incoming, person: newP });
}
setSubject(subject);
}
function handleObjectChange(object: NPSelection | undefined) { function handleObjectChange(object: NPSelection | undefined) {
if (!verb) return; if (!verb) return;
if ((verb.object === "none") || (typeof verb.object === "number")) return; if ((verb.object === "none") || (typeof verb.object === "number")) return;
if (object?.type === "pronoun" && subject?.type === "pronoun" && isInvalidSubjObjCombo(object.person, subject.person)) {
alert("That combination of pronouns is not allowed");
return;
// let newP = 0;
// do {
// newP = randomPerson();
// } while (isInvalidSubjObjCombo(newP, object.person));
// return setSubject({ ...incoming, person: newP });
}
setVerb({ setVerb({
...verb, ...verb,
object, object,
@ -33,11 +60,15 @@ export function PhraseBuilder() {
<div className="d-flex flex-row justify-content-between"> <div className="d-flex flex-row justify-content-between">
<div className="mr-2"> <div className="mr-2">
<div className="h5">Subject</div> <div className="h5">Subject</div>
<NPPicker np={subject} onChange={setSubject} /> <NPPicker
np={subject}
counterPart={verb ? verb.object : undefined}
onChange={handleSubjectChange}
/>
</div> </div>
{verb && (verb.object !== "none") && <div className="mr-2"> {verb && (verb.object !== "none") && <div className="mr-2">
<div className="h5">Object</div> <div className="h5">Object</div>
<ObjectDisplay object={verb.object} onChange={handleObjectChange} /> <ObjectDisplay object={verb.object} counterPart={subject} onChange={handleObjectChange} />
</div>} </div>}
<div> <div>
<div className="h5">Verb</div> <div className="h5">Verb</div>

View File

@ -15,11 +15,25 @@ function getRandPers(): T.Person {
return Math.floor(Math.random() * 12); return Math.floor(Math.random() * 12);
} }
export function randomPerson(p?: T.Person) { export function randomPerson(a?: { old?: T.Person, counterPart?: T.Person | VerbObject }) {
let newP = 0; let newP = 0;
do { do {
newP = getRandPers(); newP = getRandPers();
} while (newP === p); } while (
a &&
(
((a.old !== undefined) && newP === a.old) ||
(a.counterPart && (
(
(typeof a.counterPart === "object")
&& a.counterPart.type === "pronoun"
&& isInvalidSubjObjCombo(a.counterPart.person, newP)
)
||
((typeof a?.counterPart === "number") && isInvalidSubjObjCombo(a.counterPart, newP))
))
)
);
return newP; return newP;
} }