From 7fee9e12d4be384cae0a0c3c1ee5b334be3799a2 Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Wed, 1 Apr 2026 14:56:39 +0200 Subject: [PATCH] refactor(vocab): standardize text normalization methods in VocabService and VocabLessonView - Updated text normalization logic in VocabService to use a regex that replaces punctuation with spaces, improving text handling consistency. - Refactored VocabLessonView to utilize the new normalization method for comparable text, enhancing the accuracy of user input processing. - Ensured both components now share a unified approach to text normalization, streamlining code and improving maintainability. --- backend/services/vocabService.js | 4 ++-- frontend/src/views/social/VocabLessonView.vue | 11 +++++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/backend/services/vocabService.js b/backend/services/vocabService.js index 6ebbe0d..7ebde2a 100644 --- a/backend/services/vocabService.js +++ b/backend/services/vocabService.js @@ -345,7 +345,7 @@ export default class VocabService { return String(text || '') .trim() .toLowerCase() - .replace(/[.,!?;:¿¡"]/g, '') + .replace(/[\p{P}]+/gu, ' ') .replace(/\s+/g, ' '); } @@ -2689,7 +2689,7 @@ export default class VocabService { const correctTexts = correctIndices .map((i) => options[i]) .filter((opt) => opt !== undefined && opt !== null); - const norm = (s) => String(s).trim().toLowerCase(); + const norm = (s) => this._normalizeTextAnswer(s); // Nach zufälligen Distraktoren: Client sendet gewählten Optionstext statt Index if (typeof userAnswer === 'string') { diff --git a/frontend/src/views/social/VocabLessonView.vue b/frontend/src/views/social/VocabLessonView.vue index c9c270a..d94c24a 100644 --- a/frontend/src/views/social/VocabLessonView.vue +++ b/frontend/src/views/social/VocabLessonView.vue @@ -2041,7 +2041,7 @@ export default { const totalSlots = options.length; const need = totalSlots - correctTexts.length; if (need <= 0) return null; - const norm = (s) => String(s).trim().toLowerCase(); + const norm = (s) => this.normalizeComparableText(s); const correctSet = new Set(correctTexts.map(norm)); const poolRaw = side === 'target' ? this.distractorPool.target : this.distractorPool.native; let poolFiltered = poolRaw.filter((w) => w && !correctSet.has(norm(w))); @@ -2700,8 +2700,15 @@ export default { this.checkVocabAnswer(); } }, + normalizeComparableText(value) { + return String(value || '') + .trim() + .toLowerCase() + .replace(/[\p{P}]+/gu, ' ') + .replace(/\s+/g, ' '); + }, normalizeVocab(s) { - return String(s || '').trim().toLowerCase().replace(/\s+/g, ' '); + return this.normalizeComparableText(s); }, checkVocabAnswer() { if (!this.currentVocabQuestion) return;