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/language"

language = Google::Cloud::Language.new

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.



602
603
604
605
606
# File 'lib/google/cloud/language/annotation.rb', line 602

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



597
598
599
# File 'lib/google/cloud/language/annotation.rb', line 597

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



597
598
599
# File 'lib/google/cloud/language/annotation.rb', line 597

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



597
598
599
# File 'lib/google/cloud/language/annotation.rb', line 597

def polarity
  @polarity
end

Class Method Details

.from_grpc(grpc) ⇒ Object

V1beta1::AnalyzeSentimentResponse object.



611
612
613
614
# File 'lib/google/cloud/language/annotation.rb', line 611

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