-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflash_card.py
59 lines (41 loc) · 1.5 KB
/
flash_card.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import csv
import random
import requests
def GenerateRequestURL(vocab, mw_api_key):
return f'https://www.dictionaryapi.com/api/v3/references/collegiate/json/{vocab}?key={mw_api_key}'
def ConnectMWDictionary(vocab, mw_api_key):
request_url = GenerateRequestURL(vocab, mw_api_key)
params = {'word': vocab, 'key': mw_api_key}
result = requests.get(url = request_url, params = params)
if result.status_code != 200:
return False
return True, result.json()
def JsonResultParser(json_result):
query_vocab = json_result[0]['meta']['id']
part_of_speech = json_result[0]['fl']
definition = json_result[0]['shortdef'][:]
return query_vocab, part_of_speech, definition
def QueryVocab(vocab, mw_api_key):
status, json_result = ConnectMWDictionary(vocab, mw_api_key)
if status == True:
return True, JsonResultParser(json_result)
return False
vocabs = []
mw_api_key = ''
# Load your Merriam-Webster API key
with open('your_mw_api_key') as mw_api_key_file:
mw_api_key = mw_api_key_file.readline()
with open('vocab_list.csv') as vocab_list_file:
vocab_list_csv = csv.reader(vocab_list_file)
for vocab in vocab_list_csv:
vocabs.append(vocab[0])
random.shuffle(vocabs)
for vocab in vocabs:
status, (q, p, d) = QueryVocab(vocab, mw_api_key)
if status == True:
print(f'Q: {q} ({p}) \t\t Hit enter to display the answer')
input()
print(f'A: {d}', end='\r\n')
else:
print(f'Error: \"{vocabs}\" query FAILED')
print("\n==================================================\n")