-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy paths2_convert.py
296 lines (263 loc) · 10.1 KB
/
s2_convert.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
#Name: s2_convert.py
#Purpose: Convert PDFs to TXT format
#Invocation: python3 s2_convert.py <projName> <lng> <clss>
import codecs
import os
import re
import sys
#Name: valid_arguments
#Purpose: Check whether the command-line arguments are valid
#Parameters: sys.argv (globally defined list of command-line arguments)
#Returns: True (all arguments are valid) or False (at least one argument is invalid)
def valid_arguments():
lngValid = set(["danish", "dutch", "english", "finnish", "french", "german", "hungarian", "italian", "norwegian", "portuguese", "spanish", "swedish", "turkish"])
clssValid = set(["neg", "pos", "pred"])
if len(sys.argv) == 4 and re.search(r"^[a-zA-Z][a-zA-Z_-]*$", sys.argv[1]) and sys.argv[2] in lngValid and sys.argv[3] in clssValid:
return True
return False
#Name: match_page
#Purpose: Match line to an XML page tag
#Parameters: line (line of text from XML file)
#Returns: Regular expression match object
def match_page(line):
return re.search(r"<page id=\"(\d+)\"", line)
#Name: match_textbox
#Purpose: Match line to an XML textbox tag
#Parameters: line (line of text from XML file)
#Returns: Regular expression match object
def match_textbox(line):
return re.search(r"<textbox id=\"(\d+)\"", line)
#Name: match_textline
#Purpose: Match line to an XML textline tag
#Parameters: line (line of text from XML file)
#Returns: Regular expression match object
def match_textline(line):
return re.search(r"<textline", line)
#Name: match_text
#Purpose: Match line to an XML text tag
#Parameters: line (line of text from XML file)
#Returns: Regular expression match object
def match_text(line):
return re.search(r"<text.*font=\"(.*)\".*bbox=\"([0-9]+\.[0-9]+),([0-9]+\.[0-9]+),([0-9]+\.[0-9]+),([0-9]+\.[0-9]+)\".*size=\"([0-9]+\.[0-9]+)\">(.*)</text>", line)
#Name: clean_char
#Purpose: Clean character to deal with punctuation, numbers, and foreign accent marks
#Parameters: old (character)
#Returns: Cleaned character
def clean_char(old):
#Check the length of the argument
if len(old) == 0:
new = ""
elif len(old) >= 2:
new = " "
else:
#The function "ord" returns the integer representing the Unicode code point of a character
ucp = ord(old)
#Control codes
if (0 <= ucp <= 31):
new = " "
#Punctuation
elif (32 <= ucp <= 38) or (40 <= ucp <= 47) or (58 <= ucp <= 64) or (91 <= ucp <= 96) or (123 <= ucp <= 126) or ucp == 8221:
new = " "
#Apostrophe
elif ucp == 39 or ucp == 8217:
new = ""
#Numbers
elif (48 <= ucp <= 57):
new = " "
#Letters
elif (192 <= ucp <= 198) or (224 <= ucp <= 230):
new = "a"
elif ucp == 199 or ucp == 231:
new = "c"
elif (200 <= ucp <= 203) or (232 <= ucp <= 235):
new = "e"
elif (204 <= ucp <= 207) or (236 <= ucp <= 239):
new = "i"
elif ucp == 209 or ucp == 241:
new = "n"
elif (210 <= ucp <= 214) or ucp == 216 or (242 <= ucp <= 246) or ucp == 248:
new = "o"
elif ucp == 223:
new = "ss"
elif (217 <= ucp <= 220) or (249 <= ucp <= 252):
new = "u"
elif ucp == 221 or ucp == 253 or ucp == 255:
new = "y"
elif ucp >= 128:
new = " "
else:
new = old
return new
#Name: get_chars
#Purpose: Extract the character values, coordinates, hierarchy, and font information from XML file
#Parameters: xmlFile (location of XML file)
#Returns: List of tuples (one for each character) containing character data
def get_chars(xmlFile):
chars = []
page = 0
textbox = 0
textline = 0
#Open XML file and use regular expressions to parse contents
f = codecs.open(xmlFile, "r", encoding="utf8")
for l in f:
line = l.strip()
pageMatch = match_page(line)
textboxMatch = match_textbox(line)
textlineMatch = match_textline(line)
textMatch = match_text(line)
if pageMatch:
page = int(pageMatch.group(1))
elif textboxMatch:
textline = 0
textbox = int(textboxMatch.group(1))
elif textlineMatch:
textline += 1
elif textMatch:
font = textMatch.group(1)
x1 = float(textMatch.group(2))
y1 = float(textMatch.group(3))
x2 = float(textMatch.group(4))
y2 = float(textMatch.group(5))
size = float(textMatch.group(6))
value = clean_char(textMatch.group(7))
chars.append((page, textbox, textline, x1, y1, x2, y2, size, font, value))
f.close()
return chars
#Name: clean_text
#Purpose: Clean string of text and check each word against a list of stop words
#Parameters: text (string of text)
#Returns: Cleaned text
def clean_text(text):
text = text.lower()
text = re.sub("\s+", " ", text)
#Remove stop words
textClean = []
text = text.split(" ")
global stopWords
for word in text:
word = word.strip()
if word not in stopWords:
textClean.append(word)
textClean = " ".join(textClean)
return textClean
#Name: write_text
#Purpose: Construct words character by character
#Parameters: chars (list of tuples)
# txtFile (location of TXT file)
#Returns:
def write_text(chars, txtFile):
text = []
#Sort characters according to page, textbox, textline, y1, and x1
chars = sorted(chars, key = lambda z: (z[0], z[1], z[2], -z[4], z[3]))
pageCur = chars[0][0]
textboxCur = chars[0][1]
textlineCur = chars[0][2]
for char in chars:
spaceFlag = 0
pageNew = char[0]
textboxNew = char[1]
textlineNew = char[2]
if pageNew != pageCur:
pageCur = pageNew
spaceFlag = 1
if textboxNew != textboxCur:
textboxCur = textboxNew
spaceFlag = 1
if textlineNew != textlineCur:
textlineCur = textlineNew
spaceFlag = 1
if spaceFlag == 1:
text.append(" ")
text.append(char[9])
text = "".join(text)
f = codecs.open(txtFile, "w")
f.write(clean_text(text))
f.close()
return
#Name: create_output
#Purpose: Convert a PDF document of a given class to TXT format
#Parameters: projName (project name)
# clss ("pos" or "neg")
# docName (document name)
#Returns:
def create_output(projName, clss, docName):
#Create file locations
pdfFile = "/{}/{}_pdf/{}.pdf".format(projName, clss, docName)
xmlFile = "/{}/{}_xml/{}.xml".format(projName, clss, docName)
txtFile = "/{}/{}_txt/{}.txt".format(projName, clss, docName)
probFile = "/{}/{}_prob/{}.pdf".format(projName, clss, docName)
#probFlag indicates whether there is a problem extracting text from the PDF
#The problem PDFs are moved to separate folders where they can be inspected
probFlag = 0
chars = []
#If the TXT file does not already exist, then try creating it
if not os.path.isfile(txtFile):
try:
#The pdf2txt.py program comes with the PDFMiner module
os.system("pdf2txt.py -o {} -t xml {}".format(xmlFile, pdfFile))
except PDFTextExtractionNotAllowed:
#Exception indicates that text cannot be extracted from the PDF
probFlag = 1
if not os.path.isfile(xmlFile):
probFlag = 1
elif os.stat(xmlFile).st_size == 0:
probFlag = 1
if probFlag == 0:
chars = get_chars(xmlFile)
if len(chars) == 0:
probFlag = 1
#Check probFlag value and act accordingly
if probFlag == 0:
write_text(chars, txtFile)
if os.path.isfile(xmlFile):
#The intermediate XML file is deleted because it tends to be large
os.remove(xmlFile)
print(docName)
elif probFlag == 1:
if os.path.isfile(xmlFile):
#The intermediate XML file is deleted because it tends to be large
os.remove(xmlFile)
if os.path.isfile(txtFile):
#Any text that has been extracted from the problem PDF is deleted
os.remove(txtFile)
os.system("mv {} {}".format(pdfFile, probFile))
print("!!! PROBLEM: {}".format(docName))
return
#Name: convert_files
#Purpose: Convert PDFs to TXT format
#Parameters: projName (project name)
# lng (language)
# clss ("neg", "pos", or "pred")
#Returns:
def convert_files(projName, lng, clss):
#Read in stop words
stopWordsList = []
f = codecs.open("stop_{}.txt".format(lng), "r")
for word in f:
if word.strip() != "":
stopWordsList.append(word.strip())
f.close()
global stopWords
stopWords = set(stopWordsList)
#Iterate through PDFs of a given class, extract text, and create output files
print("\n***** {} *****\n".format(clss))
pdfs = sorted(os.listdir("/{}/{}_pdf/".format(projName, clss)))
for pdf in pdfs:
pdfMatch = re.search(r"^(\S+)\.([pP][dD][fF])$", pdf)
if pdfMatch:
docName = pdfMatch.group(1)
if pdfMatch.group(2) != "pdf":
oldFile = "/{}/{}_pdf/{}.{}".format(projName, clss, docName, pdfMatch.group(2))
newFile = "/{}/{}_pdf/{}.pdf".format(projName, clss, docName)
os.system("mv {} {}".format(oldFile, newFile))
create_output(projName, clss, docName)
print("")
return
def main():
if valid_arguments():
convert_files(sys.argv[1], sys.argv[2], sys.argv[3])
else:
print("\nInvalid arguments\n")
return
if __name__ == "__main__":
main()