|
| 1 | +# Access Merriam-Webster's Collegiate Thesaurus API with GUI |
| 2 | + |
| 3 | +import requests |
| 4 | +from tkinter import * |
| 5 | +from tkinter import scrolledtext |
| 6 | + |
| 7 | +def look_up(look_me_up): |
| 8 | + if look_me_up == '': |
| 9 | + return 'Entry is blank!' |
| 10 | + else: |
| 11 | + my_API_key = '7c21d5d1-a199-4a82-a9f3-1c7a16639ae9' |
| 12 | + endpoint = 'https://www.dictionaryapi.com/api/v3/references/thesaurus/json/' + look_me_up + '?key=' + my_API_key |
| 13 | + response = requests.get(endpoint) |
| 14 | + |
| 15 | + status_code = response.status_code |
| 16 | + if status_code == 200: |
| 17 | + return_str = '' |
| 18 | + response_list = response.json() |
| 19 | + entry_num = 0 |
| 20 | + for thes_entry_obj in response_list: |
| 21 | + entry_num += 1 |
| 22 | + return_str = return_str + look_me_up + ': entry no. ' + str(entry_num) + '\n' + '\n' |
| 23 | + try: |
| 24 | + return_str = return_str + 'Part of speech: ' + thes_entry_obj['fl'] + '\n' + '\n' |
| 25 | + except: |
| 26 | + return_str = 'Word not found!' |
| 27 | + break |
| 28 | + else: |
| 29 | + return_str = return_str + 'Definition: ' + thes_entry_obj['shortdef'][0] + '\n' + '\n' |
| 30 | + |
| 31 | + syn_list = thes_entry_obj['meta']['syns'][0] |
| 32 | + if len(syn_list) > 0: |
| 33 | + return_str = return_str + 'Synonyms: ' + '\n' |
| 34 | + for syn in syn_list: |
| 35 | + return_str = return_str + ' ' * 4 + syn + '\n' |
| 36 | + return_str = return_str + '\n' |
| 37 | + |
| 38 | + if 'rel_list' in thes_entry_obj['def'][0]['sseq'][0][0][1]: |
| 39 | + related_list = thes_entry_obj['def'][0]['sseq'][0][0][1]['rel_list'][0] |
| 40 | + if len(related_list) > 0: |
| 41 | + return_str = return_str + 'Other related words: ' + '\n' |
| 42 | + for rw in related_list: |
| 43 | + return_str = return_str + ' ' * 4 + rw['wd'] + '\n' |
| 44 | + |
| 45 | + return_str = return_str + '----------------------------------------' + '\n' |
| 46 | + return return_str |
| 47 | + else: |
| 48 | + return 'Request failed with status code: ' + str(status_code) |
| 49 | + |
| 50 | +def search(): |
| 51 | + look_me_up = entry.get() # Get what the user eneterd into the box |
| 52 | + results = look_up(look_me_up) # Do the lookup with the API and return results |
| 53 | + textw = scrolledtext.ScrolledText(main,width=70,height=30) |
| 54 | + textw.grid(column=1, row=2,sticky=W) |
| 55 | + textw.config(background="light grey", foreground="black", |
| 56 | + font='times 18', wrap='word') |
| 57 | + textw.insert(END, results) |
| 58 | + |
| 59 | +def quit(): |
| 60 | + main.destroy() |
| 61 | + |
| 62 | +main = Tk() |
| 63 | +main.title("Merriam-Webster's Collegiate Thesaurus API") |
| 64 | +main.geometry('900x750') |
| 65 | +main.configure(background='ivory3') |
| 66 | + |
| 67 | +entry = StringVar() |
| 68 | + |
| 69 | +lblSearch = Label(main, text = 'Word to look up:').grid(row = 0, column = 0) |
| 70 | +entSearch = Entry(main, textvariable = entry, width = 25).grid(row = 0, |
| 71 | + column = 1, sticky=W) |
| 72 | +btn = Button(main, text = 'Look it up!', bg='ivory2', width = 10, |
| 73 | + command = search).grid(row = 0, column = 10, sticky=E) |
| 74 | +quit_btn = Button(main, text = 'Quit', bg='ivory2', width = 10, command = quit).grid(row = 3, column = 10, sticky=E) |
| 75 | + |
| 76 | +main.bind('<Return>', lambda event=None: search()) |
| 77 | +main.mainloop() |
0 commit comments