TransWikia.com

How can I find synonyms and antonyms for a word?

Data Science Asked on September 4, 2021

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.
false advertising.
a bit loose compared birkenstocks size. still like though. very comfy

One Answer

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.
false advertising.
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():
            tokens_syns[token].add(lemma.name()) # Add the synonyms
            if lemma.antonyms():    # Check if antonyms are available
                tokens_ant[token].add(lemma.antonyms()[0].name()) # Add antonyms


# 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


Answered by Brian Spiering on September 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP