JanetBackEnd/Recommender.py

60 lines
2.7 KiB
Python
Raw Permalink Normal View History

2023-03-30 15:17:54 +02:00
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
import random
class Recommender:
def __init__(self, retriever):
2023-04-17 06:59:02 +02:00
self.curr_recommendations = {}
self.recommended = {}
2023-03-30 15:17:54 +02:00
self.retriever = retriever
self.rand_seed = 5
2023-04-17 09:45:22 +02:00
def _new(self, username, material):
2023-04-18 11:37:56 +02:00
if username not in self.curr_recommendations:
2023-04-17 06:59:02 +02:00
return True
for row in self.curr_recommendations[username]:
2023-04-09 22:43:13 +02:00
if row['id'] == material['id'] and row['type'] == material['type']:
return False
return True
2023-04-17 06:59:02 +02:00
def _match_tags(self, username, material, interest):
2023-03-30 15:17:54 +02:00
score = 0.7
for tag in material['tags']:
if cosine_similarity(np.array(self.retriever.encode([tag])),
np.array(self.retriever.encode([interest]))) > score:
2023-04-17 09:45:22 +02:00
if self._new(username, material):
2023-04-18 13:13:36 +02:00
#print('hi')
if username in self.curr_recommendations:
self.curr_recommendations[username].append(material)
self.recommended[username].append(False)
else:
self.curr_recommendations[username] = [material]
self.recommended[username] = [False]
2023-03-30 15:17:54 +02:00
2023-04-17 06:59:02 +02:00
def generate_recommendations(self, username, interests, new_material):
2023-03-30 15:17:54 +02:00
for interest in interests:
2023-04-09 22:43:13 +02:00
for i, material in new_material.iterrows():
2023-04-17 06:59:02 +02:00
self._match_tags(username, material, interest)
2023-03-30 15:17:54 +02:00
2023-04-18 02:59:58 +02:00
def make_recommendation(self, username, name):
2023-04-17 06:59:02 +02:00
if len(self.curr_recommendations[username]) == 0:
2023-03-30 15:17:54 +02:00
return ""
2023-04-17 06:59:02 +02:00
to_consider = [idx for idx, value in enumerate(self.recommended[username]) if value == False]
2023-04-09 22:43:13 +02:00
if len(to_consider) == 0:
return ""
index = random.choice(list(range(0, len(to_consider))))
2023-04-17 06:59:02 +02:00
index = self.recommended[username][index]
2023-04-09 22:43:13 +02:00
#while self.recommended[index] == True:
# index = random.choice(list(range(0, len(self.curr_recommendations))))
2023-04-18 02:59:58 +02:00
recommendation = "Hey " + name + "! This " + self.curr_recommendations[username][index][
2023-03-30 15:17:54 +02:00
'type'].lower() + " about " + ', '.join(
2023-04-17 06:59:02 +02:00
self.curr_recommendations[username][index]['tags']).lower() + " was posted recently by " + \
self.curr_recommendations[username][index][
2023-03-30 15:17:54 +02:00
'author'].lower() + " on the catalogue. You may wanna check it out! It is titled " + \
2023-04-17 06:59:02 +02:00
self.curr_recommendations[username][index]['title'].lower() + ". Cheers, Janet"
2023-03-30 15:17:54 +02:00
# self.curr_recommendations.remove(self.curr_recommendations[index])
2023-04-17 06:59:02 +02:00
self.recommended[username][index] = True
2023-03-30 15:17:54 +02:00
return recommendation