sad_panda 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+
3
+ describe SadPanda::Helpers do
4
+ let(:helpers) { Class.new { extend SadPanda::Helpers } }
5
+
6
+ describe '#frequencies_for' do
7
+ it 'returns a hash with the frquency of each word' do
8
+ expect(helpers.frequencies_for(['foo', 'bar', 'quxx', 'foo'])).to eq({'foo' => 2, 'bar' => 1, 'quxx' => 1})
9
+ end
10
+ end
11
+
12
+ describe '#stems_for' do
13
+ it 'returns the stems of words' do
14
+ expect(helpers.stems_for(%w[programming amazes])).to eq %w[program amaz]
15
+ end
16
+ end
17
+
18
+ describe '#remove_stopwords_in' do
19
+ it 'returns the array without stop words in it' do
20
+ expect(helpers.remove_stopwords_in(%w[this is a cool test])).to eq %w[cool test]
21
+ end
22
+ end
23
+
24
+ describe '#words_in' do
25
+ it 'returns an array' do
26
+ expect(helpers.words_in('output array')).to be_a Array
27
+ end
28
+
29
+ it 'returns an array of words from the text' do
30
+ expect(helpers.words_in('make this an array')).to eq %w[make this an array]
31
+ end
32
+ end
33
+
34
+ describe '#emojies_in' do
35
+ it 'returns sad and happy emojies from the text as an array' do
36
+ expect(helpers.emojies_in('I am :] :). But :( as well.')).to eq [':(', ':)', ':]']
37
+ end
38
+ end
39
+
40
+ describe '#sanitize' do
41
+ it 'returns text with only english alphabets' do
42
+ expect(helpers.sanitize('I am :] :). But :( as well.')).to eq 'i am but as well'
43
+ end
44
+ end
45
+
46
+ describe '#happy_emoticon?' do
47
+ context 'when words has a happy emoji' do
48
+ it 'returns true' do
49
+ expect(helpers.happy_emoticon?(['foo', ':)'])).to be true
50
+ end
51
+ end
52
+
53
+ context 'when words does not have a happy emoji' do
54
+ it 'returns false' do
55
+ expect(helpers.happy_emoticon?(['foo', 'bar'])).to be false
56
+ end
57
+ end
58
+ end
59
+
60
+ describe '#sad_emoticon?' do
61
+ context 'when words has a sad emoji' do
62
+ it 'returns true' do
63
+ expect(helpers.sad_emoticon?(['foo', ':('])).to be true
64
+ end
65
+ end
66
+
67
+ context 'when words does not have a sad emoji' do
68
+ it 'returns false' do
69
+ expect(helpers.sad_emoticon?(['foo', 'bar'])).to be false
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,87 @@
1
+ require 'spec_helper'
2
+
3
+ describe SadPanda::Polarity do
4
+ describe 'initialization' do
5
+ let(:object) { SadPanda::Polarity.new('Initialize this text in sad panda') }
6
+
7
+ it 'initializes polarities as an empty Array' do
8
+ expect(object.polarities).to eq []
9
+ end
10
+
11
+ it 'initializes words in an Array' do
12
+ expect(object.words).to eq %w[initialize this text in sad panda]
13
+ end
14
+ end
15
+
16
+ describe '#call' do
17
+ context 'when input contains no words with polarities' do
18
+ let(:object) { SadPanda::Polarity.new(' ') }
19
+
20
+ it 'returns 5.0' do
21
+ expect(object.call).to eq 5.0
22
+ end
23
+
24
+ it 'adds no polarities of words to polarities Array' do
25
+ object.call
26
+
27
+ expect(object.polarities).to eq []
28
+ end
29
+ end
30
+
31
+ context 'when input contains a word with polarity' do
32
+ let(:object) { SadPanda::Polarity.new('I love cactuses!') }
33
+
34
+ it 'returns 10.0' do
35
+ expect(object.call).to eq 10.0
36
+ end
37
+
38
+ it 'adds polarities of words to polarities Array' do
39
+ object.call
40
+
41
+ expect(object.polarities).to eq [10.0]
42
+ end
43
+ end
44
+ end
45
+
46
+ describe '#score_emoticon_polarity' do
47
+ context 'when words has a sad emoji' do
48
+ let(:object) { SadPanda::Polarity.new('I am :(') }
49
+
50
+ it 'adds 2.0 to polarities' do
51
+ object.send(:score_emoticon_polarity)
52
+
53
+ expect(object.polarities.include?(2.0)).to be true
54
+ end
55
+ end
56
+
57
+ context 'when words has a happy emoji' do
58
+ let(:object) { SadPanda::Polarity.new('I am :)') }
59
+
60
+ it 'adds 8.0 to polarities' do
61
+ object.send(:score_emoticon_polarity)
62
+
63
+ expect(object.polarities.include?(8.0)).to be true
64
+ end
65
+ end
66
+
67
+ context 'when words has sad & happy emoji' do
68
+ let(:object) { SadPanda::Polarity.new('I am :) and :(') }
69
+
70
+ it 'adds 5.0 to polarities' do
71
+ object.send(:score_emoticon_polarity)
72
+
73
+ expect(object.polarities.include?(5.0)).to be true
74
+ end
75
+ end
76
+ end
77
+
78
+ describe '#score_polarities_for' do
79
+ let(:object) { SadPanda::Polarity.new('') }
80
+
81
+ it 'appends the right polarities of words to polarities' do
82
+ object.send(:score_polarities_for, { 'abandoned' => 1, 'educated' => 1 })
83
+
84
+ expect(object.polarities).to eq [2.5, 7.5]
85
+ end
86
+ end
87
+ end
@@ -1,423 +1,95 @@
1
1
  require 'spec_helper'
