-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdfToolkit.py
325 lines (231 loc) · 12.3 KB
/
pdfToolkit.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
import glob, re, sys, os, ssl, unicodedata, itertools, lxml, bs4, requests, multiprocessing
import pandas as pd
from nltk.tokenize import sent_tokenize
from dataclasses import dataclass
from pathlib import Path
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
from multiprocessing.pool import Pool
from grobid_client_python import grobid_client as grobid
from bs4 import BeautifulSoup
############################################################################################
# Toolkit for pdf conversion to excel spreadsheet, parses out text by keywords from each pdf
# Written/modified by Corinn Small, [email protected]
############################################################################################
def convert_text(inpath,outpath):
'''
converts text from pdf to tei.xml using grobid web service api, remember computer has to be connected to the server: cd grobid-0.6.1/ -> ./gradlew run
input: path to papers
output: tei.xml file per pdf
'''
print('Converting text...')
print('')
client = grobid.grobid_client(config_path="/Users/corinnsmall/Documents/Github/grobid_client_python/config.json")
client.process("processFulltextDocument", inpath, outpath)
print('Done')
def read_tei(tei_file):
'''
input: xml file
output: a beautifulsoup object
'''
with open(tei_file, 'r') as tei:
soup = BeautifulSoup(tei, 'html.parser')
return soup
raise RuntimeError('Cannot generate a soup from the input')
def elem_to_text(elem, default='NA'):
'''
Returns element if it exists, if not, returns NA
'''
if elem:
return elem.getText()
else:
return default
class TEIFile(object):
'''
class for storing pdf info
'''
def __init__(self, filename):
self.filename = filename
self.soup = read_tei(filename) #creates soup object
self._text = None
self._title = ''
self._abstract = ''
self._keytext = {}
self._check = None
@property
def doi(self, id_='DOI'):
'''
retrieves id
'''
idno_elem = self.soup.find('idno', type='DOI')
if not idno_elem:
return 'no id'
else:
return idno_elem.getText()
@property
def title(self):
'''
retrieves title
'''
if not self._title:
self._title = self.soup.title.getText()
return self._title
@property
def abstract(self):
'''
retrieves abstract
'''
if not self._abstract:
abstract = self.soup.abstract.getText(separator=' ', strip=True)
self._abstract = abstract
return self._abstract
@property
def authors(self):
'''
retrieves authors
'''
authors_in_header = self.soup.analytic.find_all('author')
result = []
@dataclass
class Person:
firstname: str
middlename: str
surname: str
for author in authors_in_header:
persname = author.persname
if not persname:
continue
firstname = elem_to_text(persname.find("forename", type="first"))
middlename = elem_to_text(persname.find("forename", type="middle"))
surname = elem_to_text(persname.surname)
person = Person(surname, firstname, middlename)
result.append(person)
return result
@property
def text(self):
'''
retrieves text, asks for user's input about longer subsections (can include the subsection title in the searchable text)
returns dictionary by subsection
'''
#print(self.soup.prettify())
#print(self.filename.split('/')[-1])
#print('')
if not self._text:
paper_text = {}
'''abstract = self.soup.profileDesc.find_all('abstract')
sect_abs = []
for p in abstract:
sect_text = p.get_text(separator= ' ', strip = True)
sect_abs.append(sect_text)
paper_text['abstract'] = sect_abs'''
divs = self.soup.body.find_all('div')
#print(divs)
for div in divs:
if not div.get('type'): # if div is neither an appendix nor reference
heads = div.find_all('head') #find all subsections
#print(heads)
if not heads: #if there aren't any subsections
sect = []
for p in div.find_all('p'): #add all sentences to one section titled 'body'
#print(p)
sect_text = p.get_text(separator=' ', strip=True)
sect.append(sect_text)
paper_text['body'] = sect
#print(sect)
else: #otherwise for each subsection create a new list with corresponding text and add it to a div dictionary
for head in heads:
sect_ = []
head_text = head.get_text(separator=' ', strip=True).lower()
'''if len(head_text.split(' ')) > 7: #if the head title is < 7 words long (arbitrary #) ask the user to clarify its validity
print('subtitle sentence: ', head_text)
ans = input('Is this subsection a full sentence? y/n')
ok_ans = ('y','n')
while ans not in ok_ans:
print('Y or N only please!')
ans = input('Is this subsection a full sentence? y/n')
if ans == 'y': #if the title is a sentence
print('Including subsection title in searchable text, but still include it as a separate subsection...')
print('')
sect_.append(head_text) #include it in the text
for p in div.find_all('p'): #for each paragraph find all text
#print(p)
sect_text = p.get_text(separator=' ', strip=True)
sect_.append(sect_text) #add it to the list
elif ans == 'n': #if the title is not a sentence
ans1 = input('Does this subsection make sense? y/n') #does the title make sense in general?
while ans1 not in ok_ans:
print('Y or N only please!')
ans1 = input('Is this subsection a full sentence? y/n')
if ans1 == 'y': #if the title makes sense treat it as a regular subsection
print('Valid subsection...')
print('')
for p in div.find_all('p'):
#print(p)
sect_text = p.get_text(separator=' ', strip=True)
sect_.append(sect_text)
elif ans1 == 'n': #if the title doesn't make sense, mark self._check True so the user knows to check it later
print('Flagging nonsense...')
print()
self._check = True
for p in div.find_all('p'): #still include the subsection's text in list for keyword searching
#print(p)
sect_text = p.get_text(separator=' ', strip=True)
sect_.append(sect_text)
else: #if the subsection title is not longer than 7 words, find all text and add it to the list
for p in div.find_all('p'):
#print(p)
sect_text = p.get_text(separator=' ', strip=True)
sect_.append(sect_text)
#print('subsection title < 7 words')'''
paper_text[head_text] = sect_
#for figure descriptions....(WORK IN PROGRESS)
if head_text.find('fig') != -1: #if figure is treated as its own subsection, find all text and include it in its own list
print('Figure treated as subsection: ', head_text)
for p in div.find_all('p'):
#print(p)
sect_text = p.get_text(separator=' ', strip=True)
sect_.append(sect_text)
paper_text['figure'] = sect_
for fig in self.soup.body.find_all('figure'): #for figure sections find the figure descriptions and print it
figDesc = fig.find_all('figDesc')
for f in figDesc:
print('Figure Description: ',f)
#if paper is short, all the text might show up in the abstract section of the xml file instead of div
if not divs:
self._check = True
sect = []
ps = self.soup.abstract.find_all('p')
if not ps:
sect_text = self.soup.abstract.get_text(separator=' ', strip=True)
sect.append(sect_text)
else:
for p in ps:
sect_text = p.get_text(separator=' ', strip=True)
sect.append(sect_text)
paper_text['body'] = sect #add all sentences to one section titled 'body'
self._text = paper_text
return self._text
def keytext(self, keywords):
'''
retrieves sentences by keyword
returns dictionary
'''
for keyword in keywords:
self._keytext[keyword] = []
for k,v in self._text.items(): #for subsection and text
for keyword in keywords: #search thru sentences
sub_dict = {}
section = k.lower()
sub_dict[section] = [] #create list for each subsection in dictionary
for i in v: #for paragraph in text
sentences = sent_tokenize(i) #get list of sentences
sentences = [s.lower() for s in sentences]
for sentence in sentences:
result = re.findall('\\b' + keyword + '\\b', sentence) #find keyword in sentence
if len(result) > 0: #if keyword exists,
sub_dict[section].append(sentence) #add sentence to subsection list
else:
pass
#print(sub_dict)
self._keytext[keyword].append(sub_dict) #adds each subsection to keyword dictionary
return self._keytext