-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflaskapp.py
More file actions
40 lines (36 loc) · 2.65 KB
/
flaskapp.py
File metadata and controls
40 lines (36 loc) · 2.65 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
from flask import Flask
from flask import request
from nltk.corpus import wordnet
from nltk.corpus import stopwords
from nltk.tokenize import RegexpTokenizer
import random
app = Flask(__name__)
tokenizer = RegexpTokenizer(r'\w+')
@app.route("/word", methods=["GET"])
def findWord():
word = request.args.get('word')
while True:
try:
synonyms = []
syns = wordnet.synsets(word)
random_syns = random.choice(syns)
defined = random_syns.definition()
for syn in wordnet.synsets(word):
for l in syn.lemmas():
if l.name().lower() != word.lower() and len(l.name())>1 and len(l.name())<20:
synonyms.append(l.name())
if len(synonyms) == 0:
stop_words = set(stopwords.words("english"))
words = tokenizer.tokenize(defined)
filtered_definition = [w for w in words if not w in stop_words]
synonyms.append(random.choice(filtered_definition))
result_list = list(dict.fromkeys(synonyms))
result = random.choice(result_list)
final_result = result.replace("_", " ")
#return(str(final_result))
myhtml = "<head><link rel='stylesheet' type='text/css' title='regular' href='styles1.css' /><link rel='alternate stylesheet' type='text/css' title='sunset' href='sunset.css'/></head><form action='http://ec2-18-224-207-1.us-east-2.compute.amazonaws.com/word' method='get' id='form-altered' style='text-align: center;'><input type='text' name='word' id='word' style='width: 20vw; height: 6vh; padding: 3px; font-size: 2em; margin: 2px;' /><input type='submit' id='submit-form' style='cursor: pointer; padding: 5px; font-size: 1.2em;' /><br><br><label style='font-size: 2em; font-family: Georgia, 'Times New Roman', Times, serif;'>"+str(final_result)+"</label></form>"
return myhtml
except:
myhtml = "<head><link rel='stylesheet' type='text/css' title='regular' href='styles1.css' /><link rel='alternate stylesheet' type='text/css' title='sunset' href='sunset.css'/></head><form action='http://ec2-18-224-207-1.us-east-2.compute.amazonaws.com/word' method='get' id='form-altered' style='text-align: center;'><input type='text' name='word' id='word' style='width: 20vw; height: 6vh; padding: 3px; font-size: 2em; margin: 2px;' /><input type='submit' id='submit-form' style='cursor: pointer; padding: 5px; font-size: 1.2em;' /><br><br><label style='font-size: 2em; font-family: Georgia, 'Times New Roman', Times, serif;'>" + str("Try again!")+ "</label></form>"
return(myhtml)
app.run(debug=True, host="0.0.0.0", port=80)