sentimental 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +79 -0
  2. data/lib/sentimental.rb +4 -11
  3. data/lib/sentislang.txt +1 -1
  4. metadata +3 -1
data/README.md ADDED
@@ -0,0 +1,79 @@
1
+ # sentimental
2
+
3
+ Simple sentiment analysis with Ruby
4
+
5
+ ## How it works
6
+
7
+ Sentences are tokenized, tokens are assigned a numerical score
8
+ for their average sentiment. The total score is then used to
9
+ determine the overall sentiment in relation to the thresold.
10
+
11
+ For example, the default threshold is 0.0. If a sentence has
12
+ a score of 0, it is deemed "neutral". Higher than the thresold
13
+ is "positive", lower is "negative".
14
+
15
+ If you set the threshold to a non-zero amount, e.g. 0.25:
16
+
17
+ - Positive scores are > 0.25
18
+ - Neutral scores are -0.25 - 0.25
19
+ - Negative scores are < -0.25
20
+
21
+
22
+ ## Usage
23
+
24
+ ```ruby
25
+
26
+ # Load the default sentiment dictionaries
27
+ Sentimental.load_defaults
28
+
29
+ # And/or load your own dictionaries
30
+ Sentimental.load_senti_file('path/to/your/file.txt')
31
+
32
+ # Set a global threshold
33
+ Sentimental.threshold = 0.1
34
+
35
+ # Create an instance for usage:
36
+ analyzer = Sentimental.new
37
+ analyzer.get_sentiment 'I love ruby'
38
+ #=> :positive
39
+
40
+ analyzer.get_sentiment 'I like ruby'
41
+ #=> :neutral
42
+
43
+ analyzer.get_sentiment 'I really like ruby'
44
+ #=> :positive
45
+
46
+ # You can make new analyzers with individual thresholds:
47
+ analyzer = Sentimental.new(0.9)
48
+ analyzer.get_sentiment 'I love ruby'
49
+ #=> :positive
50
+
51
+ analyzer.get_sentiment 'I like ruby'
52
+ #=> :neutral
53
+
54
+ analyzer.get_sentiment 'I really like ruby'
55
+ #=> :neutral
56
+
57
+ # Get the numerical score of a string:
58
+ analyzer.get_score 'I love ruby'
59
+ #=> 0.925
60
+
61
+ ```
62
+
63
+ ## Sentiment dictionaries
64
+
65
+ These are currently plain-text files containing whitespace-separated
66
+ scores and tokens, e.g.:
67
+
68
+ 1.0 Awesome
69
+ 0.0 Meh
70
+ -1.0 Horrible
71
+
72
+ ## Installation
73
+
74
+ gem install sentimental
75
+
76
+ ## Credits
77
+
78
+ Based largely on Christopher MacLellan's script:
79
+ https://github.com/cmaclell/Basic-Tweet-Sentiment-Analyzer
data/lib/sentimental.rb CHANGED
@@ -64,18 +64,10 @@ class Sentimental
64
64
  #tokenize the string, also throw away some punctuation
65
65
  tokens = string.to_s.downcase.split(/[\s\!\?\.]+/)
66
66
 
67
- for token in tokens do
67
+ tokens.each do |token|
68
68
  sentiment_value = @@sentihash[token]
69
-
70
- if sentiment_value
71
-
72
- # for debugging purposes
73
- #puts "#{token} => #{sentiment_value}"
74
-
75
- sentiment_total += sentiment_value
76
- end
69
+ sentiment_total += sentiment_value if sentiment_value
77
70
  end
78
-
79
71
  sentiment_total
80
72
  end
81
73
 
@@ -93,6 +85,7 @@ class Sentimental
93
85
  end
94
86
  end
95
87
 
88
+ # Loads the default sentiment files
96
89
  def self.load_defaults
97
90
  load_senti_file(File.dirname(__FILE__) + '/sentiwords.txt')
98
91
  load_senti_file(File.dirname(__FILE__) + '/sentislang.txt')
@@ -109,7 +102,7 @@ class Sentimental
109
102
  # load the word file
110
103
  file = File.new(filename)
111
104
  while (line = file.gets)
112
- parsedline = line.chomp.split("\t")
105
+ parsedline = line.chomp.split(/\s/)
113
106
  sentiscore = parsedline[0]
114
107
  text = parsedline[1]
115
108
  @@sentihash[text] = sentiscore.to_f
data/lib/sentislang.txt CHANGED
@@ -26,7 +26,7 @@
26
26
  -1.0 D:
27
27
  -1.0 sux
28
28
  1.0 (o;
29
- 1.00 8-)
29
+ 1.0 8-)
30
30
  1.0 ;)
31
31
  1.0 ;o)
32
32
  1.0 %-)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sentimental
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -21,11 +21,13 @@ files:
21
21
  - lib/sentimental.rb
22
22
  - lib/sentislang.txt
23
23
  - lib/sentiwords.txt
24
+ - README.md
24
25
  homepage: https://github.com/7compass/sentimental
25
26
  licenses: []
26
27
  post_install_message:
27
28
  rdoc_options: []
28
29
  require_paths:
30
+ - .
29
31
  - lib
30
32
  required_ruby_version: !ruby/object:Gem::Requirement
31
33
  none: false