-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecommend.py
74 lines (70 loc) · 2.68 KB
/
recommend.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
import pickle as pkl
import json
from flask import Flask, request, jsonify
import pandas as pd
import requests
import random;
import os
from flask_cors import CORS, cross_origin
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
def Sorting(lst):
lst2 = sorted(lst, key=len)
return lst2
def Convert(string):
li = list(string.split(","))
return li
@app.route('/predict', methods=['POST'])
def predict():
data = request.get_json()
data=data['handle']
# data= Convert(data['questions'])
def getdata(handle):
url = f'https://codeforces.com/api/user.status?handle={handle}'
response = requests.get(url)
data1 = response.json()
count = 0
arr2 = []
for element in data1['result']:
if element['verdict'] == 'OK':
if count < 20:
count += 1
arr2.append(f"{element['problem']['contestId']}{element['problem']['index']}")
else:
break
rank=0
l=len(arr2)
cnt=0
question_list=[]
answers=[]
for i in range(l):
if(questions_dict[questions_dict['contestId']==arr2[i]].index!="Empty Dataframe"):
question_index=questions_dict[questions_dict['contestId']==arr2[i]].index[0]
rank+=questions_dict['rating'][question_index]
distances=similarity[question_index]
question_list.append(sorted(list(enumerate(distances)),reverse=True,key=lambda x:x[1]))
rank=int(rank/l)
for j in range(1,100):
for i in question_list:
if((abs((questions_dict.iloc[i[j][0]].rating)-rank))<=250):
answers.append(questions_dict.iloc[i[j][0]].contestId)
res=[]
[res.append(x) for x in answers if x not in res]
res1=[]
for i in range(30):
res1.append(res[i])
res2=[]
for i in range(10):
x=random.choice(res1)
res2.append(x)
for i in range(len(res2)):
res2[i]={"questions":res2[i],"name":questions_dict[questions_dict['contestId']==res2[i]].name.values[0],"rating":questions_dict[questions_dict['contestId']==res2[i]].rating.values[0],"tags":questions_dict[questions_dict['contestId']==res2[i]].tags1.values[0]}
return res2;
questions_dict=pkl.load(open("questions.pkl","rb"))
questions_dict=pd.DataFrame(questions_dict)
similarity=pkl.load(open("similarity.pkl","rb"))
prediction = getdata(data)
return jsonify({"prediction": prediction},)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))