diff --git a/ttstoy/ttstoy.py b/ttstoy/ttstoy.py index 168a047..c52688a 100644 --- a/ttstoy/ttstoy.py +++ b/ttstoy/ttstoy.py @@ -2887,8 +2887,77 @@ class TtsToy(Cog): content = content.strip() if not content: return None + # Skip spam/nonsense (keyboard mash, long single-char/word repeats) so + # TTS does not read out garbage. + if self._access_is_nonsense(content): + return None return content + @staticmethod + def _access_is_nonsense(text: str) -> bool: + """Heuristic: is this message spam/mash not worth reading aloud? + + Conservative by design - it must not eat short real messages ("lol", + "ok", "brb", "no u"). It only fires on clear garbage: + - long runs of one repeated character ("aaaaaaaaaa", "!!!!!!!!") + - a short token repeated many times ("lol lol lol lol lol ...") + - longer alphabetic text with almost no vowels (keyboard mash) + - very low character variety over a long message + """ + t = text.strip() + if len(t) < 8: + return False # too short to judge; let it through + + low = t.lower() + + # 1) One character repeated for most of the message (e.g. "aaaaaa", + # "!!!!!!", "wwwwwwww"). Allow some noise: >=85% one char. + stripped = re.sub(r"\s", "", low) + if stripped: + most = max(stripped.count(c) for c in set(stripped)) + if len(stripped) >= 10 and most / len(stripped) >= 0.85: + return True + + # 2) A single short token repeated many times ("lol lol lol ...", + # "haha haha haha ..."). Requires >=5 repeats of the same token. + tokens = low.split() + if len(tokens) >= 5: + uniq = set(tokens) + if len(uniq) == 1 and len(tokens) >= 5: + return True + # dominant-token spam: one token is >=80% of many tokens + if len(tokens) >= 8: + top = max(tokens.count(x) for x in uniq) + if top / len(tokens) >= 0.8: + return True + + # 3) Keyboard mash: a long, mostly-alphabetic run with almost no + # vowels. Only judge the longest whitespace-free run so a normal + # sentence (which has spaces and vowels) is never caught. + runs = re.findall(r"[a-z]+", low) + longest = max(runs, key=len) if runs else "" + if len(longest) >= 12: + vowels = sum(longest.count(v) for v in "aeiou") + if vowels / len(longest) < 0.12: # <12% vowels over 12+ letters + return True + + # 4) Very low overall character variety on a long message (e.g. + # "asdasdasdasdasdasd"). >=20 chars with <=4 distinct non-space. + if len(stripped) >= 20 and len(set(stripped)) <= 4: + return True + + # 5) A short unit repeated to fill the message ("asdasdasd", + # "abcabcabcabc"). Only fires on a 12+ char whitespace-free run whose + # smallest repeating period is <=4 and repeats >=4 times, so real + # words are never caught. + if len(stripped) >= 12: + for period in range(1, 5): + unit = stripped[:period] + if period and len(stripped) // period >= 4 and unit * (len(stripped) // period) == stripped[: period * (len(stripped) // period)] and len(stripped) % period == 0: + return True + + return False + @commands.Cog.listener("on_message") async def _accessibility_autoread(self, message: discord.Message): try: