47 lines
782 B
Python
47 lines
782 B
Python
from pathlib import Path
|
||
|
||
|
||
def normalize_word(word: str) -> str:
|
||
return word.strip().lower().replace("", "").replace("ё", "е")
|
||
|
||
|
||
class ProfanityDictionary:
|
||
|
||
def __init__(
|
||
self,
|
||
file_path: str | Path
|
||
):
|
||
|
||
self.words = self.load(file_path)
|
||
|
||
def load(
|
||
self,
|
||
file_path: str | Path
|
||
) -> frozenset[str]:
|
||
|
||
path = Path(file_path)
|
||
|
||
if not path.exists():
|
||
raise FileNotFoundError(
|
||
f"Profanity dictionary not found: {file_path}"
|
||
)
|
||
|
||
with open(
|
||
path,
|
||
"r",
|
||
encoding="utf-8"
|
||
) as file:
|
||
|
||
return frozenset(
|
||
normalize_word(line)
|
||
for line in file
|
||
if normalize_word(line)
|
||
)
|
||
|
||
def contains(
|
||
self,
|
||
word: str
|
||
) -> bool:
|
||
|
||
return word in self.words
|