forked from cmaclell/Basic-Tweet-Sentiment-Analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic-sentiment.rb
75 lines (63 loc) · 2.5 KB
/
basic-sentiment.rb
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
#############################################################################
# Filename: basic-sentiment.rb
# Copyright: Christopher MacLellan 2010
# Description: This code adds functions to the string class for calculating
# the sentivalue of strings. It is not called directly by the
# tweet-search-sentiment.rb program but is included for possible
# future use.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#############################################################################
class String
@@sentihash = {}
#####################################################################
# Function that returns the sentiment value for a given string.
# This value is the sum of the sentiment values of each of the words.
# Stop words are NOT removed.
#
# return:float -- sentiment value of the current string
#####################################################################
def get_sentiment
sentiment_total = 0.0
#tokenize the string
tokens = self.split
for token in tokens do
sentiment_value = @@sentihash[token]
if sentiment_value
# for debugging purposes
#puts "#{token} => #{sentiment_value}"
sentiment_total += sentiment_value
end
end
return sentiment_total
end
#####################################################################
# load the specified sentiment file into a hash
#
# filename:string -- name of file to load
# sentihash:hash -- hash to load data into
# return:hash -- hash with data loaded
#####################################################################
def load_senti_file (filename)
# load the word file
file = File.new(filename)
while (line = file.gets)
parsedline = line.chomp.split("\t")
sentiscore = parsedline[0]
text = parsedline[1]
@@sentihash[text] = sentiscore.to_f
end
file.close
end
end