-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetArtistSentiment.py
143 lines (110 loc) · 4.05 KB
/
getArtistSentiment.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
# getArtistSentimment.py
# Ezra Zinberg
# takes an artist name, returns sentiment metrics:
# 1) average sentiment
# 2) deltaS total
# 3) deltaS early
# 4) deltaS late
import csv
import operator
import sys
import getSentiment
from nltk.corpus import sentiwordnet as swn
from nltk import word_tokenize
from nltk.corpus import stopwords
import datetime
from dateutil import parser
# returns [[avgSent], [deltaSTotal], [deltaSEarly], [deltaSLate]] for given artist
def get(artist):
result = [[], [], [], []]
print('\nArtist: ' + artist)
hits = 0
trackSent = [0, 0, 0]
avg = [0, 0, 0]
# realpython.com
with open('songdata-all-fields.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for row in csv_reader:
if row[0] == artist:
trackSent = getSentiment.average(row[3])
for i in range(len(trackSent)):
avg[i] += trackSent[i]
hits += 1
if hits < 5:
return []
for i in range(len(trackSent)):
avg[i] = avg[i] / hits
result[0] = avg
r = delta(artist, avg)
result[1] = r[0]
result[2] = r[1]
result[3] = r[2]
for arr in range(len(result)):
for elem in range(len(result[arr])):
result[arr][elem] = round(result[arr][elem], 3)
print("avg:\t" + str(avg))
print("deltaTotal:\t" + str(result[1]))
print("deltaEarly:\t" + str(result[2]))
print("deltaLate:\t" + str(result[3]))
return result
def delta(artist, avg):
deltaTotal = [0, 0, 0]
trackSent = [0, 0, 0]
# avg = [0, 0, 0]
early = [0, 0, 0]
late = [0, 0, 0]
earliestDate = parser.parse(str(datetime.MAXYEAR))
latestDate = parser.parse("1000-01-01")
# iterate through rows and find range of dates for artist's songs
with open('songdata-all-fields.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for row in csv_reader:
if row[0] == artist:
if (row[2] == ""):
continue
releaseDate = parser.parse(row[2])
if releaseDate > parser.parse("2019-11-23"):
continue
if releaseDate < earliestDate:
earliestDate = releaseDate
if releaseDate > latestDate:
latestDate = releaseDate
earlyBound = earliestDate + (latestDate-earliestDate) / 5
lateBound = latestDate - (latestDate-earliestDate) / 5
print("early period: " + str(earliestDate) + " thru " + str(earlyBound))
print("late period: " + str(lateBound) + " thru " + str(latestDate))
earlyHits = 0
lateHits = 0
with open('songdata-all-fields.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for row in csv_reader:
if row[0] == artist:
if (row[2] == ""):
continue
releaseDate = parser.parse(row[2])
if releaseDate < earlyBound:
earlyHits += 1
trackSent = getSentiment.average(row[3])
for i in range(len(trackSent)):
early[i] += trackSent[i]
elif releaseDate > lateBound:
lateHits += 1
trackSent = getSentiment.average(row[3])
for i in range(len(trackSent)):
late[i] += trackSent[i]
print("songs in early period: " + str(earlyHits))
print("songs in late period: " + str(lateHits))
if earlyHits == 0 or lateHits == 0:
return [[],[],[]]
for i in range(len(early)):
early[i] = early[i] / earlyHits
late[i] = late[i] / lateHits
deltaTotal[i] = late[i] - early[i]
# note: late - early, NOT early-late
deltaEarly = [0,0,0]
for i in range(len(early)):
deltaEarly[i] = avg[i] - early[i]
deltaLate = [0,0,0]
for i in range(len(late)):
deltaLate[i] = late[i] - avg[i]
return [deltaTotal, deltaEarly, deltaLate]