-
Notifications
You must be signed in to change notification settings - Fork 3
/
genre_work.py
283 lines (229 loc) · 9.69 KB
/
genre_work.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# Requires 'sample' table to have been copied into the _deriv.sqlite
# database - it's just easier that way than having two db connections;
# then the original database can be left well alone. Also requires
# the 'tracks' table to have been copied across with the 'tag_list' and
# 'genre' columns processed. So run copy_tables_across() from
# genre_relationships_sample.py on the original database before running
# any of these functions.
import collections
import sqlite3
import os
import os.path
import csv
import time
the_genres=[('urban',['hiphop','rap','instrumental','trap','r&b','reggae',
'beat']),
('edm',['house','electronic','dubstep','techno','electro',
'd&b','techhouse','progressivehouse','dance','deephouse',
'trance','electrohouse','hardstyle','edm','mashup',
'ambient']),
('other',['pop','rock','acoustic','blues','music','cover','folk',
'jazz','piano','classical','indie','alternative','metal',
'soundtrack','punk','alternativerock','country','funk',
'singersongwriter'])]
def genres_od():
l=[]
for t in the_genres:
for g in t[1]:
l.append((g,set([])))
return collections.OrderedDict(l)
def genre_community_od(d):
l=[]
for t in the_genres:
l.append((t[0],set([])))
od = collections.OrderedDict(l)
for t in the_genres:
for g in t[1]:
od[t[0]].update(d[g])
return od
def comments_by_genre(curs,target_genre,user_sample=False,English=True):
if English:
sql=('SELECT id,commenter,track_creator,track_genre '
'FROM comments_corp WHERE track_genre IS NOT NULL AND language=1')
else:
sql=('SELECT id,commenter,track_creator,track_genre '
'FROM comments_corp WHERE track_genre IS NOT NULL')
if user_sample:
return {c[0] for c in curs.execute(sql)
if c[1] in user_sample
and c[1] != c[2]
and target_genre in {s.strip() for s in c[3].split('|')}}
else:
return {c[0] for c in curs.execute(sql)
if c[1] != c[2]
and target_genre in {s.strip() for s in c[3].split('|')}}
def users_by_genre(curs,target_genre,user_sample=False):
sql=('SELECT user,most_used FROM user_genres WHERE most_used '
'IS NOT NULL')
if user_sample:
return {u[0] for u in curs.execute(sql) if u[0] in user_sample
and u[1]==target_genre}
else:
return {u[0] for u in curs.execute(sql) if u[1]==target_genre}
def tracks_by_genre(curs,target_genre,user_sample=False):
sql=('SELECT id,user_id,genre FROM tracks WHERE genre IS NOT NULL')
if user_sample:
return {t[1] for t in curs.execute(sql) if t[1] in user_sample
and target_genre in {s.strip() for s in t[2].split('|')}}
else:
return {t[1] for t in curs.execute(sql)
if target_genre in {s.strip() for s in t[2].split('|')}}
def filled_genre_dict(func,curs,user_sample=False,English=False):
dict=genres_od()
for genre in dict.keys():
if English:
dict[genre].update(func(curs,genre,user_sample,English))
else:
dict[genre].update(func(curs,genre,user_sample))
return dict
def the_sample(curs):
sql=('SELECT id FROM sample')
return {s[0] for s in curs.execute(sql)}
def uploaders(curs):
sql=('SELECT user_id FROM tracks')
return {t[0] for t in curs.execute(sql)}
def sample_uploaders(curs):
return the_sample(curs) & uploaders(curs)
def join_comments(corpus):
return '\n\n'.join([c[1] for c in corpus])
class SpamFilter(object):
def __init__(self):
self.comments_seen=set([])
def check_comment(self,comment):
if comment not in self.comments_seen:
self.comments_seen.add(comment)
return True
else:
if '%%' in comment:
return False
elif len(comment.split()) > 2:
return False
else:
return True
def encode(t):
return t.encode('utf-8')
def save_corpus(corpus,path='',name='corpus',single_file=True):
filter = SpamFilter()
if single_file:
with open(os.path.join(path,name+'.txt'),'w') as f:
for comment in corpus:
if filter.check_comment(comment[1]):
f.write(encode(comment[1])+'\n\n')
else:
os.mkdir(name)
for comment in corpus:
if filter.check_comment(comment[1]):
with open(os.path.join(path,name,
'{}.txt'.format(comment[0]))
,'w') as f:
f.write(encode(comment[1]))
return filter
def get_comments(curs,comment_set):
sql=('SELECT id,filtered_text FROM comments_corp ')
return ((c[0],c[1]) for c in curs.execute(sql) if c[0] in comment_set)
def output_stats(dict,path,name):
stats=[(g,len(dict[g])) for g in dict.keys()]
with open(os.path.join(path,name+'.csv'),'wb') as f:
writer=csv.writer(f,dialect='excel')
writer.writerows(stats)
def output_corpora(curs,dict,path,single_file=True):
for g in dict.keys():
comments = get_comments(curs,dict[g])
save_corpus(comments,path,g+'_corpus',single_file)
def output_follow_stats(dict,path):
all_results=open(os.path.join(path,'all_follows_by_genre.csv'),'wb')
all_res_w=csv.writer(all_results,dialect='excel')
for gen_com in the_genres:
com_results=open(os.path.join(path,'follows_by_genre_in_'+gen_com[0]+
'_community.csv'),'wb')
com_res_w=csv.writer(com_results,dialect='excel')
for genre in gen_com[1]:
gen_results=open(os.path.join(path,'follows_by_genre_in_'+genre+
'_genre.csv'),'wb')
gen_res_w=csv.writer(gen_results,dialect='excel')
all_res_w.writerows(dict[genre])
com_res_w.writerows(dict[genre])
gen_res_w.writerows(dict[genre])
gen_results.close()
com_results.close()
all_results.close()
def percentages(a):
b=[]
for i in a[1:]:
if i:
b.append(int(i / float(a[0]) * 100 + 0.5))
else:
b.append(0)
return b
def following_within_genre(curssourc,cursderiv,gen_dict):
everyone_genre=filled_genre_dict(users_by_genre,cursderiv)
everyone_gen_com=genre_community_od(everyone_genre)
everyone=set([])
results_by_gen=collections.OrderedDict()
for v in everyone_gen_com.values():
everyone.update(v)
for genre in gen_dict.keys():
community=[c[0] for c in the_genres if genre in c[1]][0]
results=[]
for user in gen_dict[genre]:
sql='SELECT followed FROM x_follows_y WHERE follower=?'
total={f[0] for f in curssourc.execute(sql,(user,))}
with_genres=total & everyone
this_com=with_genres & everyone_gen_com[community]
this_gen=this_com & everyone_genre[genre]
figures=[len(total),len(with_genres),len(this_com),len(this_gen)]
results.append(figures+percentages(figures[1:]))
results_by_gen[genre]=results
return results_by_gen
def do_everything(db_source,single_file_corpora=True):
connsourc=sqlite3.connect(db_source)
curssourc=connsourc.cursor()
db=db_source[:-7]+'_deriv.sqlite'
conn=sqlite3.connect(db)
curs=conn.cursor()
db_path=os.path.split(db)
print 'Putting necessary directories in place...'
base_path=os.path.join(db_path[0],
'from_{}_{}'.format(db_path[1],
time.strftime('%Y%m%d_%H%M')))
stat_path=os.path.join(base_path,'statistics')
corp_path=os.path.join(base_path,'corpora')
foll_path=os.path.join(base_path,'follows')
os.mkdir(base_path)
os.mkdir(stat_path)
os.mkdir(corp_path)
os.mkdir(foll_path)
os.mkdir(os.path.join(corp_path,'g'))
os.mkdir(os.path.join(corp_path,'gc'))
sample=the_sample(curs)
data=collections.OrderedDict()
data['g']=collections.OrderedDict()
data['gc']=collections.OrderedDict()
print 'Getting data for each genre:'
print '\tThose who\'ve uploaded tracks...'
data['g']['uploaders']=filled_genre_dict(users_by_genre,curs,sample)
print '\tThe tracks...'
data['g']['tracks']=filled_genre_dict(tracks_by_genre,curs,sample)
print '\tAll the comments...'
data['g']['all_comments']=filled_genre_dict(comments_by_genre,curs,sample)
print '\tAnd just the English-language comments...'
data['g']['eng_comments']=filled_genre_dict(comments_by_genre,curs,
sample,True)
for dict_name in data['g'].keys():
print 'Writing stats on {} to {}'.format(dict_name,stat_path)
output_stats(data['g'][dict_name],stat_path,
dict_name+'_by_genre')
data['gc'][dict_name]=genre_community_od(data['g'][dict_name])
output_stats(data['gc'][dict_name],stat_path,
dict_name+'_by_genre_community')
for dict_name in data.keys():
print 'Writing English language corpus to {}'.format(corp_path)
output_corpora(curs,data[dict_name]['eng_comments'],
os.path.join(corp_path,dict_name),
single_file_corpora)
print 'Checking follow relationships vis a vis genre...'
data['f']=following_within_genre(curssourc,curs,
data['g']['uploaders'])
print 'Writing follow data within {}'.format(foll_path)
output_follow_stats(data['f'],foll_path)
return data