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,8 +57,9 @@ 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"
# count overall toxycity
try: try:
# count average between all of messages
toxic_pattern = f"toxic:{cid}:{uid}:*" toxic_pattern = f"toxic:{cid}:{uid}:*"
toxic_score = await get_average_pattern(toxic_pattern) toxic_score = await get_average_pattern(toxic_pattern)
@ -79,7 +80,6 @@ async def messages_routing(msg, state):
) )
except Exception: except Exception:
pass pass
finally:
if reply_text: if reply_text:
await telegram_api( await telegram_api(
"sendMessage", "sendMessage",

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):
try:
# Fetch the value for each key # Fetch the value for each key
value = await redis.get(key) value = await redis.get(key)
if value: if value:
texts.append(value.decode("utf-8")) 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