forked from penguin-knight/eng2jpn_srv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheng2jpn_srv.py
44 lines (36 loc) · 1.13 KB
/
eng2jpn_srv.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
#!/usr/bin/env python3
import sys
import re
import urllib.parse
from bottle import route, run, template
from bottle import request, post, get
from bottle import static_file
from googletrans import Translator as Trans
## setting
HOST = "localhost"
PORT = 8080
##
trans = Trans()
@route('/static/js/<filename>')
def js(filename):
return static_file(filename, root="static/js")
@route('/static/css/<filename>')
def css(filename):
return static_file(filename, root="static/css")
@route('/')
def index():
return template("index",eng_txt=None, jpn_txt=None)
@post('/')
def translate():
#_input = request.forms.get('eng_txt',None)
_input = request.forms.eng_txt
_input = urllib.parse.quote(_input,encoding="utf-8")
_input = re.sub('%0D%0A', ' ', _input)
_input = re.sub('- ', '', _input)
_input = urllib.parse.unquote_plus(_input,encoding="utf-8")
output = trans.translate(_input,dest='ja')
output = re.sub('。','.\n',output.text)
output = re.sub('、',',',output)
return template("index", eng_txt=_input, jpn_txt=output)
if __name__=='__main__':
run(host=HOST,port=PORT,debug=True,reloader=True)