-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
139 lines (114 loc) · 4.69 KB
/
main.py
File metadata and controls
139 lines (114 loc) · 4.69 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from neo4j import GraphDatabase
class Neo4JDriver:
def __init__(self, uri, user, password):
self.driver = GraphDatabase.driver(uri, auth=(user, password))
def close(self):
self.driver.close()
# returns all authors in the Database
def get_all_authors(self):
cypher = "MATCH(a:Author) " \
"RETURN COLLECT(a.name) AS AUTHORS"
return self._exec_cypher_query(self, cypher)
# returns all research articles in the Database
def get_all_research_articles(self):
cypher = "MATCH(b:Article) " \
"RETURN COLLECT(b.title) AS RESEARCH_ARTICLES"
return self._exec_cypher_query(self, cypher)
# given a keyword, returns all authors who
# work on a Research Topic related to that keyword
def get_authors(self, keyword):
cypher = "MATCH(a:Author)-[:AUTHOR]->(b:Article) " \
"WHERE b.title =~ '(?i).*" + keyword + ".*' " \
"RETURN COLLECT(a.name) AS AUTHORS"
return self._exec_cypher_query(self, cypher)
# Given the author name,
# return all research articles published by this author
def get_research_articles(self, author_name):
cypher = "MATCH (a:Author)-[:AUTHOR]->(b:Article) " \
"WHERE a.name =~ '(?i).*" + author_name + ".*' " \
"RETURN COLLECT(b.title) AS RESEARCH_ARTICLES"
return self._exec_cypher_query(self, cypher)
# Given the author name
# return all co-authors who have worked with this author
def get_co_authors(self, author_name):
cypher = "MATCH (a:Author)-[:CO_AUTHOR]->(b:Author) " \
"WHERE b.name =~ '(?i).*" + author_name + ".*' " \
"RETURN COLLECT(a.name) AS CO_AUTHORS"
return self._exec_cypher_query(self, cypher)
# Given the author name
# return the count of research articles published.
def get_author_article_count(self, author_name):
cypher = "MATCH (a:Author)-[:AUTHOR]->(b:Article) " \
"WHERE a.name =~ '(?i).*" + author_name + ".*' " \
"RETURN COUNT(b)"
return self._exec_cypher_query(self, cypher)
@staticmethod
def _exec_cypher_query(self, cypher):
with self.driver.session() as session:
r = session.run(cypher)
# returns a dictionary
json = [dict(i) for i in r]
res = list()
for entry in json:
for value in entry.values():
res.append(value)
return res[0]
# [1] Keyword Discovery
def keyword_discovery(driver, keyword):
authors = driver.get_authors(keyword)
print("KEYWORD DISCOVERY:")
for author in authors:
print(author)
print()
# [2] Researcher Profiling
def researcher_profiling(driver, author_name):
research_articles = driver.get_research_articles(author_name)
co_authors = driver.get_co_authors(author_name)
print("RESEARCHER PROFILE:")
print("Author: " + str(author_name))
print("Published Research Articles: " + str(research_articles))
print("Co-Authors: " + str(co_authors))
print()
# [3] Influential Author
def influential_authors(driver, article):
authors = driver.get_authors(article)
author_dict = dict()
for author in authors:
score = driver.get_author_article_count(author) / len(authors)
author_dict[author] = score
print("MOST INFLUENTIAL AUTHOR(S):")
for author in author_dict:
print("Author: " + author + ", Score: " + str(author_dict[author]))
if __name__ == '__main__':
uri = "bolt://localhost:7687"
username = "neo4j"
password = "1234"
driver = Neo4JDriver(uri, username, password)
if driver:
print("Connected to Neo4J !")
print()
# get all authors in the Database
author_list = driver.get_all_authors()
# Get all research articles in the Database
research_articles_list = driver.get_all_research_articles()
while True:
print("+--------------------------+\n"
"[1] Keyword Discovery\n"
"[2] Researcher Profiling\n"
"[3] Influential Author\n"
"[4] Exit")
ch = input("Enter your choice: ")
if ch == "1":
keyword = input("Enter a keyword: ")
keyword_discovery(driver, keyword)
elif ch == "2":
author_name = input("Enter an author name (or keyword): ")
researcher_profiling(driver, author_name)
elif ch == "3":
article = input("Enter a research article name (or keyword): ")
influential_authors(driver, article)
elif ch == "4":
break
else:
print("Invalid choice! Please try again...")
driver.close()