stealth-aws-comprehend 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ebf9ff316ff38bd0e93b7d902199389529ea3aee2aae2b074c486a4eddd66a1d
4
+ data.tar.gz: 06b6cee909c613ff518d39044b80e1dcfc7de7edcf6b531ac620e88773f513f5
5
+ SHA512:
6
+ metadata.gz: 7f58207d4b2d8b5f4bafba530586094288c0509cc2fe501581268454975b6af3c53ee2d56d19ba46951230d17c83126333a97d64e68a9ae8e326285161d22dd4
7
+ data.tar.gz: fddb2d17bff2020db94bd442f0758bf9b4932c5591398184024f513139c0d080675b08cffd414f21c5c6d72810e5e152df8cc0918b60a31579570874fccb779b
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ gemspec
2
+
3
+ source 'https://rubygems.org'
4
+ ruby '2.5.1'
data/Gemfile.lock ADDED
@@ -0,0 +1,86 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ stealth-aws-comprehend (0.9.0)
5
+ aws-sdk-comprehend (~> 1.0)
6
+ stealth (< 2.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activesupport (5.2.0)
12
+ concurrent-ruby (~> 1.0, >= 1.0.2)
13
+ i18n (>= 0.7, < 2)
14
+ minitest (~> 5.1)
15
+ tzinfo (~> 1.1)
16
+ aws-partitions (1.82.0)
17
+ aws-sdk-comprehend (1.0.0)
18
+ aws-sdk-core (~> 3)
19
+ aws-sigv4 (~> 1.0)
20
+ aws-sdk-core (3.20.2)
21
+ aws-partitions (~> 1.0)
22
+ aws-sigv4 (~> 1.0)
23
+ jmespath (~> 1.0)
24
+ aws-sigv4 (1.0.2)
25
+ concurrent-ruby (1.0.5)
26
+ connection_pool (2.2.1)
27
+ diff-lcs (1.3)
28
+ i18n (1.0.1)
29
+ concurrent-ruby (~> 1.0)
30
+ jmespath (1.4.0)
31
+ minitest (5.11.3)
32
+ multi_json (1.13.1)
33
+ mustermann (1.0.2)
34
+ puma (3.11.4)
35
+ rack (2.0.5)
36
+ rack-protection (2.0.1)
37
+ rack
38
+ redis (4.0.1)
39
+ rspec (3.6.0)
40
+ rspec-core (~> 3.6.0)
41
+ rspec-expectations (~> 3.6.0)
42
+ rspec-mocks (~> 3.6.0)
43
+ rspec-core (3.6.0)
44
+ rspec-support (~> 3.6.0)
45
+ rspec-expectations (3.6.0)
46
+ diff-lcs (>= 1.2.0, < 2.0)
47
+ rspec-support (~> 3.6.0)
48
+ rspec-mocks (3.6.0)
49
+ diff-lcs (>= 1.2.0, < 2.0)
50
+ rspec-support (~> 3.6.0)
51
+ rspec-support (3.6.0)
52
+ sidekiq (5.1.3)
53
+ concurrent-ruby (~> 1.0)
54
+ connection_pool (~> 2.2, >= 2.2.0)
55
+ rack-protection (>= 1.5.0)
56
+ redis (>= 3.3.5, < 5)
57
+ sinatra (2.0.1)
58
+ mustermann (~> 1.0)
59
+ rack (~> 2.0)
60
+ rack-protection (= 2.0.1)
61
+ tilt (~> 2.0)
62
+ stealth (0.10.6)
63
+ activesupport (~> 5.2.0.rc1)
64
+ multi_json (~> 1.12)
65
+ puma (~> 3.10)
66
+ sidekiq (~> 5.0)
67
+ sinatra (~> 2.0)
68
+ thor (~> 0.20)
69
+ thor (0.20.0)
70
+ thread_safe (0.3.6)
71
+ tilt (2.0.8)
72
+ tzinfo (1.2.5)
73
+ thread_safe (~> 0.1)
74
+
75
+ PLATFORMS
76
+ ruby
77
+
78
+ DEPENDENCIES
79
+ rspec (= 3.6.0)
80
+ stealth-aws-comprehend!
81
+
82
+ RUBY VERSION
83
+ ruby 2.5.1p57
84
+
85
+ BUNDLED WITH
86
+ 1.16.1
data/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # stealth-aws-comprehend
2
+
3
+ This gem provides NLP via the [AWS Comprehend](https://aws.amazon.com/comprehend/) service. It integrates with the Stealth Controller to provide NLP against `current_message`.
4
+
5
+ ## Setup
6
+
7
+ To use AWS Comprehend, set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables to your AWS account credentials. You may also want to optionally set your preferred AWS region by setting the environment variable `AWS_REGION`. That's the only setup required!
8
+
9
+ ## Using NLP
10
+
11
+ This gem adds the following methods to `current_message`:
12
+
13
+ 1. `language` identifies text written in over 100 languages and returns the dominant language in `current_message` with a confidence score
14
+ 2. `entities` returns the named entities ("People," "Places," "Locations," etc.) present in `current_message`
15
+ 3. `key_phrases` returns the key phrases or talking points and a confidence score to support for the `current_message`
16
+ 4. `sentiment` returns the overall sentiment (Positive, Negative, Neutral, or Mixed) for the `current_message`
17
+ 5. `sentiment_score` returns the scoring for each sentiment (Positive, Negative, Neutral, or Mixed) for the `current_message`
18
+
19
+ If `current_message.message` is not available, each of these will return `nil`.
20
+
21
+ The following examples are run on `current_message.message` when it is equal to `"I bought a brand new Craftsman Drill at Home Depot."`.
22
+
23
+ ### language
24
+
25
+ `current_message.language` will return an Array of dominate languages:
26
+
27
+ ```ruby
28
+ languages = [#<struct Aws::Comprehend::Types::DominantLanguage language_code="en", score=0.9921924471855164>]
29
+ languages.first.language_code # "en"
30
+ languages.first.score # 0.9921924471855164
31
+ ```
32
+
33
+ ### entities
34
+
35
+ `current_message.entites` will return an Array of entities:
36
+
37
+ ```ruby
38
+ entities = [#<struct Aws::Comprehend::Types::Entity score=0.5775995850563049, type="ORGANIZATION", text="Craftsman", begin_offset=21, end_offset=30>, #<struct Aws::Comprehend::Types::Entity score=0.9224900007247925, type="ORGANIZATION", text="Home Depot", begin_offset=40, end_offset=50>]
39
+ entities.first.text # "Craftsman"
40
+ entities.first.score # 5775995850563049
41
+ entities.first.type # ORGANIZATION
42
+ ```
43
+
44
+ ### key_phrases
45
+
46
+ `current_message.key_phrases` will return an Array of key phrases:
47
+
48
+ ```ruby
49
+ key_phrases = [#<struct Aws::Comprehend::Types::KeyPhrase score=0.965133786201477, text="a brand new Craftsman Drill", begin_offset=9, end_offset=36>, #<struct Aws::Comprehend::Types::KeyPhrase score=0.9943040609359741, text="Home Depot", begin_offset=40, end_offset=50>]
50
+ key_phrases.first.text # "a brand new Craftsman Drill"
51
+ key_phrases.first.score # 0.965133786201477
52
+ ```
53
+
54
+ ### sentiment
55
+
56
+ `current_message.sentiment` will return the dominate sentiment of the message:
57
+
58
+ ```ruby
59
+ current_message.sentiment # "NEUTRAL"
60
+ ```
61
+
62
+ ### sentiment_score
63
+
64
+ `current_message.sentiment_score` will return the scoring for each sentiment of the message:
65
+
66
+ ```ruby
67
+ sentiment_scores = #<struct Aws::Comprehend::Types::SentimentScore positive=0.0249068271368742, negative=0.012575631029903889, neutral=0.9605817794799805, mixed=0.0019358232384547591>
68
+ sentiment_scores.positive # 0.0249068271368742
69
+ sentiment_scores.negative # 0.012575631029903889
70
+ sentiment_scores.neutral # 0.9605817794799805
71
+ sentiment_scores.mixed # 0.0019358232384547591
72
+ ```
73
+
74
+ ## AWS Usage
75
+
76
+ Loading this gem into your bot doesn't automatically issue API requests to your AWS account. API requests are lazily made each time you call one of the above methods. One exception is `sentiment` and `sentiment_score` which share an API call and so that only a single API call is made between the two.
@@ -0,0 +1,56 @@
1
+ module Stealth
2
+ class AwsComprehend
3
+
4
+ def self.client
5
+ @client ||= Aws::Comprehend::Client.new
6
+ end
7
+
8
+ def self.detect_dominate_language(message:)
9
+ begin
10
+ client.detect_dominant_language({
11
+ text: message
12
+ }).languages
13
+ rescue Aws::Comprehend::Errors::ServiceError
14
+ Stealth::Logger.l(topic: "AWS::Comprehend", message: e.message)
15
+ return []
16
+ end
17
+ end
18
+
19
+ def self.detect_entities(message:, language: 'en')
20
+ begin
21
+ client.detect_entities({
22
+ text: message,
23
+ language_code: language
24
+ }).entities
25
+ rescue Aws::Comprehend::Errors::ServiceError => e
26
+ Stealth::Logger.l(topic: "AWS::Comprehend", message: e.message)
27
+ return []
28
+ end
29
+ end
30
+
31
+ def self.detect_key_phrases(message:, language: 'en')
32
+ begin
33
+ client.detect_key_phrases({
34
+ text: message,
35
+ language_code: language
36
+ }).key_phrases
37
+ rescue Aws::Comprehend::Errors::ServiceError
38
+ Stealth::Logger.l(topic: "AWS::Comprehend", message: e.message)
39
+ return []
40
+ end
41
+ end
42
+
43
+ def self.detect_sentiment(message:, language: 'en')
44
+ begin
45
+ client.detect_sentiment({
46
+ text: message,
47
+ language_code: language
48
+ })
49
+ rescue Aws::Comprehend::Errors::ServiceError
50
+ Stealth::Logger.l(topic: "AWS::Comprehend", message: e.message)
51
+ return []
52
+ end
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,55 @@
1
+ module Stealth
2
+ class ServiceMessage
3
+
4
+ def language
5
+ @language ||= begin
6
+ if message.present?
7
+ Stealth::AwsComprehend.detect_dominate_language(
8
+ message: message
9
+ )
10
+ end
11
+ end
12
+ end
13
+
14
+ def entities
15
+ @entities ||= begin
16
+ if message.present?
17
+ Stealth::AwsComprehend.detect_entities(
18
+ message: message
19
+ )
20
+ end
21
+ end
22
+ end
23
+
24
+ def key_phrases
25
+ @key_phrases ||= begin
26
+ if message.present?
27
+ Stealth::AwsComprehend.detect_key_phrases(
28
+ message: message
29
+ )
30
+ end
31
+ end
32
+ end
33
+
34
+ def sentiment_score
35
+ get_sentiment.sentiment_score
36
+ end
37
+
38
+ def sentiment
39
+ get_sentiment.sentiment
40
+ end
41
+
42
+ private
43
+
44
+ def get_sentiment
45
+ @sentiment ||= begin
46
+ if message.present?
47
+ Stealth::AwsComprehend.detect_sentiment(
48
+ message: message
49
+ )
50
+ end
51
+ end
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ require 'aws-sdk-comprehend'
2
+ require 'stealth-aws-comprehend/client'
3
+ require 'stealth-aws-comprehend/nlp'
@@ -0,0 +1,84 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ require 'rspec'
5
+ require 'stealth-aws-comprehend'
6
+
7
+ RSpec.configure do |config|
8
+ config.expect_with :rspec do |expectations|
9
+ # This option will default to `true` in RSpec 4. It makes the `description`
10
+ # and `failure_message` of custom matchers include text for helper methods
11
+ # defined using `chain`, e.g.:
12
+ # be_bigger_than(2).and_smaller_than(4).description
13
+ # # => "be bigger than 2 and smaller than 4"
14
+ # ...rather than:
15
+ # # => "be bigger than 2"
16
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
17
+ end
18
+
19
+ # rspec-mocks config goes here. You can use an alternate test double
20
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
21
+ config.mock_with :rspec do |mocks|
22
+ # Prevents you from mocking or stubbing a method that does not exist on
23
+ # a real object. This is generally recommended, and will default to
24
+ # `true` in RSpec 4.
25
+ mocks.verify_partial_doubles = true
26
+ end
27
+
28
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
29
+ # have no way to turn it off -- the option exists only for backwards
30
+ # compatibility in RSpec 3). It causes shared context metadata to be
31
+ # inherited by the metadata hash of host groups and examples, rather than
32
+ # triggering implicit auto-inclusion in groups with matching metadata.
33
+ config.shared_context_metadata_behavior = :apply_to_host_groups
34
+
35
+ # The settings below are suggested to provide a good initial experience
36
+ # with RSpec, but feel free to customize to your heart's content.
37
+ =begin
38
+ # This allows you to limit a spec run to individual examples or groups
39
+ # you care about by tagging them with `:focus` metadata. When nothing
40
+ # is tagged with `:focus`, all examples get run. RSpec also provides
41
+ # aliases for `it`, `describe`, and `context` that include `:focus`
42
+ # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
43
+ config.filter_run_when_matching :focus
44
+
45
+ # Allows RSpec to persist some state between runs in order to support
46
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
47
+ # you configure your source control system to ignore this file.
48
+ config.example_status_persistence_file_path = "spec/examples.txt"
49
+
50
+ # Limits the available syntax to the non-monkey patched syntax that is
51
+ # recommended. For more details, see:
52
+ # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
53
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
54
+ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
55
+ config.disable_monkey_patching!
56
+
57
+ # Many RSpec users commonly either run the entire suite or an individual
58
+ # file, and it's useful to allow more verbose output when running an
59
+ # individual spec file.
60
+ if config.files_to_run.one?
61
+ # Use the documentation formatter for detailed output,
62
+ # unless a formatter has already been configured
63
+ # (e.g. via a command-line flag).
64
+ config.default_formatter = 'doc'
65
+ end
66
+
67
+ # Print the 10 slowest examples and example groups at the
68
+ # end of the spec run, to help surface which specs are running
69
+ # particularly slow.
70
+ config.profile_examples = 10
71
+
72
+ # Run specs in random order to surface order dependencies. If you find an
73
+ # order dependency and want to debug it, you can fix the order by providing
74
+ # the seed, which is printed after each run.
75
+ # --seed 1234
76
+ config.order = :random
77
+
78
+ # Seed global randomization in this process using the `--seed` CLI option.
79
+ # Setting this allows you to use `--seed` to deterministically reproduce
80
+ # test failures related to randomization by passing the same `--seed` value
81
+ # as the one that triggered the failure.
82
+ Kernel.srand config.seed
83
+ =end
84
+ end
@@ -0,0 +1,18 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'stealth-aws-comprehend'
3
+ s.version = '0.9.0'
4
+ s.date = '2018-04-26'
5
+ s.summary = "Stealth AWS Comprehend"
6
+ s.description = "Built-in NLP for Stealth bots via AWS Comprehend."
7
+ s.authors = ["Mauricio Gomes"]
8
+ s.email = 'mauricio@edge14.com'
9
+ s.files = `git ls-files`.split("\n")
10
+ s.homepage = 'http://github.com/hellostealth/stealth-aws-comprehend'
11
+ s.license = 'MIT'
12
+
13
+ s.add_dependency 'stealth', '< 2.0'
14
+ s.add_dependency 'aws-sdk-comprehend', '~> 1.0'
15
+
16
+ s.add_development_dependency "rspec", "3.6.0"
17
+
18
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stealth-aws-comprehend
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - Mauricio Gomes
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-04-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: stealth
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "<"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "<"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: aws-sdk-comprehend
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 3.6.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 3.6.0
55
+ description: Built-in NLP for Stealth bots via AWS Comprehend.
56
+ email: mauricio@edge14.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - ".rspec"
62
+ - Gemfile
63
+ - Gemfile.lock
64
+ - README.md
65
+ - lib/stealth-aws-comprehend.rb
66
+ - lib/stealth-aws-comprehend/client.rb
67
+ - lib/stealth-aws-comprehend/nlp.rb
68
+ - spec/spec_helper.rb
69
+ - stealth-aws-comprehend.gemspec
70
+ homepage: http://github.com/hellostealth/stealth-aws-comprehend
71
+ licenses:
72
+ - MIT
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.7.6
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Stealth AWS Comprehend
94
+ test_files: []