Skip to content

Commit

Permalink
Updates to improve sentence generation (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
byronwall authored Oct 31, 2023
1 parent c3d910c commit e315593
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/components/common/icons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ import {
Trophy,
BadgeDollarSign,
BadgeCent,
Combine,
} from "lucide-react";

export type Icon = LucideIcon;

export const Icons = {
combine: Combine,
badgeCent: BadgeCent,
badgeDollarSign: BadgeDollarSign,
menu: Menu,
Expand Down
69 changes: 58 additions & 11 deletions src/components/sentences/SentenceCreatorForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { Icons } from "~/components/common/icons";
import { Textarea } from "~/components/ui/textarea";
import { Input } from "~/components/ui/input";
import { useSentenceAdder } from "~/hooks/useSentenceAdder";
import { Button } from "~/components/ui/button";

import type * as z from "zod";

Expand Down Expand Up @@ -96,17 +97,15 @@ export function SentenceCreatorForm(props: Props) {
const __rawWordGroups = form.watch("__rawWordGroups");

useEffect(() => {
if (form.formState.touchedFields.__rawWordGroups) {
const wordGroups = (__rawWordGroups ?? "").split("\n").map((line) =>
line
.trim()
.split(/\s+/)
.map((word) => word.trim())
);

form.setValue("wordGroups", wordGroups);
}
}, [__rawWordGroups, form.formState, form.setValue]);
const wordGroups = (__rawWordGroups ?? "").split("\n").map((line) =>
line
.trim()
.split(/\s+/)
.map((word) => word.trim())
);

form.setValue("wordGroups", wordGroups);
}, [__rawWordGroups, form.setValue]);

const [newSentences, setNewSentences] = useState<string[]>([]);

Expand Down Expand Up @@ -150,6 +149,54 @@ export function SentenceCreatorForm(props: Props) {
<FormLabel>Word List</FormLabel>
<FormDescription>
Add words separated by spaces. Put on lines to group.
<Button
variant="ghost"
size="sm"
onClick={(evt) => {
evt.preventDefault();

const newLocal = form.getValues("wordGroups");
// shuffle into new groups of 3

const newGroups =
newLocal?.flatMap((group) =>
group.map((c) => c.split(" "))
) ?? [];

newGroups.sort(() => Math.random() - 0.5);

const newGroups3 = [];

while (newGroups.length > 0) {
newGroups3.push(newGroups.splice(0, 3));
}

form.setValue(
"__rawWordGroups",
newGroups3.map((c) => c.join(" ")).join("\n")
);
}}
>
<Icons.shuffle className="h-4 w-4" />
</Button>
<Button
variant="ghost"
size="sm"
onClick={(evt) => {
evt.preventDefault();

const newLocal = form.getValues("wordGroups");

const newGroups =
newLocal?.flatMap((group) => group) ?? [];

newGroups.sort(() => Math.random() - 0.5);

form.setValue("__rawWordGroups", newGroups.join(" "));
}}
>
<Icons.combine className="h-4 w-4" />
</Button>
</FormDescription>
</div>
<FormControl>
Expand Down
11 changes: 6 additions & 5 deletions src/server/openai/generations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,12 @@ export async function generateSentencesWithSettings(
wordGroups.length == 0
? "Please give me 10 very simple sentences using long vowels, short vowels, and rhyming. First grade level."
: wordGroups.length == 1
? `Please give me ${sentenceCount} very simple sentences using these words: ${wordGroups[0]!.join(
", "
)}.`
: `Create a sentence for each line using the words given:\n ${wordGroups
? `Please give me ${sentenceCount} sentences with a Fountas & Pinnell reading level of "${
settings.readingLevel
}" using these words: ${wordGroups[0]!.join(", ")}.`
: `Create a sentence with a Fountas & Pinnell reading level of "${
settings.readingLevel
}" for each line using the words given:\n ${wordGroups
.map(
(wordGroup, idx) => `Sentence ${idx + 1}: ${wordGroup.join(", ")}`
)
Expand All @@ -140,7 +142,6 @@ export async function generateSentencesWithSettings(
settings.includeAlliteration
? "Try to use alliterations with the target words."
: "",
`Aim for a Fountas & Pinnell reading level of ${settings.readingLevel}.`,
];

const messages: ChatCompletionMessageParam[] = [
Expand Down

0 comments on commit e315593

Please sign in to comment.