Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 8, 2021 04:02 pm GMT

Dictionary using api calls in python (with audio pronunciation)

Dictionary is defined by Merriam-Webster as a reference book listing alphabetically the words of one language and showing their meanings or translations in another language. Using python Im creating a real-time dictionary with pronunciation.

Below is the concept:

  1. User API calls to pull the data. (Im using Oxford Dictionaries - here) in this example.
  2. Understand the data that has been pulled in , list or dict.
  3. Understand the type of data we need and extract it.
  4. Create a UI screen with buttons like Get Meaning, to fetch the meaning of the word, Pronunciation , to play sound and Clear, to erase the content and make room to request for new

Dependencies:

  1. Using Python 3.9 (Latest version) -download here
  2. Use Tkinter for GUI (pip install tkinter)
  3. Use requests for pull data via API call (pip install requests
  4. Use JSON for parsing, slicing and dicing api data in JSON format ( pip install JSON)
  5. User playsound for playing sound (.mp3 in our case) (pip install playsound)

Final Output

When application loads

Showing the meaning of the word
With Meaning

Sunny side up
Output in Spanish (language_code = "es")
Spanish
Output in French (language_code = "fr")
French
List of Languages Supported -here
Code Snippet

# Constants appid  = "Your AppID"appkey  = "Your Key"headers = {'app_id': app_id, 'app_key': app_key}language_code = "en-us" # Change language if you want to use a different once # Pull data via URL url = "https://od-  api.oxforddictionaries.com/api/v2/entries/"  + language_code + "/" + getword.lower()r_status = requests.get(url, headers=headers)# This checks if the api is live or not <Response [200]>  print(r_status)# Additional varibles for readabilityr_mean = r_statusr_audio = r_mean

Buttons

# Create a button widget    # The get_word()     b1 = Button(window,text="Get Meaning",command=get_word)    b1.grid(row=0,column=2)   # The delete function is called when the button is pushed   b2 = Button(window,text="Clear",command=delete)   b2.grid(row=1,column=2)   # The get_audio function is called when the button is    pushed   b3 =Button(window,text="Pronunciations",command=get_audio)   b3.grid(row=4,column=1)  # Create four empty text boxes t1  t1 = Text(window,height=2,width=45,font = ("Times New Roman",12) )  t1.grid(row=3,column=1, pady = 5, padx = 2.5)  t1.config(wrap=WORD)

Functions

# Logic to pull meaning of the word         def get_word():         #Multiple for loops increases the running time.         for result in mean_json['results'][0]['lexicalEntries'][0]['entries']:    for sense in result['senses']:        mean_list.append(sense['definitions'][0])   for i in mean_list:        return (i) # Logic to pull audio file    def get_audio():    for result in audio_json['results'][0]['lexicalEntries'][0]['entries']:        for pronunciations in result['pronunciations']:            audio_list.append(pronunciations)

Code will soon be added to Github.
Note: Thanks to Tim Wildsmith for this photo via @unsplash


Original Link: https://dev.to/swapanroy/dictionary-using-api-calls-in-python-with-audio-pronunciation-3hjp

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To