Skip to content

Instantly share code, notes, and snippets.

@brettkromkamp
Last active July 5, 2021 11:20
Show Gist options
  • Save brettkromkamp/e06d85f9c20b52f6aa6e5220079f0ef1 to your computer and use it in GitHub Desktop.
Save brettkromkamp/e06d85f9c20b52f6aa6e5220079f0ef1 to your computer and use it in GitHub Desktop.
pip install --upgrade nltk
python -m nltk.downloader all
ptipython
Python 3.9.5 (default, May 4 2021, 03:33:11)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.25.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: from nltk.corpus import wordnet as wn
In [2]: def get_parent_classes(synset):
...: while True:
...: try:
...: synset = synset.hypernyms()[-1]
...: print(synset)
...: except IndexError:
...: break
In [3]: bible = wn.synset("bible.n.01")
In [4]: bible.definition()
Out[4]: 'the sacred writings of the Christian religions'
In [5]: get_parent_classes(bible)
Synset('sacred_text.n.01')
Synset('writing.n.02')
Synset('written_communication.n.01')
Synset('communication.n.02')
Synset('abstraction.n.06')
Synset('entity.n.01')
In [6]:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment