forked from nishantrathi100/Yelp-Data-Mining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
portal.py
122 lines (99 loc) · 3.71 KB
/
portal.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
from flask import Flask, json, render_template, send_file, send_from_directory
from flask_restful import reqparse, abort, Api, Resource
from util import *
import random
from predictRating import *
from testDataDirectory import *
app = Flask(__name__)
api = Api(app)
# test_filename = 'pickle/'
# test_user = load_pickle(test_filename+'userid.pickle')
# test_ratings = load_pickle(test_filename+'rating.pickle')
# test_business = load_pickle(test_filename+'business.pickle')
# test_docs_csr = load_pickle(test_filename+'docs_csr.pickle')
# test_unique_users = set(test_user)
# test_unique_business = set(test_business)
TODOS = {
'user': {'task': 'API to get list of all Users'},
'business': {'task': 'API to get all Business'},
'predictUserTestReviews': {'task': 'API to get predicted User Reviews Test Data'},
}
def handleError(msg):
abort(404, message=msg)
parser = reqparse.RequestParser()
parser.add_argument('task')
class TDObject:
def __init__(self, userId, actualRating, businessId, review ):
self.userId = userId
self.businessId = businessId
self.actualRating = actualRating
self.review = review
class TestData(Resource):
def get(self):
i = random.randint(0, len(test_user)-1)
#testData = TDObject(test_user[i], test_ratings[i], test_business[i], test_docs_csr[i].todense())
testData = {}
testData["userId"] = getTestUser(i)
testData["actualRating"] = getTestActualRating(i)
testData["businessId"] = getTestBusiness(i)
testData["reviewFull"]= getTestDoc(i)
testData["testDataSampleId"]= i
return testData
class PredictTestData(Resource):
def get(self, testDataId):
userId = test_user[int(testDataId)]
actualRating = test_ratings[int(testDataId)]
businessId = test_business[int(testDataId)]
reviewCSR = test_docs_csr[int(testDataId)]
reviewNMF = test_docs_nmf[int(testDataId)]
reviewFullText = test_docs[int(testDataId)]
p = predictRating(userId, businessId, reviewFullText,reviewNMF)
predictedResponse = {}
predictedResponse["userId"] = userId
predictedResponse["actualRating"] = actualRating
predictedResponse["businessId"] = businessId
predictedResponse["reviewFull"] = test_docs[int(testDataId)]
predictedResponse["predictedRating"] = p
return predictedResponse
# Todo
# shows a single todo item and lets you delete a todo item
class Todo(Resource):
def get(self, id):
abort_if_todo_doesnt_exist(todo_id)
return TODOS[todo_id]
def delete(self, id):
abort_if_todo_doesnt_exist(todo_id)
del TODOS[todo_id]
return '', 204
def put(self, id):
args = parser.parse_args()
task = {'task': args['task']}
TODOS[todo_id] = task
return task, 201
# TodoList
# shows a list of all todos, and lets you POST to add new tasks
class TodoList(Resource):
def get(self):
return TODOS
# def post(self):
# args = parser.parse_args()
# todo_id = int(max(TODOS.keys()).lstrip('todo')) + 1
# todo_id = 'todo%i' % todo_id
# TODOS[todo_id] = {'task': args['task']}
# return TODOS[todo_id], 201
##
## Actually setup the Api resource routing here
##
class MainPage(Resource):
def get(self):
return send_file('templates/index2.html')
@app.route('/templates/<path:path>')
def send_js(path):
return send_from_directory('templates', path)
api.add_resource(MainPage,'/')
api.add_resource(TodoList, '/todos')
api.add_resource(Todo, '/todos/<id>')
api.add_resource(TestData, '/testData')
api.add_resource(PredictTestData, '/predictTestData/<testDataId>')
if __name__ == '__main__':
app.run(debug=True)