Class: Spacy::Span
Overview
See also spaCy Python API document for Span.
Instance Attribute Summary collapse
-
#doc ⇒ Doc
readonly
The document to which the span belongs.
-
#py_span ⇒ Object
readonly
A Python
Spaninstance accessible viaPyCall. -
#text ⇒ String
readonly
A text string of the span.
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.
- #instance_variables_to_inspect ⇒ Object
-
#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, include_private = false) ⇒ 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.
-
#to_s ⇒ String
String representation of the span.
-
#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).
926 927 928 929 930 |
# File 'lib/ruby-spacy.rb', line 926 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, ) @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.
1043 1044 1045 |
# File 'lib/ruby-spacy.rb', line 1043 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.
908 909 910 |
# File 'lib/ruby-spacy.rb', line 908 def doc @doc end |
#py_span ⇒ Object (readonly)
Returns a Python Span instance accessible via PyCall.
905 906 907 |
# File 'lib/ruby-spacy.rb', line 905 def py_span @py_span end |
#text ⇒ String (readonly)
Returns a text string of the span.
911 912 913 |
# File 'lib/ruby-spacy.rb', line 911 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.
984 985 986 987 988 989 990 991 |
# File 'lib/ruby-spacy.rb', line 984 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
1002 1003 1004 |
# File 'lib/ruby-spacy.rb', line 1002 def as_doc Doc.new(@doc.py_nlp, text: text) end |
#conjuncts ⇒ Array<Token>
Returns tokens conjugated to the root of the span.
1008 1009 1010 |
# File 'lib/ruby-spacy.rb', line 1008 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.
939 940 941 942 943 |
# File 'lib/ruby-spacy.rb', line 939 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.
969 970 971 972 973 |
# File 'lib/ruby-spacy.rb', line 969 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
1051 1052 1053 |
# File 'lib/ruby-spacy.rb', line 1051 def instance_variables_to_inspect [:@text] end |
#label ⇒ String
Returns the label
1032 1033 1034 |
# File 'lib/ruby-spacy.rb', line 1032 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.
1014 1015 1016 |
# File 'lib/ruby-spacy.rb', line 1014 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.
947 948 949 950 951 |
# File 'lib/ruby-spacy.rb', line 947 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
1047 1048 1049 |
# File 'lib/ruby-spacy.rb', line 1047 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.
1020 1021 1022 |
# File 'lib/ruby-spacy.rb', line 1020 def rights PyCall::List.call(@py_span.rights).map { |py_right| Token.new(py_right) } end |
#root ⇒ Token
Returns the head token
955 956 957 |
# File 'lib/ruby-spacy.rb', line 955 def root Token.new(@py_span.root) end |
#sent ⇒ Span
Returns a span that represents the sentence that the given span is part of.
977 978 979 980 |
# File 'lib/ruby-spacy.rb', line 977 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.
961 962 963 964 965 |
# File 'lib/ruby-spacy.rb', line 961 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.
996 997 998 |
# File 'lib/ruby-spacy.rb', line 996 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.
1026 1027 1028 |
# File 'lib/ruby-spacy.rb', line 1026 def subtree PyCall::List.call(@py_span.subtree).map { |py_subtree| Token.new(py_subtree) } end |
#to_s ⇒ String
String representation of the span.
1038 1039 1040 |
# File 'lib/ruby-spacy.rb', line 1038 def to_s @text end |