-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcoherence_manager.py
36 lines (27 loc) · 1.05 KB
/
coherence_manager.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
from sklearn.metrics.pairwise import cosine_similarity
class CoherenceManager:
def __init__(self, verbose=False):
self.verbose = verbose
if self.verbose:
print("Init %s" % self.__class__.__name__)
def compute_cost(self, user, item, reviewer):
raise NotImplementedError()
class DummyCoherenceManager(CoherenceManager):
def compute_cost(self, user, item, reviewer):
"""Dummy cost = 1"""
return 1
class ContextualizedCoherenceManager(CoherenceManager):
def __init__(self, preference, verbose=False):
super().__init__(verbose)
self.preference = preference
def compute_cost(self, user, item, reviewer):
user_aspect_vector = self.preference.get_aspect_vector(user, item).reshape(
1, -1
)
reviewer_aspect_vector = self.preference.get_aspect_vector(
reviewer, item
).reshape(1, -1)
score = (
1 + cosine_similarity(user_aspect_vector, reviewer_aspect_vector).sum()
) / 2
return abs(1 - score)