Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sentencify-mvp #222

Merged
merged 2 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions nepalingo-web/src/lib/getNextWord.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const newariWords = [
"hello",
"call",
"can",
"do",
"how",
"I",
Expand Down Expand Up @@ -30,6 +30,33 @@ export const newariWords = [
"salt",
];

export const WordSentences = [
"my",
"name",
"is",
"my name is Ram",
"I",
"years",
"old",
"I am five years old",
"from",
" I am from kathmandu",
"don't",
"speak",
"your",
"language",
"well",
"I don’t speak your language well",
"how",
"are",
"you",
"how are you",
"where",
"from",
"I am",
"where are you from?",
];

export async function getTajpuriyaWords(): Promise<string[]> {
const wordText = await fetch("./dictionaries/TajpuriyaDictionary.csv")
.then((r) => r.text())
Expand Down Expand Up @@ -58,13 +85,14 @@ export function* wordGenerator(words: string[]) {

export async function getNextWord(language: string) {
let words: string[] = [];
if (
language === "Newari" ||
if (language === "Newari") {
words = newariWords;
} else if (
language === "Maithili" ||
language === "Sanskrit" ||
language === "Nepali"
) {
words = newariWords;
words = WordSentences;
} else if (language === "Tajpuriya") {
words = await getTajpuriyaWords();
}
Expand Down
79 changes: 58 additions & 21 deletions nepalingo-web/src/pages/TestYourself.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import { getNextWord } from "@/lib/getNextWord";
const TestYourself: React.FC = () => {
const { updateStreak } = useStreak();
const { selectedLanguage } = useLanguage();
const [word, setWord] = useState("today");

const [word, setWord] = useState("hello");
const [wordIndex, setWordIndex] = useState(1);
const [options, setOptions] = useState<string[]>([
"hello",
"bye",
"no",
"today",
"rice",
"hot",
"salt",
]);
const [selectedOption, setSelectedOption] = useState<string | null>(null);
const [isCorrect, setIsCorrect] = useState<boolean | null>(null);
Expand All @@ -25,34 +25,62 @@ const TestYourself: React.FC = () => {
word,
});
const wordGeneratorRef = useRef<ReturnType<typeof getNextWord> | null>(null);
const sentences = [
"My name is John.",
"I am learning a new language.",
"Where is the nearest restaurant?",
"What time is it?",
"How are you doing today?",
"Please help me with this task.",
"I am from Kathmandu.",
"The weather is nice today.",
"I like to read books.",
"Can you please give me directions?",
];

useEffect(() => {
updateStreak(); // Trigger streak update on flashcard page load
wordGeneratorRef.current = getNextWord(selectedLanguage || "newari");
}, [selectedLanguage]);

const getOptions = (word: string) => {
const randomWords = generate({ exactly: 3 }) as string[];
let randomWords;

if (word.includes(" ")) {
// If the word is a sentence, generate options from sentences
randomWords = sentences
.filter((sentence) => sentence !== word)
.sort(() => 0.5 - Math.random())
.slice(0, 3);
} else {
// Otherwise, generate options from random words
randomWords = generate({ exactly: 3 }) as string[];
}

randomWords.push(word);
const shuffledOptions = randomWords.sort(() => Math.random() - 0.5);
setOptions(shuffledOptions);
};

const handleNextQuestion = async () => {
const generator = await wordGeneratorRef.current;
if (generator) {
const nextWord = generator?.next()?.value;

if (typeof nextWord === "string") {
setWord(nextWord);
setSelectedOption(null);
setIsCorrect(null);
getOptions(nextWord);
} else {
console.error("Generated word is not a string.");
let nextWord;
if (!isCorrect) {
nextWord = word;
} else {
const generator = await wordGeneratorRef.current;
if (generator) {
nextWord = generator?.next()?.value;
setWordIndex(wordIndex + 1);
}
}

if (typeof nextWord === "string") {
setWord(nextWord);
setSelectedOption(null);
setIsCorrect(null);
getOptions(nextWord);
} else {
console.error("Word generator not initialized.");
console.error("Generated word is not a string.");
}
};

Expand All @@ -75,8 +103,17 @@ const TestYourself: React.FC = () => {
return (
<div>
<Header />
<div className="flex items-center justify-center min-h-screen bg-black text-white font-primary">
<div className="p-2 max-w-xl w-full text-center mb-8">
<div className="flex flex-col items-center justify-center w-full mt-4">
<h2 className="text-3xl font-semibold text-primary">
Section - <span className="text-white">Introductions</span>
</h2>
<p className="text-2xl text-green-500 mt-3">
Progress: <span className="text-white">{wordIndex}</span> out of{" "}
<span className="text-white">{24}</span>
</p>
</div>
<div className="flex justify-center bg-black text-white font-primary">
<div className="p-2 max-w-xl w-full text-center mt-20">
<h2 className="text-4xl font-primary font-bold mb-6">
What is this word in English?
</h2>
Expand Down Expand Up @@ -109,7 +146,7 @@ const TestYourself: React.FC = () => {
</p>
) : (
<p className="text-2xl text-red-600">
Incorrect. The correct answer is {word}.
Incorrect. Please try again!
</p>
)}
<Button className="mt-6 text-2xl" onClick={handleNextQuestion}>
Expand Down
Loading