-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsentimentAlbumArt.py
284 lines (243 loc) · 8.45 KB
/
sentimentAlbumArt.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
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
import math
import random
from tkinter import *
# Utility functions
def average(c1, c2, w=0.5):
'''Compute the weighted average of two colors. With w = 0.5 we get the average.'''
(r1,g1,b1) = c1
(r2,g2,b2) = c2
r3 = w * r1 + (1 - w) * r2
g3 = w * g1 + (1 - w) * g2
b3 = w * b1 + (1 - w) * b2
return (r3, g3, b3)
def rgb(r,g,b):
'''Convert a color represented by (r,g,b) to a string understood by tkinter.'''
u = max(0, min(255, int(128 * (r + 1))))
v = max(0, min(255, int(128 * (g + 1))))
w = max(0, min(255, int(128 * (b + 1))))
return '#%02x%02x%02x' % (u, v, w)
def well(x):
'''A function which looks a bit like a well.'''
return 1 - 2 / (1 + x*x) ** 8
def tent(x):
'''A function that looks a bit like a tent.'''
return 1 - 2 * abs(x)
# We next define classes that represent expression trees.
# Each object that represents and expression should have an eval(self,x,y) method
# which computes the value of the expression at (x,y). The __init__ should
# accept the objects representing its subexpressions. The class definition
# should contain the arity attribute which tells how many subexpressions should
# be passed to the __init__ constructor.
class VariableX():
arity = 0
def __init__(self): pass
def __repr__(self): return "x"
def eval(self,x,y): return (x,x,x)
class VariableY():
arity = 0
def __init__(self): pass
def __repr__(self): return "y"
def eval(self,x,y): return (y,y,y)
class Constant():
arity = 0
def __init__(self):
self.c = (random.uniform(0,1), random.uniform(0,1), random.uniform(0,1))
def __repr__(self):
return 'Constant(%g,%g,%g)' % self.c
def eval(self,x,y): return self.c
class Sum():
arity = 2
def __init__(self, e1, e2):
self.e1 = e1
self.e2 = e2
def __repr__(self):
return 'Sum(%s, %s)' % (self.e1, self.e2)
def eval(self,x,y):
return average(self.e1.eval(x,y), self.e2.eval(x,y))
class Product():
arity = 2
def __init__(self, e1, e2):
self.e1 = e1
self.e2 = e2
def __repr__(self):
return 'Product(%s, %s)' % (self.e1, self.e2)
def eval(self,x,y):
(r1,g1,b1) = self.e1.eval(x,y)
(r2,g2,b2) = self.e2.eval(x,y)
r3 = r1 * r2
g3 = g1 * g2
b3 = b1 * b2
return (r3, g3, b3)
class Mod():
arity = 2
def __init__(self, e1, e2):
self.e1 = e1
self.e2 = e2
def __repr__(self):
return 'Mod(%s, %s)' % (self.e1, self.e2)
def eval(self,x,y):
(r1,g1,b1) = self.e1.eval(x,y)
(r2,g2,b2) = self.e2.eval(x,y)
try:
r3 = r1 % r2
g3 = g1 % g2
b3 = b1 % b2
return (r3, g3, b3)
except:
return (0,0,0)
class Well():
arity = 1
def __init__(self, e):
self.e = e
def __repr__(self):
return 'Well(%s)' % self.e
def eval(self,x,y):
(r,g,b) = self.e.eval(x,y)
return (well(r), well(g), well(b))
class Tent():
arity = 1
def __init__(self, e):
self.e = e
def __repr__(self):
return 'Tent(%s)' % self.e
def eval(self,x,y):
(r,g,b) = self.e.eval(x,y)
return (tent(r), tent(g), tent(b))
class Sin():
arity = 1
def __init__(self, e):
self.e = e
self.phase = random.uniform(0, math.pi)
self.freq = random.uniform(1.0, 6.0)
def __repr__(self):
return 'Sin(%g + %g * %s)' % (self.phase, self.freq, self.e)
def eval(self,x,y):
(r1,g1,b1) = self.e.eval(x,y)
r2 = math.sin(self.phase + self.freq * r1)
g2 = math.sin(self.phase + self.freq * g1)
b2 = math.sin(self.phase + self.freq * b1)
return (r2,g2,b2)
class Level():
arity = 3
def __init__(self, level, e1, e2):
self.treshold = random.uniform(-1.0,1.0)
self.level = level
self.e1 = e1
self.e2 = e2
def __repr__(self):
return 'Level(%g, %s, %s, %s)' % (self.treshold, self.level, self.e1, self.e2)
def eval(self,x,y):
(r1, g1, b1) = self.level.eval(x,y)
(r2, g2, b2) = self.e1.eval(x,y)
(r3, g3, b3) = self.e2.eval(x,y)
r4 = r2 if r1 < self.treshold else r3
g4 = g2 if g1 < self.treshold else g3
b4 = b2 if b1 < self.treshold else b3
return (r4,g4,b4)
class Mix():
arity = 3
def __init__(self, w, e1, e2):
self.w = w
self.e1 = e1
self.e2 = e2
def __repr__(self):
return 'Mix(%s, %s, %s)' % (self.w, self.e1, self.e2)
def eval(self,x,y):
w = 0.5 * (self.w.eval(x,y)[0] + 1.0)
c1 = self.e1.eval(x,y)
c2 = self.e2.eval(x,y)
return average(c1,c2,)
# The following list of all classes that are used for generation of expressions is
# used by the generate function below.
operators = (VariableX, VariableY, Constant, Sum, Product, Mod, Sin, Tent, Well, Level, Mix)
# We pre-compute those operators that have arity 0 and arity > 0
operators0 = [op for op in operators if op.arity == 0]
operators1 = [op for op in operators if op.arity > 0]
def generate(k = 50):
'''Randonly generate an expession of a given size.'''
if k <= 0:
# We used up available size, generate a leaf of the expression tree
op = random.choice(operators0)
return op()
else:
# randomly pick an operator whose arity > 0
op = random.choice(operators1)
# generate subexpressions
i = 0 # the amount of available size used up so far
args = [] # the list of generated subexpression
for j in sorted([random.randrange(k) for l in range(op.arity-1)]):
args.append(generate(j - i))
i = j
args.append(generate(k - 1 - i))
return op(*args)
class Art():
"""A simple graphical user interface for random art. It displays the image,
and the 'Name of the Song' button."""
def __init__(self, master, size=256):
master.title('Album art')
self.size=size
self.canvas = Canvas(master, width=size, height=size)
self.canvas.grid(row=0,column=0)
b2 = Button(master, text = get_name(), command=self.redraw)
b2.grid(row = 1, column=0)
self.draw_alarm = None
self.redraw()
def redraw(self):
if self.draw_alarm: self.canvas.after_cancel(self.draw_alarm)
self.canvas.delete(ALL)
self.art = generate(random.randrange(20,150))
self.d = 64 # current square size
self.y = 0 # current row
self.draw()
def draw(self):
if self.y >= self.size:
self.y = 0
self.d = self.d // 4
if self.d >= 1:
for x in range(0, self.size, self.d):
u = 2 * float(x + self.d/2)/self.size - 1.0
v = 2 * float(self.y + self.d/2)/self.size - 1.0
(r,g,b) = self.art.eval(u, v)
self.canvas.create_rectangle(x,
self.y,
x+self.d,
self.y+self.d,
width=0, fill=rgb(r,g,b))
self.y += self.d
self.draw_alarm = self.canvas.after(1, self.draw)
else:
self.draw_alarm = None
# A helper method to understand the cryptic score provided by the sentiment analyzer
def value_comparator(polarityScores):
negativeScore = polarityScores['neg']
neutralScore = polarityScores['neu'] # for testing
positiveScore = polarityScores['pos']
if positiveScore >= negativeScore:
return 'positive' # the sentence was positive
elif negativeScore >= positiveScore:
return 'negative' # the sentence was negative
else:
return 'neutral' # the sentence was neutral
def get_name():
with open('lyrics.txt') as f:
text = f.read()
name_of_song = text.splitlines()[0]
name_of_song = name_of_song[1:-1]
return name_of_song
# Main program
def main():
with open('lyrics.txt') as f:
text = f.read()
lines = text.splitlines()[1:]
analyzer = SentimentIntensityAnalyzer()
vs = analyzer.polarity_scores(text)
# Testing
for sentences in lines:
sentimentValue = value_comparator(vs)
# print(sentences + ": " + sentimentValue)
win = Tk()
arg = Art(win)
win.mainloop()
if __name__ == '__main__':
main()