class Spacy::PySpan

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

Attributes

doc[R]

@return [Doc] the document to which the span belongs

py_span[R]

@return [Object] a Python `Span` instance accessible via `PyCall`

Public Class Methods

new(doc, py_span: nil, start_index: nil, end_index: nil, options: {}) click to toggle source

It is recommended to use {Doc#span} method to create a span. If you need to create one using {Span#initialize}, there are two method signatures: `Span.new(doc, py_span: Object)` or `Span.new(doc, start_index: Integer, end_index: Integer, options: Hash)`. @param doc [Doc] the document to which this span belongs to @param start_index [Integer] the index of the item starting the span inside a doc @param end_index [Integer] the index of the item ending the span inside a doc @param options [Hash] options (`:label`, `:kb_id`, `:vector`)

# File lib/ruby-spacy.rb, line 374
def initialize(doc, py_span: nil, start_index: nil, end_index: nil, options: {})
  @doc = doc
  if py_span
    @py_span = py_span
  else
    @py_span = PySpan.(@doc.py_doc, start_index, end_index + 1, options)
  end
end

Public Instance Methods

[](range) click to toggle source

Returns a span if a range object is given or a token if an integer representing the position of the doc is given. @param range [Range] an ordinary Ruby's range object such as `0..3`, `1…4`, or `3 .. -1`

# File lib/ruby-spacy.rb, line 447
def [](range)
  if range.is_a?(Range)
    py_span = @py_span[range]
    return Span.new(@doc, start_index: py_span.start, end_index: py_span.end - 1)
  else
    return Token.new(@py_span[range])
  end
end
as_doc() click to toggle source

Creates a document instance from the span @return [Doc]

# File lib/ruby-spacy.rb, line 465
def as_doc
  Doc.new(@doc.py_nlp, text: self.text)
end
conjuncts() click to toggle source

Returns tokens conjugated to the root of the span. @return [Array<Token>] an array of tokens

# File lib/ruby-spacy.rb, line 471
def conjuncts
  conjunct_array = []
  PyCall::List.(@py_span.conjuncts).each do |py_conjunct|
    conjunct_array << Token.new(py_conjunct)
  end
  conjunct_array
end
each() { |token| ... } click to toggle source

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

# File lib/ruby-spacy.rb, line 394
def each
  PyCall::List.(@py_span).each do |py_token|
    yield Token.new(py_token)
  end
end
ents() click to toggle source

Returns an array of spans that represents named entities. @return [Array<Span>]

# File lib/ruby-spacy.rb, line 430
def ents
  ent_array = []
  PyCall::List.(@py_span.ents).each do |py_span|
    ent_array << Span.new(@doc, py_span: py_span)
  end
  ent_array
end
label() click to toggle source

Returns the label @return [String]

# File lib/ruby-spacy.rb, line 511
def label
  @py_span.label_
end
lefts() click to toggle source

Returns tokens that are to the left of the span, whose heads are within the span. @return [Array<Token>] an array of tokens

# File lib/ruby-spacy.rb, line 481
def lefts
  left_array = []
  PyCall::List.(@py_span.lefts).each do |py_left|
    left_array << Token.new(py_left)
  end
  left_array
end
method_missing(name, *args) click to toggle source

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

# File lib/ruby-spacy.rb, line 516
def method_missing(name, *args)
  @py_span.send(name, *args)
end
noun_chunks() click to toggle source

Returns an array of spans of noun chunks. @return [Array<Span>]

# File lib/ruby-spacy.rb, line 402
def noun_chunks
  chunk_array = []
  py_chunks = PyCall::List.(@py_span.noun_chunks)
  py_chunks.each do |py_span|
    chunk_array << Span.new(@doc, py_span: py_span)
  end
  chunk_array
end
rights() click to toggle source

Returns Tokens that are to the right of the span, whose heads are within the span. @return [Array<Token>] an array of Tokens

# File lib/ruby-spacy.rb, line 491
def rights
  right_array = []
  PyCall::List.(@py_span.rights).each do |py_right|
    right_array << Token.new(py_right)
  end
  right_array
end
root() click to toggle source

Returns the head token @return [Token]

# File lib/ruby-spacy.rb, line 413
def root 
  Token.new(@py_span.root)
end
sent() click to toggle source

Returns a span that represents the sentence that the given span is part of. @return [Span]

# File lib/ruby-spacy.rb, line 440
def sent
  py_span = @py_span.sent 
  return Span.new(@doc, py_span: py_span)
end
sents() click to toggle source

Returns an array of spans that represents sentences. @return [Array<Span>]

# File lib/ruby-spacy.rb, line 419
def sents
  sentence_array = []
  py_sentences = PyCall::List.(@py_span.sents)
  py_sentences.each do |py_span|
    sentence_array << Span.new(@doc, py_span: py_span)
  end
  sentence_array
end
similarity(other) click to toggle source

Returns a semantic similarity estimate. @param other [Span] the other span to which a similarity estimation is conducted @return [Float]

# File lib/ruby-spacy.rb, line 459
def similarity(other)
  py_span.similarity(other.py_span)
end
subtree() click to toggle source

Returns Tokens that are within the span and tokens that descend from them. @return [Array<Token>] an array of tokens

# File lib/ruby-spacy.rb, line 501
def subtree
  subtree_array = []
  PyCall::List.(@py_span.subtree).each do |py_subtree|
    subtree_array << Token.new(py_subtree)
  end
  subtree_array
end
tokens() click to toggle source

Returns an array of tokens contained in the span. @return [Array<Token>]

# File lib/ruby-spacy.rb, line 385
def tokens
  results = []
  PyCall::List.(@py_span).each do |py_token|
    results << Token.new(py_token)
  end
  results
end