7

Is there a way to get an adjective corresponding to a given adverb in NLTK or other python library. For example, for the adverb "terribly", I need to get "terrible". Thanks.

rok
  • 9,403
  • 17
  • 70
  • 126
  • 1
    Try taking a look at this post: [Convert words between verb/noun/adjective forms][1] [1]: http://stackoverflow.com/questions/14489309/convert-words-between-verb-noun-adjective-forms/16752477#16752477 – bogs Jun 22 '13 at 07:02
  • @George-Bogdan Ivanov. Thanks, I tried it, but it didn't work – rok Jun 23 '13 at 11:33

2 Answers2

13

There is a relation in wordnet that connects the adjectives to adverbs and vice versa.

>>> from itertools import chain
>>> from nltk.corpus import wordnet as wn
>>> from difflib import get_close_matches as gcm
>>> possible_adjectives = [k.name for k in chain(*[j.pertainyms() for j in chain(*[i.lemmas for i in wn.synsets('terribly')])])]
['terrible', 'atrocious', 'awful', 'rotten']
>>> gcm('terribly',possible_adjectives)
['terrible']

A more human readable way to computepossible_adjective is as followed:

possible_adj = []
for ss in wn.synsets('terribly'):
  for lemmas in ss.lemmas: # all possible lemmas.
    for lemma in lemmas: 
      for ps in lemma.pertainyms(): # all possible pertainyms.
        for p in ps:
          for ln in p.name: # all possible lemma names.
            possible_adj.append(ln)

EDIT: In the newer version of NLTK:

possible_adj = []
for ss in wn.synsets('terribly'):
  for lemmas in ss.lemmas(): # all possible lemmas
      for ps in lemmas.pertainyms(): # all possible pertainyms
          possible_adj.append(ps.name())
alvas
  • 115,346
  • 109
  • 446
  • 738
1

As MKoosej mentioned, nltk's lemmas is no longer an attribute but a method. I also made a little simplification to get the most possible word. Hope someone else can use it also:

wordtoinv = 'unduly'
s = []
winner = ""
for ss in wn.synsets(wordtoinv):
    for lemmas in ss.lemmas(): # all possible lemmas.
        s.append(lemmas)

for pers in s:
    posword = pers.pertainyms()[0].name()
    if posword[0:3] == wordtoinv[0:3]:
        winner = posword
        break

print winner # undue
Jani Bela
  • 1,660
  • 4
  • 27
  • 50
  • please test the code before posting. This fails for the word that op asked: `terribly` – Kevad Oct 22 '18 at 23:26
  • 1
    Should add "if len(pers.pertainyms())==0: continue". Is there a way to get from adjective to adverb? – Nathan B Sep 24 '19 at 14:37