Class: Spacy::Token

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

Overview

See also spaCy Python API document for 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 Spacy::Token object.

Parameters:

  • py_token (Object)

    Python Token object



525
526
527
528
# File 'lib/ruby-spacy.rb', line 525

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.



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

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



517
518
519
# File 'lib/ruby-spacy.rb', line 517

def py_token
  @py_token
end

#textString (readonly)

Returns a string representing the token.

Returns:

  • (String)

    a string representing the token



520
521
522
# File 'lib/ruby-spacy.rb', line 520

def text
  @text
end

Instance Method Details

#ancestorsArray<Token>

Returns the token's ancestors.

Returns:

  • (Array<Token>)

    an array of tokens



549
550
551
552
553
554
555
# File 'lib/ruby-spacy.rb', line 549

def ancestors
  ancestor_array = []
  PyCall::List.(@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



559
560
561
562
563
564
565
# File 'lib/ruby-spacy.rb', line 559

def children
  child_array = []
  PyCall::List.(@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)


645
646
647
# File 'lib/ruby-spacy.rb', line 645

def dep
  @py_token.dep_
end

#ent_typeString

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

Returns:

  • (String)


663
664
665
# File 'lib/ruby-spacy.rb', line 663

def ent_type 
  @py_token.ent_type_
end

#headToken

Returns the head token

Returns:



533
534
535
# File 'lib/ruby-spacy.rb', line 533

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

#langString

Returns the language by calling lang_' of@py_token` object

Returns:

  • (String)


651
652
653
# File 'lib/ruby-spacy.rb', line 651

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



569
570
571
572
573
574
575
# File 'lib/ruby-spacy.rb', line 569

def lefts
  token_array = []
  PyCall::List.(@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)


615
616
617
# File 'lib/ruby-spacy.rb', line 615

def lemma
  @py_token.lemma_
end

#lexemeLexeme

Returns a lexeme object

Returns:



669
670
671
# File 'lib/ruby-spacy.rb', line 669

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

#lowerString

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

Returns:

  • (String)


621
622
623
# File 'lib/ruby-spacy.rb', line 621

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)


596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
# File 'lib/ruby-spacy.rb', line 596

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

#posString

Returns the pos by calling pos_' of@py_token` object

Returns:

  • (String)


633
634
635
# File 'lib/ruby-spacy.rb', line 633

def pos
  @py_token.pos_
end

#rightsArray<Token>

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

Returns:

  • (Array<Token>)

    an array of tokens



579
580
581
582
583
584
585
# File 'lib/ruby-spacy.rb', line 579

def rights
  token_array = []
  PyCall::List.(@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)


627
628
629
# File 'lib/ruby-spacy.rb', line 627

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



539
540
541
542
543
544
545
# File 'lib/ruby-spacy.rb', line 539

def subtree
  descendant_array = []
  PyCall::List.(@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)


639
640
641
# File 'lib/ruby-spacy.rb', line 639

def tag 
  @py_token.tag_
end

#to_sString

String representation of the token.

Returns:

  • (String)


589
590
591
# File 'lib/ruby-spacy.rb', line 589

def to_s
  @text
end

#whitespaceString

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

Returns:

  • (String)


657
658
659
# File 'lib/ruby-spacy.rb', line 657

def whitespace 
  @py_token.whitespace_
end