-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabstract_requests.py
More file actions
277 lines (224 loc) · 10.1 KB
/
abstract_requests.py
File metadata and controls
277 lines (224 loc) · 10.1 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
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
import argparse
import json
from collections import Counter
from concurrent.futures import ThreadPoolExecutor
import nltk
import numpy as np
import pandas as pd
import requests
import spacy
from tqdm import tqdm
# Get the keywords
parser = argparse.ArgumentParser(description='Script to process keywords.')
parser.add_argument('-w', '--keywords', nargs='+', help='Keywords', default=['salinization', 'flooding'])
parser.add_argument('-k', '--api_key', help='API key')
args = parser.parse_args()
keywords = args.keywords
# Get the keywords
my_input = ' AND '.join(keywords)
# Set your API Key
if args.api_key is None:
print("\nPlease provide an API key using the -k or --api_key option.\n")
exit()
key = args.api_key
# key = 'a4dfe1b4f9535e6e41587a40f9ba9877'
# Create a session for making requests
session = requests.Session()
session.headers['X-ELS-APIKey'] = key
session.headers['X-ELS-ResourceVersion'] = 'XOCS'
session.headers['Accept'] = 'application/json'
def scopus_search(my_input: str) -> list:
api_resource = "https://api.elsevier.com/content/search/scopus?"
search_param = f'query=title-abs-key({my_input})' # for example
# Set the desired number of results per page
results_per_page = 25
# Send the first request to get the total number of results
first_page_request = session.get(api_resource + search_param + f"&count={results_per_page}&start=0")
first_page = json.loads(first_page_request.content.decode("utf-8"))
total_results = int(first_page['search-results']['opensearch:totalResults'])
total_pages = (total_results // results_per_page) + 1
# List to store all articles
articles_list = []
print(f"Scrapping Data Pages from Scopus using {my_input}...")
# Iterate over all pages
with ThreadPoolExecutor() as executor:
for page_number in tqdm(range(total_pages)):
start_index = page_number * results_per_page
page_request = session.get(api_resource + search_param + f"&count={results_per_page}&start={start_index}")
page = json.loads(page_request.content.decode("utf-8"))
try:
articles_list.extend(page['search-results']['entry'])
except:
continue
print(f"Number of articles: {len(articles_list)}")
return articles_list
def article_info(articles_list: list) -> set:
print(f"\nGetting article titles...")
article_title = []
article_doi = []
article_eid = []
article_ID = []
article_pii = []
article_url = []
article_creator = []
article_pub = []
article_coverDate = []
article_number_citations = []
global outliers
outliers = {}
# Access individual articles
with ThreadPoolExecutor() as executor:
for article in tqdm(range(len(articles_list))):
try:
article_pii.append(articles_list[article].get("pii"))
article_title.append(articles_list[article].get("dc:title"))
article_doi.append(articles_list[article].get("prism:doi"))
article_eid.append(articles_list[article].get("eid"))
article_ID.append(articles_list[article].get("dc:identifier"))
article_url.append(articles_list[article].get("prism:url"))
article_creator.append(articles_list[article].get("dc:creator"))
article_pub.append(articles_list[article].get("prism:publicationName"))
article_coverDate.append(articles_list[article].get("prism:coverDate"))
article_number_citations.append(articles_list[article].get("citedby-count"))
except:
article_pii.append(None)
article_doi.append(None)
article_title.append(articles_list[article].get("dc:title"))
article_eid.append(articles_list[article].get("eid"))
article_ID.append(articles_list[article].get("dc:identifier"))
article_url.append(articles_list[article].get("prism:url"))
article_creator.append(None)
article_pub.append(articles_list[article].get("prism:publicationName"))
article_coverDate.append(articles_list[article].get("prism:coverDate"))
article_number_citations.append(articles_list[article].get("citedby-count"))
return (
article_title, article_doi, article_eid, article_ID,
article_pii, article_url, article_creator,
article_pub, article_coverDate, article_number_citations
)
affiliation = []
area = []
author_count = []
def scopus_id_abstract_retriever(scopus_id: str) -> str:
api_endpoint = f"https://api.elsevier.com/content/abstract/scopus_id/{scopus_id}"
# Make the request to retrieve the abstract
response = session.get(api_endpoint)
data = json.loads(response.content.decode("utf-8"))
# Extract the abstract from the response
try:
abstract = data["abstracts-retrieval-response"]["coredata"]["dc:description"]
try: affiliation.append(data["abstracts-retrieval-response"]["affiliation"]["affilname"])
except:
try: affiliation.append(data["abstracts-retrieval-response"]["affiliation"])
except: affiliation.append(None)
# Study Area
try:
result = data["abstracts-retrieval-response"]["subject-areas"]["subject-area"]
subjects = [subject["$"] for subject in result]
area.append(" & ".join(subjects))
except:
area.append(None)
# Authors
try: author_count.append(len(data["abstracts-retrieval-response"]["authors"]['author']))
except: author_count.append(None)
except:
abstract = "NA"
try: affiliation.append(data["abstracts-retrieval-response"]["affiliation"]["affilname"])
except:
try: affiliation.append(data["abstracts-retrieval-response"]["affiliation"])
except: affiliation.append(None)
# Study Area
try:
result = data["abstracts-retrieval-response"]["subject-areas"]["subject-area"]
subjects = [subject["$"] for subject in result]
area.append(" & ".join(subjects))
except:
area.append(None)
# Authors
try: author_count.append(len(data["abstracts-retrieval-response"]["authors"]['author']))
except: author_count.append(None)
# Return the abstract
return abstract
def location_finder(text: str) -> dict:
# Load the pre-trained model
nlp = spacy.load("en_core_web_sm")
# Sample text
sample_text = text
# Process the text
doc = nlp(sample_text)
# Find location words and their locations
locations = [entity.text for entity in doc.ents if entity.label_ == "GPE" or entity.label_ == "LOC"]
# Sorting locations by frequency
my_dict = dict(Counter(locations))
sorted_dict = dict(sorted(my_dict.items(), key=lambda x: x[1], reverse=True))
first_five_elements = dict(list(sorted_dict.items())[:5])
return first_five_elements
def contains_country(value):
for country in countries:
if country in value:
return True
return False
# Function to check if a row should be deleted
def should_delete_row(row):
for column in columns_to_check:
value = str(row[column])
if contains_country(value) and "USA" not in value and "U.S." not in value and "United States" not in value:
return False
return True
if __name__ == "__main__":
# Perform the search and retrieve article info
my_set = article_info(scopus_search(my_input))
# Create an empty list to store the output dictionary keys
list_of_lists = []
# Loop over the IDs and find locations
print(f'\nGetting locations from {len(my_set[3])} abstracts...')
with ThreadPoolExecutor() as executor:
for n, scopus_id in tqdm(enumerate(my_set[3])):
output_dict = location_finder(scopus_id_abstract_retriever(scopus_id))
list_of_lists.append(list(output_dict.keys()))
print(f"\nMaking Dataframe...")
# Extract first and second elements
first_elements = [inner_list[0] if len(inner_list) > 0 else None for inner_list in list_of_lists]
second_elements = [inner_list[1] if len(inner_list) > 1 else None for inner_list in list_of_lists]
third_elements = [inner_list[2] if len(inner_list) > 2 else None for inner_list in list_of_lists]
fourth_elements = [inner_list[3] if len(inner_list) > 3 else None for inner_list in list_of_lists]
# Make DataFrame
df = pd.DataFrame({
"Paper Title": my_set[0],
"Scopus ID" : my_set[3],
"DOI": my_set[1],
"URL": my_set[5],
"Lead Author": my_set[6],
"Affiliation": affiliation,
"Author count": author_count,
"Area of Study": area,
"Publication": my_set[7],
"Cover Date": my_set[8],
"Number of citations": my_set[9],
"first_location" : first_elements,
"second_location" : second_elements,
"third_location" : third_elements,
"fourth_location": fourth_elements})
# Saving file
affiliation_series = df['Affiliation']
modified_series = affiliation_series.apply(lambda x: x[0]['affilname'] if isinstance(x, list) and len(x) > 0 else x)
df['Affiliation'] = modified_series
df.to_csv("output_files/output.csv")
# print("\nNow excluding useless papers...")
# from country_list import countries_for_language
# # countries_for_language returns a list of tuples now, might be changed to an OrderedDict
# countries = dict(countries_for_language('en'))
# del countries["US"]
# # List of countries
# countries = list(countries.values())
# countries.append("Africa")
# # Columns to check
# columns_to_check = df.columns[-4:]
# # Check if the last four columns are empty
# last_four_columns_empty = df.iloc[:, -4:].isnull().all(axis=1)
# # Delete rows where the last four columns are empty
# df = df[~last_four_columns_empty]
# # Delete rows that meet the criteria
# df = df[df.apply(should_delete_row, axis=1) == True]
# df.to_csv('output_files/excluded.csv')
print("DONE!")