# How can I find synonyms and antonyms for a word?

I found some code online where I can feed in a word, and find both synonyms and antonyms for this word. The code below does just that.

import nltk
from nltk.corpus import wordnet   #Import wordnet from the NLTK
syn = list()
ant = list()
for synset in wordnet.synsets("fake"):
for lemma in synset.lemmas():
syn.append(lemma.name())    #add the synonyms
if lemma.antonyms():    #When antonyms are available, add them into the list
ant.append(lemma.antonyms()[0].name())
print('Synonyms: ' + str(syn))
print('Antonyms: ' + str(ant))


My question is, how can I choose a word, like 'and', and find all synonyms and antonyms based on comments that exist in a field in a dataframe? Here is a sample of the first 10 lines from my dataframe.

feels weird; may be a fake!
package came end missing box. since itâ€™s gift i update actual fit.
birkenstock amazing shoe!!!! i wish i ten pairs!
delivered advertised.... shoe looks fake.
second pair i had. nothing beats them.
they totally fake ðŸ˜¡. they felt weird i finally noticed â€œmade germanyâ€ logo above. they also smell like glue leather. infuriating happen!
i've birkenstock wearer 35 years. i wear 10 women's size ones i ordered wear big. i need size down.
great brand, packaging good, dad likes them. the size clearly needs fixed others don't order bigger sizes wanted. some people don't want bigger size actually wear.
a bit loose compared birkenstocks size. still like though. very comfy


The general approach is:

1. Split the text in tokens.
2. For each token, find synonyms and antonyms and store them with the token
from collections import defaultdict

from nltk.corpus import wordnet
from nltk.tokenize import word_tokenize

text = """
feels weird; may be a fake!
package came end missing box. since itâ€™s gift i update actual fit.
birkenstock amazing shoe!!!! i wish i ten pairs!
delivered advertised.... shoe looks fake.
second pair i had. nothing beats them.
they totally fake ðŸ˜¡. they felt weird i finally noticed â€œmade germanyâ€ logo above. they also smell like glue leather. infuriating happen!
i've birkenstock wearer 35 years. i wear 10 women's size ones i ordered wear big. i need size down.
great brand, packaging good, dad likes them. the size clearly needs fixed others don't order bigger sizes wanted. some people don't want bigger size actually wear.
a bit loose compared birkenstocks size. still like though. very comfy"""

tokens = word_tokenize(text)

# Store results in a dict where key is token and value is a set of synonyms and antonyms
tokens_syns, tokens_ant = defaultdict(set), defaultdict(set)

for token in tokens:
for synset in wordnet.synsets(token):
for lemma in synset.lemmas():
if lemma.antonyms():    # Check if antonyms are available

# Check for 'and'
token = 'and'
print(f'For the word "{token}":')
print('The synonyms are:', tokens_syns[token])
print('The antonyms are:', tokens_ant[token])

# Conclusion 'and' has no synonyms or antonyms according to WordNet


• Thanks! Your example works perfect for me. Now, I'm trying to change it a bit so I can feed in a field from a dataframe that I am working with. I tried this: df['review_text'] = df['review_text'].astype(str) tokens = word_tokenize(df['review_text']) That gives me this error: TypeError: expected string or bytes-like object How can I convert the dataframe field to text and create tokens out of it?
– ASH
Feb 22 '20 at 13:52
• pandas.pydata.org/pandas-docs/stable/reference/api/… Feb 22 '20 at 14:48
• I tried that; I'm getting the same results s before. Maybe I need to loop through rows of the dataframe and append each to a list. Just thinking out loud. Seems like there should be a more efficient way to handle this kind of thing.
– ASH
Feb 22 '20 at 15:20
• Life is too short for manual looping, use vectorized operations or optimized functions. Feb 22 '20 at 17:13