-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
29 lines (21 loc) · 1.03 KB
/
functions.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
from scipy import spatial
def get_sim(i,j):
return 1 - spatial.distance.cosine(i,j)
def add_user(key,col,redisClient,posted):
''' Getting all users' embeddings '''
allCollections = list(col.find())
''' Getting embedding for given user_id from Collection '''
cur_doc = list(col.find({ 'user_id' : key }))[0]
embed = cur_doc['user_embed']
for doc in allCollections:
if redisClient.exists(doc['user_id']+'_post') == False:
if posted.find({'user_id':doc['user_id']}).count()>0:
redisClient.mset({doc['user_id']+'_post':'1'})
else:
redisClient.mset({doc['user_id']+'_post':'0'})
if key != doc['user_id'] and redisClient.get(doc['user_id']+'_post') == '1':
emb = doc['user_embed']
sim = get_sim(embed,emb)
if cur_doc.get('node_embed',-1)!=-1 and doc.get('node_embed',-1)!=-1:
sim = 0.6*sim + 0.4*get_sim(cur_doc['node_embed'],doc['node_embed'])
redisClient.zadd(key,{doc['user_id']:float(sim)})