-
Notifications
You must be signed in to change notification settings - Fork 1
/
nlp1.py
235 lines (178 loc) · 5.19 KB
/
nlp1.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
from gingerit.gingerit import GingerIt
import flask
from flask import request
import werkzeug
from docx import Document
from docx.enum.text import WD_COLOR_INDEX
import pytesseract
import argparse
import cv2
import os
from PIL import Image
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import requests
app = flask.Flask(__name__)
@app.route('/', methods = ['GET', 'POST'])
def handle_request():
imagefile = flask.request.files['image']
filename = werkzeug.utils.secure_filename(imagefile.filename)
print("\nReceived image File name : " + imagefile.filename)
print(request.environ.get('HTTP_X_REAL_IP', request.remote_addr))
imagefile.save(filename)
#text = 'At the start ofschool Dora was afrad of her new Teacher.'
#text = 'It the end'
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract'
print("ok")
# load the example image and convert it to grayscale
image = cv2.imread('androidFlask.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
'''
# write the grayscale image to disk as a temporary file so we can
# apply OCR to it
filename = "{}.png".format(os.getpid())
cv2.imwrite(filename, gray)
'''
# load the image as a PIL/Pillow image, apply OCR, and then delete
# the temporary file
text = pytesseract.image_to_string(Image.open(filename))
text=text.replace('\n\n',' ')
text=text.replace('\n',' ')
print(text)
parser = GingerIt()
print(parser.parse(text))
res = parser.parse(text)['result']
document = Document()
document.add_paragraph('Original Text')
document.add_paragraph(text)
document.add_paragraph("Corrected Text")
document.add_paragraph(res)
document.add_paragraph("Text with errors highlighted")
correct = parser.parse(text)['corrections']
correct.reverse()
p2 = document.add_paragraph()
l = text.split(" ")
for e in l:
if len(correct)==0:
p2.add_run(e)
p2.add_run(' ')
for ele in correct:
if '.' in e:
x = e.replace('.','')
if x == ele['text']:
font = p2.add_run(x).font
font.highlight_color = WD_COLOR_INDEX.RED
p2.add_run('.')
p2.add_run(' ')
correct.pop(0)
break
else:
p2.add_run(x)
p2.add_run('.')
p2.add_run(' ')
break
elif ',' in e:
x = e.replace(',','')
if x == ele['text']:
font = p2.add_run(x).font
font.highlight_color = WD_COLOR_INDEX.RED
p2.add_run(',')
p2.add_run(' ')
correct.pop(0)
break
else:
p2.add_run(x)
p2.add_run(',')
p2.add_run(' ')
break
if '!' in e:
x = e.replace('!','')
if x == ele['text']:
font = p2.add_run(x).font
font.highlight_color = WD_COLOR_INDEX.RED
p2.add_run('!')
p2.add_run(' ')
correct.pop(0)
break
else:
p2.add_run(x)
p2.add_run('!')
p2.add_run(' ')
break
if '?' in e:
x = e.replace('?','')
if x == ele['text']:
font = p2.add_run(x).font
font.highlight_color = WD_COLOR_INDEX.RED
p2.add_run('?')
p2.add_run(' ')
correct.pop(0)
break
else:
p2.add_run(x)
p2.add_run('?')
p2.add_run(' ')
break
else:
if e == ele['text']:
font = p2.add_run(e).font
font.highlight_color = WD_COLOR_INDEX.RED
p2.add_run(' ')
correct.pop(0)
break
else:
p2.add_run(e)
p2.add_run(' ')
break
document.save("out.docx")
fromaddr = "[email protected]"
toaddr = "[email protected]"
# instance of MIMEMultipart
msg = MIMEMultipart()
# storing the senders email address
msg['From'] = fromaddr
# storing the receivers email address
msg['To'] = toaddr
# storing the subject
msg['Subject'] = "Result from Programmar"
# string to store the body of the mail
body = '''
Hello,
Following is an attachment which contains the corrected
text document as per your request from our application
Programmar
Thank you
'''
# attach the body with the msg instance
msg.attach(MIMEText(body, 'plain'))
# open the file to be sent
filename = "out.docx"
attachment = open("out.docx", "rb")
# instance of MIMEBase and named as p
p = MIMEBase('application', 'octet-stream')
# To change the payload into encoded form
p.set_payload((attachment).read())
# encode into base64
encoders.encode_base64(p)
p.add_header('Content-Disposition', "attachment; filename= %s" % filename)
# attach the instance 'p' to instance 'msg'
msg.attach(p)
# creates SMTP session
s = smtplib.SMTP('smtp.gmail.com', 587)
# start TLS for security
s.starttls()
# Authentication
s.login(fromaddr, "Programmar123")
# Converts the Multipart msg into a string
text = msg.as_string()
# sending the mail
s.sendmail(fromaddr, toaddr, text)
# terminating the session
s.quit()
return "Please Check Email"
if __name__ == "__main__":
app.debug = True
app.run(host='0.0.0.0', port = 5000)