forked from engineeryashsaxena/Paragraph-Extraction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SectionExtractionModule.py
321 lines (277 loc) · 10.2 KB
/
SectionExtractionModule.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
from pdfminer.pdfparser import PDFParser
from pdfminer.pdfpage import PDFPage
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import PDFPageAggregator
from pdfminer.layout import LAParams, LTTextBox, LTChar, LTFigure
import sys
import os
import pandas as pd
class PdfMinerWrapper(object):
def __init__(self, pdf_doc, pdf_pwd=""):
self.pdf_doc = pdf_doc
self.pdf_pwd = pdf_pwd
def __enter__(self):
# open the pdf file
self.fp = open(self.pdf_doc, 'rb')
# create a parser object associated with the file object
parser = PDFParser(self.fp)
# create a PDFDocument object that stores the document structure
doc = PDFDocument(parser, password=self.pdf_pwd)
# connect the parser and document objects
parser.set_document(doc)
self.doc = doc
return self
def _parse_pages(self):
rsrcmgr = PDFResourceManager()
laparams = LAParams(char_margin=3.5, all_texts=True)
device = PDFPageAggregator(rsrcmgr, laparams=laparams)
interpreter = PDFPageInterpreter(rsrcmgr, device)
for page in PDFPage.create_pages(self.doc):
interpreter.process_page(page)
# receive the LTPage object for this page
layout = device.get_result()
# layout is an LTPage object which may contain child objects like LTTextBox, LTFigure, LTImage, etc.
yield layout
def __iter__(self):
return iter(self._parse_pages())
def __exit__(self, _type, value, traceback):
self.fp.close()
def page_height():
doc = self.pdf_doc
for page in doc:
print
'Page no.', page.pageid, 'Size', (page.height, page.width)
return
def PDFData(obj):
with PdfMinerWrapper(obj) as doc:
i = 1
ans = {}
for page in doc:
# print 'Page no.', page.pageid, 'Size', (page.height, page.width)
for tbox in page:
if not isinstance(tbox, LTTextBox):
continue
# print ' '*1, 'Block', 'bbox=(%0.2f, %0.2f, %0.2f, %0.2f)'% tbox.bbox
key = i
i += 1
value = []
for obj in tbox:
text = obj.get_text().encode('UTF-8')[:-1]
# print text
# value.append(text)
x = 1
for c in obj:
if not isinstance(c, LTChar):
continue
if x == 1:
# print c.fontname," ", c.size,
value.append((text, c.fontname, round(c.size, 2)))
x = 0
ans[key] = value
return ans
def RefineData(f):
ans = {}
cnt = 1
for each in f.keys():
value = f[each]
val = []
for every in value:
if len(every[0].strip()) != 0:
val.append(every)
if len(val) > 0:
ans[cnt] = val
cnt += 1
# print ans
final = {}
# print "UNIQUEVALUE"
for each in ans.keys():
key = (each)
# print ans[key]
value = Split(ans[each])
# print value
final[key] = value
return final
def Split(listOfTuples):
key = 1
sub = {}
sent = []
# print listOfTuples[0]
if (len(listOfTuples)) >= 2:
for each in range(len(listOfTuples) - 1):
# first=listOfTuples[each][]
if listOfTuples[each][2] == listOfTuples[each + 1][2]:
sent.append(listOfTuples[each][0])
# print sent
else:
sent.append(listOfTuples[each][0])
sent = " ".join(map(str, sent))
font = listOfTuples[each][1]
size = listOfTuples[each][2]
sub[(key)] = (sent, font, size)
sent = []
key = key + 1
sent.append(listOfTuples[each + 1][0])
sent = " ".join(map(str, sent))
font = listOfTuples[each + 1][1]
size = listOfTuples[each + 1][2]
sub[(key)] = (sent, font, size)
return sub
else:
return {(1): (listOfTuples[0][0], listOfTuples[0][1], listOfTuples[0][2])}
def Merge(d):
# print d
l = len(d)
small_letters = map(chr, range(ord('a'), ord('z') + 1))
big_letters = map(chr, range(ord('A'), ord('Z') + 1))
new = {}
ran = range(1, len(d))
# print d
for each in ran:
# print each
try:
fir = list(d[each])[0]
sec = list(d[each + 1])[0]
except:
break
# print fir
# print each,type(each),each+1
pos = 'A'
for eac in sec:
if eac in small_letters or eac in big_letters:
pos = eac
# print 'letter',pos
break
if pos.islower():
# print 'hy'
fir = fir + " " + sec
# print each
d[each] = (fir, d[each][1], d[each][2])
prev = len(d)
del d[each + 1]
# print each+1
ff = range(each + 1, prev)
# print ff
for i in ff:
# print i
d[i] = d.pop(i + 1)
# print d
# ran=range(i,len(d))
else:
# print 'f'
d[each] = (fir, d[each][1], d[each][2])
# print d[each]
# print d[each+1]
d[each + 1] = (sec, d[each + 1][1], d[each + 1][2])
return d
# In[25]:
def CapitalizeCount(st):
st = st.strip("-")
st = st.strip(" ")
# st=st.strip("")
l = st.split(" ")
# l=['Fexofenadine', '', 'hydrochloride,', '', 'the', '', 'active', '', 'ingredient', '', 'of', '', 'ALLEGRA,', '', 'is', '', 'a', '', 'histamine', '', 'H1-receptor', 'antagonist', '', 'with', '', 'the', '', 'chemical', '', 'name', '', '(\xc2\xb1)-4-[1', '', 'hydroxy-4-[4-(hydroxydiphenylmethyl)-1-', 'piperidinyl]-butyl]-\xce\xb1,', '', '\xce\xb1-dimethyl', '', 'benzeneacetic', '', 'acid', '', 'hydrochloride.', '', 'It', '', 'has', '', 'the', '', 'following', 'chemical', 'structure']
# print l
cnt = 0
for each in l:
if len(each.strip()) != 0 and each[0].isupper():
cnt += 1
return cnt
def NumberOfWords(st):
return len(st.strip().split(" "))
def MaximumFontSize(df):
col = df['FontSize']
print
col
return max(col)
def ContainsBold(s):
s = s.lower()
c = s.count("bold")
if c >= 1:
return 1.0
else:
return 0.0
import pandas as pd
import xml.etree.cElementTree as ET
def GetCustomisedXML(inputfile):
f = (PDFData(inputfile))
# print f
final = RefineData(f)
# print final
# return
for ea in final.keys():
final[ea] = Merge(final[ea])
dictio = final
l = []
for each in dictio.keys():
di = dictio[each]
for every in di.keys():
subsection = di[every]
row = (each, every, subsection[0], subsection[1], subsection[2])
l.append(row)
df = pd.DataFrame(l, columns=['Section', 'SubSection', 'Content', 'FontName', 'FontSize'])
df['IsBold'] = list(map(lambda x: ContainsBold(str(x)), df['FontName']))
df['CapitalizeCount'] = list(map(lambda x: CapitalizeCount(str(x)), df['Content']))
df['NumberOfWords'] = list(map(lambda x: NumberOfWords(str(x)), df['Content']))
df['PercentageOfCapitalizeCount'] = df['CapitalizeCount'] * 1.0 / df['NumberOfWords']
df['RatioToMaximumSizeInEntireDocument'] = df['FontSize'] / ([MaximumFontSize(df)] * len(df))
# return df
numberOfSections = max(df['Section'])
root = ET.Element("root")
tag = []
for each in range(1, numberOfSections + 1):
sub = df[df['Section'] == each]
sub.index = range(0, len(sub))
# print len(sub)
# doc = ET.SubElement(root, "h1 ")
if len(sub) == 1:
sub['IsMaxFontSize'] = sub['FontSize'] * 1.0 / max(sub['FontSize'])
if (float(sub['RatioToMaximumSizeInEntireDocument'][0]) == 1.0 or float(sub['IsBold'][0] == 1.0)) and float(
sub['PercentageOfCapitalizeCount'][0]) >= 0.50:
ET.SubElement(root, "title").text = sub['Content'][0] #.decode('utf-8')
tag.append("Title")
# ET.SubElement(root, "para").text = ("No Paragraph").decode('utf-8')
else:
# ET.SubElement(root, "title").text = ("No Title").decode('utf-8')
ET.SubElement(root, "para").text = sub['Content'][0] #.decode('utf-8')
tag.append("Para")
else:
sub['IsMaxFontSize'] = sub['FontSize'] * 1.0 / max(sub['FontSize'])
for l in range(len(sub)):
if (float(sub['RatioToMaximumSizeInEntireDocument'][l]) == 1.0 or float(
sub['IsBold'][l] == 1.0)) and float(sub['PercentageOfCapitalizeCount'][l]) >= 0.50:
ET.SubElement(root, "title").text = sub['Content'][l] #.decode('utf-8')
tag.append("Title")
# titlethere=1
# ET.SubElement(doc, "p").text = ("No Paragraph").decode('utf-8')
else:
ET.SubElement(root, "para").text = sub['Content'][l] #.decode('utf-8')
tag.append("Para")
# ET.SubElement(doc, "div").text = ("No Title").decode('utf-8')
tree = ET.ElementTree(root)
# tree.write(outputfile)
df['Tag'] = tag
# print "Get your file at OutputFile Location"
return df
def GetOutput(inputFile,outputFile):
df = GetCustomisedXML(inputFile)
flag = False
con = ""
title = "No Title"
t = []
for i, row in df.iterrows():
tag = row['Tag']
content = str(row['Content'])
if tag == "Title":
t.append((title, con))
con = ""
title = content
else:
con += " " + content
root = ET.Element("root")
for each in t:
ET.SubElement(root, "title").text = each[0]
ET.SubElement(root, "para").text = each[1]
tree = ET.ElementTree(root)
tree.write(outputFile)
print "Done"