10

I want to do the following in Python (I have the NLTK library, but I'm not great with Python, so I've written the following in a weird pseudocode):

from nltk.corpus import wordnet as wn  #Import the WordNet library
for each adjective as adj in wn        #Get all adjectives from the wordnet dictionary
    print adj & antonym                #List all antonyms for each adjective 
once list is complete then export to txt file

This is so I can generate a complete dictionary of antonyms for adjectives. I think it should be doable, but I don't know how to create the Python script. I'd like to do it in Python as that's the NLTK's native language.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sebastian Zeki
  • 6,690
  • 11
  • 60
  • 125
  • 1. from nltk.corpus import wordnet as wn 'Import the wordnet library 2. for each adjective as adj in wn 'Get all adjectives from the wordnet dictionary 3.print adj & antonym 'List all antonyms for each adjective 4.once list is complete then export to txt file – Sebastian Zeki Jun 13 '14 at 06:17
  • it's not that simple to list antonym in wordnet because of the choice that hyper-hyponyms are linked through synsets and antonyms linked by lemma. – alvas Jun 13 '14 at 07:19

2 Answers2

13
from nltk.corpus import wordnet as wn

for i in wn.all_synsets():
    if i.pos() in ['a', 's']: # If synset is adj or satelite-adj.
        for j in i.lemmas(): # Iterating through lemmas for each synset.
            if j.antonyms(): # If adj has antonym.
                # Prints the adj-antonym pair.
                print j.name(), j.antonyms()[0].name()

Note that there will be reverse duplicates.

[out]:

able unable
unable able
abaxial adaxial
adaxial abaxial
acroscopic basiscopic
basiscopic acroscopic
abducent adducent
adducent abducent
nascent dying
dying nascent
abridged unabridged
unabridged abridged
absolute relative
relative absolute
absorbent nonabsorbent
nonabsorbent absorbent
adsorbent nonadsorbent
nonadsorbent adsorbent
absorbable adsorbable
adsorbable absorbable
abstemious gluttonous
gluttonous abstemious
abstract concrete
...
alvas
  • 115,346
  • 109
  • 446
  • 738
  • The code doesnt seem to work. I changed it to get rid of the () after pos and after lemmas that was causing an error. Now I get this error: Traceback (most recent call last): File "", line 5, in TypeError: 'str' object is not callable when I use the following code: from nltk.corpus import wordnet as wn for i in wn.all_synsets(): if i.pos in ['a', 's']: for j in i.lemmas: if j.antonyms(): print j.name(), j.antonyms()[0].name() – Sebastian Zeki Jun 14 '14 at 06:03
  • ok ive sorted it. The code should read:from nltk.corpus import wordnet as wn for i in wn.all_synsets(): if i.pos in ['a', 's']: for j in i.lemmas: if j.antonyms(): print j.name, j.antonyms()[0].name – Sebastian Zeki Jun 14 '14 at 06:43
  • 1
    update your NLTK, the new nltk uses get functions instead of accessing the synset's properties – alvas Jun 14 '14 at 07:26
1

The following function uses WordNet to return a set of adjective-only antonyms for a given word:

from nltk.corpus import wordnet as wn

def antonyms_for(word):
    antonyms = set()
    for ss in wn.synsets(word):
        for lemma in ss.lemmas():
            any_pos_antonyms = [ antonym.name() for antonym in lemma.antonyms() ]
            for antonym in any_pos_antonyms:
                antonym_synsets = wn.synsets(antonym)
                if wn.ADJ not in [ ss.pos() for ss in antonym_synsets ]:
                    continue
                antonyms.add(antonym)
    return antonyms

Usage:

print(antonyms_for("good"))
Desmond Hume
  • 8,037
  • 14
  • 65
  • 112