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

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

Overview

Represents the smallest syntactic building block of the text. Returned by syntactic analysis.

Examples:

require "google/cloud"

gcloud = Google::Cloud.new
language = gcloud.language

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_span.text #=> "Darth"
token.text_span.offset #=> 0
token.part_of_speech #=> :NOUN
token.head_token_index #=> 1
token.label #=> :NN
token.lemma #=> "Darth"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text_span, part_of_speech, head_token_index, label, lemma) ⇒ Token

Returns a new instance of Token.



291
292
293
294
295
296
297
298
# File 'lib/google/cloud/language/annotation.rb', line 291

def initialize text_span, part_of_speech, head_token_index, label,
               lemma
  @text_span        = text_span
  @part_of_speech   = part_of_speech
  @head_token_index = head_token_index
  @label            = label
  @lemma            = lemma
end

Instance Attribute Details

#head_token_indexInteger (readonly)

Represents the head of this token in the dependency tree. This is the index of the token which has an arc going to this token. The index is the position of the token in the array of tokens returned by the API method. If this token is a root token, then the headTokenIndex is its own index.

Returns:

  • (Integer)

    the current value of head_token_index



285
286
287
# File 'lib/google/cloud/language/annotation.rb', line 285

def head_token_index
  @head_token_index
end

#labelSymbol (readonly)

The parse label for the token.

Returns:

  • (Symbol)

    the current value of label



285
286
287
# File 'lib/google/cloud/language/annotation.rb', line 285

def label
  @label
end

#lemmaString (readonly)

[Lemma](en.wikipedia.org/wiki/Lemma_(morphology)) of the token.

Returns:

  • (String)

    the current value of lemma



285
286
287
# File 'lib/google/cloud/language/annotation.rb', line 285

def lemma
  @lemma
end

#part_of_speechSymbol (readonly)

Represents part of speech information for a token.

Returns:

  • (Symbol)

    the current value of part_of_speech



285
286
287
# File 'lib/google/cloud/language/annotation.rb', line 285

def part_of_speech
  @part_of_speech
end

#text_spanTextSpan (readonly)

The token text.

Returns:

  • (TextSpan)

    the current value of text_span



285
286
287
# File 'lib/google/cloud/language/annotation.rb', line 285

def text_span
  @text_span
end

Class Method Details

.from_grpc(grpc) ⇒ Object



312
313
314
315
316
317
# File 'lib/google/cloud/language/annotation.rb', line 312

def self.from_grpc grpc
  text_span = TextSpan.from_grpc grpc.text
  new text_span, grpc.part_of_speech.tag,
      grpc.dependency_edge.head_token_index,
      grpc.dependency_edge.label, grpc.lemma
end

Instance Method Details

#offsetObject Also known as: begin_offset



305
306
307
# File 'lib/google/cloud/language/annotation.rb', line 305

def offset
  @text_span.offset
end

#textObject Also known as: content



300
301
302
# File 'lib/google/cloud/language/annotation.rb', line 300

def text
  @text_span.text
end