I imported the PyDictionary library for a project I was beginning in Python, but no matter what word I use, the meaning function returns an empty dict. Here is my script so far:
# Import Libraries
from tkinter import *
from gtts import gTTS
from PyDictionary import PyDictionary
import random
# Create Functions
def chooserandom(list: list):
return list[random.randrange(0, len(list))]
# Create Variables
words: list[str] = "".join(list(open("word-lists\mainlist.txt", "r"))).split("n")
dictionary = PyDictionary()
# Main Loop
word: str = chooserandom(words)
definition = dictionary.meaning(word)
print(word) # amber
print(definition) # {}
"word-lists\mainlist.txt"
is a txt file of English words.
print(word)
works properly and prints a random word (as a string) from the file (e.g. "amber"
).
print(definition)
always prints {}
.
Any help or advice is greatly appreciated!
7
Thanks to esqew for informing me that PyDictionary probably no longer works.
I found another library that does work; here is the new code:
# Import Libraries
from tkinter import *
from gtts import gTTS
from freedictionaryapi.clients.sync_client import DictionaryApiClient
import random
# Create Functions
def getdefinitions(word: str):
with DictionaryApiClient() as client:
parser = client.fetch_parser(word)
definitions: dict[str:str] = {}
for pair in parser.meanings:
definitions[pair.part_of_speech] = pair.definitions[0].definition
return definitions
# Create Variables
words: list[str] = "".join(list(open("word-lists\mainlist.txt", "r"))).split("n")
# Main Loop
word: str = random.choice(words)
definitions: dict[str:str] = getdefinitions(word)
print(word) # amber
print(definitions) # {'noun': 'Ambergris, the waxy product of the sperm whale.', 'verb': 'To perfume or flavour with ambergris.', 'adjective': 'Of a brownish yellow colour, like that of most amber.'}
Just make sure you have run both commands (Windows):
pip install python-freeDictionaryAPI
pip install httpx
Thanks for the help!