2
2
 
3
-
4
- describe SadPanda do
5
-
6
- let(:emotions) {EmotionBank.get_term_emotions}
7
- let(:polarities) {TermPolarities.get_term_polarities}
8
- let(:term_frequencies) {SadPanda.build_term_frequencies("My cactus collection makes me happy.")}
9
- let(:emotion_score) { {} }
10
- let(:polarity_scores) { [] }
11
- let(:polarity_hash) { TermPolarities.get_term_polarities }
12
-
13
- context "methods" do
14
- describe "#happy_emoticon" do
15
-
16
- context "when true" do
17
- it "returns true" do
18
- message = ":)"
19
- expect(SadPanda.happy_emoticon(message)).to be_true
20
- end
21
- end
22
-
23
- context "when false" do
24
- it "returns true" do
25
- message = "stuff"
26
- expect(SadPanda.happy_emoticon(message)).to be_false
27
- end
28
- end
29
-
30
-
3
+ describe SadPanda do
4
+ describe '#emotion' do
5
+ it 'returns a Symbol result' do
6
+ expect(SadPanda.emotion('panda emotion respose')).to be_a Symbol
31
7
  end
32
8
 
33
- describe "#sad_emoticon" do
34
-
35
- context "when true" do
36
- it "returns true" do
37
- message = ":("
38
- expect(SadPanda.sad_emoticon(message)).to be_true
39
- end
9
+ context 'joy' do
10
+ it 'returns :joy for a happy text' do
11
+ expect(SadPanda.emotion('my lobster collection makes me happy!')).to eq :joy
40
12
  end
41
13
 
42
- context "when false" do
43
- it "returns true" do
44
- message = "stuff"
45
- expect(SadPanda.sad_emoticon(message)).to be_false
46
- end
14
+ it 'returns :joy for a happy emoji' do
15
+ expect(SadPanda.emotion('my lobster collection makes me :)')).to eq :joy
47
16
  end
48
-
49
17
  end
50
18
 
51
- describe "#words_from_message_text" do
52
-
53
- it "removes urls and other gross stuff from tweet" do
54
- message = "lobster hickory http://www.boston.com/business #Rails"
55
-
56
- words = SadPanda.words_from_message_text(message)
57
-
58
- expect(words).to eql(["lobster", "hickory", "rails"])
19
+ context 'anger' do
20
+ it 'returns :anger for an angry text' do
21
+ expect(SadPanda.emotion('Can the JS devils focus on one framewors than having 100')).to eq :anger
59
22
  end
60
-
61
23
  end
62
24
 
63
- describe "#set_emotions" do
64
-
65
- it "modifies the emotions_score array" do
66
-
67
- term_frequencies.each do |key, value|
68
- SadPanda.set_emotions(emotions, emotion_score, key, value)
69
- end
70
- expect((emotion_score["joy"])).to eql(1)
25
+ context 'fear' do
26
+ it 'returns :fear for an fearedful text' do
27
+ expect(SadPanda.emotion('I am ril afraid of cats, homie')).to eq :fear
71
28
  end
72
-
73
29
  end
74
30
 
