-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcounts.py
204 lines (162 loc) · 6.09 KB
/
counts.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
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_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 = {'measures': relation['scoreA_ema'].split('/')[0] , 'Song_From': relation['titleB'] }#, 'Direction': relation['direction'] }
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 = {'measures': relation['scoreA_ema'].split('/')[0] , 'Song_From': relation['titleA'], }#'Direction': relation['direction'] }
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")
try:
direction = relation['direction']
except KeyError:
direction = "None"
if direction in relationship_counts.keys():
temp = relationship_counts[direction]
relationship_counts[direction] = temp+1
else: #doesnt exist yet
relationship_counts[direction] = 1
#print( 'here',info2['assertions'])
#tempdict = dict(zip(r_keys,[None]*len(r_keys)))
#makes data structure like {u'scoreA_meiids': None, u'titleA': None,...}
for key in r_keys:
try:
relat = str(relation[key])
if key == "types":
#print (relation[key])
if list(relation[key]) == []:
relat = "None"
else:
relat = list(relation[key])
#print ("lis")
#print (list(relation[key]))
except KeyError:
relat = "None"
if type(relat) == type([]):
# print ("relat list", relat)
for elem in relat:
# print ("elem", elem)
if tempdict[key] == None:
tempdict[key] = {}
if elem in list(tempdict[key]): # hash issue with lists
temp1 = tempdict[key][elem]
tempdict[key][elem] = temp1+1
else: #doesnt exist yet
tempdict[key][elem] = 1
elif tempdict[key] == None:
tempdict[key] = {}
elif relat in list(tempdict[key]): # hash issue with lists
temp1 = tempdict[key][relat]
tempdict[key][relat] = temp1+1
else: #doesnt exist yet
tempdict[key][relat] = 1
for key in a_keys:
try:
assertt = str(assertion[key])
if key == "title":
if assertt[:2] == ': ':
assertt = assertt[2:] #cleaning some text
if key == "types":
#print (relation[key])
if list(assertion[key]) == []:
assertt = "None"
else:
assertt = list(assertion[key]) # hopefully this works
except KeyError:
assertt = "None"
if type(assertt) == type([]):
for elem in assertt:
if a_tempdict[key] == None:
a_tempdict[key] = {}
if elem in list(a_tempdict[key]): # hash issue with lists
temp1 = a_tempdict[key][elem]
a_tempdict[key][elem] = temp1+1
else: #doesnt exist yet
a_tempdict[key][elem] = 1
elif a_tempdict[key] == None:
a_tempdict[key] = {}
elif assertt in list(a_tempdict[key]): # hash issue with lists
temp1 = a_tempdict[key][assertt]
a_tempdict[key][assertt] = temp1+1
else: #doesnt exist yet
a_tempdict[key][assertt] = 1
#pprint.pprint(tempdict)
#pprint.pprint(a_tempdict)
#pprint.pprint(ema_dictionary)
return user_counts,title_counts,relationship_counts,tempdict,a_tempdict, 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()
count_list,title_counts,relationship_counts,tempdict, a_tempdict, 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.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()