Make doc.from_array() consistent with doc.to_array(). Closes #3382

This commit is contained in:
Matthew Honnibal 2019-03-10 15:50:48 +01:00
parent 0426689db8
commit 4e80fc41ad
1 changed files with 12 additions and 0 deletions

View File

@ -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