Class: Google::Cloud::Language::Project

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

Overview

Project

Google Cloud Natural Language API reveals the structure and meaning of text by offering powerful machine learning models in an easy to use REST API. You can analyze text uploaded in your request or integrate with your document storage on Google Cloud Storage.

See Google::Cloud#language

Examples:

require "google/cloud/language"

language = Google::Cloud::Language.new

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

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

Instance Method Summary collapse

Instance Method Details

#annotate(content, sentiment: false, entities: false, syntax: false, format: nil, language: nil, encoding: nil) ⇒ Annotation Also known as: mark, detect

Analyzes the content and returns sentiment, entity, and syntactic feature results, depending on the option flags. Calling annotate with no arguments will perform all analysis features. Each feature is priced separately. See Pricing for details.

Examples:

require "google/cloud/language"

language = Google::Cloud::Language.new

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

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

Parameters:

  • content (String, Document, Google::Cloud::Storage::File)

    The content to annotate. This can be an Document instance, or any other type that converts to an Document. See #document for details.

  • sentiment (Boolean)

    Whether to perform the sentiment analysis. Optional. The default is false. If every feature option is false, all features will be performed.

  • entities (Boolean)

    Whether to perform the entity analysis. Optional. The default is false. If every feature option is false, all features will be performed.

  • syntax (Boolean)

    Whether to perform the syntactic analysis. Optional. The default is false. If every feature option is false, all features will be performed.

  • format (String)

    The format of the document (TEXT/HTML). Optional.

  • language (String)

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

  • encoding (String)

    The encoding type used by the API to calculate offsets. Optional.

Returns:

  • (Annotation)

    The results for the content analysis.



228
229
230
231
232
233
234
235
236
237
# File 'lib/google/cloud/language/project.rb', line 228

def annotate content, sentiment: false, entities: false, syntax: false,
             format: nil, language: nil, encoding: nil
  ensure_service!
  doc = document content, language: language, format: format
  grpc = service.annotate doc.to_grpc, sentiment: sentiment,
                                       entities: entities,
                                       syntax: syntax,
                                       encoding: encoding
  Annotation.from_grpc grpc
end

#document(content, format: nil, language: nil) ⇒ Document Also known as: doc

Returns a new document from the given content. No API call is made.

Examples:

require "google/cloud/language"

language = Google::Cloud::Language.new

document = language.document "It was the best of times, it was..."

With a Google Cloud Storage URI:

require "google/cloud/language"

language = Google::Cloud::Language.new

document = language.document "gs://bucket-name/path/to/document"

With a Google Cloud Storage File object:

require "google/cloud/storage"
storage = Google::Cloud::Storage.new

bucket = storage.bucket "bucket-name"
file = bucket.file "path/to/document"

require "google/cloud/language"
language = Google::Cloud::Language.new

document = language.document file

With format and language options:

require "google/cloud/language"

language = Google::Cloud::Language.new

document = language.document "<p>El viejo y el mar</p>",
                        format: :html, language: "es"

Parameters:

  • content (String, Google::Cloud::Storage::File)

    A string of text to be annotated, or a Cloud Storage URI of the form "gs://bucketname/path/to/document.ext"; or an instance of Google::Cloud::Storage::File of the text to be annotated.

  • format (String)

    The format of the document (TEXT/HTML). Optional.

  • language (String)

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

Returns:

  • (Document)

    An document for the Language service.



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/google/cloud/language/project.rb', line 134

def document content, format: nil, language: nil
  content = content.to_gs_url if content.respond_to? :to_gs_url
  if content.is_a? Document
    # Create new document with the provided format and language
    Document.from_source content.source, @service,
                         format: (format || content.format),
                         language: (language || content.language)
  else
    Document.from_source content, @service, format: format,
                                            language: language
  end
end

#entities(content, format: :text, language: nil, encoding: nil) ⇒ Annotation::Entities

Entity analysis inspects the given text for known entities (proper nouns such as public figures, landmarks, etc.) and returns information about those entities.

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

entities = language.entities document
entities.count #=> 3

