Class: Spacy::Span

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

Overview

See also spaCy Python API document for [Span](spacy.io/api/span).

Instance Attribute Summary collapse

Instance Method Summary collapse

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)`.

Parameters:

  • doc (Doc)

    the document to which this span belongs to

  • start_index (Integer) (defaults to: nil)

    the index of the item starting the span inside a doc

  • end_index (Integer) (defaults to: nil)

    the index of the item ending the span inside a doc

  • options (Hash) (defaults to: {})

    options (:label, :kb_id, :vector)



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

def initialize(doc, py_span: nil, start_index: nil, end_index: nil, options: {})
  @doc = doc
  @py_span = py_span || PySpan.call(@doc.py_doc, start_index, end_index + 1, options)
  @text = @py_span.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.



887
888
889
# File 'lib/ruby-spacy.rb', line 887

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

Instance Attribute Details

#docDoc (readonly)

Returns the document to which the span belongs.

Returns:

  • (Doc)

    the document to which the span belongs



752
753
754
# File 'lib/ruby-spacy.rb', line 752

def doc
  @doc
end

#py_spanObject (readonly)

Returns a Python Span instance accessible via PyCall.

Returns:

  • (Object)

    a Python Span instance accessible via PyCall



749
750
751
# File 'lib/ruby-spacy.rb', line 749

def py_span
  @py_span
end

#textString (readonly)

Returns a text string of the span.

Returns:

  • (String)

    a text string of the span



755
756
757
# File 'lib/ruby-spacy.rb', line 755

def text
  @text
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.

Parameters:

  • range (Range)

    an ordinary Ruby’s range object such as 0..3, 1...4, or ‘3 .. -1`



828
829
830
831
832
833
834
835
# File 'lib/ruby-spacy.rb', line 828

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_docDoc

Creates a document instance from the span

Returns:



846
847
848
# File 'lib/ruby-spacy.rb', line 846

def as_doc
  Doc.new(@doc.py_nlp, text: text)
end

#conjunctsArray<Token>

Returns tokens conjugated to the root of the span.

Returns:

  • (Array<Token>)

    an array of tokens



852
853
854
# File 'lib/ruby-spacy.rb', line 852

def conjuncts
  PyCall::List.call(@py_span.conjuncts).map { |py_conjunct| Token.new(py_conjunct) }
end

#eachObject

Iterates over the elements in the span yielding a token instance each time.



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

def each
  PyCall::List.call(@py_span).each do |py_token|
    yield Token.new(py_token)
  end
end

#entsArray<Span>

Returns an array of spans that represents named entities.

Returns:



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

def ents
  PyCall::List.call(@py_span.ents).map do |py_span|
    Span.new(@doc, py_span: py_span)
  end
end

#instance_variables_to_inspectObject



895
896
897
# File 'lib/ruby-spacy.rb', line 895

def instance_variables_to_inspect
  [:@text]
end

#labelString

Returns the label

Returns:

  • (String)


876
877
878
# File 'lib/ruby-spacy.rb', line 876

def label
  @py_span.label_
end

#leftsArray<Token>

Returns tokens that are to the left of the span, whose heads are within the span.

Returns:

  • (Array<Token>)

    an array of tokens



858
859
860
# File 'lib/ruby-spacy.rb', line 858

def lefts
  PyCall::List.call(@py_span.lefts).map { |py_left| Token.new(py_left) }
end

#noun_chunksArray<Span>

Returns an array of spans of noun chunks.

Returns:



791
792
793
794
795
# File 'lib/ruby-spacy.rb', line 791

def noun_chunks
  PyCall::List.call(@py_span.noun_chunks).map do |py_span|
    Span.new(@doc, py_span: py_span)
  end
end

#respond_to_missing?(sym, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


891
892
893
# File 'lib/ruby-spacy.rb', line 891

def respond_to_missing?(sym, include_private = false)
  Spacy.py_hasattr?(@py_span, sym) || super
end

#rightsArray<Token>

Returns Tokens that are to the right of the span, whose heads are within the span.

Returns:

  • (Array<Token>)

    an array of Tokens



864
865
866
# File 'lib/ruby-spacy.rb', line 864

def rights
  PyCall::List.call(@py_span.rights).map { |py_right| Token.new(py_right) }
end

#rootToken

Returns the head token

Returns:



799
800
801
# File 'lib/ruby-spacy.rb', line 799

def root
  Token.new(@py_span.root)
end

#sentSpan

Returns a span that represents the sentence that the given span is part of.

Returns:



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

def sent
  py_span = @py_span.sent
  Span.new(@doc, py_span: py_span)
end

#sentsArray<Span>

Returns an array of spans that represents sentences.

Returns:



805
806
807
808
809
# File 'lib/ruby-spacy.rb', line 805

def sents
  PyCall::List.call(@py_span.sents).map do |py_span|
    Span.new(@doc, py_span: py_span)
  end
end

#similarity(other) ⇒ Float

Returns a semantic similarity estimate.

Parameters:

  • other (Span)

    the other span to which a similarity estimation is conducted

Returns:

  • (Float)


840
841
842
# File 'lib/ruby-spacy.rb', line 840

def similarity(other)
  py_span.similarity(other.py_span)
end

#subtreeArray<Token>

Returns Tokens that are within the span and tokens that descend from them.

Returns:

  • (Array<Token>)

    an array of tokens



870
871
872
# File 'lib/ruby-spacy.rb', line 870

def subtree
  PyCall::List.call(@py_span.subtree).map { |py_subtree| Token.new(py_subtree) }
end

#to_sString

String representation of the span.

Returns:

  • (String)


882
883
884
# File 'lib/ruby-spacy.rb', line 882

def to_s
  @text
end

#tokensArray<Token>

Returns an array of tokens contained in the span.

Returns:



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

def tokens
  PyCall::List.call(@py_span).map { |py_token| Token.new(py_token) }
end