Class: Spacy::Token
- Inherits:
-
Object
- Object
- Spacy::Token
- Defined in:
- lib/ruby-spacy.rb
Overview
See also spaCy Python API document for Token.
Instance Attribute Summary collapse
-
#py_token ⇒ Object
readonly
A Python
Tokeninstance accessible viaPyCall. -
#text ⇒ String
readonly
A string representing the token.
Instance Method Summary collapse
-
#ancestors ⇒ Array<Token>
Returns the token's ancestors.
-
#children ⇒ Array<Token>
Returns a sequence of the token's immediate syntactic children.
-
#dep ⇒ String
Returns the dependency relation by calling
dep_' of@py_token` object. -
#ent_type ⇒ String
Returns the named entity type by calling
ent_type_' of@py_token` object. -
#head ⇒ Token
Returns the head token.
-
#initialize(py_token) ⇒ Token
constructor
It is recommended to use Doc#tokens or Span#tokens methods to create tokens.
-
#lang ⇒ String
Returns the language by calling
lang_' of@py_token` object. -
#lefts ⇒ Array<Token>
The leftward immediate children of the word in the syntactic dependency parse.
-
#lemma ⇒ String
Returns the lemma by calling
lemma_' of@py_token` object. -
#lexeme ⇒ Lexeme
Returns a lexeme object.
-
#lower ⇒ String
Returns the lowercase form by calling
lower_' of@py_token` object. -
#method_missing(name, *args) ⇒ Object
Methods defined in Python but not wrapped in ruby-spacy can be called by this dynamic method handling mechanism.
-
#morphology(hash = true) ⇒ Hash, String
Returns a hash or string of morphological information.
-
#pos ⇒ String
Returns the pos by calling
pos_' of@py_token` object. -
#rights ⇒ Array<Token>
The rightward immediate children of the word in the syntactic dependency parse.
-
#shape ⇒ String
Returns the shape (e.g. "Xxxxx") by calling
shape_' of@py_token` object. -
#subtree ⇒ Array<Token>
Returns the token in question and the tokens that descend from it.
-
#tag ⇒ String
Returns the fine-grained pos by calling
tag_' of@py_token` object. -
#to_s ⇒ String
String representation of the token.
-
#whitespace ⇒ String
Returns the trailing space character if present by calling
whitespace_' of@py_token` object.
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.
533 534 535 536 |
# File 'lib/ruby-spacy.rb', line 533 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.
682 683 684 |
# File 'lib/ruby-spacy.rb', line 682 def method_missing(name, *args) @py_token.send(name, *args) end |
Instance Attribute Details
#py_token ⇒ Object (readonly)
Returns a Python Token instance accessible via PyCall.
525 526 527 |
# File 'lib/ruby-spacy.rb', line 525 def py_token @py_token end |
#text ⇒ String (readonly)
Returns a string representing the token.
528 529 530 |
# File 'lib/ruby-spacy.rb', line 528 def text @text end |
Instance Method Details
#ancestors ⇒ Array<Token>
Returns the token's ancestors.
557 558 559 560 561 562 563 |
# 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 ⇒ Array<Token>
Returns a sequence of the token's immediate syntactic children.
567 568 569 570 571 572 573 |
# 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 ⇒ String
Returns the dependency relation by calling dep_' of@py_token` object
653 654 655 |
# File 'lib/ruby-spacy.rb', line 653 def dep @py_token.dep_ end |
#ent_type ⇒ String
Returns the named entity type by calling ent_type_' of@py_token` object
671 672 673 |
# File 'lib/ruby-spacy.rb', line 671 def ent_type @py_token.ent_type_ end |
#head ⇒ Token
Returns the head token
541 542 543 |
# File 'lib/ruby-spacy.rb', line 541 def head Token.new(@py_token.head) end |
#lang ⇒ String
Returns the language by calling lang_' of@py_token` object
659 660 661 |
# File 'lib/ruby-spacy.rb', line 659 def lang @py_token.lang_ end |
#lefts ⇒ Array<Token>
The leftward immediate children of the word in the syntactic dependency parse.
577 578 579 580 581 582 583 |
# 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 ⇒ String
Returns the lemma by calling lemma_' of@py_token` object
623 624 625 |
# File 'lib/ruby-spacy.rb', line 623 def lemma @py_token.lemma_ end |
#lexeme ⇒ Lexeme
Returns a lexeme object
677 678 679 |
# File 'lib/ruby-spacy.rb', line 677 def lexeme Lexeme.new(@py_token.lex) end |
#lower ⇒ String
Returns the lowercase form by calling lower_' of@py_token` object
629 630 631 |
# File 'lib/ruby-spacy.rb', line 629 def lower @py_token.lower_ end |
#morphology(hash = true) ⇒ Hash, String
Returns a hash or string of morphological information
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 |
# 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 ⇒ String
Returns the pos by calling pos_' of@py_token` object
641 642 643 |
# File 'lib/ruby-spacy.rb', line 641 def pos @py_token.pos_ end |
#rights ⇒ Array<Token>
The rightward immediate children of the word in the syntactic dependency parse.
587 588 589 590 591 592 593 |
# 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 ⇒ String
Returns the shape (e.g. "Xxxxx") by calling shape_' of@py_token` object
635 636 637 |
# File 'lib/ruby-spacy.rb', line 635 def shape @py_token.shape_ end |
#subtree ⇒ Array<Token>
Returns the token in question and the tokens that descend from it.
547 548 549 550 551 552 553 |
# 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 ⇒ String
Returns the fine-grained pos by calling tag_' of@py_token` object
647 648 649 |
# File 'lib/ruby-spacy.rb', line 647 def tag @py_token.tag_ end |
#to_s ⇒ String
String representation of the token.
597 598 599 |
# File 'lib/ruby-spacy.rb', line 597 def to_s @text end |
#whitespace ⇒ String
Returns the trailing space character if present by calling whitespace_' of@py_token` object
665 666 667 |
# File 'lib/ruby-spacy.rb', line 665 def whitespace @py_token.whitespace_ end |