-
Notifications
You must be signed in to change notification settings - Fork 1
/
GenericQueryP.py
348 lines (290 loc) · 13.6 KB
/
GenericQueryP.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import pandas as pd
import numpy as np
import relationalData_Manager as rel
import graphData_Manager as grp
import ModelClasses as mdc
from objcreator import createPublicationObj, createVenueObj, createAuthorObj, createPublisherObj, createJournalArticleObj
class GenericQueryProcessor(object):
# -- Constructor
def __init__(self):
self.queryProcessor = list()
# -- Methods
def cleanQueryProcessors(self):
self.queryProcessor = []
return True
def addQueryProcessor(self, processor):
if isinstance(processor,mdc.QueryProcessor):
self.queryProcessor.append(processor)
return True
else:
return "The processor added is not an instance of the class QueryProcessor", False
# -- Queries
# gq1 + merged DF is OK
def getPublicationsPublishedInYear(self, year):
if isinstance(year,int):
complete_result = pd.DataFrame()
for item in self.queryProcessor:
# For every query processor added to the generic query processor, call the query
partial_result = item.getPublicationsPublishedInYear(year)
#print(partial_result)
# Concatenate the result of this query (type DataFrame) to the empty df outside of the for-in cycle to *save* the result
complete_result = pd.concat([complete_result,partial_result])
# complete_result is now populated by all the results of the query for every query processor
result = list() # list[Publication]
#print(complete_result)
# Drop all duplicate values
ids = set() # unordered collection of unique elements, no worries about duplicates
# Scroll the complete_result dataframe
for idx,row in complete_result.iterrows():
ids.add(row["doi"])
#print(ids)
# Iterate over the cleaned set of DOIs and create a Publication object for each
for item in ids:
# Append the Publication object to the result list
result.append(createPublicationObj(item))
return result #list[Publication]
else:
raise Exception("The input parameter is not an integer!")
# gq2 + merged DF is OK
def getPublicationsByAuthorId(self, orcid):
if isinstance(orcid,str):
complete_result = pd.DataFrame()
for item in self.queryProcessor:
partial_result = item.getPublicationsByAuthorId(orcid)
#print(partial_result)
complete_result = pd.concat([complete_result,partial_result])
result = list() # list[Publication]
#print(complete_result)
# Drop duplicate ids
ids = set()
for idx,row in complete_result.iterrows():
ids.add(row["doi"])
#print(ids)
for item in ids:
result.append(createPublicationObj(item))
return result #list[Publication]
else:
raise Exception("The input parameter is not a string!")
# gq3 + merged DF checked in Sarzanello
def getMostCitedPublication(self):
complete_result = pd.DataFrame()
for item in self.queryProcessor:
partial_result = item.getPubCitationCount()
#print(partial_result)
complete_result = pd.concat([complete_result,partial_result])
complete_result = complete_result.dropna() # this is what was done in the finito commit, we do this to avoid the error during grouping done at the later stage.
#print(complete_result)
# Get set of unique cited_doi (column 'cited_doi')
unique_citedDoi = set()
for idx,row in complete_result.iterrows():
unique_citedDoi.add(row['cited_doi'])
# Group by column 'cited_doi'
grouped_citdoi = complete_result.groupby(["cited_doi"])
x = 0
mostcitedPub = ""
for doi in unique_citedDoi:
df = grouped_citdoi.get_group(doi)
df = df.drop_duplicates()
# Count number of rows for each df (= number of citations for each doi)
y = len(df.index)
if y > x:
x = y
mostcitedPub = doi
return createPublicationObj(mostcitedPub)
# gq4 + merged DF checked in Sarzanello
def getMostCitedVenue(self):
complete_result = pd.DataFrame()
for item in self.queryProcessor:
partial_result = item.getVenCitationCount()
complete_result = pd.concat([complete_result,partial_result])
# Get set of unique venue_name (column 'venue_name')
unique_venueName = set()
for idx,row in complete_result.iterrows():
unique_venueName.add(row['venue_name'])
# Group by 'venue_name'
grouped_venueName = complete_result.groupby(["venue_name"])
x = 0
mostcitedVen = ""
for name in unique_venueName:
df = grouped_venueName.get_group(name)
df = df.drop_duplicates()
# Count number of rows for each df ( = number of citations for each venue_name)
y = len(df.index)
if y > x:
x = y
mostcitedVen = name
return createVenueObj(mostcitedVen,"venue") # Venue
# gq5 + merged DF is OK
def getVenuesByPublisherId(self, crossref):
if isinstance(crossref,str):
complete_result = pd.DataFrame()
for item in self.queryProcessor:
partial_result = item.getVenuesByPublisherId(crossref)
#print(partial_result)
complete_result = pd.concat([complete_result,partial_result])
# complete_result is now populated by all the results of the query for every query processor
result = list() # list[Publication]
#print(complete_result)
# Drop all duplicate values
ven_name = set() # unordered collection of unique elements, no worries about duplicates
# Scroll the complete_result dataframe
for idx,row in complete_result.iterrows():
ven_name.add(row["publication_venue"])
#print(ven_name)
# Iterate over the cleaned set of publication venue names and create a Venue object for each
for item in ven_name:
# Append the Publication object to the result list
result.append(createVenueObj(item,"venue"))
return result #list[Venue]
else:
raise Exception("The input parameter is not a string!")
# gq6 + merged DF is OK
def getPublicationInVenue(self, issn_isbn):
if isinstance(issn_isbn,str):
complete_result = pd.DataFrame()
for item in self.queryProcessor:
partial_result = item.getPublicationInVenue(issn_isbn)
#print(partial_result)
complete_result = pd.concat([complete_result,partial_result])
result = list() # list[Publication]
#print(complete_result)
# Drop duplicate ids
ids = set()
for idx,row in complete_result.iterrows():
ids.add(row["doi"])
#print(ids)
for item in ids:
result.append(createPublicationObj(item))
return result #list[Publication]
else:
raise Exception("The input parameter is not a string!")
# gq7 + merged DF is OK
def getJournalArticlesInIssue(self, issue, volume, journalId):
if isinstance(issue,str) and isinstance(volume,str) and isinstance(journalId,str):
complete_result = pd.DataFrame()
for item in self.queryProcessor:
partial_result = item.getJournalArticlesInIssue(issue,volume,journalId)
#print(partial_result)
complete_result = pd.concat([complete_result,partial_result])
result = list()
#print(complete_result)
ids = set()
for idx,row in complete_result.iterrows():
ids.add(row["doi"])
for item in ids:
result.append(createJournalArticleObj(item))
return result # list[JournalArticle]
else:
raise Exception("All or some of the input parameters are not strings!")
# gq8 + merged DF is OK
def getJournalArticlesInVolume(self, volume, journalId):
if isinstance(volume,str) and isinstance(journalId,str):
complete_result = pd.DataFrame()
for item in self.queryProcessor:
partial_result = item.getJournalArticlesInVolume(volume,journalId)
#print(partial_result)
complete_result = pd.concat([complete_result,partial_result])
result = list()
#print(complete_result)
ids = set()
for idx,row in complete_result.iterrows():
ids.add(row["doi"])
for item in ids:
result.append(createJournalArticleObj(item))
return result # list[JournalArticle]
else:
raise Exception("All or some of the input parameters are not strings!")
# gq9 + merged DF is OK
def getJournalArticlesInJournal(self, journalId):
if isinstance(journalId,str):
complete_result = pd.DataFrame()
for item in self.queryProcessor:
partial_result = item.getJournalArticlesInJournal(journalId)
#print(partial_result)
complete_result = pd.concat([complete_result,partial_result])
result = list()
#print(complete_result)
ids = set()
for idx,row in complete_result.iterrows():
ids.add(row["doi"])
#print(ids)
for item in ids:
result.append(createJournalArticleObj(item))
return result # list[JournalArticle]
else:
raise Exception("The input parameter is not a string!")
# gq10 + merged DF is OK
def getProceedingsByEvent(self, eventPartialName):
if isinstance(eventPartialName,str):
complete_result = pd.DataFrame()
for item in self.queryProcessor:
partial_result = item.getProceedingsByEvent(eventPartialName)
#print(partial_result)
complete_result = pd.concat([complete_result,partial_result])
result = list()
ven_name = set()
for idx,row in complete_result.iterrows():
ven_name.add(row["publication_venue"])
#print(ven_name)
for item in ven_name:
result.append(createVenueObj(item,"proceedings"))
return result # list[Proceeding]
else:
raise Exception("The input parameter is not a string!")
# gq11 + merged DF is OK
def getPublicationAuthors(self, doi):
if isinstance(doi,str):
complete_result = pd.DataFrame()
for item in self.queryProcessor:
partial_result = item.getPublicationAuthors(doi)
#print(partial_result)
complete_result = pd.concat([complete_result,partial_result])
result = list()
#print(complete_result)
orcid = set()
for idx,row in complete_result.iterrows():
orcid.add(row["orcid"])
#print(orcid)
for item in orcid:
result.append(createAuthorObj(item))
return result # llist[Person]
else:
raise Exception("The input parameter is not a string!")
# gq12 + merged DF is OK
def getPublicationsByAuthorName(self, authorPartialName):
if isinstance(authorPartialName,str):
complete_result = pd.DataFrame()
for item in self.queryProcessor:
partial_result = item.getPublicationsByAuthorName(authorPartialName)
#print(partial_result)
complete_result = pd.concat([complete_result,partial_result])
result = list()
#print(complete_result)
ids = set()
for idx,row in complete_result.iterrows():
ids.add(row["doi"])
#print(ids)
for item in ids:
result.append(createPublicationObj(item))
return result # list[Publication]
else:
raise Exception("The input parameter is not a string!")
# gq13 + merged DF is OK
def getDistinctPublishersOfPublications(self, doiList):
if all(isinstance(n, str) for n in doiList):
complete_result = pd.DataFrame()
for item in self.queryProcessor:
partial_result = item.getDistinctPublishersOfPublications(doiList)
print(partial_result)
complete_result = pd.concat([complete_result,partial_result])
result = list()
print(complete_result)
crossrefs = set()
for idx,row in complete_result.iterrows():
crossrefs.add(row["crossref"])
print(crossrefs)
for item in crossrefs:
result.append(createPublisherObj(item))
return result # list[Organisation]
else:
raise Exception("The input parameter is not a list or it is not a list of strings!")