Add span.tensor and token.tensor attributes

This commit is contained in:
Matthew Honnibal 2019-08-01 18:30:50 +02:00
parent d3071ecdbc
commit 944a66c326
2 changed files with 17 additions and 0 deletions

View File

@ -471,6 +471,17 @@ cdef class Span:
self._vector_norm = xp.sqrt(total) if total != 0. else 0. self._vector_norm = xp.sqrt(total) if total != 0. else 0.
return self._vector_norm 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 @property
def sentiment(self): def sentiment(self):
"""RETURNS (float): A scalar value indicating the positivity or """RETURNS (float): A scalar value indicating the positivity or

View File

@ -408,6 +408,12 @@ cdef class Token:
total = (vector ** 2).sum() total = (vector ** 2).sum()
return xp.sqrt(total) if total != 0. else 0. 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 @property
def n_lefts(self): def n_lefts(self):
"""The number of leftward immediate children of the word, in the """The number of leftward immediate children of the word, in the