Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions python/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions python/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
55 changes: 55 additions & 0 deletions python/modules/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
1 change: 1 addition & 0 deletions python/modules/data_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions python/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(?<selectedUserId>\d+)\|@\w+\])?\s*(?P<operator>\+|\-)(?P<amount>[0-9]*)\s*\Z')

Expand Down
42 changes: 42 additions & 0 deletions python/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading