-
Notifications
You must be signed in to change notification settings - Fork 46
/
arena_util.py
47 lines (32 loc) · 1 KB
/
arena_util.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
# -*- coding: utf-8 -*-
import io
import os
import json
import distutils.dir_util
from collections import Counter
import numpy as np
def write_json(data, fname):
def _conv(o):
if isinstance(o, (np.int64, np.int32)):
return int(o)
raise TypeError
parent = os.path.dirname(fname)
distutils.dir_util.mkpath("./arena_data/" + parent)
with io.open("./arena_data/" + fname, "w", encoding="utf-8") as f:
json_str = json.dumps(data, ensure_ascii=False, default=_conv)
f.write(json_str)
def load_json(fname):
with open(fname, encoding="utf-8") as f:
json_obj = json.load(f)
return json_obj
def debug_json(r):
print(json.dumps(r, ensure_ascii=False, indent=4))
def remove_seen(seen, l):
seen = set(seen)
return [x for x in l if not (x in seen)]
def most_popular(playlists, col, topk_count):
c = Counter()
for doc in playlists:
c.update(doc[col])
topk = c.most_common(topk_count)
return c, [k for k, v in topk]