-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.py
205 lines (144 loc) · 5.41 KB
/
controller.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
import os
import json
from flask import Flask, render_template, redirect, request, flash, session, url_for
from werkzeug import secure_filename
import file_reader
import text_processing
import german_processing as german
import spanish_processing as spanish
import wikipedia_linker as wikipedia
import geocoding as geocoding
UPLOAD_FOLDER = 'static/uploads'
ALLOWED_EXTENSIONS = frozenset(['txt'])
app = Flask(__name__)
app.secret_key = os.environ.get("FLASK_APP_KEY")
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
langcodes = {'en': 'English',
'es': 'Spanish',
'de': 'German',
'fr': 'French',
'it': 'Italian',
'ja': 'Japanese',
'ar': 'Arabic',
'ru': 'Russian',
'pt': 'Portuguese',
'zh': 'Chinese',
}
def is_file_allowed(filename):
"""Is this file allowed?"""
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@app.route('/')
def index():
session['target_lang'] = "ru"
return render_template("index.html")
@app.route('/upload_file', methods=['POST'])
def upload_file():
""" Uploads files or converts URLs into simple text files """
#sets the source language
sourcelang = request.form.get('sourcelang')
session['source_lang'] = sourcelang
session['source_name'] = langcodes[sourcelang]
if sourcelang != 'en':
session['target_lang'] = 'en'
session['target_name'] = langcodes[session['target_lang']]
#saves an uploaded text file to the uploads folder
if request.files['filename']:
file_ = request.files['filename']
if file_ and is_file_allowed(file_.filename):
filename = secure_filename(file_.filename)
file_.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
session['filepath'] = os.path.join(app.config['UPLOAD_FOLDER'], filename)
return redirect(url_for('editor'))
else:
#flash ("Make sure you're uploading a %s file" % [i for i in ALLOWED_EXTENSIONS])
return render_template("index.html")
#grabs a URL and saves it as a temporary file in the uploads folder
elif request.form.get("url"):
url = request.form.get('url')
newfile = file_reader.read_url_all(url)
session['filepath'] = newfile
return redirect(url_for('editor'))
else:
#flash ("Enter a source file or URL")
return render_template("index.html")
@app.route('/editor', methods=["GET", "POST"])
def editor():
""" Shows the main editor template"""
filepath = session['filepath']
target_lang = session['target_lang']
session['target_name'] = langcodes[session['target_lang']]
return render_template("editor.html",
target_lang=target_lang)
@app.route('/set_target_lang', methods=["GET"])
def set_target_language():
"""Changes the target language"""
target_lang = request.args.get("target_lang")
session['target_lang'] = target_lang
return redirect(url_for('editor'))
@app.route('/text')
def get_text():
text = file_reader.read_file_pretty(session['filepath'])
return json.dumps(text)
@app.route('/named_entities')
def get_entities():
"""Loads all entity names into a JSON for highlighting"""
text = file_reader.read_file(session['filepath'])
session['text'] = (text)
source_lang = session['source_lang']
if source_lang == 'de':
fullset =text_processing.single_set(german.ner(text))
elif source_lang == 'es':
fullset =text_processing.single_set(spanish.ner(text))
else:
fullset = text_processing.single_set(text_processing.ner_tagger(text))
return json.dumps(fullset)
@app.route('/get_places', methods=["POST"])
def get_places():
"""Extracts entities from text and returns lists and links to wikipedia"""
text = file_reader.read_file(session['filepath'])
target_lang = session['target_lang']
source_lang = (session['source_lang']).encode('ascii', 'replace')
#Uses the NER tagger to get entities
if source_lang == 'de':
nouns = german.pos(text)
organizations, locations, people = german.ner(text)
elif source_lang == 'es':
nouns = spanish.pos(text)
organizations, locations, people = spanish.ner(text)
else:
nouns = text_processing.nouns_only(text_processing.preprocess(text))
organizations, locations, people = text_processing.ner_tagger(text)
#Checks the type of entity that is being requested
ent = request.form['ent']
if ent == "places":
if locations:
loclist = wikipedia.get_entity_info(locations, target_lang, source_lang)
downfile = file_reader.write_csv_file(loclist)
geocodes = geocoding.geocode(locations)
return render_template("places.html", locations = loclist, geocodes = json.dumps(geocodes), downfile=downfile)
else:
return render_template("places.html")
elif ent == "organizations":
if organizations:
orglist = wikipedia.get_entity_info(organizations, target_lang, source_lang)
downfile = file_reader.write_csv_file(orglist)
return render_template("orgs.html", organizations = orglist, downfile=downfile)
else:
return render_template("orgs.html")
elif ent == "people":
if people:
peoplelist = wikipedia.get_entity_info(people, target_lang, source_lang)
downfile = file_reader.write_csv_file(peoplelist)
return render_template("people.html", people = peoplelist, downfile=downfile)
else:
return render_template("people.html")
elif ent == "nouns":
if nouns:
nounlist = wikipedia.get_entity_info(nouns, target_lang, source_lang)
downfile = file_reader.write_csv_file(nounlist)
return render_template("other.html", nouns = nounlist, downfile=downfile)
else:
return render_template("other.html")
if __name__ == "__main__":
app.run(debug = True)