This repository has been archived by the owner on Aug 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoauthor2.py
102 lines (82 loc) · 2.13 KB
/
coauthor2.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import couchdb
import config
couchserver = couchdb.Server("http://%s:%s@%s/" % (config.user, config.password, config.server))
pubdb = couchserver["publications"]
persondb = couchserver["persons"]
def cache_search(name):
mango = {
'selector': {
'name': name
},
'fields': ['_id', 'co-authors'],
'limit': 1
}
res = list(persondb.find(mango))[0]
return res.get("co-authors")
def cache_results(name, co_authors):
mango = {
'selector': {
'name': name
},
'limit': 1
}
res = list(persondb.find(mango))[0]
res["co-authors"] = co_authors
persondb[res["_id"]] = res
def level_one(name):
cached_authors = cache_search(name)
if cached_authors is not None:
return cached_authors
mango = {
'selector': {
'name': name
},
'fields': ['author-of'],
'limit': 1
}
papers = list(persondb.find(mango))[0].get("author-of")
co_authors = set()
id_list = []
for paper in papers:
p = pubdb[paper]
ids = p["authored-by"]
id_list.extend(ids)
co_authors = set()
for i in id_list:
n = persondb[i].get("name")
co_authors.add(n)
co_authors.remove(name)
cache_results(name, list(co_authors))
return list(co_authors)
def level_n_recurse(name, n):
if n == 1:
l = level_one(name)
return {1: l}
else:
levels = level_n_recurse(name,n-1)
candidates = set()
for p in levels[n-1]:
cas = level_one(p)
candidates.update(cas)
for i in range(1,n):
candidates.difference_update(levels[i])
levels[n] = list(candidates)
return levels
def level_n(name, n):
levels = level_n_recurse(name, n)
return levels[n]
done = False
level = 1
while not done:
start = "Michael J. Franklin"
search = "Moshe Y. Vardi"
l = level_n(start, level)
if len(l) == 0:
print("Not Found")
done = True
break
if search in l:
print("Found at level {}".format(level))
done = True
break
level += 1