-
Notifications
You must be signed in to change notification settings - Fork 0
/
bayesian_goty.py
109 lines (77 loc) · 2.98 KB
/
bayesian_goty.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
import re
from compute_bayesian_rating import choose_prior, compute_bayesian_score
def load_input(filename, file_encoding='utf8'):
data = []
with open(filename, encoding=file_encoding) as f:
for line in f.readlines():
line = line.strip()
# Remove empty lines and comments
if len(line) > 0 and line[0:2] != '# ':
data.append(line)
return data
def parse_data(data):
observations = {}
for element in data:
my_list = element.rsplit('(')
if not (len(my_list) == 2):
raise AssertionError()
# Split at '|' to have the rank of the game at the head of the list
first_part = my_list[0].split('|')
# Remove the rank of the game
game_name_list = first_part[1:]
# Concatenate the remaining elements with '|'
game_name = '|'.join(game_name_list) # Remove leading and trailing whitespaces
game_name = game_name.strip()
second_part = my_list[1]
# Reference: https://stackoverflow.com/a/1059601
tokens = re.split('\\W+', second_part)
rating_sum = int(tokens[1 + tokens.index('Score')])
num_votes = int(tokens[1 + tokens.index('Votes')])
observations[game_name] = {}
observations[game_name]['score'] = rating_sum / num_votes
observations[game_name]['num_votes'] = num_votes
return observations
def compute_ranking(observations, prior):
for game_name, game in observations.items():
observations[game_name]['bayesian_score'] = compute_bayesian_score(game, prior)
ranking = sorted(
observations.keys(),
key=lambda x: observations[x]['bayesian_score'],
reverse=True,
)
return ranking
def print_ranking(ranking, observations, prior):
print('Game of the Year Votes\n')
for rank, entry in enumerate(ranking):
game_name = entry.strip()
sum_scores = (
observations[game_name]['score'] * observations[game_name]['num_votes']
)
votes = observations[game_name]['num_votes']
aps = observations[game_name]['score']
bayesian_score = observations[game_name]['bayesian_score']
sentence = '{0:3} | {1} (Score: {2:.0f} | Votes: {3} | APS: {4:.2f} | Bayesian Rating: {5:.2f})'
print(
sentence.format(
rank + 1,
game_name,
sum_scores,
votes,
aps,
bayesian_score,
),
)
sentence = '\nNB: Bayesian Prior (Score: {0:.2f} | Votes: {1:.2f})'
print(sentence.format(prior['score'], prior['num_votes']))
return
def main():
filename = 'data/steam_resetera_2017_goty.txt'
data = load_input(filename)
observations = parse_data(data)
verbose = False
prior = choose_prior(observations, verbose)
ranking = compute_ranking(observations, prior)
print_ranking(ranking, observations, prior)
return True
if __name__ == '__main__':
main()