-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathn4j.py
63 lines (45 loc) · 1.65 KB
/
n4j.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from py2neo import Graph
class N4J:
graph = None
def __init__(self, url):
self.url = url
def get_graph(self):
if self.graph == None:
self.graph = Graph(self.url)
return self.graph
def is_fof(self, ourid1, ourid2):
"""
Returns boolean whether the two users are 2nd degree
"""
cursor = self.get_graph().run(
"OPTIONAL MATCH (user1 :User{ourid:{ourid1}})-[:FRIEND]-(yourFriends),"
"(yourFriends)-[:FRIEND]-(user2 :User{ourid:{ourid2}})"
"RETURN user2 IS NOT NULL as isConnected;"
, {"ourid1": ourid1, "ourid2": ourid2})
res = cursor.evaluate()
cursor.close()
return res
def is_fofof(self, ourid1, ourid2):
"""
Returns boolean whether the two users are connected through 2nd or 3rd degree
"""
cursor = self.get_graph().run(
"OPTIONAL MATCH (user1 :User{ourid:{ourid1}})-[:FRIEND*2..3]-(user2 :User{ourid:{ourid2}})"
"RETURN user2 IS NOT NULL as isConnected;"
, {"ourid1": ourid1, "ourid2": ourid2})
res = cursor.evaluate()
cursor.close()
return res
def get_2nd_or_3rd(self, ourid):
"""
Returns a list of 2nd and 3rd degree connections for the specific ourid
"""
cursor = self.get_graph().run(
"MATCH (:User{ourid:{ourid}})-[:FRIEND*2..3]-(user) RETURN user.ourid as ourid"
, {"ourid": ourid})
res = set()
while cursor.forward():
cur = cursor.current()
res.add(cur['ourid'])
cursor.close()
return list(res)