/
app.py
124 lines (92 loc) · 3.18 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
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
import os
from flask import Flask, jsonify, send_from_directory
from flask_cors import CORS
from flask_restful import reqparse, Api, Resource
import classifier
app = Flask(__name__, static_folder='build')
app.config['SECRET_KEY'] = 'supersecretkey'
api = Api(app)
CORS(app)
parser = reqparse.RequestParser()
parser.add_argument('month')
parser.add_argument('startDay')
parser.add_argument('endDay')
parser.add_argument('hour')
parser.add_argument('crime')
parser.add_argument('location')
parser.add_argument('day')
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def index(path):
if path != "" and os.path.exists("build/" + path):
return send_from_directory('build', path)
else:
return send_from_directory('build', 'index.html')
@app.route('/dataset')
def dataset():
return send_from_directory('dataset', 'crime_dataset.csv')
@app.route('/locationIds')
def location_ids():
return jsonify(classifier.location_ids())
@app.route('/crimeIds')
def crime_ids():
return jsonify(classifier.crime_ids())
class UpdateDataset(Resource):
def post(self):
args = parser.parse_args()
classifier.update_dataset(
int(args['month']),
int(args['day']),
int(args['hour']),
int(args['location']),
int(args['crime']))
return 'SUCCESS', 200
# noinspection PyMethodMayBeStatic
class CrimeClassifier(Resource):
def post(self):
args = parser.parse_args()
temp_array = []
start_day = int(args['startDay'])
end_day = int(args['endDay'])
end_day = start_day + 1 if end_day == -1 else end_day
end_day = start_day + 1 if start_day == end_day else end_day
# if end_day == -1:
# end_day = start_day + 1
# if start_day == end_day:
# end_day = start_day + 1
temp_result = classifier.crime_interval_classify(
int(args['month']),
int(start_day),
int(end_day),
int(args['hour']),
int(args['location']))
for key, value in temp_result.items():
temp_array.append({"crime": key, "value": value})
return temp_array
# noinspection PyMethodMayBeStatic
class LocationClassifier(Resource):
def post(self):
args = parser.parse_args()
temp_array = []
start_day = int(args['startDay'])
end_day = int(args['endDay'])
end_day = start_day + 1 if end_day == -1 else end_day
end_day = start_day + 1 if start_day == end_day else end_day
# if end_day == -1:
# end_day = start_day + 1
# if start_day == end_day:
# end_day = start_day + 1
temp_result = classifier.location_interval_classify(
int(args['month']),
int(start_day),
int(end_day),
int(args['hour']),
int(args['crime']))
for key, value in temp_result.items():
temp_array.append({"brgy": key, "value": value})
return temp_array
api.add_resource(CrimeClassifier, '/crime')
api.add_resource(LocationClassifier, '/location')
api.add_resource(UpdateDataset, '/update')
if __name__ == '__main__':
app.run()