Class: Spacy::Token
- Inherits:
-
Object
- Object
- Spacy::Token
- Defined in:
- lib/ruby-spacy.rb
Overview
See also spaCy Python API document for Token.
Instance Attribute Summary collapse
-
#py_token ⇒ Object
readonly
A Python
Tokeninstance accessible viaPyCall. -
#text ⇒ String
readonly
A string representing the token.
Instance Method Summary collapse
-
#ancestors ⇒ Array<Object>
Returns the token's ancestors.
-
#children ⇒ Array<Object>
Returns a sequence of the token's immediate syntactic children.
-
#initialize(py_token) ⇒ Token
constructor
It is recommended to use Doc#tokens or Span#tokens methods to create tokens.
-
#lefts ⇒ Array<Object>
The leftward immediate children of the word in the syntactic dependency parse.
-
#method_missing(name, *args) ⇒ Object
Methods defined in Python but not wrapped in ruby-spacy can be called by this dynamic method handling mechanism.
-
#rights ⇒ Array<Object>
The rightward immediate children of the word in the syntactic dependency parse.
-
#subtree ⇒ Array<Object>
Returns the token in question and the tokens that descend from it.
-
#to_s ⇒ String
String representation of the token.
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.
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_token ⇒ Object (readonly)
Returns a Python Token instance accessible via PyCall.
187 188 189 |
# File 'lib/ruby-spacy.rb', line 187 def py_token @py_token end |
#text ⇒ String (readonly)
Returns a string representing the token.
190 191 192 |
# File 'lib/ruby-spacy.rb', line 190 def text @text end |
Instance Method Details
#ancestors ⇒ Array<Object>
Returns the token's ancestors.
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 |
#children ⇒ Array<Object>
Returns a sequence of the token's immediate syntactic children.
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 |
#lefts ⇒ Array<Object>
The leftward immediate children of the word in the syntactic dependency parse.
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 |
#rights ⇒ Array<Object>
The rightward immediate children of the word in the syntactic dependency parse.
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 |
#subtree ⇒ Array<Object>
Returns the token in question and the tokens that descend from it.
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_s ⇒ String
String representation of the token.
251 252 253 |
# File 'lib/ruby-spacy.rb', line 251 def to_s @text end |