refactor(vocab): standardize text normalization methods in VocabService and VocabLessonView
All checks were successful
Deploy to production / deploy (push) Successful in 3m1s

- 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.
This commit is contained in:
Torsten Schulz (local)
2026-04-01 14:56:39 +02:00
parent 0421b2bc00
commit 7fee9e12d4
2 changed files with 11 additions and 4 deletions

View File

@@ -345,7 +345,7 @@ export default class VocabService {
return String(text || '') return String(text || '')
.trim() .trim()
.toLowerCase() .toLowerCase()
.replace(/[.,!?;:¿¡"]/g, '') .replace(/[\p{P}]+/gu, ' ')
.replace(/\s+/g, ' '); .replace(/\s+/g, ' ');
} }
@@ -2689,7 +2689,7 @@ export default class VocabService {
const correctTexts = correctIndices const correctTexts = correctIndices
.map((i) => options[i]) .map((i) => options[i])
.filter((opt) => opt !== undefined && opt !== null); .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 // Nach zufälligen Distraktoren: Client sendet gewählten Optionstext statt Index
if (typeof userAnswer === 'string') { if (typeof userAnswer === 'string') {

View File

@@ -2041,7 +2041,7 @@ export default {
const totalSlots = options.length; const totalSlots = options.length;
const need = totalSlots - correctTexts.length; const need = totalSlots - correctTexts.length;
if (need <= 0) return null; 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 correctSet = new Set(correctTexts.map(norm));
const poolRaw = side === 'target' ? this.distractorPool.target : this.distractorPool.native; const poolRaw = side === 'target' ? this.distractorPool.target : this.distractorPool.native;
let poolFiltered = poolRaw.filter((w) => w && !correctSet.has(norm(w))); let poolFiltered = poolRaw.filter((w) => w && !correctSet.has(norm(w)));
@@ -2700,8 +2700,15 @@ export default {
this.checkVocabAnswer(); this.checkVocabAnswer();
} }
}, },
normalizeComparableText(value) {
return String(value || '')
.trim()
.toLowerCase()
.replace(/[\p{P}]+/gu, ' ')
.replace(/\s+/g, ' ');
},
normalizeVocab(s) { normalizeVocab(s) {
return String(s || '').trim().toLowerCase().replace(/\s+/g, ' '); return this.normalizeComparableText(s);
}, },
checkVocabAnswer() { checkVocabAnswer() {
if (!this.currentVocabQuestion) return; if (!this.currentVocabQuestion) return;