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. -
#spacy_span_id ⇒ String
readonly
An identifier string that can be used when referring to the Python object inside
PyCall::execorPyCall::eval.
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.
-
#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.
-
#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.
-
#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.
-
#rights ⇒ Array<Token>
Returns Tokens that are to the right of the span, whose heads are within the span.
-
#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.
-
#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, either of the two method signatures should be used: Spacy.new(doc, py_span) and Spacy.new(doc, start_index, end_index, options).
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ruby-spacy.rb', line 43 def initialize(doc, py_span: nil, start_index: nil, end_index: nil, options: {}) @doc = doc @spacy_span_id = "doc_#{doc.object_id}_span_#{start_index}_#{end_index}" if py_span @py_span = py_span else = PyCall::Dict.() PyCall.exec("#{@spacy_span_id}_opts = #{options}") PyCall.exec("#{@spacy_span_id} = Span(#{@doc.spacy_doc_id}, #{start_index}, #{end_index + 1}, **#{@spacy_span_id}_opts)") @py_span = PyCall.eval(@spacy_span_id) 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.
178 179 180 |
# File 'lib/ruby-spacy.rb', line 178 def method_missing(name, *args) @py_span.send(name, *args) end |
Instance Attribute Details
#doc ⇒ Doc (readonly)
Returns the document to which the span belongs.
29 30 31 |
# File 'lib/ruby-spacy.rb', line 29 def doc @doc end |
#py_span ⇒ Object (readonly)
Returns a Python Span instance accessible via PyCall.
26 27 28 |
# File 'lib/ruby-spacy.rb', line 26 def py_span @py_span end |
#spacy_span_id ⇒ String (readonly)
Returns an identifier string that can be used when referring to the Python object inside PyCall::exec or PyCall::eval.
23 24 25 |
# File 'lib/ruby-spacy.rb', line 23 def spacy_span_id @spacy_span_id 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.
115 116 117 118 119 120 121 122 |
# File 'lib/ruby-spacy.rb', line 115 def [](range) if range.is_a?(Range) py_span = @py_span[range] return Spacy::Span.new(@doc, start_index: py_span.start, end_index: py_span.end - 1) else return Spacy::Token.new(@py_span[range]) end end |
#as_doc ⇒ Doc
Creates a document instance
133 134 135 |
# File 'lib/ruby-spacy.rb', line 133 def as_doc Spacy::Doc.new(@doc.spacy_nlp_id, self.text) end |
#conjuncts ⇒ Array<Token>
Returns Tokens conjugated to the root of the span.
139 140 141 142 143 144 145 |
# File 'lib/ruby-spacy.rb', line 139 def conjuncts conjunct_array = [] PyCall::List.(@py_span.conjuncts).each do |py_conjunct| conjunct_array << Spacy::Token.new(py_conjunct) end conjunct_array end |
#each ⇒ Object
Iterates over the elements in the span yielding a token instance.
67 68 69 70 71 |
# File 'lib/ruby-spacy.rb', line 67 def each PyCall::List.(@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.
97 98 99 100 101 102 103 104 |
# File 'lib/ruby-spacy.rb', line 97 def ents ent_array = [] PyCall::List.(@py_span.ents).each do |py_span| # ent_array << ent ent_array << Spacy::Span.new(@doc, py_span: py_span) end ent_array end |
#lefts ⇒ Array<Token>
Returns Tokens that are to the left of the span, whose heads are within the span.
149 150 151 152 153 154 155 |
# File 'lib/ruby-spacy.rb', line 149 def lefts left_array = [] PyCall::List.(@py_span.lefts).each do |py_left| left_array << Spacy::Token.new(py_left) end left_array end |
#noun_chunks ⇒ Array<Span>
Returns an array of spans of noun chunks.
75 76 77 78 79 80 81 82 |
# File 'lib/ruby-spacy.rb', line 75 def noun_chunks chunk_array = [] py_chunks = PyCall::List.(@py_span.noun_chunks) py_chunks.each do |py_span| chunk_array << Spacy::Span.new(@doc, py_span: py_span) end chunk_array end |
#rights ⇒ Array<Token>
Returns Tokens that are to the right of the span, whose heads are within the span.
159 160 161 162 163 164 165 |
# File 'lib/ruby-spacy.rb', line 159 def rights right_array = [] PyCall::List.(@py_span.rights).each do |py_right| right_array << Spacy::Token.new(py_right) end right_array end |
#sent ⇒ Span
Returns a span that represents the sentence that the given span is part of.
108 109 110 111 |
# File 'lib/ruby-spacy.rb', line 108 def sent py_span =@py_span.sent return Spacy::Span.new(@doc, py_span: py_span) end |
#sents ⇒ Array<Span>
Returns an array of spans that represents sentences.
86 87 88 89 90 91 92 93 |
# File 'lib/ruby-spacy.rb', line 86 def sents sentence_array = [] py_sentences = PyCall::List.(@py_span.sents) py_sentences.each do |py_span| sentence_array << Spacy::Span.new(@doc, py_span: py_span) end sentence_array end |
#similarity(other) ⇒ Float
Returns a semantic similarity estimate.
127 128 129 |
# File 'lib/ruby-spacy.rb', line 127 def similarity(other) PyCall.eval("#{@spacy_span_id}.similarity(#{other.spacy_span_id})") end |
#subtree ⇒ Array<Token>
Returns Tokens that are within the span and tokens that descend from them.
169 170 171 172 173 174 175 |
# File 'lib/ruby-spacy.rb', line 169 def subtree subtree_array = [] PyCall::List.(@py_span.subtree).each do |py_subtree| subtree_array << Spacy::Token.new(py_subtree) end subtree_array end |
#tokens ⇒ Array<Token>
Returns an array of tokens contained in the span.
58 59 60 61 62 63 64 |
# File 'lib/ruby-spacy.rb', line 58 def tokens results = [] PyCall::List.(@py_span).each do |py_token| results << Token.new(py_token) end results end |