refactor(vocabService, VocabLessonView): unify answer language classification for multiple-choice questions
All checks were successful
Deploy to production / deploy (push) Successful in 2m51s

- Renamed and refactored the method for classifying answer languages in multiple-choice questions from _classifyMcQuestionSide to _resolveMcAnswerSide for clarity and consistency.
- Updated the logic to classify answer languages based on questionData.answerLanguage and answerLanguageId, improving accuracy in determining the correct language context.
- Adjusted VocabLessonView to utilize the new classification method, ensuring alignment with backend changes and enhancing the handling of distractor options based on answer language.
- Enhanced documentation to clarify the expected input and output for the new method, improving maintainability and understanding of the codebase.
This commit is contained in:
Torsten Schulz (local)
2026-04-07 08:34:19 +02:00
parent 07ab648143
commit d192bcae2d
2 changed files with 59 additions and 18 deletions

View File

@@ -3593,20 +3593,30 @@ export default class VocabService {
}
/**
* Ordnet eine Multiple-Choice-Frage der Zielsprache (zu lernen) oder Muttersprache (Erklärung) zu,
* damit Distraktoren aus dem passenden Wortpool gewählt werden können.
* Explizite Zuordnung der Antwortsprache (sprachneutral).
* questionData.answerLanguage: 'target' | 'native'
* oder questionData.answerLanguageId: 1 = target (Lernsprache), 2 = native (Muttersprache)
* Ohne diese Felder: 'unknown' (kein Eintrag in den Distraktor-Pools für diese Frage).
* @param {object} questionData
* @returns {'target'|'native'|'unknown'}
*/
_classifyMcQuestionSide(question) {
const q = String(question || '');
if (/Wie sagt man\s/i.test(q) || /Übersetze/i.test(q)) return 'target';
if (/Was bedeutet/i.test(q)) return 'native';
_resolveMcAnswerSide(questionData) {
if (!questionData || typeof questionData !== 'object') return 'unknown';
const raw = questionData.answerLanguage;
if (typeof raw === 'string') {
const s = raw.trim().toLowerCase();
if (s === 'target' || s === 'learning' || s === 'l2') return 'target';
if (s === 'native' || s === 'l1') return 'native';
}
const id = questionData.answerLanguageId;
if (id === 1 || id === '1') return 'target';
if (id === 2 || id === '2') return 'native';
return 'unknown';
}
/**
* Sammelt Vokabeln aus allen Multiple-Choice-Übungen von Lektionen **vor** der angegebenen Lektion
* (gleicher Kurs), getrennt nach Ziel- vs. Muttersprache anhand der Frageformulierung.
* (gleicher Kurs), getrennt nach Ziel- vs. Muttersprache anhand von answerLanguage / answerLanguageId.
*/
async getVocabDistractorPool(hashedUserId, courseId, beforeLessonId) {
if (!beforeLessonId) {
@@ -3659,10 +3669,9 @@ export default class VocabService {
for (const ex of exercises) {
const qd =
typeof ex.questionData === 'string' ? JSON.parse(ex.questionData) : ex.questionData;
const question = qd?.question || '';
const opts = qd?.options;
if (!Array.isArray(opts)) continue;
const side = this._classifyMcQuestionSide(question);
const side = this._resolveMcAnswerSide(qd);
if (side === 'target') {
opts.forEach((o) => target.add(String(o).trim()));
} else if (side === 'native') {

View File

@@ -2166,11 +2166,22 @@ export default {
}
return a;
},
/** Muss zu backend/services/vocabService _classifyMcQuestionSide passen */
_classifyMcQuestionSide(question) {
const q = String(question || '');
if (/Wie sagt man\s/i.test(q) || /Übersetze/i.test(q)) return 'target';
if (/Was bedeutet/i.test(q)) return 'native';
/**
* Welche Sprache haben die richtigen MC-Antworten? (Backend: vocabService._resolveMcAnswerSide)
* Nur über questionData.answerLanguage ('target'|'native') oder answerLanguageId (1|2).
* Ohne diese Felder: 'unknown' — dann kein Distraktor-Randomize (bis Inhalte annotiert sind).
*/
_resolveMcAnswerSide(questionData) {
const qd = questionData || {};
const raw = qd.answerLanguage;
if (typeof raw === 'string') {
const s = raw.trim().toLowerCase();
if (s === 'target' || s === 'learning' || s === 'l2') return 'target';
if (s === 'native' || s === 'l1') return 'native';
}
const id = qd.answerLanguageId;
if (id === 1 || id === '1') return 'target';
if (id === 2 || id === '2') return 'native';
return 'unknown';
},
/**
@@ -2181,8 +2192,7 @@ export default {
const q = this.getQuestionData(exercise);
const a = this.getAnswerData(exercise);
if (!q || !a || q.type !== 'multiple_choice' || q.randomizeDistractors === false) return null;
const qtext = q.question || '';
const side = this._classifyMcQuestionSide(qtext);
const side = this._resolveMcAnswerSide(q);
if (side === 'unknown') return null;
const options = q.options || [];
let correctIndices = [];
@@ -2697,12 +2707,30 @@ export default {
const answerField = direction === 'L2R' ? 'reference' : 'learning';
const allNativeNorm = new Set();
const allTargetNorm = new Set();
allVocabs.forEach((v) => {
if (v.learning) allNativeNorm.add(this.normalizeVocab(v.learning));
if (v.reference) allTargetNorm.add(this.normalizeVocab(v.reference));
});
/** Verhindert gemischte Sprachen in den Optionen (z. B. deutsche Wörter unter Zielsprachen-Distraktoren). */
const tokenMatchesAnswerLanguage = (token) => {
const n = this.normalizeVocab(token);
if (!n) return false;
if (direction === 'L2R') {
if (allNativeNorm.has(n) && !allTargetNorm.has(n)) return false;
} else {
if (allTargetNorm.has(n) && !allNativeNorm.has(n)) return false;
}
return true;
};
const allDistractors = [];
allVocabs.forEach((vocab) => {
const candidate = vocab[answerField];
if (!candidate || !candidate.trim()) return;
const normalized = this.normalizeVocab(candidate);
if (!normalizedExcludeSet.has(normalized)) {
if (!normalizedExcludeSet.has(normalized) && tokenMatchesAnswerLanguage(candidate)) {
allDistractors.push(candidate);
}
});
@@ -2731,7 +2759,11 @@ export default {
const candidate = vocab[answerField];
if (candidate && candidate.trim()) {
const normalized = this.normalizeVocab(candidate);
if (!normalizedExcludeSet.has(normalized) && !options.has(candidate)) {
if (
!normalizedExcludeSet.has(normalized) &&
!options.has(candidate) &&
tokenMatchesAnswerLanguage(candidate)
) {
options.add(candidate);
}
}