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

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

Overview

Annotation

The results of all requested document analysis features.

See Project#annotate and Document#annotate.

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

annotation.sentiment.score #=> 1.0
annotation.sentiment.magnitude #=> 0.8999999761581421
annotation.entities.count #=> 2
annotation.sentences.count #=> 1
annotation.tokens.count #=> 10

Defined Under Namespace

Classes: Entities, Entity, PartOfSpeech, Sentence, Sentiment, Syntax, TextSpan, Token

Instance Method Summary collapse

Instance Method Details

#entitiesEntities

The entities returned by entity 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

entities = annotation.entities
entities.count #=> 2
entity = entities.first

entity.name #=> "Darth Vader"
entity.type #=> :PERSON
entity.salience #=> 0.8421939611434937
entity.mentions.count #=> 1
entity.mentions.first.text # => "Darth Vader"
entity.mentions.first.offset # => 0
entity.wikipedia_url #=> "http://en.wikipedia.org/wiki/Darth_Vader"

Returns:



166
167
168
# File 'lib/google/cloud/language/annotation.rb', line 166

def entities
  @entities ||= Entities.from_grpc @grpc
end

#languageString

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

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
annotation.language #=> "en"

Returns:

  • (String)

    the language code



215
216
217
# File 'lib/google/cloud/language/annotation.rb', line 215

def language
  @grpc.language
end

#sentencesArray<TextSpan>

The sentences returned by syntactic analysis.

Examples:

require "google/cloud/language"

language = Google::Cloud::Language.new

content = "I love dogs. I hate cats."
document = language.document content
annotation = document.annotate

sentence = annotation.sentences.last
sentence.text #=> "I hate cats."
sentence.offset #=> 13

Returns:

  • (Array<TextSpan>)

    an array of pieces of text including relative location



73
74
75
# File 'lib/google/cloud/language/annotation.rb', line 73

def sentences
  @sentences ||= Array(grpc.sentences).map { |g| Sentence.from_grpc g }
end

#sentimentSentiment

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.score #=> 1.0
sentiment.magnitude #=> 0.8999999761581421
sentiment.language #=> "en"

sentence = sentiment.sentences.first
sentence.sentiment.score #=> 1.0
sentence.sentiment.magnitude #=> 0.8999999761581421

Returns:



193
194
195
196
# File 'lib/google/cloud/language/annotation.rb', line 193

def sentiment
  return nil if @grpc.document_sentiment.nil?
  @sentiment ||= Sentiment.from_grpc @grpc
end

#syntaxSyntax

The result of syntax 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
syntax = annotation.syntax

sentence = syntax.sentences.last
sentence.text #=> "Darth Vader is the best villain in Star Wars."
sentence.offset #=> 0

syntax.tokens.count #=> 10
token = syntax.tokens.first

token.text #=> "Darth"
token.offset #=> 0
token.part_of_speech #=> :NOUN
token.head_token_index #=> 1
token.label #=> :NN
token.lemma #=> "Darth"

Returns:



135
136
137
138
# File 'lib/google/cloud/language/annotation.rb', line 135

def syntax
  return nil if @grpc.tokens.nil?
  @syntax ||= Syntax.from_grpc @grpc
end

#tokensArray<Token>

The tokens returned by syntactic 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

annotation.tokens.count #=> 10
token = annotation.tokens.first

token.text #=> "Darth"
token.offset #=> 0
token.part_of_speech #=> :NOUN
token.head_token_index #=> 1
token.label #=> :NN
token.lemma #=> "Darth"

Returns:

  • (Array<Token>)

    an array of the smallest syntactic building blocks of the text



102
103
104
# File 'lib/google/cloud/language/annotation.rb', line 102

def tokens
  @tokens ||= Array(grpc.tokens).map { |g| Token.from_grpc g }
end