Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lemmatization with POS Tagging | Stemming and Lemmatization
Introduction to NLP

Lemmatization with POS Tagging Lemmatization with POS Tagging

The English language is full of words that can serve as multiple parts of speech with different meanings. For example, "running" can be a verb ("He is running.") or a noun ("Running is fun.").

As we have already seen, a lemmatizer can only accurately reduce a word to its base form if it knows the word's part of speech in the given context. POS tagging in turn provides this context, making lemmatization more precise.

Lemmatization with POS Tagging in NLTK

Since we are already familiar with both of these techniques separately, it's time to combine them. However, there is one important aspect to take into consideration, namely the difference in POS tags format between pos_tagand the format that WordNet Lemmatizer expects.

NLTK's pos_tag function utilizes the Penn Treebank tag set, which includes a wide range of tags for detailed grammatical categorization. The WordNet Lemmatizer, on the other hand, expects POS tags in a simplified form that aligns with WordNet's own categorization. Specifically, it only differentiates among nouns (n), verbs (v), adjectives (a or s for satellite adjectives), and adverbs (r).

The mapping process involves converting the detailed Penn Treebank tags to the broader categories recognized by WordNet. For example, both 'VBD' (past tense verb) and 'VBG' (gerund or present participle) from Penn Treebank would map to 'v' (verb) for use with the WordNet Lemmatizer.

Let's write a function for this purpose:

This function simply checks the first letter of the Penn Treebank tag: if it's 'J', it returns the WordNet tag for adjectives; if 'V', for verbs; if 'R', for adverbs.

For all other cases, including when the tag starts with 'N' or doesn't match any specified condition, it defaults to returning the WordNet tag for nouns. These ADJ, VERB etc. are just constants, where ADJ, ADJ_SAT, ADV, NOUN, VERB = "a", "s", "r", "n", "v".

Given this function, let's now perform lemmatization with POS tagging beforehand:

As you can see, we first performed POS tagging using the pos_tag() function, next we used list comprehension to create a list of lemmatized tokens by applying the lemmatize() method with the current token and correctly formatted tag (using our function get_wordnet_pos(tag)) as its arguments. We intentionally did not remove stop words to demonstrate that the code effectively processes all tokens.

As mentioned in one of the previous chapters, ' '.join(lemmatized_tokens) is used here to create a string, given a list of strings (tokens), so that they will be separated by a whitespace, effectively reconstructing the sentence from its individual, processed tokens.

Task

It's time to combine all the text preprocessing techniques we have learned so far to get lemmatized text without the stop words, given the initial raw text. Your task is the following:

  1. Convert text to lowercase.
  2. Load the list of English stop words and convert it to set.
  3. Initialize a lemmatizer.
  4. Tokenize the text string.
  5. Filter out the stop words using list comprehension.
  6. Perform POS tagging using the respective function.
  7. Lemmatize the resulting tokens taking their POS tags into account using list comprehension.

Everything was clear?

Section 2. Chapter 5
toggle bottom row
course content

Course Content

Introduction to NLP

Lemmatization with POS Tagging Lemmatization with POS Tagging

The English language is full of words that can serve as multiple parts of speech with different meanings. For example, "running" can be a verb ("He is running.") or a noun ("Running is fun.").

As we have already seen, a lemmatizer can only accurately reduce a word to its base form if it knows the word's part of speech in the given context. POS tagging in turn provides this context, making lemmatization more precise.

Lemmatization with POS Tagging in NLTK

Since we are already familiar with both of these techniques separately, it's time to combine them. However, there is one important aspect to take into consideration, namely the difference in POS tags format between pos_tagand the format that WordNet Lemmatizer expects.

NLTK's pos_tag function utilizes the Penn Treebank tag set, which includes a wide range of tags for detailed grammatical categorization. The WordNet Lemmatizer, on the other hand, expects POS tags in a simplified form that aligns with WordNet's own categorization. Specifically, it only differentiates among nouns (n), verbs (v), adjectives (a or s for satellite adjectives), and adverbs (r).

The mapping process involves converting the detailed Penn Treebank tags to the broader categories recognized by WordNet. For example, both 'VBD' (past tense verb) and 'VBG' (gerund or present participle) from Penn Treebank would map to 'v' (verb) for use with the WordNet Lemmatizer.

Let's write a function for this purpose:

This function simply checks the first letter of the Penn Treebank tag: if it's 'J', it returns the WordNet tag for adjectives; if 'V', for verbs; if 'R', for adverbs.

For all other cases, including when the tag starts with 'N' or doesn't match any specified condition, it defaults to returning the WordNet tag for nouns. These ADJ, VERB etc. are just constants, where ADJ, ADJ_SAT, ADV, NOUN, VERB = "a", "s", "r", "n", "v".

Given this function, let's now perform lemmatization with POS tagging beforehand:

As you can see, we first performed POS tagging using the pos_tag() function, next we used list comprehension to create a list of lemmatized tokens by applying the lemmatize() method with the current token and correctly formatted tag (using our function get_wordnet_pos(tag)) as its arguments. We intentionally did not remove stop words to demonstrate that the code effectively processes all tokens.

As mentioned in one of the previous chapters, ' '.join(lemmatized_tokens) is used here to create a string, given a list of strings (tokens), so that they will be separated by a whitespace, effectively reconstructing the sentence from its individual, processed tokens.

Task

It's time to combine all the text preprocessing techniques we have learned so far to get lemmatized text without the stop words, given the initial raw text. Your task is the following:

  1. Convert text to lowercase.
  2. Load the list of English stop words and convert it to set.
  3. Initialize a lemmatizer.
  4. Tokenize the text string.
  5. Filter out the stop words using list comprehension.
  6. Perform POS tagging using the respective function.
  7. Lemmatize the resulting tokens taking their POS tags into account using list comprehension.

Everything was clear?

Section 2. Chapter 5
toggle bottom row
some-alt