Skip to content

Commit

Permalink
Add span.tensor and token.tensor attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
honnibal committed Aug 1, 2019
1 parent d3071ec commit 944a66c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
11 changes: 11 additions & 0 deletions spacy/tokens/span.pyx
Expand Up @@ -471,6 +471,17 @@ cdef class Span:
self._vector_norm = xp.sqrt(total) if total != 0. else 0.
return self._vector_norm

@property
def tensor(self):
"""The span's slice of the doc's tensor.
RETURNS (ndarray[ndim=2, dtype='float32']): A 2D numpy or cupy array
representing the span's semantics.
"""
if self.doc.tensor is None:
return None
return self.doc.tensor[self.start : self.end]

@property
def sentiment(self):
"""RETURNS (float): A scalar value indicating the positivity or
Expand Down
6 changes: 6 additions & 0 deletions spacy/tokens/token.pyx
Expand Up @@ -408,6 +408,12 @@ cdef class Token:
total = (vector ** 2).sum()
return xp.sqrt(total) if total != 0. else 0.

@property
def tensor(self):
if self.doc.tensor is None:
return None
return self.doc.tensor[self.i]

@property
def n_lefts(self):
"""The number of leftward immediate children of the word, in the
Expand Down

0 comments on commit 944a66c

Please sign in to comment.