'spacy.tokens.doc.Doc' object has no attribute 'pos_'

Viewed 2719

I am working on part-of-speech tagging. I am new to Spacy. I receive such an error.

AttributeError: 'spacy.tokens.doc.Doc' object has no attribute 'pos_'

I have checked that the data type is string, so the code should work. Where I am mistaken?

The full code is right above.

import pandas as pd
df = pd.read_excel('combined_file.xlsx', engine='openpyxl', index_col=None)
import spacy

df['body_string'] = df.body.astype('string')
sp = spacy.load('en_core_web_sm')
doc = df["body_string"].apply(sp)

for word in doc:
 print(word.text, word.pos_, word.dep_)

The link to the excel file is here: https://www.dropbox.com/scl/fi/43nu0yf45obbyzprzc86n/combined_file.xlsx?dl=0&rlkey=7j959kz0urjxflf6r536brppt

1 Answers

You need to call each model not the series of models, e.g.

import pandas as pd
import spacy

nlp = spacy.load('en_core_web_sm')
df = pd.read_excel(r"<location of xlsx>")

docs = df['body'].apply(nlp)
for token in docs[0]:
    print(token.text, token.pos_, token.dep_)

Output from doc/model 0:

I PRON nsubj
love VERB ROOT
ememis ADV advmod
but CCONJ cc
... PUNCT punct
this DET nsubj
is AUX ROOT
probably ADV advmod
the DET det
worst ADJ amod
and CCONJ cc
most ADV advmod
useless ADJ conj
eye NOUN compound
serum NOUN attr
i PRON nsubj
ve AUX aux
ever ADV advmod
used VERB relcl
. PUNCT punct
Ever ADV advmod
a DET det
cheap ADJ amod
£ SYM quantmod
5 NUM compound
one NUM nsubj
from ADP prep
boots NOUN pobj
is AUX ROOT
better ADJ acomp

If you wanted to print out some other doc.model (say the second one):

for token in docs[1]:
    print(token.text, token.pos_, token.dep_)

Basically docs is a series that contains the spacy applied models. E.g. if you wanted to print out all the tokens, etc. you could do so with (I don't recommend you do this):

for doc in docs:
    for token in doc:
        print(token.text, token.pos_, token.dep_)
Related