-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
58 lines (40 loc) · 1.38 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# -*- coding: utf-8 -*-
from flask import Flask, jsonify
from flaskext.mysql import MySQL
from lib.nobunaga import Nobunaga
from janome.tokenizer import Tokenizer
import yaml
app = Flask(__name__)
config = yaml.load(open('./config.yml', 'r'))
app.config['MYSQL_DATABASE_USER'] = config['db']['username']
app.config['MYSQL_DATABASE_PASSWORD'] = config['db']['password']
app.config['MYSQL_DATABASE_DB'] = config['db']['database']
app.config['MYSQL_DATABASE_HOST'] = config['db']['hostname']
mysql = MySQL()
mysql.init_app(app)
app.config['JSON_AS_ASCII'] = False
tokenizer = Tokenizer()
@app.route("/favicon.ico")
def favicon():
return app.send_static_file("favicon.ico")
@app.route("/log")
@app.route("/log/<word>")
def log(word=None):
nobunaga = Nobunaga(mysql, tokenizer)
res = nobunaga.showlog(word)
return json_response(res)
@app.route('/<message>', methods=['GET'])
def index(message=None):
nobunaga = Nobunaga(mysql, tokenizer)
tokens = nobunaga.parse(message)
query = nobunaga.query(tokens)
result = nobunaga.search(query)
res = nobunaga.answer(message, query, result)
nobunaga.logging(res['error'], message, res['message'])
return json_response(res)
def json_response(res):
response = jsonify(res)
response.headers.add('Access-Control-Allow-Origin', '*')
return response
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)