Class: Spacy::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-spacy.rb

Overview

See also spaCy Python API document for Token.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(py_token) ⇒ Token

It is recommended to use Doc#tokens or Span#tokens methods to create tokens. There is no way to generate a token from scratch but relying on a pre-exising Python Spacy::Token object.

Parameters:

  • py_token (Object)

    Python Token object



194
195
196
197
# File 'lib/ruby-spacy.rb', line 194

def initialize(py_token)
  @py_token = py_token
  @text = @py_token.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.



256
257
258
# File 'lib/ruby-spacy.rb', line 256

def method_missing(name, *args)
  @py_token.send(name, *args)
end

Instance Attribute Details

#py_tokenObject (readonly)

Returns a Python Token instance accessible via PyCall.

Returns:

  • (Object)

    a Python Token instance accessible via PyCall



187
188
189
# File 'lib/ruby-spacy.rb', line 187

def py_token
  @py_token
end

#textString (readonly)

Returns a string representing the token.

Returns:

  • (String)

    a string representing the token



190
191
192
# File 'lib/ruby-spacy.rb', line 190

def text
  @text
end

Instance Method Details

#ancestorsArray<Object>

Returns the token's ancestors.

Returns:

  • (Array<Object>)

    an (Ruby) array of Python Token objects



211
212
213
214
215
216
217
# File 'lib/ruby-spacy.rb', line 211

def ancestors
  ancestor_array = []
  PyCall::List.(@py_token.ancestors).each do |ancestor|
    ancestor_array << ancestor
  end
  ancestor_array
end

#childrenArray<Object>

Returns a sequence of the token's immediate syntactic children.

Returns:

  • (Array<Object>)

    an (Ruby) array of Python Token objects



221
222
223
224
225
226
227
# File 'lib/ruby-spacy.rb', line 221

def children
  child_array = []
  PyCall::List.(@py_token.children).each do |child|
    child_array << child
  end
  child_array
end

#leftsArray<Object>

The leftward immediate children of the word in the syntactic dependency parse.

Returns:

  • (Array<Object>)

    an (Ruby) array of Python Token objects



231
232
233
234
235
236
237
# File 'lib/ruby-spacy.rb', line 231

def lefts
  token_array = []
  PyCall::List.(@py_token.lefts).each do |token|
    token_array << token
  end
  token_array
end

#rightsArray<Object>

The rightward immediate children of the word in the syntactic dependency parse.

Returns:

  • (Array<Object>)

    an (Ruby) array of Python Token objects



241
242
243
244
245
246
247
# File 'lib/ruby-spacy.rb', line 241

def rights
  token_array = []
  PyCall::List.(@py_token.rights).each do |token|
    token_array << token
  end
  token_array
end

#subtreeArray<Object>

Returns the token in question and the tokens that descend from it.

Returns:

  • (Array<Object>)

    an (Ruby) array of Python Token objects



201
202
203
204
205
206
207
# File 'lib/ruby-spacy.rb', line 201

def subtree
  descendant_array = []
  PyCall::List.(@py_token.subtree).each do |descendant|
    descendant_array << descendant
  end
  descendant_array
end

#to_sString

String representation of the token.

Returns:

  • (String)


251
252
253
# File 'lib/ruby-spacy.rb', line 251

def to_s
  @text
end