Janet/JanetBackEnd/User.py

40 lines
1.6 KiB
Python
Raw Normal View History

2023-03-27 07:35:28 +02:00
import pandas as pd
import os
class User:
def __init__(self, username, token, num_interests=3, directory='./', interests_file='interests.json'):
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:
self.interests = self.interests.append({'interest': topic, 'frequency': max(
self.interests['frequency']) if not self.interests.empty else 6}, ignore_index=True)
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