-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSentiment.rb
86 lines (76 loc) · 2.34 KB
/
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
76
77
78
79
80
81
82
83
84
85
86
class Sentiment
attr_accessor :sentihash
def initialize
@sentihash = load_senti_file ('Sentiment/sentiwords.txt')
@sentihash.merge!(load_senti_file ('Sentiment/sentislang.txt'))
end
#####################################################################
# Function analyzes the sentiment of a tweet. Very basic. This just
# imports a list of words with sentiment scores from file and uses
# these to perform the analysis.
#
# tweet: string -- string to analyze the sentiment of
# return: int -- 0 negative, 1 means neutral, and 2 means positive
#####################################################################
def total_sentiment(text)
# tokenize the text
tokens = text.split
sentiment_total = 0.0
for token in tokens do
sentiment_value = self.sentihash[token]
if sentiment_value
sentiment_total += sentiment_value
end
end
# threshold for classification
threshold = 0.0
# if less then the negative threshold classify negative
if sentiment_total < (-1 * threshold)
return 0
# if greater then the positive threshold classify positive
elsif sentiment_total > threshold
return 2
# otherwise classify as neutral
else
return 1
end
end
def sentiment_sentence(text)
tokens = text.split
sentiment_total = 0.0
sentiment_array = Array.new
for token in tokens do
sentiment_value = self.sentihash[token]
if sentiment_value
sentiment_array << sentiment_value
else
sentiment_array << 0.0
end
end
return sentiment_array
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)
sentihash = {}
# load the word file
begin
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
rescue
puts "sentislang.txt or sentiswords.txt are missing"
end
return sentihash
end
end