Parameters:

  • content (String, Document)

    The content to annotate. This can be an Document instance, or any other type that converts to an Document. See #document for details.

  • format (String)

    The format of the document (TEXT/HTML). Optional.

  • language (String)

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

  • encoding (String)

    The encoding type used by the API to calculate offsets. Optional.

Returns:



322
323
324
325
326
327
# File 'lib/google/cloud/language/project.rb', line 322

def entities content, format: :text, language: nil, encoding: nil
  ensure_service!
  doc = document content, language: language, format: format
  grpc = service.entities doc.to_grpc, encoding: encoding
  Annotation::Entities.from_grpc grpc
end

#html(content, language: nil) ⇒ Document

Returns a new document from the given content with the format value :html. No API call is made.

Parameters:

  • content (String, Google::Cloud::Storage::File)

    A string of text to be annotated, or a Cloud Storage URI of the form "gs://bucketname/path/to/document.ext"; or an instance of Google::Cloud::Storage::File of the text to be annotated.

  • language (String)

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

Returns:

  • (Document)

    An document for the Language service.



180
181
182
# File 'lib/google/cloud/language/project.rb', line 180

def html content, language: nil
  document content, format: :html, language: language
end

#projectObject

The Language project connected to.

Examples:

require "google/cloud/language"

language = Google::Cloud::Language.new(
  project: "my-project-id",
  keyfile: "/path/to/keyfile.json"
)

language.project #=> "my-project-id"


72
73
74
# File 'lib/google/cloud/language/project.rb', line 72

def project
  service.project
end

#sentiment(content, format: :text, language: nil, encoding: nil) ⇒ Annotation::Sentiment

Sentiment analysis inspects the given text and identifies the prevailing emotional opinion within the text, especially to determine a writer's attitude as positive, negative, or neutral. Currently, only English is supported for 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

sentiment = language.sentiment document

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

Parameters:

  • content (String, Document)

    The content to annotate. This can be an Document instance, or any other type that converts to an Document. See #document for details.

  • format (String)

    The format of the document (TEXT/HTML). Optional.

  • language (String)

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

  • encoding (String)

    The encoding type used by the API to calculate offsets. Optional.

Returns:



367
368
369
370
371
372
# File 'lib/google/cloud/language/project.rb', line 367

def sentiment content, format: :text, language: nil, encoding: nil
  ensure_service!
  doc = document content, language: language, format: format
  grpc = service.sentiment doc.to_grpc, encoding: encoding
  Annotation::Sentiment.from_grpc grpc
end

#syntax(content, format: nil, language: nil, encoding: nil) ⇒ Annotation::Syntax

Syntactic analysis extracts linguistic information, breaking up the given text into a series of sentences and tokens (generally, word boundaries), providing further analysis on those tokens.

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 = language.syntax document
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"

Parameters:

  • content (String, Document, Google::Cloud::Storage::File)

    The content to annotate. This can be an Document instance, or any other type that converts to an Document. See #document for details.

  • format (String)

    The format of the document (TEXT/HTML). Optional.

  • language (String)

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

  • encoding (String)

    The encoding type used by the API to calculate offsets. Optional.

Returns:



286
287
288
289
290
291
# File 'lib/google/cloud/language/project.rb', line 286

def syntax content, format: nil, language: nil, encoding: nil
  ensure_service!
  doc = document content, language: language, format: format
  grpc = service.syntax doc.to_grpc, encoding: encoding
  Annotation::Syntax.from_grpc grpc
end

#text(content, language: nil) ⇒ Document

Returns a new document from the given content with the format value :text. No API call is made.

Parameters:

  • content (String, Google::Cloud::Storage::File)

    A string of text to be annotated, or a Cloud Storage URI of the form "gs://bucketname/path/to/document.ext"; or an instance of Google::Cloud::Storage::File of the text to be annotated.

  • language (String)

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

Returns:

  • (Document)

    An document for the Language service.



162
163
164
# File 'lib/google/cloud/language/project.rb', line 162

def text content, language: nil
  document content, format: :text, language: language
end