Class: Spacy::Span
Overview
See also spaCy Python API document for [‘Span`](spacy.io/api/span).
Instance Attribute Summary collapse
-
#doc ⇒ Doc
readonly
The document to which the span belongs.
-
#py_span ⇒ Object
readonly
A Python ‘Span` instance accessible via `PyCall`.
Instance Method Summary collapse
-
#[](range) ⇒ Object
Returns a span if a range object is given or a token if an integer representing the position of the doc is given.
-
#as_doc ⇒ Doc
Creates a document instance from the span.
-
#conjuncts ⇒ Array<Token>
Returns tokens conjugated to the root of the span.
-
#each ⇒ Object
Iterates over the elements in the span yielding a token instance each time.
-
#ents ⇒ Array<Span>
Returns an array of spans that represents named entities.
-
#initialize(doc, py_span: nil, start_index: nil, end_index: nil, options: {}) ⇒ Span
constructor
It is recommended to use Doc#span method to create a span.
-
#label ⇒ String
Returns the label.
-
#lefts ⇒ Array<Token>
Returns tokens that are to the left of the span, whose heads are within the span.
-
#method_missing(name, *args) ⇒ Object
Methods defined in Python but not wrapped in ruby-spacy can be called by this dynamic method handling mechanism.
-
#noun_chunks ⇒ Array<Span>
Returns an array of spans of noun chunks.
- #respond_to_missing?(sym) ⇒ Boolean
-
#rights ⇒ Array<Token>
Returns Tokens that are to the right of the span, whose heads are within the span.
-
#root ⇒ Token
Returns the head token.
-
#sent ⇒ Span
Returns a span that represents the sentence that the given span is part of.
-
#sents ⇒ Array<Span>
Returns an array of spans that represents sentences.
-
#similarity(other) ⇒ Float
Returns a semantic similarity estimate.
-
#subtree ⇒ Array<Token>
Returns Tokens that are within the span and tokens that descend from them.
-
#tokens ⇒ Array<Token>
Returns an array of tokens contained in the span.
Constructor Details
#initialize(doc, py_span: nil, start_index: nil, end_index: nil, options: {}) ⇒ Span
It is recommended to use Doc#span method to create a span. If you need to create one using #initialize, there are two method signatures: ‘Span.new(doc, py_span: Object)` or `Span.new(doc, start_index: Integer, end_index: Integer, options: Hash)`.
540 541 542 543 |
# File 'lib/ruby-spacy.rb', line 540 def initialize(doc, py_span: nil, start_index: nil, end_index: nil, options: {}) @doc = doc @py_span = py_span || @py_span = PySpan.call(@doc.py_doc, start_index, end_index + 1, ) 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.
678 679 680 |
# File 'lib/ruby-spacy.rb', line 678 def method_missing(name, *args) @py_span.send(name, *args) end |
Instance Attribute Details
#doc ⇒ Doc (readonly)
Returns the document to which the span belongs.
525 526 527 |
# File 'lib/ruby-spacy.rb', line 525 def doc @doc end |
#py_span ⇒ Object (readonly)
Returns a Python ‘Span` instance accessible via `PyCall`.
522 523 524 |
# File 'lib/ruby-spacy.rb', line 522 def py_span @py_span end |
Instance Method Details
#[](range) ⇒ Object
Returns a span if a range object is given or a token if an integer representing the position of the doc is given.
609 610 611 612 613 614 615 616 |
# File 'lib/ruby-spacy.rb', line 609 def [](range) if range.is_a?(Range) py_span = @py_span[range] Span.new(@doc, start_index: py_span.start, end_index: py_span.end - 1) else Token.new(@py_span[range]) end end |
#as_doc ⇒ Doc
Creates a document instance from the span
627 628 629 |
# File 'lib/ruby-spacy.rb', line 627 def as_doc Doc.new(@doc.py_nlp, text: text) end |
#conjuncts ⇒ Array<Token>
Returns tokens conjugated to the root of the span.
633 634 635 636 637 638 639 |
# File 'lib/ruby-spacy.rb', line 633 def conjuncts conjunct_array = [] PyCall::List.call(@py_span.conjuncts).each do |py_conjunct| conjunct_array << Token.new(py_conjunct) end conjunct_array end |
#each ⇒ Object
Iterates over the elements in the span yielding a token instance each time.
556 557 558 559 560 |
# File 'lib/ruby-spacy.rb', line 556 def each PyCall::List.call(@py_span).each do |py_token| yield Token.new(py_token) end end |
#ents ⇒ Array<Span>
Returns an array of spans that represents named entities.
592 593 594 595 596 597 598 |
# File 'lib/ruby-spacy.rb', line 592 def ents ent_array = [] PyCall::List.call(@py_span.ents).each do |py_span| ent_array << Span.new(@doc, py_span: py_span) end ent_array end |
#label ⇒ String
Returns the label
673 674 675 |
# File 'lib/ruby-spacy.rb', line 673 def label @py_span.label_ end |
#lefts ⇒ Array<Token>
Returns tokens that are to the left of the span, whose heads are within the span.
643 644 645 646 647 648 649 |
# File 'lib/ruby-spacy.rb', line 643 def lefts left_array = [] PyCall::List.call(@py_span.lefts).each do |py_left| left_array << Token.new(py_left) end left_array end |
#noun_chunks ⇒ Array<Span>
Returns an array of spans of noun chunks.
564 565 566 567 568 569 570 571 |
# File 'lib/ruby-spacy.rb', line 564 def noun_chunks chunk_array = [] py_chunks = PyCall::List.call(@py_span.noun_chunks) py_chunks.each do |py_span| chunk_array << Span.new(@doc, py_span: py_span) end chunk_array end |
#respond_to_missing?(sym) ⇒ Boolean
682 683 684 |
# File 'lib/ruby-spacy.rb', line 682 def respond_to_missing?(sym) sym ? true : super end |
#rights ⇒ Array<Token>
Returns Tokens that are to the right of the span, whose heads are within the span.
653 654 655 656 657 658 659 |
# File 'lib/ruby-spacy.rb', line 653 def rights right_array = [] PyCall::List.call(@py_span.rights).each do |py_right| right_array << Token.new(py_right) end right_array end |
#root ⇒ Token
Returns the head token
575 576 577 |
# File 'lib/ruby-spacy.rb', line 575 def root Token.new(@py_span.root) end |
#sent ⇒ Span
Returns a span that represents the sentence that the given span is part of.
602 603 604 605 |
# File 'lib/ruby-spacy.rb', line 602 def sent py_span = @py_span.sent Span.new(@doc, py_span: py_span) end |
#sents ⇒ Array<Span>
Returns an array of spans that represents sentences.
581 582 583 584 585 586 587 588 |
# File 'lib/ruby-spacy.rb', line 581 def sents sentence_array = [] py_sentences = PyCall::List.call(@py_span.sents) py_sentences.each do |py_span| sentence_array << Span.new(@doc, py_span: py_span) end sentence_array end |
#similarity(other) ⇒ Float
Returns a semantic similarity estimate.
621 622 623 |
# File 'lib/ruby-spacy.rb', line 621 def similarity(other) py_span.similarity(other.py_span) end |
#subtree ⇒ Array<Token>
Returns Tokens that are within the span and tokens that descend from them.
663 664 665 666 667 668 669 |
# File 'lib/ruby-spacy.rb', line 663 def subtree subtree_array = [] PyCall::List.call(@py_span.subtree).each do |py_subtree| subtree_array << Token.new(py_subtree) end subtree_array end |
#tokens ⇒ Array<Token>
Returns an array of tokens contained in the span.
547 548 549 550 551 552 553 |
# File 'lib/ruby-spacy.rb', line 547 def tokens results = [] PyCall::List.call(@py_span).each do |py_token| results << Token.new(py_token) end results end |