emotidragon 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +24 -0
- data/README.md +88 -0
- data/Rakefile +2 -0
- data/emotidragon.gemspec +23 -0
- data/lib/emotidragon.rb +84 -0
- data/lib/emotidragon/version.rb +3 -0
- data/spec/emotidragon_spec.rb +147 -0
- data/spec/spec_helper.rb +23 -0
- metadata +74 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
emotidragon (1.0.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.1.3)
|
10
|
+
rspec (2.12.0)
|
11
|
+
rspec-core (~> 2.12.0)
|
12
|
+
rspec-expectations (~> 2.12.0)
|
13
|
+
rspec-mocks (~> 2.12.0)
|
14
|
+
rspec-core (2.12.2)
|
15
|
+
rspec-expectations (2.12.1)
|
16
|
+
diff-lcs (~> 1.1.3)
|
17
|
+
rspec-mocks (2.12.2)
|
18
|
+
|
19
|
+
PLATFORMS
|
20
|
+
ruby
|
21
|
+
|
22
|
+
DEPENDENCIES
|
23
|
+
emotidragon!
|
24
|
+
rspec
|
data/README.md
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# Emotidragon: Emoticons! ROAR!
|
2
|
+
|
3
|
+
Emotidragon acts on String objects to help ruby devs work with emoticons, what can you do:
|
4
|
+
|
5
|
+
* get an array of all emoticons
|
6
|
+
* get an array of just positive/negative emoticons
|
7
|
+
* get sentiment of the text based on emoticons
|
8
|
+
* determine if string contains an emoticon
|
9
|
+
* determine if string contains just positive/negative emoticon
|
10
|
+
* get number of emoticons
|
11
|
+
* get number of positive/negative emoitcons
|
12
|
+
* get sentiment of text based on sentiment of emoticons
|
13
|
+
* get an informative hash of sentiment, number of positive emoticons, number of negative emoticons,and difference between the number of emoticons
|
14
|
+
|
15
|
+
## I always wanted to ride a dragon myself, so I decided to do this for a year in my imagination.
|
16
|
+
|
17
|
+
Use RubyGems to install emotidragon
|
18
|
+
|
19
|
+
$ gem install emotidragon
|
20
|
+
|
21
|
+
## What can I do? Remember, emotidragon gets included into String class, so you can call methods on an instance of String
|
22
|
+
|
23
|
+
## Method List
|
24
|
+
### emoticons - get array of all emoticons
|
25
|
+
"I love science :). I hate nickelback :(".emoticions
|
26
|
+
-> [":)", ":("]
|
27
|
+
|
28
|
+
### positive_emoticons - get array of positive emoticons
|
29
|
+
"I love science :). I hate nickelback :(".positive_emoticions
|
30
|
+
-> [":)"]
|
31
|
+
|
32
|
+
### negative_emoticons - get array of negative emoticons
|
33
|
+
"I love science :). I hate nickelback :(".positive_emoticions
|
34
|
+
-> [":("]
|
35
|
+
|
36
|
+
### emoticon? - determine if string contains an emoticon
|
37
|
+
"i love science :). i hate nickelback :(".emoticion?
|
38
|
+
-> true
|
39
|
+
|
40
|
+
### positive_emoticon? - determine if string contains positive emoticon
|
41
|
+
"I love science :)".positive_emoticion?
|
42
|
+
-> true
|
43
|
+
|
44
|
+
"I hate nickelback :(".positive_emoticion?
|
45
|
+
-> false
|
46
|
+
|
47
|
+
### negative_emoticon? - determine if string contains negative emoticon
|
48
|
+
"I hate nickelback :(".negative_emoticion?
|
49
|
+
-> true
|
50
|
+
|
51
|
+
"I love science :)".negative_emoticion?
|
52
|
+
-> false
|
53
|
+
|
54
|
+
### number_of_emoticon - get total number of emoticonss
|
55
|
+
"i love science :). i hate nickelback :(".number_of_emoticons
|
56
|
+
-> 2
|
57
|
+
|
58
|
+
### positive_count - get number of positive emoticons
|
59
|
+
"i love science :). i hate nickelback :(".positive_count
|
60
|
+
-> 1
|
61
|
+
|
62
|
+
### negative_count - get number of negative emoticons
|
63
|
+
"i love science :). i hate nickelback :(".negative_count
|
64
|
+
-> 1
|
65
|
+
|
66
|
+
### text_sentiment - get sentiment of text
|
67
|
+
"I love science :). I hate nickelback :(".text_sentiment
|
68
|
+
-> "neutral
|
69
|
+
|
70
|
+
"I love science :)".text_sentiment
|
71
|
+
-> "positive"
|
72
|
+
|
73
|
+
"I hate nickelback :(".text_sentiment
|
74
|
+
-> "negative"
|
75
|
+
|
76
|
+
### sentiment_info - get a hash of information about the text (sentiment, # of positive emoticons, # of negative emoticons, difference)
|
77
|
+
"I love science :). I hate nickelback :(".sentiment_info
|
78
|
+
-> { :sentiment => "neutral", :positive_count => 1, :negative_count => 1, :difference => 0 }
|
79
|
+
|
80
|
+
"I love science :)".sentiment_info
|
81
|
+
-> { :sentiment => "neutral", :positive_count => 1, :negative_count => 0, :difference => 1 }
|
82
|
+
|
83
|
+
"I hate nickelback :(".sentiment_info
|
84
|
+
-> { :sentiment => "neutral", :positive_count => 0, :negative_count => 1, :difference => 1 }
|
85
|
+
|
86
|
+
### Dependencies
|
87
|
+
|
88
|
+
Just rspec, for science and testing!
|
data/Rakefile
ADDED
data/emotidragon.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "emotidragon/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'emotidragon'
|
7
|
+
s.version = Emotidragon::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Chris Nixon"]
|
10
|
+
s.email = ['chris@liamneesonsarm.com']
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{Emoticons! ROAR!}
|
13
|
+
s.description = %q{A simple gem to collect an array of, get sentiment about a string based on, determine if a string contains...emoticons}
|
14
|
+
|
15
|
+
s.add_development_dependency "rspec"
|
16
|
+
|
17
|
+
s.rubyforge_project = "emotidragon"
|
18
|
+
|
19
|
+
s.files = `git ls-files`.split("\n")
|
20
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
21
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
22
|
+
s.require_paths = ["lib"]
|
23
|
+
end
|
data/lib/emotidragon.rb
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
module Emotidragon #http://en.wikipedia.org/wiki/List_of_emoticons
|
2
|
+
POSITIVE_EMOTICONS = /\|?>?[:*;Xx8=]-?o?\^?[DPpb3)}\]>]\)?/
|
3
|
+
NEGATIVE_EMOTICONS = /([:><].?-?[@><cC(\[{\|]\|?|[D][:8;=X]<?|v.v)/
|
4
|
+
|
5
|
+
#gets an array of all matching emoticons in the text
|
6
|
+
def emoticons
|
7
|
+
(positive_emoticons + negative_emoticons)
|
8
|
+
end
|
9
|
+
|
10
|
+
#gets an array of the matching positive emoticons in the text
|
11
|
+
def positive_emoticons
|
12
|
+
self.scan(POSITIVE_EMOTICONS).flatten
|
13
|
+
end
|
14
|
+
|
15
|
+
#gets an array of the matching negative emoticons in the text
|
16
|
+
def negative_emoticons
|
17
|
+
self.scan(NEGATIVE_EMOTICONS).flatten
|
18
|
+
end
|
19
|
+
|
20
|
+
#contains any emoticon?
|
21
|
+
def emoticon?
|
22
|
+
(positive_emoticon? || negative_emoticon?)
|
23
|
+
end
|
24
|
+
|
25
|
+
#checks if the text has a positive emoticon
|
26
|
+
def positive_emoticon?
|
27
|
+
self =~ POSITIVE_EMOTICONS
|
28
|
+
end
|
29
|
+
|
30
|
+
#checks if the text has a negative emoticon
|
31
|
+
def negative_emoticon?
|
32
|
+
self =~ NEGATIVE_EMOTICONS
|
33
|
+
end
|
34
|
+
|
35
|
+
#get total number of emoticons
|
36
|
+
def number_of_emoticons
|
37
|
+
positive_count + negative_count
|
38
|
+
end
|
39
|
+
|
40
|
+
#number of positive emoticons
|
41
|
+
def positive_count
|
42
|
+
positive_emoticons.count
|
43
|
+
end
|
44
|
+
|
45
|
+
#number of negative emoticons
|
46
|
+
def negative_count
|
47
|
+
negative_emoticons.count
|
48
|
+
end
|
49
|
+
|
50
|
+
#sentiment based on emoticons
|
51
|
+
def text_sentiment
|
52
|
+
count = positive_emoticons.count - negative_emoticons.count
|
53
|
+
if count > 0
|
54
|
+
"positive"
|
55
|
+
elsif count < 0
|
56
|
+
"negative"
|
57
|
+
else
|
58
|
+
"neutral"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
#gets more info regarding the sentiment of the text
|
63
|
+
def sentiment_info
|
64
|
+
pos_count, neg_count = positive_count, negative_count
|
65
|
+
count = pos_count - neg_count
|
66
|
+
if count > 0
|
67
|
+
Emotidragon.score_hash("positive", pos_count, neg_count, count)
|
68
|
+
elsif count < 0
|
69
|
+
Emotidragon.score_hash("negative", pos_count, neg_count, count)
|
70
|
+
else
|
71
|
+
Emotidragon.score_hash("neutral", pos_count, neg_count, count)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
#include Emotidragon into String class
|
76
|
+
String.send(:include, Emotidragon)
|
77
|
+
|
78
|
+
private
|
79
|
+
|
80
|
+
#hash returned that contains information regarding text
|
81
|
+
def self.score_hash(sentiment, pos_count, neg_count, count)
|
82
|
+
{ sentiment: sentiment, positive_count: pos_count, negative_count: neg_count, difference: count.abs }
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,147 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Emotidragon do
|
4
|
+
let(:positive_string) { 'This is a positive :) string' }
|
5
|
+
let(:negative_string) { 'This is a negative :( string' }
|
6
|
+
let(:neutral_string) { 'This is a neutral string' }
|
7
|
+
let(:emoticon_string) { 'This contains :) and :( emoticons. :o) :*(' }
|
8
|
+
|
9
|
+
describe '#emoticons' do
|
10
|
+
context 'emoticons present' do
|
11
|
+
it('returns all positive and negative emoticons') { emoticon_string.emoticons.should =~ [':)', ':(', ':o)', ':*('] }
|
12
|
+
end
|
13
|
+
context 'no emoticons' do
|
14
|
+
it('returns empty array') { neutral_string.emoticons.should == [] }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#positive_emoticons' do
|
19
|
+
context 'positive emoticons' do
|
20
|
+
it('returns all positive') { emoticon_string.positive_emoticons.should =~ [':)', ':o)'] }
|
21
|
+
end
|
22
|
+
context 'no positive emoticons, just negative' do
|
23
|
+
it('should not return any negative') { emoticon_string.positive_emoticons.should_not == [':(', ':*('] }
|
24
|
+
end
|
25
|
+
context 'no emoticons' do
|
26
|
+
it('returns empty array') { neutral_string.positive_emoticons.should == [] }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '#negative_emoticons' do
|
31
|
+
context 'negative emoticons' do
|
32
|
+
it('returns all negative') { emoticon_string.negative_emoticons.should =~ [':(', ':*('] }
|
33
|
+
end
|
34
|
+
context 'no negative emoticons, just positive' do
|
35
|
+
it('should not return any positive') { emoticon_string.negative_emoticons.should_not == [':)', ':o)'] }
|
36
|
+
end
|
37
|
+
context 'no emoticons' do
|
38
|
+
it('returns empty array') { neutral_string.negative_emoticons.should == [] }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe '#emoticon?' do
|
43
|
+
context 'both' do
|
44
|
+
it('returns true') { emoticon_string.emoticon?.should be_true }
|
45
|
+
end
|
46
|
+
context 'just positive' do
|
47
|
+
it('returns true') { positive_string.emoticon?.should be_true }
|
48
|
+
end
|
49
|
+
context 'just negative' do
|
50
|
+
it('returns true') { negative_string.emoticon?.should be_true }
|
51
|
+
end
|
52
|
+
context 'neither' do
|
53
|
+
it('returns false') { neutral_string.emoticon?.should be_false }
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe '#positive_emoticon?' do
|
58
|
+
context 'text contains a positive emoticon' do
|
59
|
+
it('returns true') { positive_string.positive_emoticon?.should be_true }
|
60
|
+
end
|
61
|
+
context 'text does not contain a positive emoticon' do
|
62
|
+
it('returns true') { negative_string.positive_emoticon?.should be_false }
|
63
|
+
end
|
64
|
+
context 'neither' do
|
65
|
+
it('returns false') { neutral_string.positive_emoticon?.should be_false }
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe '#negative_emoticon?' do
|
70
|
+
context 'text contains a negative emoticon' do
|
71
|
+
it('returns true') { negative_string.negative_emoticon?.should be_true }
|
72
|
+
end
|
73
|
+
context 'text does not contain a negative emoticon' do
|
74
|
+
it('returns true') { positive_string.negative_emoticon?.should be_false }
|
75
|
+
end
|
76
|
+
context 'neither' do
|
77
|
+
it('returns false') { neutral_string.negative_emoticon?.should be_false }
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe '#number_of_emoticons' do
|
82
|
+
it('returns the count') { emoticon_string.number_of_emoticons.should == 4 }
|
83
|
+
it('returns the count') { positive_string.number_of_emoticons.should == 1 }
|
84
|
+
it('returns the count') { negative_string.number_of_emoticons.should == 1 }
|
85
|
+
it('returns the count') { neutral_string.number_of_emoticons.should == 0 }
|
86
|
+
end
|
87
|
+
|
88
|
+
describe '#positive_count' do
|
89
|
+
context 'text contains positive emoticon' do
|
90
|
+
it('returns the count') { positive_string.positive_count.should == 1 }
|
91
|
+
it('returns the count') { emoticon_string.positive_count.should == 2 }
|
92
|
+
end
|
93
|
+
context 'text does not contain a positive emoticon' do
|
94
|
+
it('returns the count') { neutral_string.positive_count.should == 0 }
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
describe '#negative_count' do
|
99
|
+
context 'text contains positive emoticon' do
|
100
|
+
it('returns the count') { negative_string.negative_count.should == 1 }
|
101
|
+
it('returns the count') { emoticon_string.negative_count.should == 2 }
|
102
|
+
end
|
103
|
+
context 'text does not contain a positive emoticon' do
|
104
|
+
it('returns the count') { neutral_string.negative_count.should == 0 }
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
describe '#text_sentiment' do
|
109
|
+
context 'positive' do
|
110
|
+
it('returns "positive"') { positive_string.text_sentiment.should == "positive" }
|
111
|
+
end
|
112
|
+
context 'negative' do
|
113
|
+
it('returns "negative"') { negative_string.text_sentiment.should == "negative" }
|
114
|
+
end
|
115
|
+
context 'neutral' do
|
116
|
+
it('returns "neutral"') { emoticon_string.text_sentiment.should == "neutral" }
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
describe '#sentiment_info' do
|
121
|
+
context 'positive count' do
|
122
|
+
it 'returns hash containing positive' do
|
123
|
+
positive_string.sentiment_info.should == { sentiment: "positive", positive_count: 1, negative_count: 0, difference: 1 }
|
124
|
+
end
|
125
|
+
end
|
126
|
+
context 'negative count' do
|
127
|
+
it 'returns hash containing negative' do
|
128
|
+
negative_string.sentiment_info.should == { sentiment: "negative", positive_count: 0, negative_count: 1, difference: 1 }
|
129
|
+
end
|
130
|
+
end
|
131
|
+
context 'neutral count' do
|
132
|
+
it 'returns hash containing neutral' do
|
133
|
+
neutral_string.sentiment_info.should == { sentiment: "neutral", positive_count: 0, negative_count: 0, difference: 0 }
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
describe '#score_hash' do
|
139
|
+
let(:sentiment) { "sentiment" }
|
140
|
+
let(:pos_count) { Random.rand(0...10) }
|
141
|
+
let(:neg_count) { Random.rand(0...10) }
|
142
|
+
let(:difference) { (pos_count - neg_count).abs }
|
143
|
+
it 'returns hash of what is passed in' do
|
144
|
+
Emotidragon.score_hash(sentiment, pos_count, neg_count, difference).should == { sentiment: sentiment, positive_count: pos_count, negative_count: neg_count, difference: difference }
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
|
8
|
+
require 'rubygems'
|
9
|
+
require 'bundler/setup'
|
10
|
+
|
11
|
+
require 'emotidragon'
|
12
|
+
|
13
|
+
RSpec.configure do |config|
|
14
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
15
|
+
config.run_all_when_everything_filtered = true
|
16
|
+
config.filter_run :focus
|
17
|
+
|
18
|
+
# Run specs in random order to surface order dependencies. If you find an
|
19
|
+
# order dependency and want to debug it, you can fix the order by providing
|
20
|
+
# the seed, which is printed after each run.
|
21
|
+
# --seed 1234
|
22
|
+
config.order = 'random'
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: emotidragon
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Chris Nixon
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-02 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: A simple gem to collect an array of, get sentiment about a string based
|
31
|
+
on, determine if a string contains...emoticons
|
32
|
+
email:
|
33
|
+
- chris@liamneesonsarm.com
|
34
|
+
executables: []
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
files:
|
38
|
+
- .gitignore
|
39
|
+
- Gemfile
|
40
|
+
- Gemfile.lock
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- emotidragon.gemspec
|
44
|
+
- lib/emotidragon.rb
|
45
|
+
- lib/emotidragon/version.rb
|
46
|
+
- spec/emotidragon_spec.rb
|
47
|
+
- spec/spec_helper.rb
|
48
|
+
homepage: ''
|
49
|
+
licenses: []
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ! '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
requirements: []
|
67
|
+
rubyforge_project: emotidragon
|
68
|
+
rubygems_version: 1.8.24
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Emoticons! ROAR!
|
72
|
+
test_files:
|
73
|
+
- spec/emotidragon_spec.rb
|
74
|
+
- spec/spec_helper.rb
|