Class: GooglePerspective::CommentAnalyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/google_perspective/comment_analizer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text:, score: 0.5) ⇒ CommentAnalyzer

Returns a new instance of CommentAnalyzer.



5
6
7
8
9
# File 'lib/google_perspective/comment_analizer.rb', line 5

def initialize(text:, score: 0.5)
  @client = Client.new
  @text = text
  @score = score
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



3
4
5
# File 'lib/google_perspective/comment_analizer.rb', line 3

def client
  @client
end

#scoreObject

Returns the value of attribute score.



3
4
5
# File 'lib/google_perspective/comment_analizer.rb', line 3

def score
  @score
end

#textObject

Returns the value of attribute text.



3
4
5
# File 'lib/google_perspective/comment_analizer.rb', line 3

def text
  @text
end

Instance Method Details

#bodyObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/google_perspective/comment_analizer.rb', line 29

def body
  {
    comment: {
      text: text,
      type: 'PLAIN_TEXT'
    },
    requestedAttributes: {
      TOXICITY: {},
      SEVERE_TOXICITY: {}
    }
  }
end

#responseObject



25
26
27
# File 'lib/google_perspective/comment_analizer.rb', line 25

def response
  @response ||= client.post(body)
end

#result_scoreObject



15
16
17
# File 'lib/google_perspective/comment_analizer.rb', line 15

def result_score
  result_scores.min
end

#result_scoresObject



19
20
21
22
23
# File 'lib/google_perspective/comment_analizer.rb', line 19

def result_scores
  @result_scores ||= response['attributeScores'].map do |_model, model_score|
    model_score.dig('summaryScore', 'value')
  end
end

#valid?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/google_perspective/comment_analizer.rb', line 11

def valid?
  result_score <= score
end