This repository has been archived by the owner on Aug 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
scoring.py
55 lines (39 loc) · 1.54 KB
/
scoring.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
from datetime import timedelta
from django.utils import timezone
import numpy as np
from sui_hei.models import *
def calc_exp(soup, goodq, trueq, cmnt):
return int(soup + goodq * 0.2 + trueq * 0.5 + cmnt)
def softmax(X):
return np.exp(X) / np.sum(np.exp(X), axis=0)
def calc_score(stars):
if stars.count() < 1: return 0
userexp = np.array([s.user_id.experience for s in stars.all()])
userstar = np.array(stars.values_list('value', flat=True))
userw = softmax(userexp / userexp.mean())
return userw.dot(userstar)
def update_user_exp(recent=None):
if recent:
users = User.objects.filter(
last_login__gt=timezone.now() - recent).all()
else:
users = User.objects.all()
for user in users:
_put_ques = user.shitumon_set
put_soups = user.mondai_set.count()
put_goodques = _put_ques.filter(good=True).count()
put_trueques = _put_ques.filter(true=True).count()
put_comments = Comment.objects.filter(user_id=user).count()
user.experience = calc_exp(put_soups, put_goodques, put_trueques,
put_comments)
print("Update: %s exp->%.2f" % (user.nickname, user.experience))
# TODO: grant awards to users depend on experience here.
user.save()
def update_all_soup_score():
soups = Mondai.objects.all()
for soup in soups:
update_soup_score(soup)
def update_soup_score(soup, save=True):
score = calc_score(soup.star_set)
soup.score = 0 if np.isnan(score) else score
soup.save()