Reputation: 69
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!
Upvotes: 0
Views: 111
Reputation: 104
If you want to continue using jsonpath_ng
, a slight modification to one of your code blocks will fix your problem. The issue at hand is that one POS may be associated with more than one definition. Your current code does not account for that. However it is certainly possible to achieve what you are trying to do.
pos = []
query_pos = jsonpath_ng.parse('[*].meanings[*].partOfSpeech[*]')
for match in query_pos.find(data):
# first modification
number_of_defs = len(match.full_path.left.left.child(jsonpath_ng.Fields('definitions')).find(data)[0].value)
# second change
pos.extend(number_of_defs * [(json.dumps(match.value)).strip('"')])
Upvotes: 1
Reputation: 10799
Not exactly sure what you're trying to achieve with itertools.zip_longest
. I've also never used the jsonpath_ng
module, so that may explain why I don't understand what you're trying to do. Here is the simplest thing I could come up with, which prints all definitions of all meanings of all entries, though I'm not sure if that's what you're after. For each definition it prints, it also prints the corresponding part of speech. It just prints all these things in the order in which they appear in the JSON:
def main():
import requests
word = "zoom"
url = "https://api.dictionaryapi.dev/api/v2/entries/en_US/{}".format(word)
response = requests.get(url)
entries = response.json()
for entry in entries:
for meaning in entry["meanings"]:
for definition in meaning["definitions"]:
print("[{}] - {}".format(meaning["partOfSpeech"], definition["definition"]))
return 0
if __name__ == "__main__":
import sys
sys.exit(main())
Output:
[intransitive verb] - Move or travel very quickly.
[intransitive verb] - (of a camera or user) change smoothly from a long shot to a close-up or vice versa.
[noun] - A camera shot that changes smoothly from a long shot to a close-up or vice versa.
[exclamation] - Used to express sudden fast movement.
[intransitive verb] - Communicate with someone over the internet, typically by video chat, using the software application Zoom.
>>>
Maybe you can describe (and show some examples of) what you want your expected output to look like, and what your actual output looks like.
Upvotes: 2