Part-of-speech Tagging using TextBlob in Python

In this article, we’ll learn about Part-of-Speech (POS) Tagging in Python using TextBlob.

POS Tagging or Grammatical tagging assigns part of speech to the words in a text (corpus). This means that each word of the text is labeled with a tag that can either be a noun, adjective, preposition or more.

We’ll use textblob library for implementing POS Tagging. So, install textblob using the given command below –

pip install textblob

POS Tags in Python

These are some of the POS Tags mentioned below –

C: conjunction, coordinating
CD: numeral, cardinal
DT: determiner
IN: preposition or conjunction, subordinating
JJ: adjective or numeral, ordinal
NNP: noun, proper, singular

If you want to learn about more of these tags, follow the steps below –

  1. Install NLTK library using the command given below –
    pip install nltk
  2. Import NLTK library
    import nltk
  3. Enter this command to download required NLTK data –'tagsets')
  4. Enter the following command for the POS tags list –

Now let’s implement POS tags using the TextBlob library through an example.

Example of part-of-speech tagging in Python programming

from textblob import TextBlob
text = ("Codespeedy is a programming blog. "
       "Blog posts contain articles and tutorials on Python, CSS and even much more")
tb = TextBlob(text) 
  1. Import textblob library using import keyword.
  2. Create a TextBlob object tb. This tokenizes all the words of the text which will then be passed onto the tag attribute.
  3. The tag attribute assigns each word with the respective POS tag. This will give an output in the form of (word, tag).

This gives the following output –

[('Codespeedy', 'NNP'), ('is', 'VBZ'), ('a', 'DT'), ('programming', 'VBG'), ('blog', 'NN'), ('Blog', 'NNP'), ('posts', 'NNS'), ('contain', 'VBP'), ('articles', 'NNS'), ('and', 'CC'), ('tutorials', 'NNS'), ('on', 'IN'), ('Python', 'NNP'), ('CSS', 'NNP'), ('and', 'CC'), ('even', 'RB'), ('much', 'RB'), ('more', 'JJR')]

I hope you all liked the article!

Also read-
Introduction to Natural Language Processing- NLP


Leave a Reply

Your email address will not be published. Required fields are marked *