-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram
288 lines (265 loc) · 10.7 KB
/
Program
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
"""
LOBO 1 - corrector de textos que usam a "nova norma ortográfica" para
a norma ortográfica anterior. (simple spellcorrector in Python 3)
Copyright [2018] [J. Manuel Caeiro D. P.]
Licensed under the Academic Free License version 3.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://opensource.org/licenses/AFL-3.0
"""
import re
import PyPDF2
import docx
from docx import Document
import zipfile
import xml.dom.minidom
import ezodf
from bs4 import BeautifulSoup
import lxml
import ebooklib
from ebooklib import epub
from tkinter import *
import tkinter.filedialog
import tkinter as tk
wordlist1 = 'wordlistao.txt'
wordlist2 = 'wordlistcr.txt'
repls = {'[ ':'[', '( ':'(', '/ ':'/', '- ':'-'}
def make_dictionary(wordlist1, wordlist2):
"""Creates a dictionary from wordlists in 2 files. Wordlists
have the same number of (corresponding) words, line by line.
input: wordlist1 contains the words to be replaced; wordlist 2
contains the words to replace them.
return: a dictionary with pair of corresponding words.
"""
with open(wordlist1) as f1, open(wordlist2) as f2:
dicio = dict([s1.strip(), s2.strip()] for s1, s2
in zip(f1, f2))
return dicio
def correct_txt_text(filepath, filename_r, dicio):
"""Reads a text file, changes some words; creates and writes a
new text file with the changed text.
input: a .txt file with the text to correct.
output: a new .txt file with the text corrected.
"""
fr = open(filepath)
filename_w = 'new-' + filename_r
fw = open(filename_w, 'w')
textlist = []
for line in fr.readlines():
worklist = re.findall(r"[\w']+|[-.;!?:,/()%*]", line)
correct = [dicio[w] if w in dicio else w for w in worklist]
punct = set('.,;:?!-/%)]ªº')
corrected = ''.join(p if set(p) <= punct
else ' '+p for p in correct).strip()
for k in repls.keys():
corrected = corrected.replace(k, repls[k])
textlist.append(corrected + '\n')
fw = open(filename_w, 'r+')
fw.writelines(textlist)
fr.close(); fw.close()
def correct_pdf_text(filepath, filename_r, dicio):
"""Reads a pdf file, changes some words; creates and writes a
text file with the changed text. (only the text)
input: a .pdf file with the text to correct.
output: a .txt file with the corrected text.
"""
pdfFileObj = open(filepath, 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFileObj, strict=False)
numPages = pdfReader.getNumPages()
filename_w = filename_r + '.txt'
ft = open(filename_w, 'w')
pages = []
for pageNum in range(pdfReader.numPages):
pageObj = pdfReader.getPage(pageNum)
pageContent = pageObj.extractText()
clean = re.findall(r"[\w]+|[^\s\w]", pageContent)
correct = [dicio[w] if w in dicio else w for w in clean]
pages.append(correct)
joinpages = sum(pages, [])
punct = set('.,;:?!-/%)]ªº')
cleantext = ''.join(p if set(p) <= punct else ' '+p for p in joinpages).strip()
for k in repls.keys():
cleantext = cleantext.replace(k, repls[k])
ft = open(filename_w, 'r+', 1, 'utf-16')
ft.write(cleantext)
pdfFileObj.close(); ft.close()
def correct_docx_text(filepath, filename_r, dicio):
"""Reads a docx file, changes some words; creates and writes a
new docx file with the changed text. (only the text)
input: a .docx file with the text to correct.
output: a new .docx file with the corrected text.
"""
doc = docx.Document(filepath)
filename_w = 'new-' + filename_r
newdoc = docx.Document()
for par in doc.paragraphs:
text = par.text
worklist = re.findall(r"[\w]+|[^\s\w]", text)
correct = [dicio[w] if w in dicio else w for w in worklist]
punct = set('.,;:?!-/%)]ªº')
corrected = ''.join(p if set(p) <= punct else ' '+p for p in correct).strip()
for k in repls.keys():
corrected = corrected.replace(k, repls[k])
newdoc.add_paragraph(corrected)
newdoc.save(filename_w)
def correct_odt_text(filepath, filename_r, dicio):
"""Reads an odt file, changes some words; creates and writes a
new odt file with the changed text. (only the text)
input: a .odt file with the text to correct.
output: a new .odt file with the corrected text.
"""
zf = zipfile.ZipFile(filepath,"r")
con = zf.read('content.xml')
doc = xml.dom.minidom.parseString(con)
pars = doc.getElementsByTagName('text:p')
filename_w = 'new-' + filename_r
newdoc = ezodf.newdoc('odt', filename_w)
for par in pars:
for ch in par.childNodes:
if ch.nodeType == ch.TEXT_NODE:
text = ch.data
worklist = re.findall(r"[\w]+|[^\s\w]", text)
correct = [dicio[w] if w in dicio else w for w in worklist]
punct = set('.,;:?!-/%)]ªº')
corrected = ''.join(p if set(p) <= punct else ' '+p for p in correct).strip()
for k in repls.keys():
corrected = corrected.replace(k, repls[k])
newdoc.body.append(ezodf.Paragraph(corrected))
newdoc.save()
def correct_html_text(filepath, filename_r, dicio):
"""Reads a html file, changes some words; creates and writes a
new html file with the changed text. (only the text)
input: a .html file with the text to correct.
output: a new .html file with the corrected text.
"""
html_doc = open(filepath)
soup = BeautifulSoup(html_doc, 'lxml')
text = soup.get_text()
worklist = re.findall(r"[\w]+|[^\s\w]", text)
correct = [dicio[w] if w in dicio else w for w in worklist]
punct = set('.,;:?!-/%)]ªº')
corrected = ''.join(p if set(p) <= punct else ' '+p for p in correct).strip()
for k in repls.keys():
corrected = corrected.replace(k, repls[k])
filename_w = 'new-' + filename_r
new_doc = open(filename_w, 'w', 1, 'utf-16')
new_doc.write(corrected)
html_doc.close(), new_doc.close()
def correct_epub_text(filepath, filename_r, dicio):
"""Reads a epub file, changes some words; creates and writes a
text file with the changed text. (only the text)
input: a .epub file with the text to correct.
output: a .txt file with the corrected text.
"""
book = epub.read_epub(filepath)
filename_w = filename_r + '.txt'
f = open(filename_w, 'w')
for chapter in book.get_items_of_type(ebooklib.ITEM_DOCUMENT):
bytes = chapter.get_body_content()
soup = BeautifulSoup(bytes, 'html.parser')
text = soup.get_text()
worklist = re.findall(r"[\w]+|[^\s\w]", text)
correct = [dicio[w] if w in dicio else w for w in worklist]
punct = set('.,;:?!-/%)]ªº')
corrected = ''.join(p if set(p) <= punct else ' '+p for p in correct).strip()
for k in repls.keys():
corrected = corrected.replace(k, repls[k])
f = open(filename_w, 'a')
f.write(corrected)
f.close()
# Create minimalistic GUI with tkinter.
# Button to get filepath.
filepath = ''
def callback():
global filepath
filepath = tkinter.filedialog.askopenfilename()
inst.destroy()
inst = Tk()
Button(inst, text='Abrir ficheiro', command=callback).pack(fill=X)
# Text window left side with logo.
root = Toplevel()
text1 = Text(root, height=30, width=15)
text1.insert(END,'\n\n\n\n'' ')
photo = PhotoImage(file='./OrderChristCross-100px-logo1.png')
text1.tag_configure('big', foreground='#906c28',
font=('Tempus Sans ITC', 26, 'bold'))
text1.tag_configure('bold_italics', font=('Arial', 12, 'bold', 'italic'))
text1.image_create(END, image=photo)
text1.pack(side=LEFT)
text1.insert(END,'\nLOBO 1\n', 'big')
text1.insert(END, ''' modificador
ortográfico
para pré-AO90
[ pt-PT ]
v. 0.1.0-beta\n''', 'follow')
# Text window righty side.
text2 = Text(root, height=30, width=50)
scroll = Scrollbar(root, command=text2.yview)
text2.configure(yscrollcommand=scroll.set)
text2.tag_configure('color', font=('Verdana', 10))
text2.tag_bind('follow', '<1>', lambda e,
t=text2: t.insert(END, "Not now, maybe later!"))
info = """
O LOBO corrige, na medida do possível, textos escritos
com a ortografia do AO90 para a ortografia anterior, em
documentos de formato electrónico.
O LOBO lê texto em ficheiros com diversos formatos,
corrige-o e escreve-o num novo ficheiro com o mesmo
formato , se possível, ou em formato mais simples.
O LOBO lê, processa e escreve entre ficheiros com as
seguintes extensões:
docx --> docx | odt --> odt | pdf --> txt
epub --> txt | html --> html | txt --> txt
COMO USAR:
- Clique no botão 'Abrir ficheiro';
- Navegue na janela de busca até encontar o ficheiro;
- Seleccione e abra-o. (Tudo o mais será automático.)
Na pasta do LOBO irá aparecer um novo ficheiro com
nome semelhante e com o texto modificado/corrigido.
"""
text2.insert(END, info, 'color')
disclaim = """ ***
Limitações desta versão inicial do programa:
- Abre e processa apenas um ficheiro de cada vez.
- Lê, modifica e grava somente o texto dos
documentos originais, não os reconstitui.
- Não lê alguns 'pdf' resultantes de conversões a
partir de outros formatos de texto.
- Não tem capacidade para processar correctamente
páginas 'web', só documentos com extensão 'html'.
(Para corrigir textos em páginas 'web' instale as
extensões existentes para o Chrome e o Firefox.)
"""
text2.insert(END, disclaim, 'follow')
text2.pack(side=LEFT)
scroll.pack(side=RIGHT, fill=Y)
mainloop()
# Get filename and file extension from filepath.
print(filepath)
filename_r = filepath.split('/')[-1]
print(filename_r)
ext = filename_r.split('.')[-1]
print(ext)
dicio = make_dictionary(wordlist1, wordlist2)
# Select function using file extension type.
if ext == 'txt' or ext == 'TXT':
correct_txt_text(filepath, filename_r, dicio)
elif ext == 'pdf' or ext == 'PDF':
correct_pdf_text(filepath, filename_r, dicio)
elif ext == 'docx' or ext == 'DOCX':
correct_docx_text(filepath, filename_r, dicio)
elif ext == 'odt' or ext == 'ODT':
correct_odt_text(filepath, filename_r, dicio)
elif ext == 'html' or ext == 'HTML' or ext == 'htm' or ext == 'HTM':
correct_html_text(filepath, filename_r, dicio)
elif ext == 'xml' or ext == 'XML':
correct_html_text(filepath, filename_r, dicio)
elif ext == 'epub' or ext == 'EPUB':
correct_epub_text(filepath, filename_r, dicio)
else:
# On fail message window.
master = tk.Tk()
warning = "Algo impede o acesso ao seu ficheiro."
msg = tk.Message(master, text = warning)
msg.config(bg='PaleVioletRed1', font=('Helvetica', 20, 'italic'))
msg.pack()