I am trying to create a program that returns the definitions of words along with their part of speech. This is my code:
import requests
import jsonpath_ng
import json, itertools
URL = 'https://api.dictionaryapi.dev/api/v2/entries/en_US/zoom'
data = requests.get(URL).json()
#word
query_word = jsonpath_ng.parse('[0].word[*]')
for match in query_word.find(data):
word = (json.dumps(match.value)).strip('"')
#pronunciation (text plus audio)
query_pronun = jsonpath_ng.parse('[0].phonetics[*].text[*]')
for match in query_pronun.find(data):
pronun = json.dumps(match.value)
query_audio = jsonpath_ng.parse('[0].phonetics[*].audio[*]')
for match in query_audio.find(data):
audio_link = (json.dumps(match.value)).strip('"')
#meanings (part of speech, definition, examples)
defs = []
query_def = jsonpath_ng.parse('[*].meanings[*].definitions[*].definition')
for match in query_def.find(data):
defs.append((json.dumps(match.value)).strip('"'))
pos = []
query_pos = jsonpath_ng.parse('[*].meanings[*].partOfSpeech[*]')
for match in query_pos.find(data):
pos.append((json.dumps(match.value)).strip('"'))
exs = []
query_exs = jsonpath_ng.parse('[*].meanings[*].definitions[*].example')
for match in query_exs.find(data):
exs.append((json.dumps(match.value)).strip('"'))
print(word)
print('==========')
print(f'{pronun} || Audio: {audio_link}')
for part, means, ex in itertools.zip_longest(pos, defs, exs):
if part != None:
print(f'**{part}**')
print('-=-=-=-=-=-=-=-')
print(means)
print('-=-=-=-=-=-=-=-')
if ex != None:
print(ex)
print('=============')
However, the problem that I'm encountering is that the first instance of the intransitive verb has two meanings, them being:
Move or travel very quickly.
and
(of a camera or user) change smoothly from a long shot to a close-up or vice versa.
When I'm trying to print out the definitions along with their part of speech using "itertools.zip_longest()", it returns the second definition as a noun instead of an intransitive verb. So, to avoid this, can I figure out if the dictionary contains other definitions so that I can print its correct part of speech instead of the part of speech for the next definition?
This is the result I'm getting:
zoom
==========
"/zum/" || Audio: https://lex-audio.useremarkable.com/mp3/zoom_us_1.mp3
**intransitive verb**
-=-=-=-=-=-=-=-
Move or travel very quickly.
-=-=-=-=-=-=-=-
he jumped into his car and zoomed off
=============
**noun**
-=-=-=-=-=-=-=-
(of a camera or user) change smoothly from a long shot to a close-up or vice versa.
-=-=-=-=-=-=-=-
the camera zoomed in for a close-up of his face
=============
**exclamation**
-=-=-=-=-=-=-=-
A camera shot that changes smoothly from a long shot to a close-up or vice versa.
-=-=-=-=-=-=-=-
the zoom button
=============
**intransitive verb**
-=-=-=-=-=-=-=-
Used to express sudden fast movement.
-=-=-=-=-=-=-=-
then suddenly, zoom!, he's off
=============
-=-=-=-=-=-=-=-
Communicate with someone over the internet, typically by video chat, using the software application Zoom.
-=-=-=-=-=-=-=-
most of us have probably Zoomed, especially since the coronavirus pandemic has forced us to stay home
=============
I want this:
zoom
==========
"/zum/" || Audio: https://lex-audio.useremarkable.com/mp3/zoom_us_1.mp3
**intransitive verb**
-=-=-=-=-=-=-=-
Move or travel very quickly.
-=-=-=-=-=-=-=-
he jumped into his car and zoomed off
=============
-=-=-=-=-=-=-=-
(of a camera or user) change smoothly from a long shot to a close-up or vice versa.
-=-=-=-=-=-=-=-
the camera zoomed in for a close-up of his face
=============
**noun**
-=-=-=-=-=-=-=-
A camera shot that changes smoothly from a long shot to a close-up or vice versa.
-=-=-=-=-=-=-=-
the zoom button
=============
**exclamation**
-=-=-=-=-=-=-=-
Used to express sudden fast movement.
-=-=-=-=-=-=-=-
then suddenly, zoom!, he's off
=============
**intransitive verb**
-=-=-=-=-=-=-=-
Communicate with someone over the internet, typically by video chat, using the software application Zoom.
-=-=-=-=-=-=-=-
most of us have probably Zoomed, especially since the coronavirus pandemic has forced us to stay home
=============
Any help is much appreciated!