forked from mfhodges/CRIM_Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheatmap.py
142 lines (104 loc) · 4.16 KB
/
heatmap.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
# creates ema.json
from JSON_API import data_set_importer
import json
import pprint
import csv
# Modify this to inlcude desired counts
counts_wanted = ['user', 'title', 'relationship_type', 'assertion_type']
# Rerturn counts for desired count types
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 get_counts(counts_wanted, data):
user_counts = {}
title_counts = {}
relationship_counts = {}
ema_dictionary = {}
r_keys = [ u'titleA', u'titleB', u'scoreA_ema', u'scoreB_ema', u'direction', u'types'] # ideally not use all of these?
#r_keys = [u'scoreA_meiids', u'titleA', u'titleB', u'scoreA_ema', u'scoreB_ema', u'scoreA', u'scoreB', u'cid', u'boolDir', u'direction', u'comment', u'scoreB_meiids', u'scoreAassert', u'scoreBassert', u'id', u'types'] # ideally not use all of these?
a_keys = [u'ema', u'score', u'title', u'types']
tempdict = dict(zip(r_keys,[None]*len(r_keys)))
a_tempdict = dict(zip(a_keys,[None]*len(a_keys)))
for rel in data:
info = rel["text"]
info2 = json.loads(info) # turns 'text' into usable dict
for i, char in enumerate(info):
if char == "r" and info[i-1] == "e" and info[i-2] == "s" and info[i-3] == "u" and info[i+1] == '"':
usr = info[i+4]
if info[i+5] != '"':
usr = usr + info[i+5]
if usr not in user_counts:
user_counts[usr] = 1
else:
user_counts[usr] += 1
relation = info2['relationships'][0]
#print (info2['assertions'])
if info2['assertions'] != []:
assertion = info2['assertions'][0]
versions = info2['scores']
#print(versions)
for x in versions:
title = x['title']
#print(x)
if title[:2] == ': ':
title = title[2:] #cleaning some text
if title in title_counts.keys():
temp = title_counts[title]
title_counts[title] = temp+1
ema_sub_dict = {'record_id': rel['record_id'], 'measures': relation['scoreA_ema'].split('/')[0] , 'Song_From': relation['titleB'] }#, 'Direction': relation['direction'] }
ema_sub_dict['typee'] = get_key(relation, 'types')
ema_sub_dict['m_type'] = get_key(assertion, 'types')
#pprint.pprint(ema_sub_dict)
ema_dictionary[title] = ema_dictionary[title]+ [ema_sub_dict]
else: #doesnt exist yet
title_counts[title]= 1
ema_sub_dict = {'record_id': rel['record_id'], 'measures': relation['scoreA_ema'].split('/')[0] , 'Song_From': relation['titleA'], }#'Direction': relation['direction'] }
ema_sub_dict['typee'] = get_key(relation, 'types')
ema_sub_dict['m_type'] = get_key(assertion, 'types')
ema_dictionary[title] = [ema_sub_dict]
#print ("ema_1", relation['scoreA_ema'])
#print ("ema_2", relation['scoreA_ema'].split('/')[0])
#print ("ema_3", relation['scoreA_ema'].split('/'), "\n")
#pprint.pprint(tempdict)
#pprint.pprint(a_tempdict)
#pprint.pprint(ema_dictionary)
return ema_dictionary
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_counts(counts_wanted, data)
#print ('User: Count')
#pprint.pprint(count_list)
#print ("\n")
#print ('Song: Count')
#pprint.pprint(title_counts)
#print ("\n")
#print ('Relationship(direction): Count')
#pprint.pprint(relationship_counts,indent=2)
to_json(ema_dictionary, 'ema_2.json')
#basic_dict_csv(ema_dictionary, 'ema', 'ema.csv')
#basic_dict_csv(tempdict['types'],'realationship_types', 'relationship_types.csv')
#basic_dict_csv(count_list,'users','user_counts.csv')
#basic_dict_csv(title_counts,'titles','title_counts.csv')
#basic_dict_csv(a_tempdict['types'], 'assertion_types', 'assertion_types.csv')
#basic_dict_csv(a_tempdict['title'], 'assertion_titles', 'assertion_titles.csv')
#basic_dict_csv(a_tempdict['score'], 'assertion_scores', 'assertion_scores.csv')
if __name__ == "__main__":
main()