-
Notifications
You must be signed in to change notification settings - Fork 0
/
conjugator.py
343 lines (279 loc) · 10.4 KB
/
conjugator.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import pandas as pd
import requests
from flask import Flask, request
from flask_jwt import JWT, jwt_required
from werkzeug.security import safe_str_cmp
from big_list_generator.big_list_generator import generate_big_list
from language_functions import armenian, russian, polish, thai, estonian, japanese, czech, mandarin
from mandarin_flashcard_generator.mandarin_flashcard_generator import generate_dataframe
from users_password import users
username_table = {u.username: u for u in users}
userid_table = {u.id: u for u in users}
def authenticate(username, password):
user = username_table.get(username, None)
if user and safe_str_cmp(user.password.encode('utf-8'), password.encode('utf-8')):
return user
def identity(payload):
user_id = payload['identity']
return userid_table.get(user_id, None)
app = Flask(__name__)
app.config['SECRET_KEY'] = '0075e7f61334ae26a0e0fd2be0e1f4dd4241a6291bffbec1d9c403c3b9f1'
jwt = JWT(app, authenticate, identity)
bad_request_or_no_data = 'BRDN'
# ******************* Polish functions *******************
@app.route('/conjugate_polish', methods=['POST'])
@jwt_required()
def conjugate_polish():
language = 'Polish'
verb = request.form['verb']
tense = request.form['tense']
person = request.form['person']
aspect = request.form['aspect']
try:
conjugation_cell = polish.conjugate(language, verb, tense, person, aspect)
return conjugation_cell
except Exception:
return bad_request_or_no_data
@app.route('/decline_polish_noun', methods=['POST'])
@jwt_required()
def decline_polish_noun():
language = 'Polish'
noun = request.form['noun']
# "case" is a reserved word in JS
noun_case = request.form['noun_case']
number = request.form['number']
try:
declined_noun = polish.decline_noun(language, noun, noun_case, number)
return declined_noun
except Exception:
return bad_request_or_no_data
@app.route('/decline_polish_adjective', methods=['POST'])
@jwt_required()
def decline_polish_adjective():
language = 'Polish'
adjective = request.form['adjective']
adjective_case = request.form['adjective_case']
gender_and_number = request.form['gender_and_number']
try:
declined_adjective = polish.decline_adjective(language, adjective, adjective_case, gender_and_number)
return declined_adjective
except Exception:
return bad_request_or_no_data
# ******************* Russian functions *******************
@app.route('/conjugate_russian', methods=['POST'])
@jwt_required()
def conjugate_russian():
language = 'Russian'
verb = request.form['verb']
tense = request.form['tense']
person = request.form['person']
aspect = request.form['aspect']
try:
conjugation_cell = russian.conjugate(language, verb, tense, person, aspect)
return conjugation_cell
except Exception:
return bad_request_or_no_data
@app.route('/decline_russian_noun', methods=['POST'])
@jwt_required()
def decline_russian_noun():
language = 'Russian'
noun = request.form['noun']
# "case" is a reserved word in JS
noun_case = request.form['noun_case']
number = request.form['number']
try:
declined_noun = russian.decline_noun(language, noun, noun_case, number)
return declined_noun
except Exception:
return bad_request_or_no_data
@app.route('/decline_russian_adjective', methods=['POST'])
@jwt_required()
def decline_russian_adjective():
language = 'Russian'
adjective = request.form['adjective']
adjective_case = request.form['adjective_case']
gender_and_number = request.form['gender_and_number']
try:
declined_adjective = russian.decline_adjective(language, adjective, adjective_case, gender_and_number)
return declined_adjective
except Exception:
return bad_request_or_no_data
# ******************* Czech functions *******************
@app.route('/conjugate_czech', methods=['POST'])
@jwt_required()
def conjugate_czech():
language = 'Czech'
verb = request.form['verb']
tense = request.form['tense']
person = request.form['person']
aspect = request.form['aspect']
try:
conjugation_cell = czech.conjugate(language, verb, tense, person, aspect)
return conjugation_cell
except Exception:
return bad_request_or_no_data
@app.route('/decline_czech_noun', methods=['POST'])
@jwt_required()
def decline_czech_noun():
language = 'Czech'
noun = request.form['noun']
# "case" is a reserved word in JS
noun_case = request.form['noun_case']
number = request.form['number']
try:
declined_noun = czech.decline_noun(language, noun, noun_case, number)
return declined_noun
except Exception:
return bad_request_or_no_data
@app.route('/decline_czech_adjective', methods=['POST'])
@jwt_required()
def decline_czech_adjective():
language = 'Czech'
adjective = request.form['adjective']
adjective_case = request.form['adjective_case']
gender_and_number = request.form['gender_and_number']
try:
declined_adjective = czech.decline_adjective(language, adjective, adjective_case, gender_and_number)
return declined_adjective
except Exception:
return bad_request_or_no_data
# ******************* Armenian functions *******************
@app.route('/conjugate_armenian', methods=['POST'])
@jwt_required()
def conjugate_armenian():
language = 'Armenian'
verb = request.form['verb']
tense = request.form['tense']
person = request.form['person']
try:
conjugation_cell = armenian.conjugate(language, verb, tense, person)
return conjugation_cell
except Exception:
return bad_request_or_no_data
# ******************* Thai functions *******************
@app.route('/thai_to_ipa', methods=['POST'])
@jwt_required()
def send_ipa():
thai_text = request.form['thai_text']
try:
ipa = thai.thai_to_ipa(thai_text)
return ipa
except Exception:
return bad_request_or_no_data
@app.route('/tokenize_thai', methods=['POST'])
@jwt_required()
def tokenize_thai():
thai_text = request.form['thai_text']
try:
return thai.tokenize_thai(thai_text)
except Exception:
return bad_request_or_no_data
# ******************* Japanese functions *******************
@app.route('/baidu_translate', methods=['POST'])
@jwt_required()
def send_baidu_translation():
source_text = request.form['sourceText']
source_language = request.form['sourceLanguage']
target_language = request.form['targetLanguage']
try:
translator = japanese.Translator()
translation = translator.translate(source_language, target_language, source_text)
return translation
except Exception:
return bad_request_or_no_data
@app.route('/mirai_translate', methods=['POST'])
@jwt_required()
def send_mirai_translation():
source_text = request.form['sourceText']
source_language = request.form['sourceLanguage']
target_language = request.form['targetLanguage']
try:
session = requests.session()
translation = japanese.mirai_translate(source_text, source_language, target_language, session)
return translation
except Exception:
return bad_request_or_no_data
# ******************* Estonian functions *******************
@app.route('/conjugate_estonian', methods=['POST'])
@jwt_required()
def conjugate_estonian():
verb = request.form['verb']
tense = request.form['tense']
try:
conjugation_cell = estonian.conjugate_verb(verb, tense)
return conjugation_cell
except Exception:
return bad_request_or_no_data
@app.route('/decline_estonian_noun', methods=['POST'])
@jwt_required()
def decline_estonian_noun():
noun = request.form['noun']
case = request.form['case']
try:
conjugation_cell = estonian.decline_noun(noun, case)
return conjugation_cell
except Exception:
return bad_request_or_no_data
@app.route('/decline_estonian_adjective', methods=['POST'])
@jwt_required()
def decline_estonian_adjective():
adjective = request.form['adjective']
case = request.form['case']
try:
conjugation_cell = estonian.decline_adjective(adjective, case)
return conjugation_cell
except Exception:
return bad_request_or_no_data
# ******************* Chinese functions *******************
@app.route('/generate_biglist', methods=['GET', 'POST'])
@jwt_required()
def send_big_list():
json_array = request.json
chinese_big_list = []
chinese_sentences_column = []
for element in json_array:
try:
chinese_sentences_column.append(element[0][0])
# exception handling is necessary because some elements are None and cannot be subscripted
except TypeError:
chinese_sentences_column.append('')
try:
chinese_big_list.append(element[1][0])
except TypeError:
chinese_big_list.append('')
# each row is a list of lists: [[el_1], [el_2]]. We "flatten" so it becomes like this: [el_1, el_2]
flattened_json = []
for element in json_array:
row = []
for cell in element:
try:
row.append(cell[0])
except TypeError:
row.append('')
flattened_json.append(row)
# create the dataframe with the resulting json
df = pd.DataFrame(flattened_json, columns=['Chinese sentence', 'Chinese keyword'])
# generate the dataframe to send back to google sheets
dataframe_to_send = generate_big_list(df)
dataframe_to_send_json = dataframe_to_send.to_json(orient='values')
return dataframe_to_send_json
@app.route('/pinyin', methods=['GET', 'POST'])
@jwt_required()
def pinyin():
sentence = request.form['sentence']
try:
return mandarin.convert_mandarin_script_to_pinyin(sentence)
except Exception:
return bad_request_or_no_data
@app.route('/generate_mandarin_flashcards', methods=['GET', 'POST'])
@jwt_required()
def generate_mandarin_flashcards():
json_array = request.json
mandarin_lesson_dataframe = pd.DataFrame.from_dict(json_array, orient='columns')
flashcards_dataframe = generate_dataframe(mandarin_lesson_dataframe)
flashcards_dataframe.to_csv('flashcards_dataframe.csv')
flashcards_dataframe_json = flashcards_dataframe.to_json(orient='values')
print(flashcards_dataframe_json)
return flashcards_dataframe_json
if __name__ == "__main__":
app.run(host='0.0.0.0', debug=True)