Search code examples
pythonpython-3.xnlpspacypart-of-speech

spaCy NLP word.pos returns digits instead of POS tags


I am using spaCy library for POS tagging but when I run this code, it returns numbers in the place of the pos tags:

import spacy
from spacy.lang.fr.examples import sentences

nlp = spacy.load('en')
mystring = " I am missing my lovely family a lot."
exuu = nlp(mystring)
for word in exuu: 
  print(word.text, word.pos)

Here is how the output looks like:

102
I 94
am 99
missing 99
my 83
dear 83
family 91
a 89
lot 91
. 96

Solution

  • You are reading the "wrong" attribute. word.pos returns the PoS tag id, not the PoS tag string. To do what you want, just replace word.pos with word.pos_.

    The following code will work fine:

    import spacy
    from spacy.lang.fr.examples import sentences
    nlp = spacy.load('en')
    mystring = " I am missing my lovely family a lot."
    exuu = nlp(mystring)
    for word in exuu: 
      print(word.text, word.pos_)