-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_system.py
36 lines (27 loc) · 932 Bytes
/
log_system.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import os
import re
LOG_FILE = "logs.txt"
async def apply_filter(text):
filtered_text = re.sub(r"[^a-z0-9\s,.!?']", '', text.lower())
return filtered_text
async def store_log(prompt, response, username):
log_str = prompt + f'{response}"'
with open(LOG_FILE, 'a') as file:
file.write(log_str)
async def retrieve_logs(amount, users):
if not os.path.exists(LOG_FILE):
return {user: [] for user in users}
with open(LOG_FILE, 'r') as file:
logs = file.readlines()
user_logs = {user: [] for user in users}
for log in logs:
pattern = r'\b(\w+)\b(?=\s+said)'
matches = re.findall(pattern, log)
username = "unknown"
if matches:
username = matches[0]
if username in users:
user_logs[username].append(log.strip())
for user in user_logs:
user_logs[user] = user_logs[user][-amount:]
return user_logs