sentiwordnet_ruby 0.0.1

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,116 @@
1
+ class SentiWordNet
2
+
3
+ @@sentihash = {}
4
+
5
+
6
+ def get_score(string)
7
+ sentiment_total = 0.0
8
+
9
+ #tokenize the string, also throw away some punctuation
10
+ tokens = tokenize(string)
11
+
12
+ tokens.each do |token|
13
+
14
+ if @@sentihash[token+"#n"]
15
+ sentiment_total += @@sentihash[token+"#n"]
16
+ end
17
+
18
+ if @@sentihash[token+"#a"]
19
+ sentiment_total += @@sentihash[token+"#a"]
20
+ end
21
+
22
+ if @@sentihash[token+"#r"]
23
+ sentiment_total += @@sentihash[token+"#r"]
24
+ end
25
+
26
+ if @@sentihash[token+"#v"]
27
+ sentiment_total += @@sentihash[token+"#v"]
28
+ end
29
+ end
30
+
31
+ sentiment_total
32
+ end
33
+
34
+ def get_sentiment(string)
35
+ score = get_score(string)
36
+
37
+ if (score > 0.75)
38
+ return "very_positive"
39
+ elsif (score > 0.25 && score <= 0.75)
40
+ return "positive"
41
+ elsif (score > 0 && score <= 0.25)
42
+ return "weak_positive"
43
+ elsif (score == 0)
44
+ return "neutral"
45
+ elsif (score < 0 && score >= -0.25)
46
+ return "weak_negative"
47
+ elsif (score < -0.25 && score >= -0.75)
48
+ return "negative"
49
+ elsif (score < -0.75)
50
+ return "very_negative"
51
+ end
52
+ end
53
+
54
+ def tokenize string
55
+ fold_case = false
56
+ string.split(/\s+/).map do |token|
57
+ stripped = token.gsub(/[^a-zA-Z0-9\']+/, '')
58
+ fold_case ? stripped.downcase : stripped
59
+ end
60
+ end
61
+
62
+
63
+ def self.load_defaults
64
+ load_senti_file(File.dirname(__FILE__) + '/SentiWordNet_3.0.0.txt')
65
+ end
66
+
67
+ def self.load_senti_file(filename)
68
+ word_temp = Hash.new
69
+
70
+ File.readlines(filename).each do |line|
71
+ data = line.split("\t")
72
+ score = data[2].to_f - data[3].to_f
73
+ words = data[4].split(" ")
74
+ words.each do |w|
75
+ w_n = w.split("#")
76
+ w_n[0] += "#"+data[0]
77
+ index = w_n[1].to_i - 1
78
+ if word_temp.has_key?(w_n[0])
79
+ v = word_temp[w_n[0]]
80
+ if (index > v.size)
81
+ ((v.size)..(index - 1)).each do |i|
82
+ v << 0.0
83
+ end
84
+ end
85
+ v[index] = score
86
+ word_temp[w_n[0]] = v
87
+ else
88
+ v = []
89
+ (0..(index - 1)).each do |i|
90
+ v << 0.0
91
+ end
92
+ v[index] = score
93
+ word_temp[w_n[0]] = v
94
+ end
95
+ end
96
+ end
97
+
98
+ word_temp.keys.each do |k|
99
+ word = k
100
+ v = word_temp[k]
101
+ score = 0.0
102
+ sum = 0.0
103
+ (0..(v.size - 1)).each do |i|
104
+ score += (1.0/(i+1).to_f)* v[i]
105
+ end
106
+ (1..(v.size)).each do |i|
107
+ sum += 1.to_f/i.to_f
108
+ end
109
+ score /= sum
110
+ @@sentihash[word] = score
111
+ end
112
+ end
113
+ end
114
+
115
+ #SentiWordNet.load_senti_file('SentiWordNet_3.0.0.txt')
116
+
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sentiwordnet_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Shailesh Kalamkar
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-23 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A simple SentiWordNet utility for Ruby
15
+ email: shailesh.kalamkar@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/SentiWordNet_3.0.0.txt
21
+ - lib/sentiwordnet_ruby.rb
22
+ - README.md
23
+ homepage: https://github.com/kalamkar/sentiwordnet_ruby
24
+ licenses: []
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - .
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 1.8.25
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: SentiWordNet 3.0
48
+ test_files: []