TTS blocklist: starter list of slurs/TOS phrases, whole-word matching

- src/tts-blocklist.txt: slurs, self-harm/threat phrases, sexual violence,
  hate slogans (no normal swearing); merged into Settings once
  (tts_blocklist_seeded), editable there afterwards
- Matching is now whole words/phrases with plurals, look-alike swaps and
  spaced-out letters, so raccoon / spicy / therapist / grape are fine

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Bashy 2026-11-21 15:09:12 +02:00
parent cf90b6be54
commit 9e356b0875
5 changed files with 90 additions and 11 deletions

View file

@ -87,7 +87,15 @@ function cleanText(text) {
// Returns { text, blocked }. blocked = a blocklist word was found: do not read anything.
const LEET = { 0: 'o', 1: 'i', 3: 'e', 4: 'a', 5: 's', 7: 't', '@': 'a', $: 's', '!': 'i' };
function parseBlocklist(list) {
return String(list || '').split(/[\n,]/).map(w => w.trim().toLowerCase()).filter(Boolean);
return String(list || '').split(/[\n,]/).map(w => w.trim().toLowerCase()).filter(w => w && !w.startsWith('#'));
}
// lowercase, look-alike swaps, spaced-out letters joined ("k y s" -> "kys"), only letters/digits, single spaces
function normalizeForMatch(text) {
return ' ' + String(text).toLowerCase()
.replace(/[013457@$!]/g, c => LEET[c] || c)
.replace(/(^|[^a-z0-9\u00c0-\u024f])((?:[a-z][\s.\-_*]+){2,}[a-z])(?![a-z])/g, (_, pre, letters) => pre + letters.replace(/[^a-z]/g, ''))
.replace(/([a-z])\1{2,}/g, '$1') // niiiice -> nice
.replace(/[^a-z0-9\u00c0-\u024f]+/g, ' ').trim() + ' ';
}
function prepareViewerText(input, { maxChars = 200, blocklist = '' } = {}) {
let t = cleanText(input)
@ -98,13 +106,12 @@ function prepareViewerText(input, { maxChars = 200, blocklist = '' } = {}) {
.replace(/\s+/g, ' ').trim();
const max = Math.max(20, Math.min(MAX_CHARS, maxChars | 0 || 200));
if (t.length > max) t = t.slice(0, max).replace(/\s+\S*$/, '') + '…';
const plain = t.toLowerCase().replace(/[013457@$!]/g, c => LEET[c] || c);
const words = ' ' + plain.replace(/[^a-z0-9\u00c0-\u024f]+/g, ' ') + ' ';
const squashed = plain.replace(/[^a-z\u00c0-\u024f]+/g, '');
// whole words/phrases only (so "raccoon", "spicy", "therapist" are fine), plurals included
const hay = normalizeForMatch(t);
const blocked = parseBlocklist(blocklist).some(w => {
const ww = w.replace(/[013457@$!]/g, c => LEET[c] || c);
return ww.length >= 4 ? squashed.includes(ww.replace(/[^a-z\u00c0-\u024f]+/g, '')) // longer words: also when spaced out
: words.includes(' ' + ww.replace(/[^a-z0-9\u00c0-\u024f]+/g, ' ').trim() + ' ');
const term = normalizeForMatch(w).trim();
if (!term) return false;
return new RegExp(` ${term.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}(?:s|es|z)? `).test(hay);
});
return { text: t, blocked };
}