Getting errors while using token.morph in spacy

564 Views Asked by At
import spacy

nlp = spacy.load("en_core_web_sm")
print("Pipeline:", nlp.pipe_names)
doc = nlp("I was reading the paper.")
token = doc[2] 
print(token.morph)
print(token.morph.get("PronType"))

TypeError: str returned non-string (type list)

Giving above error for token.morph

1

There are 1 best solutions below

0
Wiktor Stribiżew On

Here is a test using Spacy 3.0:

import spacy
nlp = spacy.load("en_core_web_trf")
print("Pipeline:", nlp.pipe_names)
# => Pipeline: ['transformer', 'tagger', 'parser', 'ner', 'attribute_ruler', 'lemmatizer']
doc = nlp("I was reading the paper.")
token = doc[2]
print(token.morph)
# => Aspect=Prog|Tense=Pres|VerbForm=Part

Then, there is no PronType attribute here, so printing what you want yields an empty array:

print(token.morph.get("PronType"))
# => []