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)



991
992
993
994
995
# File 'lib/ruby-spacy.rb', line 991

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.



1117
1118
1119
# File 'lib/ruby-spacy.rb', line 1117

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

Instance Attribute Details

#doc ⇒ Doc (readonly)

Returns the document to which the span belongs.

Returns:

  • (Doc) —

    the document to which the span belongs



973
974
975
# File 'lib/ruby-spacy.rb', line 973

def doc
  @doc
end

#py_span ⇒ Object (readonly)

Returns a Python Span instance accessible via PyCall.

Returns:

  • (Object) —

    a Python Span instance accessible via PyCall



970
971
972
# File 'lib/ruby-spacy.rb', line 970

def py_span
  @py_span
end

#text ⇒ String (readonly)

Returns a text string of the span.

Returns:

  • (String) —

    a text string of the span



976
977
978
# File 'lib/ruby-spacy.rb', line 976

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



1049
1050
1051
1052
1053
1054
1055
1056
# File 'lib/ruby-spacy.rb', line 1049

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

Returns:



1067
1068
1069
# File 'lib/ruby-spacy.rb', line 1067

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

#conjuncts ⇒ Array<Token>

Returns tokens conjugated to the root of the span.

Returns:

  • (Array<Token>) —

    an array of tokens



1082
1083
1084
# File 'lib/ruby-spacy.rb', line 1082

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

#each ⇒ Object

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



1004
1005
1006
1007
1008
# File 'lib/ruby-spacy.rb', line 1004

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.

Returns:



1034
1035
1036
1037
1038
# File 'lib/ruby-spacy.rb', line 1034

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

#instance_variables_to_inspect ⇒ Object



1125
1126
1127
# File 'lib/ruby-spacy.rb', line 1125

def instance_variables_to_inspect
  [:@text]
end

#label ⇒ String

Returns the label

Returns:

  • (String)


1106
1107
1108
# File 'lib/ruby-spacy.rb', line 1106

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.

Returns:

  • (Array<Token>) —

    an array of tokens



1088
1089
1090
# File 'lib/ruby-spacy.rb', line 1088

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

#noun_chunks ⇒ Array<Span>

Returns an array of spans of noun chunks.

Returns:



1012
1013
1014
1015
1016
# File 'lib/ruby-spacy.rb', line 1012

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)


1121
1122
1123
# File 'lib/ruby-spacy.rb', line 1121

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

#rights ⇒ Array<Token>

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

Returns:

  • (Array<Token>) —

    an array of Tokens



1094
1095
1096
# File 'lib/ruby-spacy.rb', line 1094

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

#root ⇒ Token

Returns the head token

Returns:



1020
1021
1022
# File 'lib/ruby-spacy.rb', line 1020

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

#sent ⇒ Span

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

Returns:



1042
1043
1044
1045
# File 'lib/ruby-spacy.rb', line 1042

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.

Returns:



1026
1027
1028
1029
1030
# File 'lib/ruby-spacy.rb', line 1026

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)


1061
1062
1063
# File 'lib/ruby-spacy.rb', line 1061

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.

Returns:

  • (Array<Token>) —

    an array of tokens



1100
1101
1102
# File 'lib/ruby-spacy.rb', line 1100

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

#syntax_tree(**options) ⇒ String

Generates a syntax tree for the span in rsyntaxtree bracket notation, or renders it with the rsyntaxtree gem. The span must have a single root (e.g. a sentence from Doc#sents). See Doc#syntax_tree for the available options.

Returns:

  • (String) —

    the bracket notation or the rendered output



1076
1077
1078
# File 'lib/ruby-spacy.rb', line 1076

def syntax_tree(**options)
  SyntaxTree.generate(self, **options)
end

#to_s ⇒ String

String representation of the span.

Returns:

  • (String)


1112
1113
1114
# File 'lib/ruby-spacy.rb', line 1112

def to_s
  @text
end

#tokens ⇒ Array<Token>

Returns an array of tokens contained in the span.

Returns:



999
1000
1001
# File 'lib/ruby-spacy.rb', line 999

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