-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
176 lines (139 loc) · 4.57 KB
/
api.py
File metadata and controls
176 lines (139 loc) · 4.57 KB
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
#coding:utf8
"""
API is used to check taboo and corrector words:
* Taboo check:
Request method is `POST`, URL is `http://<hostname>:<port>/text/api/v1.0/taboo`,
payload data contains some keys, like `id`, `text`
Example:
$ curl -i -H "Content-Type: application/json" -X POST -d '{"id": 123, "text": "历史学系"}' http://localhost:5000/text/api/v1.0/taboo
HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 236
Server: Werkzeug/0.11.15 Python/3.6.10
Date: Thu, 07 May 2020 03:27:30 GMT
{
"cost_time": 0.00012493133544921875,
"data": {
"taboo_status": false,
"words": []
},
"description": "it's successfull that check taboo words",
"id": 123,
"status": 200,
"text": "\u5386\u53f2\u5b66\u7cfb"
}
* Correct check:
Request method is `POST`, URL is `http://<hostname>:<port>/text/api/v1.0/correct`,
payload data contains some keys, like `id`, `text`
Example:
$ curl -i -H "Content-Type: application/json" -X POST -d '{"id": 123, "text": "历史学系"}' http://localhost:5000/text/api/v1.0/correct
HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 186
Server: Werkzeug/0.11.15 Python/3.6.10
Date: Wed, 06 May 2020 08:57:16 GMT
{
"cost_time": 0.017215967178344727,
"description": "it's successfull that check words",
"id": 123,
"report": false,
"status": 200,
"text": "\u5386\u53f2\u5b66\u7cfb"
}
"""
from __future__ import absolute_import
import time
from flask import Flask, jsonify, make_response, abort, request
from TextAPI import check_taboo, corrector
app = Flask(__name__)
@app.route("/", methods=["GET"])
def index():
"""Homepage"""
return "<h1>访问文本处理API</h1>"
@app.route("/text/api/v1.0/taboo", methods=["GET"])
def report_taboo(id_):
return f"<h1>访问文本处理敏感词检验 API,发送 POST 请求</h1>"
@app.route("/text/api/v1.0/correct", methods=["GET"])
def report_corrector(id_):
return f"<h1>访问文本处理错误检验 API,发送 POST 请求</h1>"
@app.route("/text/api/v1.0/taboo", methods=["POST"])
def taboo_stamp():
"""Request Taboo Check
Check taboo
"""
if not request.json or "text" not in request.json:
abort(400)
# check taboo
start_time = time.time()
taboo = check_taboo.taboo_stamp(request.json["text"])
end_time = time.time()
result = {
"id": request.json["id"],
"cost_time": end_time - start_time,
"text": request.json["text"],
"data": taboo,
"description": "it's successfull that check taboo words",
"status": 200
}
return jsonify(result), 200
@app.route("/text/api/v2.0/taboo", methods=["POST"])
def taboo_stamp2():
"""Request Taboo Check
Check taboo with regex
"""
if not request.json or "text" not in request.json:
abort(400)
# check taboo
start_time = time.time()
taboo = check_taboo.taboo_stamp2(request.json["text"])
end_time = time.time()
result = {
"id": request.json["id"],
"cost_time": end_time - start_time,
"text": request.json["text"],
"data": taboo,
"description": "it's successfull that check taboo words",
"status": 200
}
return jsonify(result), 200
@app.route("/text/api/v1.0/correct", methods=["POST"])
def correct_stamp():
"""Request Correct Check
Check word correct
"""
if not request.json or "text" not in request.json:
abort(400)
# check correct word
start_time = time.time()
correct = corrector.text_corrector(request.json["text"])
end_time = time.time()
result = {
"id": request.json["id"],
"cost_time": end_time - start_time,
"text": request.json["text"],
"data": correct,
"description": "it's successfull that check words",
"status": 200
}
return jsonify(result), 200
##################################
### flask customize error handler
####
@app.errorhandler(404)
def not_found(error):
"""Not Found
Status code 404 response
"""
return make_response(jsonify(
{"status": "fail", "description": "Not found"}
), 404)
@app.errorhandler(400)
def bad_request(error):
"""Bad Request
Get wrong parameters, which result in can't parse text information.
"""
return make_response(jsonify(
{"status": "fail", "description": "Bad Request"}
), 400)
if __name__ == "__main__":
app.run(debug=True)