From 944a66c326f18a3fcf3cd76798ecdc2405e1f026 Mon Sep 17 00:00:00 2001 From: Matthew Honnibal Date: Thu, 1 Aug 2019 18:30:50 +0200 Subject: [PATCH] Add span.tensor and token.tensor attributes --- spacy/tokens/span.pyx | 11 +++++++++++ spacy/tokens/token.pyx | 6 ++++++ 2 files changed, 17 insertions(+) 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