class Spacy::PyToken

See also spaCy Python API document for [`Token`](spacy.io/api/token).

Attributes

py_token[R]

@return [Object] a Python `Token` instance accessible via `PyCall`

text[R]

@return [String] a string representing the token

Public Class Methods

new(py_token) click to toggle source

It is recommended to use {Doc#tokens} or {Span#tokens} methods to create tokens. There is no way to generate a token from scratch but relying on a pre-exising Python {Token} object. @param py_token [Object] Python `Token` object

# File lib/ruby-spacy.rb, line 533
def initialize(py_token)
  @py_token = py_token
  @text = @py_token.text
end

Public Instance Methods

ancestors() click to toggle source

Returns the token's ancestors. @return [Array<Token>] an array of tokens

# File lib/ruby-spacy.rb, line 557
def ancestors
  ancestor_array = []
  PyCall::List.(@py_token.ancestors).each do |ancestor|
    ancestor_array << Token.new(ancestor)
  end
  ancestor_array
end
children() click to toggle source

Returns a sequence of the token's immediate syntactic children. @return [Array<Token>] an array of tokens

# File lib/ruby-spacy.rb, line 567
def children
  child_array = []
  PyCall::List.(@py_token.children).each do |child|
    child_array << Token.new(child)
  end
  child_array
end
dep() click to toggle source

Returns the dependency relation by calling `dep_' of `@py_token` object @return [String]

# File lib/ruby-spacy.rb, line 653
def dep
  @py_token.dep_
end
ent_type() click to toggle source

Returns the named entity type by calling `ent_type_' of `@py_token` object @return [String]

# File lib/ruby-spacy.rb, line 671
def ent_type 
  @py_token.ent_type_
end
head() click to toggle source

Returns the head token @return [Token]

# File lib/ruby-spacy.rb, line 541
def head 
  Token.new(@py_token.head)
end
lang() click to toggle source

Returns the language by calling `lang_' of `@py_token` object @return [String]

# File lib/ruby-spacy.rb, line 659
def lang 
  @py_token.lang_
end
lefts() click to toggle source

The leftward immediate children of the word in the syntactic dependency parse. @return [Array<Token>] an array of tokens

# File lib/ruby-spacy.rb, line 577
def lefts
  token_array = []
  PyCall::List.(@py_token.lefts).each do |token|
    token_array << Token.new(token)
  end
  token_array
end
lemma() click to toggle source

Returns the lemma by calling `lemma_' of `@py_token` object @return [String]

# File lib/ruby-spacy.rb, line 623
def lemma
  @py_token.lemma_
end
lexeme() click to toggle source

Returns a lexeme object @return [Lexeme]

# File lib/ruby-spacy.rb, line 677
def lexeme
  Lexeme.new(@py_token.lex)
end
lower() click to toggle source

Returns the lowercase form by calling `lower_' of `@py_token` object @return [String]

# File lib/ruby-spacy.rb, line 629
def lower
  @py_token.lower_
end
method_missing(name, *args) click to toggle source

Methods defined in Python but not wrapped in ruby-spacy can be called by this dynamic method handling mechanism.

# File lib/ruby-spacy.rb, line 682
def method_missing(name, *args)
  @py_token.send(name, *args)
end
morphology(hash = true) click to toggle source

Returns a hash or string of morphological information @param hash [Boolean] if true, a hash will be returned instead of a string @return [Hash, String]

# File lib/ruby-spacy.rb, line 604
def morphology(hash = true)
  if @py_token.has_morph
    morph_analysis = @py_token.morph
    if hash 
      return morph_analysis.to_dict
    else
      return morph_analysis.to_s
    end
  else
    if hash
      results = {}
    else
      return ""
    end
  end
end
pos() click to toggle source

Returns the pos by calling `pos_' of `@py_token` object @return [String]

# File lib/ruby-spacy.rb, line 641
def pos
  @py_token.pos_
end
rights() click to toggle source

The rightward immediate children of the word in the syntactic dependency parse. @return [Array<Token>] an array of tokens

# File lib/ruby-spacy.rb, line 587
def rights
  token_array = []
  PyCall::List.(@py_token.rights).each do |token|
    token_array << Token.new(token)
  end
  token_array
end
shape() click to toggle source

Returns the shape (e.g. “Xxxxx”) by calling `shape_' of `@py_token` object @return [String]

# File lib/ruby-spacy.rb, line 635
def shape
  @py_token.shape_
end
subtree() click to toggle source

Returns the token in question and the tokens that descend from it. @return [Array<Token>] an array of tokens

# File lib/ruby-spacy.rb, line 547
def subtree
  descendant_array = []
  PyCall::List.(@py_token.subtree).each do |descendant|
    descendant_array << Token.new(descendant)
  end
  descendant_array
end
tag() click to toggle source

Returns the fine-grained pos by calling `tag_' of `@py_token` object @return [String]

# File lib/ruby-spacy.rb, line 647
def tag 
  @py_token.tag_
end
to_s() click to toggle source

String representation of the token. @return [String]

# File lib/ruby-spacy.rb, line 597
def to_s
  @text
end
whitespace() click to toggle source

Returns the trailing space character if present by calling `whitespace_' of `@py_token` object @return [String]

# File lib/ruby-spacy.rb, line 665
def whitespace 
  @py_token.whitespace_
end