0
$\begingroup$

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
$\endgroup$
0
$\begingroup$

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


$\endgroup$
4
  • $\begingroup$ 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? $\endgroup$ – ASH Feb 22 '20 at 13:52
  • $\begingroup$ pandas.pydata.org/pandas-docs/stable/reference/api/… $\endgroup$ – Brian Spiering Feb 22 '20 at 14:48
  • $\begingroup$ 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. $\endgroup$ – ASH Feb 22 '20 at 15:20
  • 1
    $\begingroup$ Life is too short for manual looping, use vectorized operations or optimized functions. $\endgroup$ – Brian Spiering Feb 22 '20 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.