-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictionary.py
More file actions
48 lines (39 loc) · 1.51 KB
/
dictionary.py
File metadata and controls
48 lines (39 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#dictionary.py
#Note: run command in terminal first, 'pip install nltk'
import nltk
nltk.download('wordnet')
nltk.download('omw-1.4') # For multilingual wordnet
from nltk.corpus import wordnet
def get_word_info(word):
# Get synsets
synsets = wordnet.synsets(word)
if not synsets:
return {
"definition": "No definition found.",
"synonyms": "No synonyms found.",
"related_words": "No related words found."
}
# Get definitions
definitions = [synset.definition() for synset in synsets]
# Get synonyms
synonyms = set()
for synset in synsets:
synonyms.update(lemma.name() for lemma in synset.lemmas())
# Get related words (hypernyms and hyponyms)
related_words = set()
for synset in synsets:
for hypernym in synset.hypernyms():
related_words.update(lemma.name() for lemma in hypernym.lemmas())
for hyponym in synset.hyponyms():
related_words.update(lemma.name() for lemma in hyponym.lemmas())
return {
"definition": definitions[0] if definitions else "No definition found.",
"synonyms": ', '.join(synonyms) if synonyms else "No synonyms found.",
"related_words": ', '.join(related_words) if related_words else "No related words found."
}
# Example usage
word = input("Enter a word: ")
info = get_word_info(word)
print(f"Definition: {info['definition']}")
print(f"Synonyms: {info['synonyms']}")
print(f"Related Words: {info['related_words']}")