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 I’m creating a real-time dictionary with pronunciation.
Below is the concept:
- User API calls to pull the data. (I’m using Oxford Dictionaries – here) in this example.
- Understand the data that has been pulled in – , list or dict.
- Understand the type of data we need and extract it.
- 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:
- Using Python 3.9 (Latest version) -download here
- Use Tkinter for GUI (pip install tkinter)
- Use requests for pull data via API call (pip install requests
- Use JSON for parsing, slicing and dicing api data in JSON format ( pip install JSON)
- User playsound for playing sound (.mp3 in our case) (pip install playsound)
Final Output
Showing the meaning of the word
Output in Spanish (language_code = “es”)
Output in French (language_code = “fr”)
List of Languages Supported –here
Code Snippet
# Constantsappid = "Your AppID"appkey = "Your Key"headers = {'app_id': app_id, 'app_key': app_key}language_code = "en-us" # Change language if you want to usea different once# Pull data via URLurl = "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# 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 readability r_mean = r_status r_audio = r_mean# 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 readability r_mean = r_status r_audio = r_mean
Enter fullscreen mode Exit fullscreen mode
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 pushedb2 = Button(window,text="Clear",command=delete)b2.grid(row=1,column=2)# The get_audio function is called when the button ispushedb3 =Button(window,text="Pronunciations",command=get_audio)b3.grid(row=4,column=1)# Create four empty text boxes t1t1 = 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)# 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)# 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)
Enter fullscreen mode Exit fullscreen mode
Functions
# Logic to pull meaning of the worddef 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 filedef get_audio():for result in audio_json['results'][0]['lexicalEntries'][0]['entries']:for pronunciations in result['pronunciations']:audio_list.append(pronunciations)# 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)# 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)
Enter fullscreen mode Exit fullscreen mode
Code will soon be added to Github.
Note: Thanks to Tim Wildsmith for this photo via @unsplash
原文链接:GUI Dictionary using api calls in python (with pronunciation)
暂无评论内容