-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyse_lyrics.py
373 lines (305 loc) · 9.79 KB
/
analyse_lyrics.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import json
import gzip
from csv import DictReader
from pathlib import Path
from wordcloud import WordCloud
import matplotlib.pyplot as plt
from itertools import islice, groupby, chain
from operator import itemgetter
from multiprocessing import Pool
from collections import Counter, defaultdict
from pprint import pprint
import nltk
import matplotlib.ticker as mtick
from matplotlib.font_manager import FontProperties
import matplotlib, mplcairo
import re
matplotlib.use("module://mplcairo.gtk")
lemmatizer = nltk.wordnet.WordNetLemmatizer()
parsings_dir = Path.cwd() / "parsings"
word_lists = {
path.stem: [lemmatizer.lemmatize(word.strip()) for word in path.read_text("utf-8").splitlines()]
for path
in (Path.cwd() / "words").glob("*.txt")
}
# https://github.com/bfelbo/DeepMoji/blob/master/emoji_overview.png
# https://github.com/bfelbo/DeepMoji/blob/master/emoji_unicode.csv
# emoji_categories = dict(
# humor=set('😏😂😅🙈😋😉💀😜😈💁'),
# love={'😍', '❤', '😳', '💕', '😘', '♥', '💔', '♡', '💜', '💖', '💙', },
# music={'🎶', '🎧', },
# positive={'👌', '😊', '😁', '💯', '😌', '☺', '🙌', '🙏', '✌', '😎', '👍', '👏', '👀', '😄', '💪', '👊', '✨', },
# neutral={'😴', '😐', '✋', },
# negative={'😩', '😭', '😔', '😑', '😕', '😞', '😫', '😢', '😪', '😷', '🔫', '😣', '😓', '🙊', '😖', '🙅', '😬', },
# anger={'😒', '😡', '😤', '😠', },
# )
def detect_rhyme_scheme(scheme: str):
if not scheme:
return None
# monorhyme: /A+/
if len(set(scheme)) == 1:
return 'monorhyme'
# enclosed rhyme: ABBA
if re.search(r'(?P<outer>\w)(?P<inner>\w)(?P=inner)(?P=outer)', scheme):
return 'enclosed'
# alternating rhyme: ABAB
if re.search(r"(?P<first>\w)(?P<second>\w)(?P=first)(?P=second)", scheme):
return 'alternating'
# clumped rhyme: ABAB
if re.search(r"(?P<first>\w)(?P=first)(?P<second>\w)(?P=second)", scheme):
return 'clumped'
def extract_id(url: str):
return url.split("/")[-1]
# Sources
def top_songs():
f = open("top_songs.csv")
try:
yield from DictReader(f)
finally:
f.close()
def top_blues():
f = open("top_blues.csv")
try:
yield from islice(DictReader(f), 5000)
finally:
f.close()
def top_pop():
f = open("top_pop.csv")
try:
yield from islice(DictReader(f), 5000)
finally:
f.close()
def top_rock():
f = open("top_rock.csv")
try:
yield from islice(DictReader(f), 5000)
finally:
f.close()
def top_rap():
f = open("top_rap.csv")
try:
yield from islice(DictReader(f), 5000)
finally:
f.close()
source = sorted(top_songs(), key=itemgetter('release_year'))
# By year
# grouping = { k: list(v) for k, v in groupby(source, itemgetter('release_year')) }
# By decade
grouping = { k: list(v) for k, v in groupby(source, lambda x: x['release_year'][2] + '0s' if x['release_year'] else '') }
def load_word_frequencies(path: Path):
with gzip.open(path, 'rt', encoding="utf-8") as f:
parsing = json.load(f)
return parsing['freqs']
def load_word_frequencies_filter_by_category(path: Path):
with gzip.open(path, 'rt', encoding="utf-8") as f:
parsing = json.load(f)
stanzas_freqs = parsing['freqs']
return (
[
{
category: { k: v for k, v in stanza_freqs.items() if k in word_list }
for category, word_list
in word_lists.items()
}
for stanza_freqs
in stanzas_freqs
],
sum(Counter(stanza_freqs).total() for stanza_freqs in stanzas_freqs),
)
def load_rhyme_frequencies(path: Path):
with gzip.open(path, 'rt', encoding="utf-8") as f:
parsing = json.load(f)
return Counter(frozenset(rhyme) for rhyme in chain.from_iterable(parsing['rhymes']))
def load_rhyme_schemes(path: Path):
with gzip.open(path, 'rt', encoding="utf-8") as f:
parsing = json.load(f)
return Counter(detect_rhyme_scheme(rhyme_scheme) for rhyme_scheme in parsing['rhyme_structure'])
def frequency_by_group():
for group, songs in grouping.items():
if not group:
continue
print(group)
track_ids = {extract_id(song['spotify']) for song in songs}
word_freq = Counter()
with Pool() as p:
for index, stanza_freqs in enumerate(p.imap_unordered(
load_word_frequencies,
(file for track_id in track_ids if (file := parsings_dir / f"{track_id}.json.gz").exists())
)):
print(index)
for stanza_freq in stanza_freqs:
word_freq.update(stanza_freq)
try:
wc = WordCloud(width=1920*2, height=1080*2)
wc.generate_from_frequencies(word_freq)
wc.to_file(f"output/word_freq/{group} ({len(track_ids)} songs).png")
except ValueError:
print("No lyrics for this group. :(")
def category_frequency_by_group():
storage = {}
for group, songs in grouping.items():
if not group or group in ['40s', '50s']:
continue
print(group)
track_ids = {extract_id(song['spotify']) for song in songs}
frequencies = {
'total': 0,
**{
category: Counter()
for category
in word_lists.keys()
}
}
with Pool() as p:
for index, (stanza_freqs, total) in enumerate(p.imap_unordered(
load_word_frequencies_filter_by_category,
(file for track_id in track_ids if (file := parsings_dir / f"{track_id}.json.gz").exists())
)):
print(index)
frequencies['total'] += total
for stanza_freq in stanza_freqs:
for category, counts in stanza_freq.items():
frequencies[category].update(counts)
storage[group] = frequencies
percentages = {
group: {
category: freqs.total() / frequencies['total'] * 100
for category, freqs
in frequencies.items()
if category != 'total'
}
for group, frequencies
in storage.items()
}
fig, ax = plt.subplots()
ax.yaxis.set_major_formatter(mtick.PercentFormatter())
for category in word_lists.keys():
ax.plot(percentages.keys(), [percs[category] for percs in percentages.values()], label=category)
ax.set_title("Word frequency by category over time")
ax.set_xlabel("Decade")
ax.set_ylabel("Percentage of corpus")
ax.legend()
fig.set_size_inches(11, 8.5)
fig.savefig("output/frequency_by_category.png")
def most_common_rhymes():
track_ids = {extract_id(song['spotify']) for song in source}
rhyme_freqs = Counter()
with Pool() as p:
for index, rhyme_freq in enumerate(p.imap_unordered(
load_rhyme_frequencies,
(file for track_id in track_ids if (file := parsings_dir / f"{track_id}.json.gz").exists())
)):
# print(index)
rhyme_freqs.update(rhyme_freq)
print("Top 100 rhymes:")
pprint(rhyme_freqs.most_common(100))
def load_sentiments(path: Path):
with gzip.open(path, 'rt', encoding="utf-8") as f:
parsing = json.load(f)
sentiments = list(chain.from_iterable(parsing['sentiment']))
counter = Counter()
for line in sentiments:
counter.update(Counter({ k: v * 100 for k, v in line.items() }))
return (
counter,
len(sentiments) * 100,
)
def sentiment_by_group():
storage = {}
for group, songs in grouping.items():
if not group or group in ['40s', '50s']:
continue
print(group)
track_ids = {extract_id(song['spotify']) for song in songs}
frequencies = {
'total': 0,
'sentiments': Counter()
}
with Pool() as p:
for index, (sentiments, total) in enumerate(p.imap_unordered(
load_sentiments,
(file for track_id in track_ids if (file := parsings_dir / f"{track_id}.json.gz").exists())
)):
frequencies['sentiments'].update(sentiments)
frequencies['total'] += total
storage[group] = frequencies
percentages_ungrouped = {
group: {
category: freqs / sentiments['total'] * 100
for category, freqs
in sentiments['sentiments'].items()
}
for group, sentiments
in storage.items()
}
percentages = {
group: {
emoji_category: sum(sentiments[emoji] for emoji in emojis)
for emoji_category, emojis
in emoji_categories.items()
}
for group, sentiments
in percentages_ungrouped.items()
}
fig, ax = plt.subplots()
ax.yaxis.set_major_formatter(mtick.PercentFormatter())
for emotion in percentages[list(storage.keys())[0]].keys():
# I think this might be due to music note emojis
# being included in lyrics to signify an instrumental
# section...
# if emotion in ['🎶', '🎧']:
if emotion == 'music':
continue
ax.plot(percentages.keys(), [percs[emotion] for percs in percentages.values()], label=emotion)
ax.set_title("Sentiment over time")
ax.set_xlabel("Decade")
ax.set_ylabel("Percentage of corpus")
ax.legend()
fig.set_size_inches(11, 8.5)
fig.savefig("output/sentiment.png")
def most_common_rhyme_schemes():
storage = {}
for group, songs in grouping.items():
if not group or group in ['40s', '50s']:
continue
print(group)
track_ids = {extract_id(song['spotify']) for song in songs}
rhyme_schemes_count = Counter()
with Pool() as p:
for index, rhyme_schemes in enumerate(p.imap_unordered(
load_rhyme_schemes,
(file for track_id in track_ids if (file := parsings_dir / f"{track_id}.json.gz").exists())
)):
rhyme_schemes_count.update(rhyme_schemes)
storage[group] = rhyme_schemes_count
percentages = {
group: {
scheme_type: amount / schemes.total() * 100
for scheme_type, amount
in schemes.items()
if scheme_type
}
for group, schemes
in storage.items()
}
fig, ax = plt.subplots()
ax.yaxis.set_major_formatter(mtick.PercentFormatter())
for scheme in percentages[list(storage.keys())[0]].keys():
ax.plot(percentages.keys(), [percs[scheme] for percs in percentages.values()], label=scheme)
ax.set_title("Rhyme schemes over time")
ax.set_xlabel("Decade")
ax.set_ylabel("Percentage of corpus")
ax.legend()
fig.set_size_inches(11, 8.5)
fig.savefig("output/rhyme_schemes.png")
# frequency_by_group()
# category_frequency_by_group()
# most_common_rhymes()
# sentiment_by_group()
most_common_rhyme_schemes()
# def gen_sentiment_histogram():
# sentiments = list(chain.from_iterable(stanza_sentiment(stanza) for stanza in stanzas))
# plt.title("Sentiment of 'The Fever'")
# axes = plt.subplot()
# axes.hist(sentiments, bins=50)
# plt.show()