less-norm-2

This commit is contained in:
Untone 2024-09-27 14:14:51 +03:00
parent 5606c69035
commit 98b842ef18
2 changed files with 53 additions and 43 deletions

View File

@ -49,7 +49,7 @@ async def messages_routing(msg, state):
if not reply_to_msg_id and latest_toxic_message_id: if not reply_to_msg_id and latest_toxic_message_id:
reply_to_msg_id = int(latest_toxic_message_id) reply_to_msg_id = int(latest_toxic_message_id)
# count toxicity # count one msg toxicity
if reply_to_msg_id: if reply_to_msg_id:
# count one message score # count one message score
one_score = await redis.get(f"toxic:{cid}:{uid}:{reply_to_msg_id}") one_score = await redis.get(f"toxic:{cid}:{uid}:{reply_to_msg_id}")
@ -57,36 +57,36 @@ async def messages_routing(msg, state):
if one_score: if one_score:
logger.debug(one_score) logger.debug(one_score)
reply_text += f"{int(one_score)}% токсичности\n" reply_text += f"{int(one_score)}% токсичности\n"
try:
# count average between all of messages
toxic_pattern = f"toxic:{cid}:{uid}:*"
toxic_score = await get_average_pattern(toxic_pattern)
if toxic_score: # count overall toxycity
emoji = ( try:
"😳" toxic_pattern = f"toxic:{cid}:{uid}:*"
if toxic_score > 90 toxic_score = await get_average_pattern(toxic_pattern)
else "😟"
if toxic_score > 80 if toxic_score:
else "😏" emoji = (
if toxic_score > 60 "😳"
else "🙂" if toxic_score > 90
if toxic_score > 20 else "😟"
else "😇" if toxic_score > 80
) else "😏"
reply_text += ( if toxic_score > 60
f"Средняя токсичность сообщений: {toxic_score}% {emoji}" else "🙂"
) if toxic_score > 20
except Exception: else "😇"
pass )
finally: reply_text += (
if reply_text: f"Средняя токсичность сообщений: {toxic_score}% {emoji}"
await telegram_api( )
"sendMessage", except Exception:
chat_id=cid, pass
reply_to_message_id=reply_to_msg_id, if reply_text:
text=reply_text, await telegram_api(
) "sendMessage",
chat_id=cid,
reply_to_message_id=reply_to_msg_id,
text=reply_text,
)
try: try:
await telegram_api("deleteMessage", chat_id=cid, message_id=mid) await telegram_api("deleteMessage", chat_id=cid, message_id=mid)
except Exception: except Exception:

View File

@ -9,15 +9,18 @@ logger = logging.getLogger("state.scan")
async def get_all_pattern(uid): async def get_all_pattern(uid):
pattern = f"removed:{uid}:*" pattern = f"removed:{uid}:*"
# Create a dictionary to hold the keys and values # Create a list to hold the values
texts = [] texts = []
# Use scan_iter to find all keys matching the pattern # Use scan_iter to find all keys matching the pattern
async for key in redis.scan_iter(pattern): async for key in redis.scan_iter(pattern):
# Fetch the value for each key try:
value = await redis.get(key) # Fetch the value for each key
if value: value = await redis.get(key)
texts.append(value.decode("utf-8")) if value:
texts.append(value.decode("utf-8"))
except Exception as e:
logger.error(f"Error fetching key {key}: {e}")
return texts return texts
@ -25,15 +28,22 @@ async def get_all_pattern(uid):
async def get_average_pattern(pattern): async def get_average_pattern(pattern):
scores = [] scores = []
toxic_score = 0 toxic_score = 0
try: try:
async for key in redis.scan_iter(pattern): async for key in redis.scan_iter(pattern):
scr = await redis.get(str(key)) scr = await redis.get(key)
scr = int(scr)
if isinstance(scr, int): # Ensure scr is not None before converting to int
scores.append(scr) if scr is not None:
logger.debug(f"found {len(scores)} messages") try:
score = int(scr) # Convert to integer
scores.append(score)
except ValueError:
logger.warning(f"Value for key {key} is not an integer: {scr}")
logger.debug(f"Found {len(scores)} messages")
toxic_score = math.floor(sum(scores) / len(scores)) if scores else 0 toxic_score = math.floor(sum(scores) / len(scores)) if scores else 0
except Exception: except Exception as e:
pass logger.error(f"Error while calculating average: {e}")
finally: finally:
return toxic_score return toxic_score