aws_detect_sentiment 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/lib/aws_detect_sentiment.rb +8 -0
- data/lib/aws_detect_sentiment/assign.rb +34 -0
- data/lib/aws_detect_sentiment/aws_comprehend_client.rb +47 -0
- data/lib/aws_detect_sentiment/configuration.rb +13 -0
- data/lib/aws_detect_sentiment/version.rb +3 -0
- metadata +94 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e60d0e3760cbb1b5f9363fdd7d0f7a6ff778a1d99104b0bb7271ac5d022cb8fe
|
4
|
+
data.tar.gz: 6b3e97e635bcd601a0a5f3113b10ac873cbcd4e25ee8b0c14fc60ac82bf4b1db
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0716a10136a64a6ef45e01eedcdb50a6797547b0d06e865cde84e4968dfa1ee787494f55452426d44ff75fa6f9d907ead9dcb053078d5d14f748ed9467c10f68
|
7
|
+
data.tar.gz: 10bcdfb2af32ebbd4dd831c9118dd47e3197984bbf3cb047991a0a7c8514addc6906ff7ab3cc74e0fd037045c14dc64b7163522882ea755c0acb0799d627b714
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 Aleksey Strizhak
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
@@ -0,0 +1,8 @@
|
|
1
|
+
require_relative "aws_detect_sentiment/version"
|
2
|
+
require_relative "aws_detect_sentiment/configuration"
|
3
|
+
require_relative "aws_detect_sentiment/aws_comprehend_client"
|
4
|
+
require_relative "aws_detect_sentiment/assign"
|
5
|
+
|
6
|
+
module AwsDetectSentiment
|
7
|
+
class Error < StandardError; end
|
8
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module AwsDetectSentiment
|
2
|
+
class Assign
|
3
|
+
# @param scope [Array<::Object>]
|
4
|
+
# @param text_field [Symbol]
|
5
|
+
# @param api_client [Object]
|
6
|
+
def initialize(scope:, text_field:, api_client: AwsDetectSentiment::AwsComprehendClient.new)
|
7
|
+
self.scope = scope
|
8
|
+
self.text_field = text_field
|
9
|
+
self.api_client = api_client
|
10
|
+
end
|
11
|
+
|
12
|
+
def perform
|
13
|
+
records.each_with_index do |record, index|
|
14
|
+
yield(record, sentiments[index])
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def records
|
21
|
+
scope.reject { |record| record.public_send(text_field).to_s.empty? }
|
22
|
+
end
|
23
|
+
|
24
|
+
attr_accessor :scope, :text_field, :api_client
|
25
|
+
|
26
|
+
def sentiments
|
27
|
+
@sentiments ||= api_client.detect_sentiments(texts)
|
28
|
+
end
|
29
|
+
|
30
|
+
def texts
|
31
|
+
scope.map { |record| record.public_send(text_field) }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require "aws-sdk-comprehend"
|
2
|
+
|
3
|
+
module AwsDetectSentiment
|
4
|
+
class AwsComprehendClient
|
5
|
+
BATCH_LIMIT = 25
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
self.s3_client =
|
9
|
+
Aws::Comprehend::Client.new(
|
10
|
+
stub_responses: stub_enabled?,
|
11
|
+
region: AwsDetectSentiment.configuration.aws_region,
|
12
|
+
access_key_id: AwsDetectSentiment.configuration.aws_access_key_id,
|
13
|
+
secret_access_key: AwsDetectSentiment.configuration.aws_secret_access_key
|
14
|
+
)
|
15
|
+
end
|
16
|
+
|
17
|
+
def detect_sentiment(text, options: inner_options, stub_response: {})
|
18
|
+
s3_client.stub_responses(:detect_sentiment, stub_response) if stub_enabled?
|
19
|
+
sentiment(s3_client.detect_sentiment(text: text, **options))
|
20
|
+
end
|
21
|
+
|
22
|
+
def detect_sentiments(texts, batch_limit: BATCH_LIMIT, options: inner_options, stub_responses: [{}])
|
23
|
+
texts.each_slice(batch_limit).map.with_index do |texts_slice, index|
|
24
|
+
s3_client.stub_responses(:batch_detect_sentiment, stub_responses[index]) if stub_enabled?
|
25
|
+
s3_client.batch_detect_sentiment(text_list: texts_slice, **options).result_list
|
26
|
+
end.flatten.map(&method(:sentiment))
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
attr_accessor :s3_client
|
32
|
+
|
33
|
+
def sentiment(result)
|
34
|
+
result.sentiment.downcase
|
35
|
+
end
|
36
|
+
|
37
|
+
def inner_options
|
38
|
+
{
|
39
|
+
language_code: "en"
|
40
|
+
}
|
41
|
+
end
|
42
|
+
|
43
|
+
def stub_enabled?
|
44
|
+
AwsDetectSentiment.configuration.stub_client_requests
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: aws_detect_sentiment
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Aleksey Strizhak
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-08-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: aws-sdk-comprehend
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.48.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.48.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: byebug
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '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.8'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.8'
|
55
|
+
description: Provided Client-object to detect the sentiment of provided word/words
|
56
|
+
by Aws::Comprehend API
|
57
|
+
email:
|
58
|
+
- alexei.mifrill.strizhak@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- LICENSE.txt
|
64
|
+
- lib/aws_detect_sentiment.rb
|
65
|
+
- lib/aws_detect_sentiment/assign.rb
|
66
|
+
- lib/aws_detect_sentiment/aws_comprehend_client.rb
|
67
|
+
- lib/aws_detect_sentiment/configuration.rb
|
68
|
+
- lib/aws_detect_sentiment/version.rb
|
69
|
+
homepage: https://github.com/Mifrill/aws_detect_sentiment.git
|
70
|
+
licenses:
|
71
|
+
- MIT
|
72
|
+
metadata:
|
73
|
+
homepage_uri: https://github.com/Mifrill/aws_detect_sentiment.git
|
74
|
+
changelog_uri: https://github.com/Mifrill/aws_detect_sentiment/blob/master/CHANGELOG.md
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: 2.6.0
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubygems_version: 3.2.22
|
91
|
+
signing_key:
|
92
|
+
specification_version: 4
|
93
|
+
summary: Detect sentiment by Aws::Comprehend::Client
|
94
|
+
test_files: []
|