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

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

Overview

Represents a phrase in the text that is a known entity, such as a person, an organization, or location. The API associates information, such as salience and mentions, with entities.

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"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, metadata, salience, mentions) ⇒ Entity

Returns a new instance of Entity.



469
470
471
472
473
474
475
# File 'lib/google/cloud/language/annotation.rb', line 469

def initialize name, type, , salience, mentions
  @name     = name
  @type     = type
  @metadata = 
  @salience = salience
  @mentions = mentions
end

Instance Attribute Details

#mentionsArray<TextSpan> (readonly)

The mentions of this entity in the input document. The API currently supports proper noun mentions.

Returns:

  • (Array<TextSpan>)

    the current value of mentions



464
465
466
# File 'lib/google/cloud/language/annotation.rb', line 464

def mentions
  @mentions
end

#metadataHash<String,String> (readonly)

Metadata associated with the entity. Currently, only Wikipedia URLs are provided, if available. The associated key is “wikipedia_url”.

Returns:

  • (Hash<String,String>)

    the current value of metadata



464
465
466
# File 'lib/google/cloud/language/annotation.rb', line 464

def 
  @metadata
end

#nameString (readonly)

The representative name for the entity.

Returns:

  • (String)

    the current value of name



464
465
466
# File 'lib/google/cloud/language/annotation.rb', line 464

def name
  @name
end

#salienceFloat (readonly)

The salience score associated with the entity in the [0, 1.0] range. The salience score for an entity provides information about the importance or centrality of that entity to the entire document text. Scores closer to 0 are less salient, while scores closer to 1.0 are highly salient.

Returns:

  • (Float)

    the current value of salience



464
465
466
# File 'lib/google/cloud/language/annotation.rb', line 464

def salience
  @salience
end

#typeSymbol (readonly)

The type of the entity.

Returns:

  • (Symbol)

    the current value of type



464
465
466
# File 'lib/google/cloud/language/annotation.rb', line 464

def type
  @type
end

Class Method Details

.from_grpc(grpc) ⇒ Object



561
562
563
564
565
566
567
# File 'lib/google/cloud/language/annotation.rb', line 561

def self.from_grpc grpc
   = Core::GRPCUtils.map_to_hash grpc.
  mentions = Array(grpc.mentions).map do |g|
    TextSpan.from_grpc g.text
  end
  new grpc.name, grpc.type, , grpc.salience, mentions
end

Instance Method Details

#artwork?Boolean

Returns ‘true` if #type is `:WORK_OF_ART`.

Returns:

  • (Boolean)


528
529
530
# File 'lib/google/cloud/language/annotation.rb', line 528

def artwork?
  type == :WORK_OF_ART
end

#event?Boolean

Returns ‘true` if #type is `:EVENT`.

Returns:

  • (Boolean)


519
520
521
# File 'lib/google/cloud/language/annotation.rb', line 519

def event?
  type == :EVENT
end

#good?Boolean

Returns ‘true` if #type is `:CONSUMER_GOOD`.

Returns:

  • (Boolean)


537
538
539
# File 'lib/google/cloud/language/annotation.rb', line 537

def good?
  type == :CONSUMER_GOOD
end

#location?Boolean Also known as: place?

Returns ‘true` if #type is `:LOCATION`.

Returns:

  • (Boolean)


500
501
502
# File 'lib/google/cloud/language/annotation.rb', line 500

def location?
  type == :LOCATION
end

#organization?Boolean

Returns ‘true` if #type is `:ORGANIZATION`.

Returns:

  • (Boolean)


510
511
512
# File 'lib/google/cloud/language/annotation.rb', line 510

def organization?
  type == :ORGANIZATION
end

#other?Boolean

Returns ‘true` if #type is `:OTHER`.

Returns:

  • (Boolean)


546
547
548
# File 'lib/google/cloud/language/annotation.rb', line 546

def other?
  type == :OTHER
end

#person?Boolean

Returns ‘true` if #type is `:PERSON`.

Returns:

  • (Boolean)


491
492
493
# File 'lib/google/cloud/language/annotation.rb', line 491

def person?
  type == :PERSON
end

#unknown?Boolean

Returns ‘true` if #type is `:UNKNOWN`.

Returns:

  • (Boolean)


482
483
484
# File 'lib/google/cloud/language/annotation.rb', line 482

def unknown?
  type == :UNKNOWN
end

#wikipedia_urlString

Returns the ‘wikipedia_url` property of the #metadata.

Returns:

  • (String)


555
556
557
# File 'lib/google/cloud/language/annotation.rb', line 555

def wikipedia_url
  ["wikipedia_url"]
end