75
- describe "#set_polarities" do
76
-
77
- it "modifies the polarity_scores array" do
78
- term_frequencies = {'sad' => 1}
79
- term_frequencies.each do |key, value|
80
- SadPanda.set_polarities(key, value, polarity_hash, polarity_scores)
81
- end
82
- expect(polarity_scores).to eql([0.0])
31
+ context 'disgust' do
32
+ it 'returns :disgust for an text with disgust' do
33
+ expect(SadPanda.emotion('I am disgusted')).to eq :disgust
83
34
  end
84
-
85
35
  end
86
36
 
87
- describe "#store_emotions" do
88
- it "stores emotions in emotion_score hash" do
89
-
90
- emotions = {"joy" => "zorg" }
91
- key,value = "zorg", 1
92
-
93
- emotions.keys.each do |k|
94
- SadPanda.store_emotions(emotions, emotion_score, k, key, value)
95
- end
96
- expect(emotion_score["joy"]).to eql(1)
37
+ context 'surprise' do
38
+ it 'returns :surprise for an surprising text' do
39
+ expect(SadPanda.emotion('ActionCable mystifies me')).to eq :surprise
97
40
  end
98
-
99
41
  end
100
42
 
101
- describe "#store_polarities" do
102
-
103
- context "word in polarity_hash" do
104
-
105
- it "adds a polarity to polarity_scores" do
106
- term = "sad"
107
- word = "sad"
108
- SadPanda.store_polarities(term, word, polarity_hash, polarity_scores)
109
- expect(polarity_scores).to eql([0.0])
110
- end
111
-
43
+ context 'ambiguous' do
44
+ it 'returns :ambiguous for a neutral text' do
45
+ expect(SadPanda.emotion('Python is ok')).to eq :ambiguous
112
46
  end
113
47
 
114
- context "word not in polarity_hash" do
115
-
116
- it "does not add a polarity to polarity_scores" do
117
- term = "sad"
118
- word = "cactus"
119
- SadPanda.store_polarities(term, word, polarity_hash, polarity_scores)
120
- expect(polarity_scores).to eql([])
121
- end
122
-
123
- end
124
-
125
- end
126
-
127
- describe "#create_term_frequencies" do
128
-
129
- it "populates a word-stem frequency hash" do
130
- words = ["yo", "stuff"]
131
- term_frequencies = {}
132
- word_stems = SadPanda.get_word_stems(words)
133
- term_frequencies = SadPanda.create_term_frequencies(word_stems, term_frequencies)
134
-
135
- expect(term_frequencies).to eql({"yo"=>1, "stuff"=>1})
48
+ it 'returns :ambiguous for an empty string' do
49
+ expect(SadPanda.emotion(' ')).to be :ambiguous
136
50
  end
137
-
138
51
  end
139
52
 
140
- describe "#check_emoticon_for_emotion" do
141
- context "contains happy emoticon" do
142
-
143
- it "returns 'joy'" do
144
- message = ":)"
145
- output = SadPanda.check_emoticon_for_emotion(emotion_score, message)
146
- expect(output).to eql("joy")
147
- end
148
-
53
+ context 'sadness' do
54
+ it 'returns :sadness for a sad text' do
55
+ expect(SadPanda.emotion('Slow tests make me sad')).to eq :sadness
149
56
  end
150
57
 
151
- context "contains sad emoticon" do
152
-
153
- it "returns 'sadness'" do
154
- message = ":("
155
- output = SadPanda.check_emoticon_for_emotion(emotion_score, message)
156
- expect(output).to eql("sadness")
157
- end
158
-
159
- end
160
-
161
- context "contains both a happy and a sad emoticon" do
162
-
163
- it "returns 'ambiguous'" do
164
- message = ":( :)"
165
- output = SadPanda.check_emoticon_for_emotion(emotion_score, message)
166
- expect(output).to eql("ambiguous")
167
- end
168
-
169
- end
170
-
171
- context "contains no emoticons and emotion_score is not empty" do
172
-
173
- it "returns joy" do
174
- message = "no emoticons in hur"
175
- emotion_score = {"joy" => 1}
176
- output = SadPanda.check_emoticon_for_emotion(emotion_score, message)
177
- expect(output).to eql("joy")
178
- end
179
-
180
- end
181
-
182
- context "contains no emoticons and emotion_score is empty" do
183
-
184
- it "returns joy" do
185
- message = "no emoticons in hur"
186
- output = SadPanda.check_emoticon_for_emotion(emotion_score, message)
187
- expect(output).to eql("ambiguous")
188
- end
189
-
58
+ it 'returns :anger for an sad emoji' do
59
+ expect(SadPanda.emotion('Angular, React & Vue. Now what? :(')).to eq :sadness
190
60
  end
