JanetBackEnd/User.py

39 lines
1.6 KiB
Python
Raw Normal View History

2023-03-30 15:17:54 +02:00
import pandas as pd
import os
class User:
2023-04-08 22:51:44 +02:00
def __init__(self, username, token, num_interests=3, directory='/app/', interests_file='interests.json'):
2023-03-30 15:17:54 +02:00
self.username = username
self.token = token
self.num_interests = num_interests
self.interests_file = directory + username+'_'+interests_file
self.interests = pd.read_json(self.interests_file) if os.path.isfile(self.interests_file) else pd.DataFrame(columns=['interest', 'frequency']) # {'interest': 'frequency':}
def initialize(self):
if self.interests.empty:
self.interests = pd.DataFrame(columns=['interest', 'frequency'])
def update_interests(self, topics):
for topic in topics:
index = self.interests.index[self.interests['interest'] == topic]
if len(index) > 0:
self.interests.at[index[0], 'frequency'] += 1
else:
2023-04-04 05:34:47 +02:00
self.interests = self.interests.append({'interest': topic, 'frequency': max(self.interests['frequency']) if not self.interests.empty else 6}, ignore_index=True)
2023-03-30 15:17:54 +02:00
self.interests = self.interests.sort_values(by='frequency', ascending=False, ignore_index=True)
self.interests.to_json(self.interests_file)
def decay_interests(self):
for i, interest in self.interests.iterrows():
if interest['frequency'] > 1:
self.interests.at[i, 'frequency'] -= 1
def get_user_interests(self):
current_interests = []
for i, row in self.interests.iterrows():
if i < self.num_interests:
current_interests.append(row['interest'])
return current_interests