Class: Google::Cloud::Language::Annotation::Sentiment

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/language/annotation.rb

Overview

Represents the result of sentiment analysis.

Examples:

require "google/cloud"

gcloud = Google::Cloud.new
language = gcloud.language

content = "Darth Vader is the best villain in Star Wars."
document = language.document content
annotation = document.annotate

sentiment = annotation.sentiment
sentiment.polarity #=> 1.0
sentiment.magnitude #=> 0.8999999761581421
sentiment.language #=> "en"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(polarity, magnitude, language) ⇒ Sentiment

Returns a new instance of Sentiment.



613
614
615
616
617
# File 'lib/google/cloud/language/annotation.rb', line 613

def initialize polarity, magnitude, language
  @polarity  = polarity
  @magnitude = magnitude
  @language  = language
end

Instance Attribute Details

#languageString (readonly)

The language of the document (if not specified, the language is automatically detected). Both ISO and BCP-47 language codes are supported.

Returns:

  • (String)

    the current value of language



608
609
610
# File 'lib/google/cloud/language/annotation.rb', line 608

def language
  @language
end

#magnitudeFloat (readonly)

A non-negative number in the [0, +inf] range, which represents the absolute magnitude of sentiment regardless of polarity (positive or negative).

Returns:

  • (Float)

    the current value of magnitude



608
609
610
# File 'lib/google/cloud/language/annotation.rb', line 608

def magnitude
  @magnitude
end

#polarityFloat (readonly)

Polarity of the sentiment in the

-1.0, 1.0

range. Larger numbers represent more positive

sentiments.

Returns:

  • (Float)

    the current value of polarity



608
609
610
# File 'lib/google/cloud/language/annotation.rb', line 608

def polarity
  @polarity
end

Class Method Details

.from_grpc(grpc) ⇒ Object

V1beta1::AnalyzeSentimentResponse object.



622
623
624
625
# File 'lib/google/cloud/language/annotation.rb', line 622

def self.from_grpc grpc
  new grpc.document_sentiment.polarity,
      grpc.document_sentiment.magnitude, grpc.language
end