8

I need to list all the forms (verb , noun, comparative, superlative, adjective, and adverb) of a word using NLTK library in python . For example if I have the word "write" the result should be: wrote writing writer written etc..., also if the word can be written in comparative and superlative form e.g; cold then colder, coldest. And quick : quickly etc. Is there a way to do that?

ANjell
  • 171
  • 1
  • 3
  • 8

1 Answers1

3

Hi this is my late answer. Hope this still help. I just improve it a little and some small debugging to fit new nltk version. The original code can be found in George-Bogdan Ivanov's answer here Convert words between verb/noun/adjective forms

from nltk.corpus import wordnet as wn

def morphify(word,org_pos,target_pos):
    """ morph a word """
    synsets = wn.synsets(word, pos=org_pos)

    # Word not found
    if not synsets:
        return []

    # Get all  lemmas of the word
    lemmas = [l for s in synsets \
                   for l in s.lemmas() if s.name().split('.')[1] == org_pos]

    # Get related forms
    derivationally_related_forms = [(l, l.derivationally_related_forms()) \
                                    for l in    lemmas]

    # filter only the targeted pos
    related_lemmas = [l for drf in derivationally_related_forms \
                           for l in drf[1] if l.synset().name().split('.')[1] == target_pos]

    # Extract the words from the lemmas
    words = [l.name() for l in related_lemmas]
    len_words = len(words)

    # Build the result in the form of a list containing tuples (word, probability)
    result = [(w, float(words.count(w))/len_words) for w in set(words)]
    result.sort(key=lambda w: -w[1])

    # return all the possibilities sorted by probability
    return result

print morphify('sadness','n','v')
Community
  • 1
  • 1
Duc Anh
  • 47
  • 4
  • 1
    Experimentally, this function seems not to work. The example you give maps "sadness" to "sorrow" (which apparently *can* be used as a verb, though I've never encountered such usage) but misses the more obvious and directly-related verb "sadden". Converting the verb "die" to an adjective gives "breakable" but not "dead" or "dying", converting "explode" to an adjective gives no results (while "explosive" is obvious), and so on. And even if it did work, it wouldn't quite be an answer to the question asked. – Mark Amery Feb 09 '17 at 11:36
  • 1
    This function does something interesting but does not answer the question. – jxmorris12 Aug 02 '19 at 16:28