From 70fc0112439895655e0aae0ecdfbd00fb2920e78 Mon Sep 17 00:00:00 2001 From: NancyAanchal Date: Fri, 16 Aug 2024 20:46:49 +0545 Subject: [PATCH] sentencify-mvp --- nepalingo-web/src/lib/getNextWord.ts | 36 +++++++++-- nepalingo-web/src/pages/TestYourself.tsx | 79 +++++++++++++++++------- 2 files changed, 90 insertions(+), 25 deletions(-) diff --git a/nepalingo-web/src/lib/getNextWord.ts b/nepalingo-web/src/lib/getNextWord.ts index b5ce530..052131f 100644 --- a/nepalingo-web/src/lib/getNextWord.ts +++ b/nepalingo-web/src/lib/getNextWord.ts @@ -1,6 +1,6 @@ export const newariWords = [ - "hello", "call", + "can", "do", "how", "I", @@ -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 { const wordText = await fetch("./dictionaries/TajpuriyaDictionary.csv") .then((r) => r.text()) @@ -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(); } diff --git a/nepalingo-web/src/pages/TestYourself.tsx b/nepalingo-web/src/pages/TestYourself.tsx index 474832c..b610026 100644 --- a/nepalingo-web/src/pages/TestYourself.tsx +++ b/nepalingo-web/src/pages/TestYourself.tsx @@ -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([ + "hello", + "bye", + "no", "today", - "rice", - "hot", - "salt", ]); const [selectedOption, setSelectedOption] = useState(null); const [isCorrect, setIsCorrect] = useState(null); @@ -25,6 +25,18 @@ const TestYourself: React.FC = () => { word, }); const wordGeneratorRef = useRef | 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 @@ -32,27 +44,43 @@ const TestYourself: React.FC = () => { }, [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."); } }; @@ -75,8 +103,17 @@ const TestYourself: React.FC = () => { return (
-
-
+
+

+ Section - Introductions +

+

+ Progress: {wordIndex} out of{" "} + {24} +

+
+
+

What is this word in English?

@@ -109,7 +146,7 @@ const TestYourself: React.FC = () => {

) : (

- Incorrect. The correct answer is {word}. + Incorrect. Please try again!

)}