Created
August 27, 2017 14:28
-
-
Save victor-iyi/8a8aefb077d0b09964a30cf872d2b9f6 to your computer and use it in GitHub Desktop.
A WordNet dictionary.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from nltk.corpus import wordnet | |
| class Dictionary: | |
| def __init__(self, word): | |
| Dictionary.word = word | |
| @staticmethod | |
| def definition(): | |
| syns = Dictionary.synsets() | |
| definitions = [] | |
| for syn in syns: | |
| syn_name = syn.name() | |
| syn_definition = syn.definition() | |
| definitions.append(syn_name+' -> ' + syn_definition) | |
| return definitions | |
| # returns all possible synsets | |
| @staticmethod | |
| def synsets(): | |
| return [syn for syn in wordnet.synsets(Dictionary.word)] | |
| @staticmethod | |
| def synset_len(): | |
| return len(Dictionary.synsets()) | |
| @staticmethod | |
| def synonyms(): | |
| return {l.name().replace('_', ' ') for syn in Dictionary.synsets() for l in syn.lemmas()} | |
| @staticmethod | |
| def antonyms(): | |
| return {l.antonyms()[0].name().replace('_', ' ') for syn in Dictionary.synsets() for l in syn.lemmas() if l.antonyms()} | |
| @staticmethod | |
| def examples(): | |
| syns = Dictionary.synsets() | |
| return [syn.examples() for syn in syns] | |
| if __name__ == '__main__': | |
| print('Welcome to the WORDNET') | |
| print('( the largest collection of dictionary words and meanings)') | |
| word = input('Enter a word you would like to lookup --> ') | |
| dic = Dictionary(word) | |
| print('Word:', word) | |
| print('\nSynset length:', dic.synset_len()) | |
| print('\n__Definitions__') | |
| definitions = dic.definition() | |
| for definition in definitions: | |
| print(definition) | |
| print('\n__Examples__') | |
| for e in [ex for examples in dic.examples() for ex in examples]: | |
| print(e) | |
| print('\n__Synonyms__') | |
| for synonym in Dictionary.synonyms(): | |
| print(synonym) | |
| print('\n__Antonyms__') | |
| for antonyms in Dictionary.antonyms(): | |
| print(antonyms) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment