Class: Spacy::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-spacy.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(py_token) ⇒ Token

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.

Parameters:

  • py_token (Object)

    Python ‘Token` object



679
680
681
682
# File 'lib/ruby-spacy.rb', line 679

def initialize(py_token)
  @py_token = py_token
  @text = @py_token.text
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

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



825
826
827
# File 'lib/ruby-spacy.rb', line 825

def method_missing(name, *args)
  @py_token.send(name, *args)
end

Instance Attribute Details

#py_tokenObject (readonly)

Returns a Python ‘Token` instance accessible via `PyCall`.

Returns:

  • (Object)

    a Python ‘Token` instance accessible via `PyCall`



671
672
673
# File 'lib/ruby-spacy.rb', line 671

def py_token
  @py_token
end

#textString (readonly)

Returns a string representing the token.

Returns:

  • (String)

    a string representing the token



674
675
676
# File 'lib/ruby-spacy.rb', line 674

def text
  @text
end

Instance Method Details

#ancestorsArray<Token>

Returns the token’s ancestors.

Returns:

  • (Array<Token>)

    an array of tokens



702
703
704
705
706
707
708
# File 'lib/ruby-spacy.rb', line 702

def ancestors
  ancestor_array = []
  PyCall::List.call(@py_token.ancestors).each do |ancestor|
    ancestor_array << Token.new(ancestor)
  end
  ancestor_array
end

#childrenArray<Token>

Returns a sequence of the token’s immediate syntactic children.

Returns:

  • (Array<Token>)

    an array of tokens



712
713
714
715
716
717
718
# File 'lib/ruby-spacy.rb', line 712

def children
  child_array = []
  PyCall::List.call(@py_token.children).each do |child|
    child_array << Token.new(child)
  end
  child_array
end

#depString

Returns the dependency relation by calling ‘dep_’ of ‘@py_token` object

Returns:

  • (String)


796
797
798
# File 'lib/ruby-spacy.rb', line 796

def dep
  @py_token.dep_
end

#ent_typeString

Returns the named entity type by calling ‘ent_type_’ of ‘@py_token` object

Returns:

  • (String)


814
815
816
# File 'lib/ruby-spacy.rb', line 814

def ent_type
  @py_token.ent_type_
end

#headToken

Returns the head token

Returns:



686
687
688
# File 'lib/ruby-spacy.rb', line 686

def head
  Token.new(@py_token.head)
end

#langString

Returns the language by calling ‘lang_’ of ‘@py_token` object

Returns:

  • (String)


802
803
804
# File 'lib/ruby-spacy.rb', line 802

def lang
  @py_token.lang_
end

#leftsArray<Token>

The leftward immediate children of the word in the syntactic dependency parse.

Returns:

  • (Array<Token>)

    an array of tokens



722
723
724
725
726
727
728
# File 'lib/ruby-spacy.rb', line 722

def lefts
  token_array = []
  PyCall::List.call(@py_token.lefts).each do |token|
    token_array << Token.new(token)
  end
  token_array
end

#lemmaString

Returns the lemma by calling ‘lemma_’ of ‘@py_token` object

Returns:

  • (String)


766
767
768
# File 'lib/ruby-spacy.rb', line 766

def lemma
  @py_token.lemma_
end

#lexemeLexeme

Returns a lexeme object

Returns:



820
821
822
# File 'lib/ruby-spacy.rb', line 820

def lexeme
  Lexeme.new(@py_token.lex)
end

#lowerString

Returns the lowercase form by calling ‘lower_’ of ‘@py_token` object

Returns:

  • (String)


772
773
774
# File 'lib/ruby-spacy.rb', line 772

def lower
  @py_token.lower_
end

#morphology(hash: true) ⇒ Hash, String

Returns a hash or string of morphological information

Parameters:

  • hash (Boolean) (defaults to: true)

    if true, a hash will be returned instead of a string

Returns:

  • (Hash, String)


749
750
751
752
753
754
755
756
757
758
759
760
761
762
# File 'lib/ruby-spacy.rb', line 749

def morphology(hash: true)
  if @py_token.has_morph
    morph_analysis = @py_token.morph
    if hash
      morph_analysis.to_dict
    else
      morph_analysis.to_s
    end
  elsif hash
    {}
  else
    ""
  end
end

#posString

Returns the pos by calling ‘pos_’ of ‘@py_token` object

Returns:

  • (String)


784
785
786
# File 'lib/ruby-spacy.rb', line 784

def pos
  @py_token.pos_
end

#respond_to_missing?(sym) ⇒ Boolean

Returns:

  • (Boolean)


829
830
831
# File 'lib/ruby-spacy.rb', line 829

def respond_to_missing?(sym)
  sym ? true : super
end

#rightsArray<Token>

The rightward immediate children of the word in the syntactic dependency parse.

Returns:

  • (Array<Token>)

    an array of tokens



732
733
734
735
736
737
738
# File 'lib/ruby-spacy.rb', line 732

def rights
  token_array = []
  PyCall::List.call(@py_token.rights).each do |token|
    token_array << Token.new(token)
  end
  token_array
end

#shapeString

Returns the shape (e.g. “Xxxxx”) by calling ‘shape_’ of ‘@py_token` object

Returns:

  • (String)


778
779
780
# File 'lib/ruby-spacy.rb', line 778

def shape
  @py_token.shape_
end

#subtreeArray<Token>

Returns the token in question and the tokens that descend from it.

Returns:

  • (Array<Token>)

    an array of tokens



692
693
694
695
696
697
698
# File 'lib/ruby-spacy.rb', line 692

def subtree
  descendant_array = []
  PyCall::List.call(@py_token.subtree).each do |descendant|
    descendant_array << Token.new(descendant)
  end
  descendant_array
end

#tagString

Returns the fine-grained pos by calling ‘tag_’ of ‘@py_token` object

Returns:

  • (String)


790
791
792
# File 'lib/ruby-spacy.rb', line 790

def tag
  @py_token.tag_
end

#to_sString

String representation of the token.

Returns:

  • (String)


742
743
744
# File 'lib/ruby-spacy.rb', line 742

def to_s
  @text
end

#whitespaceString

Returns the trailing space character if present by calling ‘whitespace_’ of ‘@py_token` object

Returns:

  • (String)


808
809
810
# File 'lib/ruby-spacy.rb', line 808

def whitespace
  @py_token.whitespace_
end