diff --git a/python/__main__.py b/python/__main__.py index cdcbf7f6..e93fc6f1 100644 --- a/python/__main__.py +++ b/python/__main__.py @@ -52,6 +52,7 @@ def __init__( (patterns.REMOVE_GITHUB_PROFILE, lambda: self.commands.change_github_profile(False)), (patterns.KARMA, self.commands.karma_message), + (patterns.CENSORED_WORDS_RATING, self.commands.censored_words_rating_message), (patterns.TOP, self.commands.top), (patterns.PEOPLE, self.commands.top), (patterns.BOTTOM, diff --git a/python/config.py b/python/config.py index 1613aec9..f76d4564 100644 --- a/python/config.py +++ b/python/config.py @@ -155,3 +155,13 @@ DEFAULT_PROGRAMMING_LANGUAGES_PATTERN_STRING = "|".join(DEFAULT_PROGRAMMING_LANGUAGES) GITHUB_COPILOT_LANGUAGES_PATTERN_STRING = "|".join([i for i in GITHUB_COPILOT_LANGUAGES.keys()]) + +# Censored words for rating system +CENSORED_WORDS = [ + "блядь", "бляд", "сука", "хуй", "пизда", "ебать", "ебаный", "хуйня", "сраный", + "говно", "дерьмо", "дебил", "идиот", "урод", "тупой", "долбоеб", "мудак", + "ублюдок", "гнида", "сволочь", "падла", "засранец", "говнюк", "пидор", + # English words + "fuck", "fucking", "shit", "bitch", "asshole", "damn", "crap", "bastard", + "stupid", "idiot", "moron", "dickhead", "motherfucker", "bullshit" +] diff --git a/python/modules/commands.py b/python/modules/commands.py index 93d99817..588b412c 100644 --- a/python/modules/commands.py +++ b/python/modules/commands.py @@ -119,6 +119,26 @@ def karma_message(self) -> NoReturn: self.vk_instance.send_msg( CommandsBuilder.build_karma(self.user, self.data_service, is_self), self.peer_id) + + def censored_words_rating_message(self) -> NoReturn: + """Shows user's censored words rating.""" + is_self = self.user.uid == self.from_id + rating = self.user.censored_words_rating + user_name = self.vk_instance.get_user_name(self.user.uid, "gen") + + if is_self: + message = f"Ваш рейтинг использования цензурных слов: {rating}" + else: + message = f"Рейтинг использования цензурных слов у {user_name}: {rating}" + + if rating > 0: + message += "\n✅ Превосходно! Вы общаетесь культурно." + elif rating == 0: + message += "\n⚖️ Нейтральный рейтинг." + else: + message += "\n❌ Рекомендуется следить за речью." + + self.vk_instance.send_msg(message, self.peer_id) def top( self, @@ -308,6 +328,36 @@ def apply_user_karma( user.karma = new_karma return (user.uid, user.name, initial_karma, new_karma) + def process_censored_words_rating(self) -> NoReturn: + """Process message for censored words and update rating.""" + if not self.current_user or self.from_id < 0: + return + + # Convert message to lowercase for case-insensitive matching + message_lower = self.msg.lower() + + # Count censored words in the message + censored_count = 0 + for word in config.CENSORED_WORDS: + # Simple word boundary check to avoid partial matches + import re + pattern = r'\b' + re.escape(word.lower()) + r'\b' + censored_count += len(re.findall(pattern, message_lower)) + + # Calculate rating change: +1 for clean message, -1 for each censored word + if censored_count == 0: + rating_change = 1 # +1 for clean message + else: + rating_change = -censored_count # -1 for each censored word + + # Update user's censored words rating + current_rating = self.current_user.censored_words_rating + new_rating = current_rating + rating_change + self.current_user.censored_words_rating = new_rating + + # Save the updated user + self.data_service.save_user(self.current_user) + def what_is(self) -> NoReturn: """Search on wikipedia and sends if available""" question = self.matched.groups() @@ -435,4 +485,9 @@ def process( self.match_command(cmd) if self.matched: action() + # Process censored words rating for all messages including commands + self.process_censored_words_rating() return + + # Process censored words rating for non-command messages + self.process_censored_words_rating() diff --git a/python/modules/data_service.py b/python/modules/data_service.py index 45a0faeb..56076fdc 100644 --- a/python/modules/data_service.py +++ b/python/modules/data_service.py @@ -19,6 +19,7 @@ def __init__(self, db_name: str = "users"): self.base.addPattern("supporters", []) self.base.addPattern("opponents", []) self.base.addPattern("karma", 0) + self.base.addPattern("censored_words_rating", 0) def get_or_create_user( self, diff --git a/python/patterns.py b/python/patterns.py index 1834c72c..dd68c212 100644 --- a/python/patterns.py +++ b/python/patterns.py @@ -18,6 +18,9 @@ KARMA = recompile( r'\A\s*(карма|karma)\s*\Z', IGNORECASE) +CENSORED_WORDS_RATING = recompile( + r'\A\s*(рейтинг|rating|цензура|censored)\s*\Z', IGNORECASE) + APPLY_KARMA = recompile( r'\A(\[id(?\d+)\|@\w+\])?\s*(?P\+|\-)(?P[0-9]*)\s*\Z') diff --git a/python/tests.py b/python/tests.py index 4be4241e..659ec264 100644 --- a/python/tests.py +++ b/python/tests.py @@ -222,6 +222,48 @@ def test_apply_karma_change( self.commands.apply_karma_change('-', 6) self.commands.karma_message() + @ordered + def test_censored_words_rating_clean_message( + self + ) -> NoReturn: + """Test rating for clean message (+1)""" + self.commands.msg = "Hello, this is a clean message" + self.commands.current_user = db.get_user(1) + initial_rating = self.commands.current_user.censored_words_rating + + self.commands.process_censored_words_rating() + + # Should get +1 for clean message + assert self.commands.current_user.censored_words_rating == initial_rating + 1 + + @ordered + def test_censored_words_rating_with_censored_words( + self + ) -> NoReturn: + """Test rating for message with censored words (-1 per word)""" + self.commands.msg = "This is stupid shit" + self.commands.current_user = db.get_user(2) + initial_rating = self.commands.current_user.censored_words_rating + + self.commands.process_censored_words_rating() + + # Should get -2 for two censored words (stupid, shit) + assert self.commands.current_user.censored_words_rating == initial_rating - 2 + + @ordered + def test_censored_words_rating_message_display( + self + ) -> NoReturn: + """Test censored words rating message display""" + self.commands.user = db.get_user(1) + self.commands.from_id = 1 + self.commands.censored_words_rating_message() + + # Test for another user + self.commands.user = db.get_user(2) + self.commands.from_id = 1 + self.commands.censored_words_rating_message() + if __name__ == '__main__': db = BetterBotBaseDataService("test_db")