NLTK POS Tagging – Python Examples

NLTK Parts of Speech (POS) Tagging

To perform Parts of Speech (POS) Tagging with NLTK in Python, use nltk.pos_tag() method with tokens passed as argument.

tagged = nltk.pos_tag(tokens)

where tokens is the list of words and pos_tag() returns a list of tuples with each

The prerequisite to use pos_tag() function is that, you should have averaged_perceptron_tagger package downloaded or download it programmatically before using the tagging method. In the following examples, we will use second method. The first method will be covered in: How to download nltk nlp packages?

Parts of Speech Tagging using NLTK

In the following example, we will take a piece of text and convert it to tokens. Then we shall do parts of speech tagging for these tokens using pos_tag() method.

import nltk

# download required nltk packages
# required for tokenization'punkt')
# required for parts of speech tagging'averaged_perceptron_tagger')

# input text
sentence = """Today morning, Arthur felt very good."""

# tokene into words
tokens = nltk.word_tokenize(sentence)

# parts of speech tagging
tagged = nltk.pos_tag(tokens)

# print tagged tokens
[('Today', 'NN'), ('morning', 'NN'), (',', ','), ('Arthur', 'NNP'), ('felt', 'VBD'), ('very', 'RB'), ('good', 'JJ'), ('.', '.')]

The Parts Of Speech Tag List

In the above example, the output contained tags like NN, NNP, VBD, etc. Following is the complete list of such POS tags.

CC Coordinating Conjunction
CD Cardinal Digit
DT Determiner
EX Existential There. Example: “there is” … think of it like “there exists”)
FW Foreign Word.
IN Preposition/Subordinating Conjunction.
JJ Adjective.
JJR Adjective, Comparative.
JJS Adjective, Superlative.
LS List Marker 1.
MD Modal.
NN Noun, Singular.
NNS Noun Plural.
NNP Proper Noun, Singular.
NNPS Proper Noun, Plural.
PDT Predeterminer.
POS Possessive Ending. Example: parent’s
PRP Personal Pronoun. Examples: I, he, she
PRP$ Possessive Pronoun. Examples: my, his, hers
RB Adverb. Examples: very, silently,
RBR Adverb, Comparative. Example: better
RBS Adverb, Superlative. Example: best
RP Particle. Example: give up
TO to. Example: go ‘to’ the store.
UH Interjection. Example: errrrrrrrm
VB Verb, Base Form. Example: take
VBD Verb, Past Tense. Example: took
VBG Verb, Gerund/Present Participle. Example: taking
VBN Verb, Past Participle. Example: taken
VBP Verb, Sing Present, non-3d take
VBZ Verb, 3rd person sing. present takes
WDT wh-determiner. Example: which
WP wh-pronoun. Example: who, what
WP$ possessive wh-pronoun. Example: whose
WRB wh-abverb. Example: where, when