-
Notifications
You must be signed in to change notification settings - Fork 46
/
genre_most_popular.py
72 lines (52 loc) · 2.23 KB
/
genre_most_popular.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
# -*- coding: utf-8 -*-
from collections import Counter
import fire
from tqdm import tqdm
from arena_util import load_json
from arena_util import write_json
from arena_util import remove_seen
from arena_util import most_popular
class GenreMostPopular:
def _song_mp_per_genre(self, song_meta, global_mp):
res = {}
for sid, song in song_meta.items():
for genre in song['song_gn_gnr_basket']:
res.setdefault(genre, []).append(sid)
for genre, sids in res.items():
res[genre] = Counter({k: global_mp.get(int(k), 0) for k in sids})
res[genre] = [k for k, v in res[genre].most_common(200)]
return res
def _generate_answers(self, song_meta_json, train, questions):
song_meta = {int(song["id"]): song for song in song_meta_json}
song_mp_counter, song_mp = most_popular(train, "songs", 200)
tag_mp_counter, tag_mp = most_popular(train, "tags", 100)
song_mp_per_genre = self._song_mp_per_genre(song_meta, song_mp_counter)
answers = []
for q in tqdm(questions):
genre_counter = Counter()
for sid in q["songs"]:
for genre in song_meta[sid]["song_gn_gnr_basket"]:
genre_counter.update({genre: 1})
top_genre = genre_counter.most_common(1)
if len(top_genre) != 0:
cur_songs = song_mp_per_genre[top_genre[0][0]]
else:
cur_songs = song_mp
answers.append({
"id": q["id"],
"songs": remove_seen(q["songs"], cur_songs)[:100],
"tags": remove_seen(q["tags"], tag_mp)[:10]
})
return answers
def run(self, song_meta_fname, train_fname, question_fname):
print("Loading song meta...")
song_meta_json = load_json(song_meta_fname)
print("Loading train file...")
train_data = load_json(train_fname)
print("Loading question file...")
questions = load_json(question_fname)
print("Writing answers...")
answers = self._generate_answers(song_meta_json, train_data, questions)
write_json(answers, "results/results.json")
if __name__ == "__main__":
fire.Fire(GenreMostPopular)