-
Notifications
You must be signed in to change notification settings - Fork 3
/
app_backup.py
183 lines (145 loc) · 3.72 KB
/
app_backup.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
import os, re, json
from flask import *
from ocr import ocr
#from entity_seperation import seperate_entities
# INITIALIZATION -----------------------------------------------------------------------------------------------
UPLOAD_FOLDER = 'uploads'
FILE_NAME = ''
ocrtext = ''
#parts = ['topic', 'section', 'olist', 'ulist']
syntax = {
'#': 'title',
'##': 'subtitle',
'-': 'upoint',
')': 'opoint',
'*': 'def',
'=>': 'eq'
}
rsyntax = dict((b,a) for (a,b) in syntax.items())
seps = ''.join([sep for sep in syntax.keys()])
app = Flask(__name__, static_url_path='/static')
app.config['TEMPLATES_AUTO_RELOAD'] = True
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# FUNCTIONS -----------------------------------------------------------------------------------------------
@app.route('/upload', methods = ['POST'])
def upload():
global FILE_NAME
if request.method == 'POST':
f = request.files['file']
f.save(os.path.join(UPLOAD_FOLDER, f.filename))
FILE_NAME = f.filename
return redirect(url_for('display'))
def label(text):
global syntax, seps
labelled = []
titlesecs = re.split(r'(^|\s)#\s', text)[2:]
print(titlesecs)
for titlesec in titlesecs:
title = titlesec.split('\n')[0]
subtitlesecs = re.split(r'\s##\s', titlesec)[1:]
print(subtitlesecs)
temp = {
'text': title,
'tag': 'title',
'body': []
}
for subtitlesec in subtitlesecs:
subtitle = subtitlesec.split('\n')[0]
temp2 = {
'text': subtitle,
'tag': 'subtitle',
'body': []
}
items = subtitlesec.split('\n')[1:]
inOList = False
inUList = False
for item in items:
if item != '':
splitted = item.split(' ')
first = splitted[0]
if first.endswith(')'):
first = ')'
try:
tag = syntax[first]
except:
tag = 'other'
# list grouping
if tag == 'opoint':
if inUList == True:
temp2['body'].append(temp3)
inUList = False
if inOList == False:
tagtype = 'olist'
temp3 = {
'tag': tagtype,
'body': [{
'text': item,
'tag': tag
}]
}
inOList = True
else:
temp4 = {
'text': item,
'tag': tag
}
temp3['body'].append(temp4)
elif tag == 'upoint':
if inOList == True:
temp2['body'].append(temp3)
inOList = False
if inUList == False:
tagtype = 'ulist'
temp3 = {
'tag': tagtype,
'body': [{
'text': item,
'tag': tag
}]
}
inUList = True
else:
temp4 = {
'text': item,
'tag': tag
}
temp3['body'].append(temp4)
else:
if inUList == True:
temp2['body'].append(temp3)
inUList = False
elif inOList == True:
temp2['body'].append(temp3)
inOList = False
temp3 = {
'text': ' '.join(splitted[1:]),
'tag': tag
}
inOList = False
inUList = False
temp2['body'].append(temp3)
temp['body'].append(temp2)
labelled.append(temp)
print(labelled)
return labelled
# ROUTES -----------------------------------------------------------------------------------------------
@app.route('/')
def index():
return render_template('/index.html')
@app.route('/display')
def display():
global UPLOAD_FOLDER, FILE_NAME, ocrtext
ocrtext = ocr(FILE_NAME)
f = open('test.txt', 'w+')
f.write(ocrtext)
f.close()
info = label(ocrtext)
with open('test.json', 'w+') as g:
json.dump(info, g)
return render_template('display.html', ocrtext = info)
@app.route('/about')
def about():
return render_template('about.html')
# -----------------------------------------------------------------------------------------------
if __name__ == '__main__':
app.run(debug=True)