forked from mfhodges/CRIM_Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhm.py
148 lines (109 loc) · 4.28 KB
/
hm.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import sys
from JSON_API import data_set_importer
import json
import pprint
import csv
def get_key(dictt, keyy):
try:
answer = str(dictt[keyy])
if list(dictt[keyy]) == []:
answer = "None"
else:
answer = list(dictt[keyy])[0]
except KeyError:
answer = "None"
return answer
def clean_title(title_name):
#print(title_name[0])
if title_name[0] == ":":
#print("before: ", title_name)
title_name = title_name[2:]
#print("now: ", title_name)
return title_name
def get_catLabel(mea_num):
categoryLabels = ["part_1", "part_2", "part_3", "part_4", "part_5", "part_6", "part_7"]
catLabels = {"part_1":range(0, 40), "part_2":range(40, 80), "part_3":range(80, 120),
"part_4":range(120, 160), "part_5":range(160, 200), "part_6":range(200, 240), "part_7":range(240, 10000)}
#categoryLabels = [0-40, 40-80, 80-120, 120-160, 160-200, 200-240, 240-above]
num = mea_num.split("-")
if len(num) == 1:
num = int(num[0])
else:
num = int((int(num[0])+ int(num[1]))/2)
for key, value in catLabels.items():
if num in value:
return key
return "Error"
def get_user_dict(data):
user_title_viz = {}
user_lst = []
for rel in data:
info = rel["text"]
info2 = json.loads(info) # turns 'text' into usable dict
relation = info2['relationships'][0]
u_id = info2['user']
if info2['assertions'] != []:
assertion = info2['assertions'][0]
versions = info2['scores']
mea = (relation['scoreA_ema'].split('/')[0]).split(",")
# for x in versions:
# pprint.pprint(x)
# #title = x['title']
if u_id not in user_lst:
user_lst.append(u_id)
user_title_viz[u_id] = []
#print(mea)
if len(mea) == 1:
user_sub_dict = {'measures': mea[0] , 'Song_A': clean_title(relation['titleA']), 'Song_B': clean_title(relation['titleB']) }#, 'Direction': relation['direction'] }
#user_sub_dict['typee'] = get_key(relation, 'types')
# user_sub_dict['typee'] = get_catLabel(mea[0])
# user_title_viz[u_id] = [user_sub_dict]
user_sub_dict['typee'] = get_key(relation, 'types')
user_sub_dict['m_type'] = get_key(assertion, 'types')
user_title_viz[u_id] = [user_sub_dict]
else:
for m_num in mea:
user_sub_dict = {'measures': m_num , 'Song_A': clean_title(relation['titleA']), 'Song_B': clean_title(relation['titleB']) }#, 'Direction': relation['direction'] }
#user_sub_dict['typee'] = get_key(relation, 'types')
# user_sub_dict['typee'] = get_catLabel(m_num)
# user_title_viz[u_id] = [user_sub_dict]
user_sub_dict['typee'] = get_key(relation, 'types')
user_sub_dict['m_type'] = get_key(assertion, 'types')
user_title_viz[u_id] = [user_sub_dict]
else:
if len(mea) == 1:
user_sub_dict = {'measures': mea[0] , 'Song_A': clean_title(relation['titleA']), 'Song_B': clean_title(relation['titleB']) }#, 'Direction': relation['direction'] }
#user_sub_dict['typee'] = get_key(relation, 'types')
# user_sub_dict['typee'] = get_catLabel(mea[0])
# user_title_viz[u_id] = user_title_viz[u_id] + [user_sub_dict]
user_sub_dict['typee'] = get_key(relation, 'types')
user_sub_dict['m_type'] = get_key(assertion, 'types')
user_title_viz[u_id] = user_title_viz[u_id] + [user_sub_dict]
else:
for m_num in mea:
user_sub_dict = {'measures': m_num, 'Song_A': clean_title(relation['titleA']), 'Song_B': clean_title(relation['titleB']) }#, 'Direction': relation['direction'] }
#user_sub_dict['typee'] = get_key(relation, 'types')
# user_sub_dict['typee'] = get_catLabel(m_num)
# user_title_viz[u_id] = user_title_viz[u_id] + [user_sub_dict]
user_sub_dict['typee'] = get_key(relation, 'types')
user_sub_dict['m_type'] = get_key(assertion, 'types')
user_title_viz[u_id] = user_title_viz[u_id] + [user_sub_dict]
#pprint.pprint(user_title_viz)
return user_title_viz
def basic_dict_csv(d,header,filename):
with open(filename, 'w') as f:
f.write(header+ ',counts\n')
[f.write('{0},{1}\n'.format(key.replace(',', ''), value)) for key, value in d.items()]
def to_json(d, filename):
with open(filename, 'w') as f:
json_str = json.dump(d, f, indent=4)
def write_to_csv(count_vals):
pass
def main():
crim = data_set_importer.get_json()
crim.set_url(crim.CRIM_url)
data = crim.get_data()
ema_dictionary = get_user_dict(data)
to_json(ema_dictionary, 'user_timeline.json')
if __name__ == "__main__":
main()