import { allWords, type Word } from "@/data/vocab"; export interface DrillQ { id: string; prompt: string; hintAr: string; options: string[]; answer: number; explain?: string; speak?: string; } function shuffle(a: T[]): T[] { const b = [...a]; for (let i = b.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [b[i], b[j]] = [b[j], b[i]]; } return b; } function withOptions(correct: string, wrong: string[]): { options: string[]; answer: number } { const options = shuffle([correct, ...shuffle([...new Set(wrong.filter((w) => w !== correct))]).slice(0, 3)]); return { options, answer: options.indexOf(correct) }; } const escape = (s: string) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); const isSimple = (w: Word) => !/[\s()…/]/.test(w.de); /** Lückentext: das Wort fehlt im Beispielsatz. */ export function gapFillQuestions(words: Word[]): DrillQ[] { const out: DrillQ[] = []; for (const w of words) { if (!isSimple(w)) continue; const re = new RegExp(`(^|[^A-Za-zÄÖÜäöüß])${escape(w.de)}(?=[^A-Za-zÄÖÜäöüß]|$)`); if (!re.test(w.ex)) continue; const sentence = w.ex.replace(re, (_m, pre: string) => `${pre}_____`); const sameKind = allWords.filter((x) => x.id !== w.id && isSimple(x) && Boolean(x.art) === Boolean(w.art) && x.de !== w.de); const sameTopic = sameKind.filter((x) => x.topic === w.topic); const pool = (sameTopic.length >= 3 ? sameTopic : sameKind).map((x) => x.de); const { options, answer } = withOptions(w.de, pool); out.push({ id: w.id, prompt: sentence, hintAr: w.exAr, options, answer, speak: w.ex, explain: `${w.art ? w.art + " " : ""}${w.de} = ${w.ar}` }); } return out; } const umlaut = (s: string) => { const m = s.match(/(au|a|o|u)(?=[^aouäöü]*$)/); if (!m || m.index === undefined) return s; const rep = m[1] === "au" ? "äu" : m[1] === "a" ? "ä" : m[1] === "o" ? "ö" : "ü"; return s.slice(0, m.index) + rep + s.slice(m.index + m[1].length); }; /** Pluralformen der Nomen. */ export function pluralQuestions(words: Word[]): DrillQ[] { const out: DrillQ[] = []; for (const w of words) { if (!w.art || !w.pl || !w.pl.startsWith("die ") || !isSimple(w)) continue; const correct = w.pl; const b = w.de.endsWith("e") ? w.de.slice(0, -1) : w.de; const candidates = [w.de, w.de + "e", w.de + "en", w.de + "n", w.de + "s", w.de + "er", umlaut(w.de) + "e", umlaut(w.de) + "er", umlaut(b) + "en", b + "en"].map((c) => `die ${c}`); const { options, answer } = withOptions(correct, candidates); if (options.length < 3) continue; out.push({ id: w.id, prompt: `Wie heißt der Plural von „${w.art} ${w.de}“?`, hintAr: `ما جمع كلمة «${w.ar}»؟`, options, answer, speak: correct }); } return out; } /** Perfekt mit „hat“ oder „ist“? */ export function perfektQuestions(words: Word[]): DrillQ[] { const out: DrillQ[] = []; for (const w of words) { if (w.art || !w.pl || !/^(hat|ist) /.test(w.pl)) continue; const [aux, ...rest] = w.pl.split(" "); const options = ["hat", "ist"]; out.push({ id: w.id, prompt: `${w.de} → er ___ ${rest.join(" ")}`, hintAr: `${w.ar} – هل يُصرَّف في الماضي التام مع hat أم ist؟`, options, answer: options.indexOf(aux), speak: `er ${w.pl}`, explain: aux === "ist" ? "„ist“: Bewegung / Veränderung (gehen, fahren, umziehen …)" : "„hat“: die meisten Verben", }); } return out; }