An Introduction To Part-Of-Speech Tagging: What It Is And How You Can Use It In Natural Language Processing

Natural language processing (NLP) is the practice of analysing written and spoken language to extract meaningful insights from text. Part-of-speech (POS) tagging is a crucial part of NLP that helps identify the function of each word in a sentence or phrase. In this article, we will explore what POS tagging is, how it works, and how you can use it in your own projects.
What is Part-Of-Speech (POS) Tagging?
Part-of-speech tagging is the process of assigning a part of speech to each word in a sentence. The most common parts of speech are noun, verb, adjective, adverb, pronoun, preposition, and conjunction. There are also a few less common ones, such as interjection and article.
There are several different algorithms that can be used for POS tagging, but the most common one is the hidden Markov model. This algorithm looks at a sequence of words and uses statistical information to decide which part of speech each word is likely to be.
POS tagging can be used for a variety of tasks in natural language processing, including text classification and information extraction. It can also be used to improve the accuracy of other NLP tasks, such as parsing and machine translation.
Why Should You Use POS Tagging?
Part-of-speech tagging can be an extremely helpful tool in natural language processing, as it can help you to more easily identify the function of each word in a sentence. This can be particularly useful when you are trying to parse a sentence or when you are trying to determine the meaning of a word in context.
There are a variety of different POS taggers available, and each has its own strengths and weaknesses. However, if you are just getting started with POS tagging, then the NLTK module’s default pos_tag function is a good place to start.
In order to use POS tagging effectively, it is important to have a good understanding of grammar. If you are not familiar with grammar terms such as “noun,” “verb,” and “adjective,” then you may want to brush up on your grammar knowledge before using POS tagging (or see bullet list next).
The Different Parts of Speech and Their Tags
There are nine main parts of speech: noun, pronoun, verb, adjective, adverb, conjunction, preposition, interjection, and article.
Part-of-speech (POS) tags are labels that are assigned to words in a text, indicating their grammatical role in a sentence. The most common types of POS tags include:
- Noun (NN): A person, place, thing, or idea
- Verb (VB): An action or occurrence
- Adjective (JJ): A word that describes a noun or pronoun
- Adverb (RB): A word that describes a verb, adjective, or other adverb
- Pronoun (PRP): A word that takes the place of a noun
- Conjunction (CC): A word that connects words, phrases, or clauses
- Preposition (IN): A word that shows a relationship between a noun or pronoun and other elements in a sentence
- Interjection (UH): A word or phrase used to express strong emotion
This is just a sample of the most common POS tags, different libraries and models may have different sets of tags, but the purpose remains the same — to categorise words based on their grammatical function.
Parts of speech can also be categorised by their grammatical function in a sentence. There are three primary categories: subjects (which perform the action), objects (which receive the action), and modifiers (which describe or modify the subject or object). Each primary category can be further divided into subcategories. For example, subjects can be further classified as simple (one word), compound (two or more words), or complex (sentences containing subordinate clauses).
In addition to the primary categories, there are also two secondary categories: complements and adjuncts. Complements are elements that complete the meaning of the verb; they typically come after the verb and are often necessary for the sentence to make sense. Adjuncts are optional elements that provide additional information about the verb; they can come before or after the verb.
Example POS using NLTK
Here’s a simple example of part-of-speech tagging program using the Natural Language Toolkit (NLTK) library in Python:
import nltk
nltk.download('averaged_perceptron_tagger')
text = "The quick brown fox jumps over the lazy dog."
# Tokenize the text into words
tokens = nltk.word_tokenize(text)
# Perform part-of-speech tagging on the tokenized words
tagged_words = nltk.pos_tag(tokens)
print(tagged_words)The output will be a list of tuples, where each tuple consists of a word and its corresponding part-of-speech tag:
[('The', 'DT'),
('quick', 'JJ'),
('brown', 'JJ'),
('fox', 'NN'),
('jumps', 'VBZ'),
('over', 'IN'),
('the', 'DT'),
('lazy', 'JJ'),
('dog', 'NN'), ('.', '.')]Algorithms Used for POS Tagging
There are a few different algorithms that can be used for part-of-speech tagging, the most common one is the Hidden Markov Model (HMM). This algorithm uses a statistical approach to predict the next word in a sentence, based on the previous words in the sentence.
The HMM algorithm starts with a list of all of the possible parts of speech (nouns, verbs, adjectives, etc.), and then looks at each word in the sentence and tries to assign it a part of speech. The algorithm looks at the surrounding words in order to try to determine which part of speech makes the most sense. For example, if a word is surrounded by other words that are all nouns, it’s likely that that word is also a noun.
Here are a few other POS algorithms available in the wild:
Some current major algorithms for part-of-speech tagging include the Viterbi algorithm, Brill tagger, Constraint Grammar, and the Baum-Welch algorithm (also known as the forward-backward algorithm). Hidden Markov model and visible Markov model taggers can both be implemented using the Viterbi algorithm. [Source: Wiki ]
Accuracy in POS
In addition to our code example above where we have tagged our POS, we don’t really have an understanding of how well the tagger is performing, in order for us to get a clearer picture we can check the accuracy score.
This is a measure of how well a part-of-speech tagger performs on a test set of data. It is a useful metric because it provides a quantitative way to evaluate the performance of the HMM part-of-speech tagger.
The accuracy score is calculated as the number of correctly tagged words divided by the total number of words in the test set. A high accuracy score indicates that the tagger is correctly identifying the part of speech of a large number of words in the test set, while a low accuracy score suggests that the tagger is making a large number of mistakes.
Having an accuracy score allows you to compare the performance of different part-of-speech taggers, or to compare the performance of the same tagger with different settings or parameters. This can help you to identify which tagger is the most effective for a particular task, and to make informed decisions about which tagger to use in a production environment.
You can do this in Python using the NLTK library. Here’s a simple example:
import nltk
nltk.download('brown')
nltk.download('universal_tagset')
# Load the sample Brown corpus
brown_corpus = nltk.corpus.brown
# Obtain the tagged sentences
tagged_sents = brown_corpus.tagged_sents(tagset='universal')
# Split the data into training and testing sets
train_sents = tagged_sents[:int(len(tagged_sents) * 0.9)]
test_sents = tagged_sents[int(len(tagged_sents) * 0.9):]
# Train an HMM part-of-speech tagger
hmm_tagger = nltk.HiddenMarkovModelTagger.train(train_sents)
# Evaluate the tagger on the test data
print(hmm_tagger.accuracy(test_sents))Output:
0.9406655350893737This code first loads the Brown corpus and obtains the tagged sentences using the universal tagset. It then splits the data into training and testing sets, with 90% of the data used for training and 10% for testing. The code trains an HMM part-of-speech tagger on the training data, and finally, evaluates the tagger on the test data, printing the accuracy score.
Examples of Using POS Tagging for NLP Problems
When it comes to POS tagging, there are a number of different ways that it can be used in natural language processing. Here are just a few examples:
- named entity recognition — This is where POS tagging can be used to identify proper nouns in a text, which can then be used to extract information about people, places, organizations, etc.
- sentiment analysis — By identifying words with positive or negative connotations, POS tagging can be used to calculate the overall sentiment of a piece of text.
- topic identification — By looking at which words are most commonly used together, POS tagging can help automatically identify the main topics of a document.
- machine translation — In order for machines to translate one language into another, they need to understand the grammar and structure of the source language. POS tagging can be used to provide this understanding, allowing for more accurate translations.
- question answering — When trying to answer questions based on documents, machines need to be able to identify the key parts of speech in the question in order to correctly find the relevant information in the text.
Advantages & Disadvantages of POS Tagging
When it comes to part-of-speech tagging, there are both advantages and disadvantages that come with the territory. On the plus side, POS tagging can help to improve the accuracy of NLP algorithms. This is because it can provide context for words that might otherwise be ambiguous. For example, the word “fly” could be either a verb or a noun. But if we know that it’s being used as a verb in a particular sentence, then we can more accurately interpret the meaning of that sentence.
On the downside, POS tagging can be time-consuming and resource-intensive. In addition, it doesn’t always produce perfect results — sometimes words will be tagged incorrectly, which, can lead to errors in downstream NLP applications.
Conclusion
Part-of-speech tagging is an essential tool in natural language processing. It helps us identify words and phrases in text to determine their respective parts of speech, which are then used for further analysis such as sentiment or salience determinations. We have discussed some practical applications that make use of part-of-speech tagging, as well as popular algorithms used to implement it. With these foundational concepts in place, you can now start leveraging this powerful method to enhance your NLP projects!
Thanks for Reading.
David.




