-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtraWordinary.py
More file actions
109 lines (88 loc) · 3.38 KB
/
ExtraWordinary.py
File metadata and controls
109 lines (88 loc) · 3.38 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from flask import Flask, request, render_template, redirect, url_for
import requests
NOUN = 'n'
VERB = 'v'
ADJECTIVE = 'adj'
ADVERB = 'adv'
def get_def_api(search_word):
resp = requests.get(f'https://api.datamuse.com/words?sp={search_word}&md=d')
resp_json = resp.json()
placeholders = {}
nouns_l = []
verbs_l = []
adjectives_l = []
adverbs_l = []
if 'defs' in resp_json[0]:
for i in range(len(resp_json[0]['defs'])):
part_of_speech, defi = resp_json[0]['defs'][i].split('\t')
if part_of_speech == NOUN:
placeholders[NOUN] = 'noun'
nouns_l.append(defi)
if part_of_speech == VERB:
placeholders[VERB] = 'verb'
verbs_l.append(defi)
if part_of_speech == ADJECTIVE:
placeholders[ADJECTIVE] = 'adjective'
adjectives_l.append(defi)
if part_of_speech == ADVERB:
placeholders[ADVERB] = 'adverb'
adverbs_l.append(defi)
placeholders.update({'nouns': nouns_l, 'verbs': verbs_l, 'adjectives': adjectives_l, 'adverbs': adverbs_l})
return placeholders
def get_ryme_api(search_word):
resp = requests.get(f'https://api.datamuse.com/words?rel_rhy={search_word}')
resp_json = resp.json()
rhymes_l = []
for i in range(len(resp_json)):
rhymes_l.append(resp_json[i]['word'])
return {'rhymes': rhymes_l}
def get_syn_api(search_word):
resp = requests.get(f'https://api.datamuse.com/words?rel_syn={search_word}')
resp_json = resp.json()
syns_l = []
for i in range(len(resp_json)):
syns_l.append(resp_json[i]['word'])
return {'syns': syns_l}
def get_ant_api(search_word):
resp = requests.get(f'https://api.datamuse.com/words?rel_ant={search_word}')
resp_json = resp.json()
ants_l = []
for i in range(len(resp_json)):
ants_l.append(resp_json[i]['word'])
return {'ants': ants_l}
def get_freq_api(search_word):
resp = requests.get(f'https://api.datamuse.com/words?sp={search_word}&md=f')
resp_json = resp.json()
placeholder = {}
_, freq_str = resp_json[0]['tags'][0].split(':')
placeholder['freq'] = round(float(freq_str) / (10 ** 4), 10)
return placeholder
RESULTS = [get_def_api, get_ryme_api, get_syn_api, get_ant_api, get_freq_api]
app = Flask(__name__)
@app.route('/results/<word>', methods=["POST", "GET"])
def results(word):
if request.method == "POST":
search_word = request.form["search"]
return redirect(url_for("results", word=search_word))
else:
search_word = word.lower()
overall = {"s_word": search_word}
for func in RESULTS:
overall.update(func(search_word))
return render_template("results.html", **overall)
@app.route('/', methods=["POST", "GET"])
@app.route('/home/', methods=["POST", "GET"])
def home():
if request.method == "POST":
search_word = request.form["search"]
return redirect(url_for("results", word=search_word))
else:
return render_template("index.html")
@app.route('/admin/')
def admin():
return redirect(url_for("home"))
@app.route('/results/')
def results_example():
return redirect(url_for("results", word="example"))
if __name__ == "__main__":
app.run(debug=True)