In [1]:
# Import Dependencies
import nltk
from nltk.corpus import wordnet
In [2]:
text = 'I was not happy with the teams performance.'
In [3]:
words = nltk.word_tokenize(text)
In [4]:
words
Out[4]:
In [5]:
new_words = []
In [6]:
temp_word = ""
antonyms = []
In [7]:
for word in words:
if word == 'not':
temp_word = 'not_'
elif temp_word == 'not_':
for syn in wordnet.synsets(word):
for s in syn.lemmas():
for a in s.antonyms():
antonyms.append(a.name())
if len(antonyms) >= 1:
word = antonyms[0]
else:
word = temp_word + word
temp_word = ''
if word != 'not':
new_words.append(word)
In [8]:
new_words
Out[8]:
In [9]:
sentence = ' '.join(new_words)
In [10]:
sentence
Out[10]: