From 4e80fc41ad16a4c9b1cb08168ff393cb80c5ad4c Mon Sep 17 00:00:00 2001 From: Matthew Honnibal Date: Sun, 10 Mar 2019 15:50:48 +0100 Subject: [PATCH] Make doc.from_array() consistent with doc.to_array(). Closes #3382 --- spacy/tokens/doc.pyx | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/spacy/tokens/doc.pyx b/spacy/tokens/doc.pyx index ff38d825f..fce2a6e7c 100644 --- a/spacy/tokens/doc.pyx +++ b/spacy/tokens/doc.pyx @@ -737,6 +737,18 @@ cdef class Doc: DOCS: https://spacy.io/api/doc#from_array """ + # Handle scalar/list inputs of strings/ints for py_attr_ids + # See also #3064 + if isinstance(attrs, basestring_): + # Handle inputs like doc.to_array('ORTH') + attrs = [attrs] + elif not hasattr(attrs, "__iter__"): + # Handle inputs like doc.to_array(ORTH) + attrs = [attrs] + # Allow strings, e.g. 'lemma' or 'LEMMA' + attrs = [(IDS[id_.upper()] if hasattr(id_, "upper") else id_) + for id_ in attrs] + if SENT_START in attrs and HEAD in attrs: raise ValueError(Errors.E032) cdef int i, col