-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraphapi.py
219 lines (190 loc) · 6.52 KB
/
graphapi.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
from datetime import datetime
from typing import List, Optional
from neo4j import GraphDatabase, Driver, AsyncGraphDatabase
from semanticscholar.Paper import Paper
import asyncio
URI = "bolt://localhost:7687"
AUTH = ("", "")
driver = AsyncGraphDatabase.driver(uri=URI, auth=AUTH)
semaphore = asyncio.Semaphore(3)
async def create_index():
async with semaphore:
await driver.execute_query(
"""
CREATE INDEX ON :Paper(corpusId);
CREATE INDEX ON :Venue(name);
CREATE INDEX ON :Author(name);
CREATE INDEX ON :Topic(name);
"""
)
async def get_paper(corpus_id: int) -> Optional[dict]:
try:
async with semaphore:
result = await driver.execute_query(
"""
MATCH (p:Paper {corpusId:$corpusId})
RETURN p
""",
corpusId=corpus_id,
)
return result.records[0].data()["p"]
except:
return None
async def add_paper(p: Paper, state: int = 2) -> int:
# We use corpusId as the unique identifier of a paper.
async with semaphore:
if p.corpusId is None:
return
corpusId = int(p.corpusId)
publicationDate = (
p.publicationDate.date() if p.publicationDate is not None else None
)
await driver.execute_query(
"""
MERGE (p:Paper {corpusId:$corpusId})
ON CREATE SET p.addedDate = $date
SET p.name = $name
SET p.year = $year
SET p.abstract = $abstract
SET p.publicationDate = $publicationDate
SET p.referenceCount = $referenceCount
SET p.citationCount = $citationCount
SET p.lastUpdate = $date
SET p.state = CASE WHEN p.state > $state THEN p.state ELSE $state END
""",
name=p.title,
corpusId=corpusId,
year=p.year,
abstract=p.abstract,
publicationDate=publicationDate,
referenceCount=p.referenceCount,
citationCount=p.citationCount,
date=datetime.now(),
state=state,
)
# Find or create the authors.
for idx, author in enumerate(p.authors):
await driver.execute_query(
"""
MATCH (p:Paper {corpusId:$corpusId})
MERGE (a:Author {id: $author_id, name: $author_name})
MERGE (p)<-[r:AUTHORS]-(a)
SET r.rank = $rank
""",
corpusId=corpusId,
author_id=int(author["authorId"]) if author["authorId"] else 0,
author_name=author["name"],
rank=idx,
)
IGNORE_VENUES = ["arXiv.org"]
if (
p.publicationVenue is not None
and p.publicationVenue.name not in IGNORE_VENUES
):
# Find or create the publication venue.
await driver.execute_query(
"""
MATCH (p:Paper {corpusId:$corpusId})
MERGE (v:Venue {id: $venue_id})
MERGE (p)-[:JOINS]->(v)
SET v.name = $venue_name
SET v.type = $venue_type
SET v.url = $venue_url
""",
corpusId=corpusId,
venue_id=p.publicationVenue.id,
venue_name=p.publicationVenue.name,
venue_type=p.publicationVenue.type,
venue_url=p.publicationVenue.url,
)
# Update external IDs:
if p.externalIds is not None:
for extName, extId in p.externalIds.items():
await driver.execute_query(
"MATCH (p:Paper {corpusId:$corpusId})"
+ f" SET p.{extName} = $extId",
corpusId=corpusId,
extId=extId,
)
return corpusId
async def add_papers(papers: List[Paper], state: int = 0) -> List[int]:
tasks = [add_paper(paper, state=state) for paper in papers]
ids = await asyncio.gather(*tasks, return_exceptions=False)
ids = [id for id in ids if isinstance(id, int)]
return ids
async def add_citations(src_id: int, tgt_ids: List[int]) -> None:
async with semaphore:
await driver.execute_query(
"""
UNWIND $tgt_ids AS tgt_id
MATCH (p:Paper {corpusId:$src_id}), (q:Paper {corpusId:tgt_id})
MERGE (q)-[:CITES]->(p)
""",
src_id=src_id,
tgt_ids=tgt_ids,
)
async def add_references(src_ids: List[int], tgt_id: int):
async with semaphore:
await driver.execute_query(
"""
UNWIND $src_ids AS src_id
MATCH (p:Paper {corpusId:src_id}), (q:Paper {corpusId:$tgt_id})
MERGE (q)-[:CITES]->(p)
""",
src_ids=src_ids,
tgt_id=tgt_id,
)
async def set_paper_stars(corpus_id: int, stars: int):
async with semaphore:
await driver.execute_query(
"""
MATCH (p:Paper {corpusId:$corpusId})
SET p.stars = $stars
""",
corpusId=corpus_id,
stars=stars,
)
async def drop_topics(corpus_id: int):
async with semaphore:
await driver.execute_query(
"""
MATCH (p:Paper {corpusId:$corpus_id})<-[r:CONTAIN]-(t:Topic)
DELETE r
""",
corpus_id=corpus_id,
)
async def add_topic(corpus_id: int, tag: str):
tag = tag.strip().lower()
async with semaphore:
await driver.execute_query(
"""
MATCH (p:Paper {corpusId:$corpus_id})
MERGE (t:Topic {name:$tag})
MERGE (t)-[:CONTAIN]->(p)
""",
corpus_id=corpus_id,
tag=tag,
)
async def add_topics(corpus_id: int, topics: List[str]):
topics = [topic.strip().lower() for topic in topics]
async with semaphore:
await driver.execute_query(
"""
UNWIND $tags AS tag
MATCH (p:Paper {corpusId:$corpus_id})
MERGE (t:Topic {name:tag})
MERGE (t)-[:CONTAIN]->(p)
""",
corpus_id=corpus_id,
tags=topics,
)
async def list_topics() -> List[str]:
async with semaphore:
result = await driver.execute_query(
"""
MATCH (t:Topic)
RETURN t.name
"""
)
topics = [rec.data()["t.name"] for rec in result.records]
return topics