191
61
  end
192
-
193
- describe "#check_emoticon_for_polarity" do
194
- context "contains happy emoticon" do
195
-
196
- it "returns 8" do
197
- message = ":)"
198
- polarity_scores = [2.0,3.0]
199
- output = SadPanda.check_emoticon_for_polarity(polarity_scores, message)
200
- expect(output).to eql(8)
201
- end
202
-
203
- end
204
-
205
- context "contains sad emoticon" do
206
-
207
- it "returns 2" do
208
- message = ":("
209
- polarity_scores = [2.0,3.0]
210
- output = SadPanda.check_emoticon_for_polarity(polarity_scores, message)
211
- expect(output).to eql(2)
212
- end
213
-
214
- end
215
-
216
- context "contains both a happy and a sad emoticon" do
217
-
218
- it "returns 5" do
219
- message = ":( :)"
220
- polarity_scores = [2.0,3.0]
221
- output = SadPanda.check_emoticon_for_polarity(polarity_scores, message)
222
- expect(output).to eql(5)
223
- end
224
-
225
- end
226
-
227
-
228
- context "contains no emoticons and polarity_scores is empty" do
229
-
230
- it "returns joy" do
231
- message = "no emoticons in hur"
232
- polarity_scores = []
233
- output = SadPanda.check_emoticon_for_polarity(polarity_scores, message)
234
- expect(output).to eql(5)
235
- end
236
-
237
- end
238
-
239
- context "contains no emoticons and emotion_score is not empty" do
240
-
241
- it "returns joy" do
242
- message = "no emoticons in hur"
243
- polarity_scores = [8.0]
244
- output = SadPanda.check_emoticon_for_polarity(polarity_scores, message)
245
- expect(output).to eql(8.0)
246
- end
247
-
248
- end
249
- end
250
-
251
- end
252
-
253
- describe "when 'build_term_frequencies' method is called" do
254
-
255
- context "when status_message is an empty string" do
256
- it "returns an empty hash" do
257
- empty_message = " "
258
- expect(SadPanda.build_term_frequencies(empty_message)).to be_empty
259
- end
260
- end
261
-
262
- context "when input is a non-recogizable word" do
263
- it "returns a empty hash with key == zorg and and value == 1" do
264
- word = "zorg"
265
- expect(SadPanda.build_term_frequencies(word)).to eql({"zorg" => 1})
266
- end
267
- end
268
-
269
- context "when input includes recognizable words" do
270
- it "returns a non-empty hash" do
271
- hash = SadPanda.build_term_frequencies("I am happy")
272
- expect(hash).to_not be_empty
273
- end
274
- end
275
-
276
62
  end
277
63
 
278
- describe "when 'get_emotion_score' method is called" do
279
- it 'returns a string' do
280
- message = "this is a message!"
281
- output = SadPanda.get_emotion_score(message, emotions,term_frequencies)
282
- expect(output.class).to eql(String)
283
- end
284
- end
64
+ describe '#polarity' do
65
+ let(:polarity) { SadPanda.polarity('I love cactuses!') }
285
66
 
286
- describe "when 'get_polarity_score' method is called" do
287
- it 'returns a string' do
288
- message = "this is another message!"
289
- output = SadPanda.get_polarity_score(message, polarities, term_frequencies)
290
- expect(output.class).to eql(Fixnum)
67
+ it 'returns a Float value polarity' do
68
+ expect(polarity).to be_a Float
291
69
  end
292
- end
293
-
294
70
 
295
- describe "when polarity method is called" do
296
-
297
- it "returns a fixnum" do
298
- expect(SadPanda.polarity("My cactus collection makes me happy.").class).to eql(Fixnum)
71
+ it 'returns 10.0' do
72
+ expect(polarity).to eq 10.0
299
73
  end
300
74
 
301
- context "when status_message == 'my lobster collection makes me happy' " do
302
- it "emotion == 'joy' " do
303
- status_message = "my lobster collection makes me happy"
304
- expect(SadPanda.emotion(status_message)).to eql("joy")
305
- end
75
+ it 'returns 8.0 for happy emoji' do
76
+ expect(SadPanda.polarity(':)')).to eq 8.0
306
77
  end
