Files
post-moderation/ai-moderation/app/moderation/profanity/detector.py
2026-09-07 00:13:55 +03:00

43 lines
745 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import re
from app.moderation.profanity.dictionary import ProfanityDictionary
from app.moderation.profanity.lemmatizer import Lemmatizer
class ProfanityDetector:
def __init__(
self,
dictionary: ProfanityDictionary,
lemmatizer: Lemmatizer
):
self.dictionary = dictionary
self.lemmatizer = lemmatizer
def detect(
self,
text: str
) -> list[str]:
words = re.findall(
r"[а-яА-ЯёЁ]+",
text.lower()
)
result = []
for word in words:
lemma = self.lemmatizer.normalize(word)
for bad_word in self.dictionary.words:
if lemma == bad_word or lemma.startswith(bad_word + "-"):
result.append(word)
break
return result