afinn 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 21253857ac7169f67f8046dcb58f812125142c2120b5aeb72328f66164714747
4
+ data.tar.gz: 8665dac467c7fdcc2cff7cc0b46cee4c204330c1dfff769b8d61781d8d895e24
5
+ SHA512:
6
+ metadata.gz: aec3baf34e1572d3001f04612b9058754b4440c0643921292962778f1ce88f22cc84b223e95d2c9d9d64d8b2cb5d0439799fc342a5093e01c1dc02ba9d482a73
7
+ data.tar.gz: df29fa460a84ab34895ed362e56b3e32ea9685353a298043a7fd4d627ef441cf4b89c7d566a80ebc8510c84109b1752d04f6809b81457099bde8c538fd40cb75
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.1
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-02-24
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in afinn.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
13
+
14
+ gem "rubocop-rspec", require: false
data/Gemfile.lock ADDED
@@ -0,0 +1,58 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ afinn (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ diff-lcs (1.5.0)
11
+ parallel (1.21.0)
12
+ parser (3.1.1.0)
13
+ ast (~> 2.4.1)
14
+ rainbow (3.1.1)
15
+ rake (13.0.6)
16
+ regexp_parser (2.2.1)
17
+ rexml (3.2.5)
18
+ rspec (3.11.0)
19
+ rspec-core (~> 3.11.0)
20
+ rspec-expectations (~> 3.11.0)
21
+ rspec-mocks (~> 3.11.0)
22
+ rspec-core (3.11.0)
23
+ rspec-support (~> 3.11.0)
24
+ rspec-expectations (3.11.0)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.11.0)
27
+ rspec-mocks (3.11.0)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.11.0)
30
+ rspec-support (3.11.0)
31
+ rubocop (1.25.1)
32
+ parallel (~> 1.10)
33
+ parser (>= 3.1.0.0)
34
+ rainbow (>= 2.2.2, < 4.0)
35
+ regexp_parser (>= 1.8, < 3.0)
36
+ rexml
37
+ rubocop-ast (>= 1.15.1, < 2.0)
38
+ ruby-progressbar (~> 1.7)
39
+ unicode-display_width (>= 1.4.0, < 3.0)
40
+ rubocop-ast (1.16.0)
41
+ parser (>= 3.1.1.0)
42
+ rubocop-rspec (2.8.0)
43
+ rubocop (~> 1.19)
44
+ ruby-progressbar (1.11.0)
45
+ unicode-display_width (2.1.0)
46
+
47
+ PLATFORMS
48
+ x86_64-linux
49
+
50
+ DEPENDENCIES
51
+ afinn!
52
+ rake (~> 13.0)
53
+ rspec (~> 3.0)
54
+ rubocop (~> 1.21)
55
+ rubocop-rspec
56
+
57
+ BUNDLED WITH
58
+ 2.3.7
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # afinn
2
+
3
+ Sentiment analysis in Ruby.
4
+
5
+ Dictionaries included:
6
+ * 🇬🇧 English Language
7
+ * 🇩🇰 Danish Language
8
+ * Emoticions
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'afinn'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle install
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install afinn
25
+
26
+ ## Usage
27
+
28
+ ```ruby
29
+ require 'afinn'
30
+
31
+ afinn = Afinn.new(language = :da, emoticons = true)
32
+ afinn.score('Hvis ikke det er det mest afskyelige flueknepperi...')
33
+ #=> -8.0
34
+
35
+ afinn = Afinn.new(language = :en)
36
+ afinn.score_to_words("I had a slow puncture that needed attending to and they took care of it very well. Friendly and efficient staff and a clean and tidy work area. Happy to recommend them and will use them in the future.")
37
+ #=> "Positive"
38
+ ```
39
+
40
+ ## Contributing
41
+
42
+ Bug reports and pull requests are welcome on GitHub at https://github.com/prograils/afinn.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/lib/afinn.rb ADDED
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ LANGUAGE_TO_FILENAME = {
4
+ da: "da.txt",
5
+ en: "en.txt",
6
+ emoticons: "emoticons.txt"
7
+ }.freeze
8
+
9
+ class Afinn
10
+ attr_reader :word_dict
11
+
12
+ def initialize(language = :en, emoticons = false)
13
+ read_dictionaries(language, emoticons)
14
+ end
15
+
16
+ def score(text)
17
+ text = text.gsub(/[!'",.?]/, "").split(" ")
18
+ scoring = 0
19
+ text.each do |word|
20
+ scoring += @word_dict[word].to_f if @word_dict.include? word
21
+ end
22
+ scoring
23
+ end
24
+
25
+ def score_to_words(text)
26
+ scored = score(text)
27
+ if scored.between?(-1.0, 1.0)
28
+ :neutral
29
+ elsif scored > 1.0
30
+ :positive
31
+ else
32
+ :negative
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def read_dictionaries(language, emoticons)
39
+ filename = LANGUAGE_TO_FILENAME[language]
40
+ full_filename = File.join(__dir__, "dictionaries", filename)
41
+ data = read_word_file(full_filename)
42
+
43
+ if emoticons
44
+ filename_emoticons = LANGUAGE_TO_FILENAME[:emoticons]
45
+ full_filename_emoticons = File.join(__dir__, "dictionaries", filename_emoticons)
46
+ emo = read_word_file(full_filename_emoticons)
47
+ data = data.merge(emo)
48
+ end
49
+ @word_dict = data
50
+ end
51
+
52
+ def read_word_file(file)
53
+ @word_dict = {}
54
+ File.read(file).each_line do |line|
55
+ line = line.strip.split("\t")
56
+ @word_dict.store(line[0], line[1])
57
+ end
58
+ @word_dict
59
+ end
60
+ end