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.

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)



374
375
376
377
378
379
380
381
# 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

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.



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

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



359
360
361
# File 'lib/ruby-spacy.rb', line 359

def doc
  @doc
end

#py_spanObject (readonly)

Returns a Python Span instance accessible via PyCall.

Returns:

  • (Object)

    a Python Span instance accessible via PyCall



356
357
358
# File 'lib/ruby-spacy.rb', line 356

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.

Parameters:

  • range (Range)

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



447
448
449
450
451
452
453
454
# 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_docDoc

Creates a document instance from the span

Returns:



465
466
467
# File 'lib/ruby-spacy.rb', line 465

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

#conjunctsArray<Token>

Returns tokens conjugated to the root of the span.

Returns:

  • (Array<Token>)

    an array of tokens



471
472
473
474
475
476
477
# 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

#eachObject

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



394
395
396
397
398
# File 'lib/ruby-spacy.rb', line 394

def each
  PyCall::List.(@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:



430
431
432
433
434
435
436
# 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

#labelString

Returns the label

Returns:

  • (String)


511
512
513
# File 'lib/ruby-spacy.rb', line 511

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



481
482
483
484
485
486
487
# 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

#noun_chunksArray<Span>

Returns an array of spans of noun chunks.

Returns:



402
403
404
405
406
407
408
409
# 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

#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



491
492
493
494
495
496
497
# 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

#rootToken

Returns the head token

Returns:



413
414
415
# File 'lib/ruby-spacy.rb', line 413

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

#sentSpan

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

Returns:



440
441
442
443
# File 'lib/ruby-spacy.rb', line 440

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

#sentsArray<Span>

Returns an array of spans that represents sentences.

Returns:



419
420
421
422
423
424
425
426
# 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) ⇒ Float

Returns a semantic similarity estimate.

Parameters:

  • other (Span)

    the other span to which a similarity estimation is conducted

Returns:

  • (Float)


459
460
461
# File 'lib/ruby-spacy.rb', line 459

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



501
502
503
504
505
506
507
# 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

#tokensArray<Token>

Returns an array of tokens contained in the span.

Returns:



385
386
387
388
389
390
391
# 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