From ed19ece5c4e40d8250e6d73a375f33a50fde1928 Mon Sep 17 00:00:00 2001 From: Adriane Boyd Date: Tue, 6 Dec 2022 14:24:05 +0100 Subject: [PATCH] Reduce number of individual casts --- spacy/tokens/doc.pyx | 8 +++++--- spacy/training/example.pyx | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/spacy/tokens/doc.pyx b/spacy/tokens/doc.pyx index 1788bcb58..fb4d590e9 100644 --- a/spacy/tokens/doc.pyx +++ b/spacy/tokens/doc.pyx @@ -359,11 +359,12 @@ cdef class Doc: for annot in annotations: if annot: if annot is heads or annot is sent_starts or annot is ent_iobs: + annot = numpy.array(annot).astype(numpy.uint64) for i in range(len(words)): if attrs.ndim == 1: - attrs[i] = numpy.array(annot[i]).astype(numpy.uint64) + attrs[i] = annot[i] else: - attrs[i, j] = numpy.array(annot[i]).astype(numpy.uint64) + attrs[i, j] = annot[i] elif annot is morphs: for i in range(len(words)): morph_key = vocab.morphology.add(morphs[i]) @@ -1558,8 +1559,9 @@ cdef class Doc: for j, (attr, annot) in enumerate(token_annotations.items()): if attr is HEAD: + annot = numpy.array(annot).astype(numpy.uint64) for i in range(len(words)): - array[i, j] = numpy.array(annot[i]).astype(numpy.uint64) + array[i, j] = annot[i] elif attr is MORPH: for i in range(len(words)): array[i, j] = self.vocab.morphology.add(annot[i]) diff --git a/spacy/training/example.pyx b/spacy/training/example.pyx index 7be363cef..79160bb00 100644 --- a/spacy/training/example.pyx +++ b/spacy/training/example.pyx @@ -462,7 +462,7 @@ def _annot2array(vocab, tok_annot, doc_annot): types = set([type(v) for v in value]) raise TypeError(Errors.E969.format(field=key, types=types)) from None row = [vocab.strings.add(v) for v in value] - values.append([numpy.array(v).astype(numpy.uint64) for v in row]) + values.append([numpy.array(v).astype(numpy.uint64) if v < 0 else v for v in row]) array = numpy.array(values, dtype=numpy.uint64) return attrs, array.T