Replacing negations with antonyms

The opposite of synonym replacement is antonym replacement. An antonym is a word that has the opposite meaning of another word. This time, instead of creating custom word mappings, we can use WordNet to replace words with unambiguous antonyms. Refer to the Looking up lemmas and synonyms in WordNet recipe in Chapter 1, Tokenizing Text and WordNet Basics, for more details on antonym lookups.

How to do it...

Let's say you have a sentence like let's not uglify our code. With antonym replacement, you can replace not uglify with beautify, resulting in the sentence let's beautify our code. To do this, we will create an AntonymReplacer class in replacers.py as follows:

from nltk.corpus import wordnet class AntonymReplacer(object): ...

Get Natural Language Processing: Python and NLTK now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.