Skip to content

Instantly share code, notes, and snippets.

@mkakh
Last active June 13, 2023 02:12
Show Gist options
  • Save mkakh/397ee5a40b95d419c70e6c76a55cf488 to your computer and use it in GitHub Desktop.
Save mkakh/397ee5a40b95d419c70e6c76a55cf488 to your computer and use it in GitHub Desktop.
generated by Bing AI
#!/usr/bin/env python3
import requests
import json
import re
api_key = "YOUR_API_KEY"
def clean_text(text):
text = re.sub(r'{[a-z]+}', '', text)
text = re.sub(r'{/[a-z]+}', '', text)
return text
try:
while True:
word = input("Enter a word: ")
url = f"https://www.dictionaryapi.com/api/v3/references/collegiate/json/{word}?key={api_key}"
response = requests.get(url)
data = json.loads(response.text)
if isinstance(data[0], dict):
i = 1
for entry in data:
if 'shortdef' in entry and len(entry['shortdef']) > 0:
print(f"[{i}]")
print(f"{clean_text(entry['shortdef'][0])}")
if 'hwi' in entry and 'prs' in entry['hwi']:
print(f"IPA: /{entry['hwi']['prs'][0]['mw']}/")
if 'def' in entry:
usage_displayed = False
for sense in entry['def'][0]['sseq']:
if 'dt' in sense[0][1]:
for dt in sense[0][1]['dt']:
if dt[0] == 'vis':
if not usage_displayed:
print("Usage:")
usage_displayed = True
for vis in dt[1]:
print(f"- {clean_text(vis['t'])}")
print()
i += 1
else:
print("Did you mean:")
for suggestion in data:
print(f"- {suggestion}")
print()
except (KeyboardInterrupt, EOFError):
print('\nExiting...')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment