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:
reply_to_msg_id = int(latest_toxic_message_id)
# count toxicity
# count one msg toxicity
if reply_to_msg_id:
# count one message score
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:
logger.debug(one_score)
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:
emoji = (
"😳"
if toxic_score > 90
else "😟"
if toxic_score > 80
else "😏"
if toxic_score > 60
else "🙂"
if toxic_score > 20
else "😇"
)
reply_text += (
f"Средняя токсичность сообщений: {toxic_score}% {emoji}"
)
except Exception:
pass
finally:
if reply_text:
await telegram_api(
"sendMessage",
chat_id=cid,
reply_to_message_id=reply_to_msg_id,
text=reply_text,
)
# count overall toxycity
try:
toxic_pattern = f"toxic:{cid}:{uid}:*"
toxic_score = await get_average_pattern(toxic_pattern)
if toxic_score:
emoji = (
"😳"
if toxic_score > 90
else "😟"
if toxic_score > 80
else "😏"
if toxic_score > 60
else "🙂"
if toxic_score > 20
else "😇"
)
reply_text += (
f"Средняя токсичность сообщений: {toxic_score}% {emoji}"
)
except Exception:
pass
if reply_text:
await telegram_api(
"sendMessage",
chat_id=cid,
reply_to_message_id=reply_to_msg_id,
text=reply_text,
)
try:
await telegram_api("deleteMessage", chat_id=cid, message_id=mid)
except Exception:

View File

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