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 = "Star Wars is a great movie. The Death Star is fearsome."
document = language.document content
annotation = document.annotate

annotation.sentiment.score #=> 0.10000000149011612
annotation.sentiment.magnitude #=> 1.100000023841858
annotation.entities.count #=> 3
annotation.sentences.count #=> 2
annotation.tokens.count #=> 13

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 = "Star Wars is a great movie. The Death Star is fearsome."
document = language.document content
annotation = document.annotate

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

entity.name #=> "Star Wars"
entity.type #=> :WORK_OF_ART
entity.salience #=> 0.6457656025886536
entity.mentions.count #=> 1
entity.mentions.first.text # => "Star Wars"
entity.mentions.first.offset # => 0
entity.mid #=> "/m/06mmr"
entity.wikipedia_url #=> "http://en.wikipedia.org/wiki/Star_Wars"

Returns:



168
169
170
# File 'lib/google/cloud/language/annotation.rb', line 168

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 = "Star Wars is a great movie. The Death Star is fearsome."
document = language.document content
annotation = document.annotate
annotation.language #=> "en"

Returns:

  • (String)

    the language code



217
218
219
# File 'lib/google/cloud/language/annotation.rb', line 217

def language
  @grpc.language
end

#sentencesArray<TextSpan>

The sentences returned by syntactic analysis.

Examples:

require "google/cloud/language"

language = Google::Cloud::Language.new

content = "Star Wars is a great movie. The Death Star is fearsome."
document = language.document content
annotation = document.annotate

sentence = annotation.sentences.last
sentence.text #=> "The Death Star is fearsome."
sentence.offset #=> 28

Returns:

  • (Array<TextSpan>)

    an array of pieces of text including relative location



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

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 = "Star Wars is a great movie. The Death Star is fearsome."
document = language.document content
annotation = document.annotate
sentiment = annotation.sentiment

sentiment.score #=> 0.10000000149011612
sentiment.magnitude #=> 1.100000023841858
sentiment.language #=> "en"

sentence = sentiment.sentences.first
sentence.sentiment.score #=> 0.699999988079071
sentence.sentiment.magnitude #=> 0.699999988079071

Returns:



195
196
197
198
# File 'lib/google/cloud/language/annotation.rb', line 195

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 = "Star Wars is a great movie. The Death Star is fearsome."
document = language.document content
annotation = document.annotate
syntax = annotation.syntax

sentence = syntax.sentences.last
sentence.text #=> "The Death Star is fearsome."
sentence.offset #=> 28

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

token.text #=> "Star"
token.offset #=> 0
token.part_of_speech.tag #=> :NOUN
token.head_token_index #=> 1
token.label #=> :TITLE
token.lemma #=> "Star"

Returns:



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

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 = "Star Wars is a great movie. The Death Star is fearsome."
document = language.document content
annotation = document.annotate

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

token.text #=> "Star"
token.offset #=> 0
token.part_of_speech.tag #=> :NOUN
token.head_token_index #=> 1
token.label #=> :TITLE
token.lemma #=> "Star"

Returns:

  • (Array<Token>)

    an array of the smallest syntactic building blocks of the text



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

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