File size: 5,106 Bytes
5173453 |
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 |
# coding=utf-8
from flask import Flask, request
import json
import os
import time
import csv
import sys
import re
app=Flask(__name__)
def read_chat_intents_from_file(file_path,domain):
queries = dict()
with open(file_path ) as f:
lines=f.readlines()
count=0
for line in lines:
line=line.strip()
query=json.loads(line)
if query['domain']==domain:
queries[query["iid"]]=query
return queries
datep=re.compile(r"202\d-\d\d-\d\d")
timep=re.compile(r"(202\d-\d\d-\d\d )*\d\d:\d\d[( am)|( pm)|( AM)|(( PM))]*")
@app.route("/transport", methods=["GET", "POST"])
def check():
intent=""
slots=dict()
start_time = time.time()
if request.method == "POST":
jsondata = request.get_data(as_text=True)
if jsondata:
data = json.loads(jsondata)
print(data)
else:
return_dict = {'code': 'FAIL', 'msg': '失败,缺少参数'}
return return_dict
params = ['intent', ]
for param in params:
if param not in data:
return_dict = {'code': 'FAIL', 'msg': '失败,缺少参数:' + param}
return return_dict
intent = data.get('intent')
#iid = str(data.get('iid'))
if "transport_type" in data:
slots["transport_type"] = data.get('transport_type')
if "transport_agency" in data:
slots["transport_agency"] = data.get('transport_agency')
if "business_type" in data:
slots["business_type"] = data.get('business_type')
if "business_name" in data:
slots["business_name"] = data.get('business_name')
if "place_name" in data:
slots["place_name"] = data.get('place_name')
if "to_place_name" in data:
slots["to_place_name"] = data.get('to_place_name')
if "from_place_name" in data:
slots["from_place_name"] = data.get('from_place_name')
if "query" in data:
slots["query"] = data.get('query')
if "date" in data:
slots["date"] = data.get('date')
if "time" in data:
slots["time"] = data.get('time')
#if "timeofday" in data:
# slots["timeofday"] = data.get('timeofday')
if "descriptor" in data:
slots["descriptor"] = data.get('descriptor')
if request.method == "GET":
if "intent" in request.args:
intent = request.args.get("intent")
data=request.args
if "transport_type" in data:
slots["transport_type"] = data.get('transport_type')
if "transport_agency" in data:
slots["transport_agency"] = data.get('transport_agency')
if "business_type" in data:
slots["business_type"] = data.get('business_type')
if "business_name" in data:
slots["business_name"] = data.get('business_name')
if "place_name" in data:
slots["place_name"] = data.get('place_name')
if "to_place_name" in data:
slots["to_place_name"] = data.get('to_place_name')
if "from_place_name" in data:
slots["from_place_name"] = data.get('from_place_name')
if "query" in data:
slots["query"] = data.get('query')
if "date" in data:
slots["date"] = data.get('date')
if "time" in data:
slots["time"] = data.get('time')
#if "timeofday" in data:
# slots["timeofday"] = data.get('timeofday')
if "descriptor" in data:
slots["descriptor"] = data.get('descriptor')
response="operated successfully"
query = {"intent":intent,"slots":slots}
if intent =="transport_taxi" or intent =="transport_ticket":
if "to_place_name" not in slots and "from_place_name" not in slots:
response="what is your destination?"
elif intent =="transport_traffic":
response="find the following results. from ... road to ...."
elif intent=="transport_query":
if len(slots)==0:
response="what do you want to query?"
else:
response="find the following results. from ... to ...."
else:
response="unsupported intent"
for key,value in slots.items() :
if key.find("time")!=-1:
if timep.match(value) is None:
response="time format not right"
if key.find("date")!=-1:
if datep.match(value) is None:
response="date format not right"
print('耗时:' + str(time.time()-start_time))
print('----------------------')
return_dict = {}
return_dict['code'] = 'SUCCESS'
return_dict['msg'] = '成功'
contents = {}
contents['response'] = response
contents['query'] = query
return_dict['data'] = contents
return return_dict
print("模型load完毕")
if __name__ == "__main__":
print("启动开始---------")
port = sys.argv[1]
app.run(debug=False, host='0.0.0.0',port=port)
print("启动完成---------")
|