diff --git a/spacy/tokens/span.pyx b/spacy/tokens/span.pyx index 460972369..f702133af 100644 --- a/spacy/tokens/span.pyx +++ b/spacy/tokens/span.pyx @@ -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 diff --git a/spacy/tokens/token.pyx b/spacy/tokens/token.pyx index 909ebecbb..07c6f1c99 100644 --- a/spacy/tokens/token.pyx +++ b/spacy/tokens/token.pyx @@ -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