refactor(vocabService, VocabLessonView): unify answer language classification for multiple-choice questions
All checks were successful
Deploy to production / deploy (push) Successful in 2m51s
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:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user