-
Notifications
You must be signed in to change notification settings - Fork 2
/
read-in.py
233 lines (174 loc) · 7.45 KB
/
read-in.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
import pandas as pd
import numpy as np
import requests
import json
import os
# text files partially needed to be cleaned in Excel to obtain bibcodes -- inconsistent labelling
# Excel also needed to properly format the text file to be read in as a CSV
# 2mass = used 2MASS, skrutskie = cited skrutskie yes/no
## 2017-01to06
##coverted txt to csv, read in
cite_1 = pd.read_csv("./data/raw/2017-01to06-citeSkrutskie-NOTES.csv", skiprows=14, sep=',', header=None)
cite_1.columns = ["article", "note1", "note2", "note3", "note4", "note5"]
cite_1 = cite_1.iloc[:, 0:2]
##remove ambiguous answers
notna = cite_1['note1'].str.contains("YES|NO")
cite_1 = cite_1[notna]
##create col of only yes/no
cite_1['2mass'] = np.where(cite_1['note1'].str.contains("YES"), 'yes', 'no')
# create col of Skrutskie yes/no
cite_1['skrutskie'] = 'yes'
##isolate article id to bibcode, reset index to prevent key errors
cite_1['bibcode'] = cite_1['article'].str.slice(start=9)
cite_1 = cite_1.reset_index()
##fetch article doi from API - search by bibcode
token = 'Fv6aJu1i3oV4uuG6LfStmnIg9Txxe1NfFY2vLRap'
doi = []
for bibcode in range(len(cite_1['bibcode'])):
r = requests.get("https://api.adsabs.harvard.edu/v1/search/query?q=bibcode:" + cite_1['bibcode'][bibcode] + "&fl=doi", headers = {"Authorization": "Bearer " + token})
newtext = json.loads(r.text)
if not newtext['response']['docs']:
doi.append(np.nan)
else:
if not newtext['response']['docs'][0]:
doi.append(np.nan)
else:
doi.append(newtext['response']['docs'][0]['doi'])
doi = pd.Series(doi)
cite_1['doi'] = doi.str[0]
# export csv of identifiers
cleandf = cite_1.drop(['index','note1'],axis=1)
cleandf.to_csv("cleandf.csv",sep=',')
## 2017-01to06-NO
##coverted txt to csv, read in
cite_2 = pd.read_csv("./data/raw/2017-01to06-NOciteSkrutskie-NOTES.csv", skiprows=23, sep=',', header=None)
cite_2.columns = ["article", "note1", "note2", "note3", "note4"]
cite_2 = cite_2.iloc[:, 0:2]
##remove ambiguous answers
notna = cite_2['note1'].str.contains("YES|NO")
cite_2 = cite_2[notna]
##create col of only yes/no
cite_2['2mass'] = np.where(cite_2['note1'].str.contains("YES"), 'yes', 'no')
# create col of Skrutskie yes/no
cite_2['skrutskie'] = 'no'
##isolate article id to bibcode, reset index to prevent key errors
cite_2['bibcode'] = cite_2['article'].str.slice(start=9)
cite_2 = cite_2.reset_index()
##fetch article doi from API - search by bibcode
token = 'Fv6aJu1i3oV4uuG6LfStmnIg9Txxe1NfFY2vLRap'
doi = []
for bibcode in range(len(cite_2['bibcode'])):
r = requests.get("https://api.adsabs.harvard.edu/v1/search/query?q=bibcode:" + cite_2['bibcode'][bibcode] + "&fl=doi", headers = {"Authorization": "Bearer " + token})
newtext = json.loads(r.text)
if not newtext['response']['docs']:
doi.append(np.nan)
else:
if not newtext['response']['docs'][0]:
doi.append(np.nan)
else:
doi.append(newtext['response']['docs'][0]['doi'])
doi = pd.Series(doi)
cite_2['doi'] = doi.str[0]
# export csv of identifiers
cleandf2 = cite_2.drop(['index','note1'],axis=1)
cleandf2.to_csv("cleandf2.csv",sep=',')
## 2017-06to12
##coverted txt to csv, read in
cite_3 = pd.read_csv("./data/raw/2017-06to12-citeSkrutskie-NOTES.csv", skiprows=10, sep=',', header=None)
cite_3.columns = ["article", "note1", "note2", "note3"]
cite_3 = cite_3.iloc[:, 0:2]
##remove ambiguous answers
notna = ~pd.isnull(cite_3['note1'].str.contains("YES|NO"))
cite_3 = cite_3[notna]
##create col of only yes/no
cite_3['2mass'] = np.where(cite_3['note1'].str.contains("YES"), 'yes', 'no')
# create col of Skrutskie yes/no
cite_3['skrutskie'] = 'yes'
##isolate article id to bibcode, reset index to prevent key errors
cite_3['bibcode'] = cite_3['article'].str.slice(start=9)
cite_3 = cite_3.reset_index()
##fetch article doi from API - search by bibcode
token = 'Fv6aJu1i3oV4uuG6LfStmnIg9Txxe1NfFY2vLRap'
doi = []
for bibcode in range(len(cite_3['bibcode'])):
r = requests.get("https://api.adsabs.harvard.edu/v1/search/query?q=bibcode:" + cite_3['bibcode'][bibcode] + "&fl=doi", headers = {"Authorization": "Bearer " + token})
newtext = json.loads(r.text)
if not newtext['response']['docs']:
doi.append(np.nan)
else:
if not newtext['response']['docs'][0]:
doi.append(np.nan)
else:
doi.append(newtext['response']['docs'][0]['doi'])
doi = pd.Series(doi)
cite_3['doi'] = doi.str[0]
# export csv of identifiers
cleandf3 = cite_3.drop(['index','note1'],axis=1)
cleandf3.to_csv("cleandf3.csv",sep=',')
## 2017-06to12-NO
##converted txt to csv, read in
cite_4 = pd.read_csv("./data/raw/2017-06to12-NOciteSkrutskie-NOTES.csv", skiprows=11, sep=',', header=None)
cite_4.columns = ["article", "note1", "note2", "note3"]
cite_4 = cite_4.iloc[:, 0:2]
##remove ambiguous answers
notna = cite_4['note1'].str.contains("YES|NO")
cite_4 = cite_4[notna]
##create col of only yes/no
cite_4['2mass'] = np.where(cite_4['note1'].str.contains("YES"), 'yes', 'no')
# create col of Skrutskie yes/no
cite_4['skrutskie'] = 'yes'
##isolate article id to bibcode, reset index to prevent key errors
cite_4['bibcode'] = cite_4['article'].str.slice(start=9)
cite_4 = cite_4.reset_index()
##fetch article doi from API - search by bibcode
token = 'Fv6aJu1i3oV4uuG6LfStmnIg9Txxe1NfFY2vLRap'
doi = []
for bibcode in range(len(cite_4['bibcode'])):
r = requests.get("https://api.adsabs.harvard.edu/v1/search/query?q=bibcode:" + cite_4['bibcode'][bibcode] + "&fl=doi", headers = {"Authorization": "Bearer " + token})
newtext = json.loads(r.text)
if not newtext['response']['docs']:
doi.append(np.nan)
else:
if not newtext['response']['docs'][0]:
doi.append(np.nan)
else:
doi.append(newtext['response']['docs'][0]['doi'])
doi = pd.Series(doi)
cite_4['doi'] = doi.str[0]
# export csv of identifiers
cleandf4 = cite_4.drop(['index','note1'],axis=1)
cleandf4.to_csv("cleandf4.csv",sep=',')
# combine dataframes
frames = [cleandf, cleandf2, cleandf3, cleandf4]
fulldata = pd.concat(frames)
fulldata = fulldata.reset_index(drop=True)
# find and download arxiv PDF with DOI
print(getthempdfs(fulldata["doi"]))
#returns 115 out of 312 in first batch
#returns 451 out of 1436 in full batch
# take pdfs from first directory, convert to text, save in second directory
pdf_text_save('./pdfs', 0, len(os.listdir('./pdfs')) + 1, './txts')
# chunked individually to check progress and prevent stoppage
pdf_text_save('./pdfs', 0, 50, './txts')
pdf_text_save('./pdfs', 50, 100, './txts')
pdf_text_save('./pdfs', 100, 150, './txts')
pdf_text_save('./pdfs', 150, 200, './txts')
pdf_text_save('./pdfs', 200, 250, './txts')
pdf_text_save('./pdfs', 250, 300, './txts')
pdf_text_save('./pdfs', 300, 350, './txts')
pdf_text_save('./pdfs', 350, 400, './txts')
pdf_text_save('./pdfs', 400, 452, './txts')
#skip index 135, 178, 186, 205, 217, 240, 263, 289, 391, 392 - took too long to convert
#later successful: 178, 186, 205, 240, 263, 289, 391, 392
# 217 exited without conversion via pdf_text_save -- saved individually with pdf_to_text
#filelist = os.listdir('./pdfs')
#filelist[217:218]
#pdf_text_save('./pdfs', 0, 218, './txts')
#join text to dataframe, export
for index, row in fulldata.iterrows():
if str(index) + '.pdf.txt' in os.listdir('./txts'):
f = open('./txts/' + str(index) + '.pdf.txt', encoding = 'utf-8')
fread = f.read()
fread = fread.replace('\n', ' ')
fulldata.loc[index,'text'] = fread
fulldata.to_csv('./data/fulldata.csv', sep=',')