307
78
 
308
- context "when status_message == 'sad' " do
309
- it "emotion == 'sadness' " do
310
- status_message = "sad"
311
- expect(SadPanda.emotion(status_message)).to eql("sadness")
312
- end
79
+ it 'returns 8.0 for sad emoji' do
80
+ expect(SadPanda.polarity(':(')).to eq 2.0
313
81
  end
314
82
 
315
- context "when status_message == 'angry' " do
316
- it "emotion == 'anger' " do
317
- status_message = "angry"
318
- expect(SadPanda.emotion(status_message)).to eql('anger')
319
- end
83
+ it 'returns 5.0 for happy & sademoji' do
84
+ expect(SadPanda.polarity(':) :(')).to eq 5.0
320
85
  end
321
86
 
322
- context "when status_message == 'I am ril afraid of cats, homie' " do
323
- it "emotion == 'fear' " do
324
- status_message = "I am ril afraid of cats, homie"
325
- expect(SadPanda.emotion(status_message)).to eql("fear")
326
- end
327
- end
328
-
329
- context "when status == 'I am disgusted' " do
330
- it "emotion == 'disgust'" do
331
- status_message = "I am disgusted"
332
- expect(SadPanda.emotion(status_message)).to eql('disgust')
333
- end
87
+ it 'returns 5.0 for empty string' do
88
+ expect(SadPanda.polarity(' ')).to eq 5.0
334
89
  end
335
90
 
336
- context "when status == 'I am so surprised'" do
337
- it "emotion == 'surprise'" do
338
- status_message = "I am so surprised"
339
- expect(SadPanda.emotion(status_message)).to eql('surprise')
340
- end
341
- end
342
-
343
- context "when status_message == 'blarg' " do
344
- it "emotion == 'ambiguous' " do
345
- status_message = "blarg"
346
- expect(SadPanda.emotion(status_message)).to eql('ambiguous')
347
- end
348
- end
349
-
350
- context "when status_message == ' ' " do
351
- it "emotion is 'ambiguous'" do
352
- status_message = " "
353
- expect(SadPanda.emotion(status_message)).to eql('ambiguous')
354
- end
355
- end
356
-
357
- end
358
-
359
- describe "when emotion method is called" do
360
-
361
- it "returns a string" do
362
- status_message="joy"
363
- expect(SadPanda.emotion(status_message).class).to eql(String)
364
- end
365
-
366
- context "when status_message == 'I am happy' " do
367
- it "polarity is greater than zero" do
368
- status_message = "I am happy"
369
- expect(SadPanda.polarity(status_message)).to be > 0
370
- end
371
- end
372
-
373
-
374
- context "when status_message == 'sad' " do
375
- it "polarity is less than zero" do
376
- status_message = "sad"
377
- expect(SadPanda.polarity(status_message)).to be < 5
378
- end
379
- end
380
-
381
- context "when status_message == 'anger' " do
382
- it "polarity is zero" do
383
- status_message = "anger"
384
- expect(SadPanda.polarity(status_message)).to be < 5
385
- end
386
- end
387
-
388
- context "when status_message == 'I am terrified' " do
389
- it "polarity is zero" do
390
- status_message = "I am fearful"
391
- expect(SadPanda.polarity(status_message)).to be < 5
392
- end
393
- end
394
-
395
- context "when status == 'I am disgusted' " do
396
- it "has a non-zero polarity value" do
397
- status_message = "I am disgusted"
398
- expect(SadPanda.polarity(status_message)).to be < 5
399
- end
400
- end
401
-
402
- context "when status == 'This is surprising'" do
403
- it "has a neutral polarity value" do
404
- status_message = "This is surprising"
405
- expect(SadPanda.polarity(status_message)).to eql(5)
406
- end
407
- end
408
-
409
- context "when status_message == 'blarg' " do
410
- it "polarity is zero" do
411
- status_message = "blarg"
412
- expect(SadPanda.polarity(status_message)).to eql(5)
413
- end
414
- end
415
-
416
- context "when status_message == ' ' " do
417
- it "polarity is zero" do
418
- status_message = " "
419
- expect(SadPanda.polarity(status_message)).to eql(5)
420
- end
91
+ it 'returns 5.0 for a neutral' do
92
+ expect(SadPanda.polarity('This is surprising')).to eq 5.0
421
93
  end
422
94
  end
423
95
  end