Search code examples
pythonpython-2.7nlpnltkwordnet

with NLTK, How can I generate different form of word, when a certain word is given?


For example, Suppose the word "happy" is given, I want to generate other forms of happy such as happiness, happily... etc.

I have read some other previous questions on Stackoverflow and NLTK references. However, there are only POS tagging, morph just like identifying the grammatical form of certain words within sentences, not generating a list of different words. Is there anyone who bumped into similar issues? Thank you.


Solution

  • This type of information is included in the Lemma class of NLTK's WordNet implementation. Specifically, it's found in Lemma.derivationally_related_forms().

    Here's an example script for finding all possible derivation forms of "happy":

    from nltk.corpus import wordnet as wn
    
    forms = set() #We'll store the derivational forms in a set to eliminate duplicates
    for happy_lemma in wn.lemmas("happy"): #for each "happy" lemma in WordNet
        forms.add(happy_lemma.name()) #add the lemma itself
        for related_lemma in happy_lemma.derivationally_related_forms(): #for each related lemma
            forms.add(related_lemma.name()) #add the related lemma
    

    Unfortunately, the information in WordNet is not complete. The above script finds "happy" and "happiness" but it fails to find "happily", even though there are multiple "happily" lemmas.