-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
37 lines (27 loc) · 951 Bytes
/
app.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
from flask import Flask, request, abort, jsonify
from flask_cors import CORS
from furigana.furigana import split_furigana
app = Flask(__name__)
CORS(app)
def convert_text_to_furigana_html(text):
for pair in split_furigana(text):
if len(pair) == 2:
kanji, hira = pair
yield ("<ruby><rb>{0}</rb><rt>{1}</rt></ruby>".
format(kanji, hira))
else:
yield pair[0]
yield ''
@app.route('/')
def hello_world():
return ''.join(convert_text_to_furigana_html('お寿司を食べたい'))
@app.route('/furigana', methods=['POST', 'OPTIONS'])
def furigana():
if request.method == 'OPTIONS':
return jsonify({})
if not request.json or not 'kanji' in request.json:
abort(400)
return jsonify({'furigana': ''.join(
convert_text_to_furigana_html(request.json['kanji']))}), 200
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')