-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapplication.py
115 lines (102 loc) · 3.46 KB
/
application.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
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
110
111
112
113
114
115
import os
import re
import json
from flask import Flask, jsonify, render_template, request, url_for
from flask_jsglue import JSGlue
from random import *
from nltk.corpus import wordnet as wn
from nltk.tokenize import word_tokenize
# from random import shuffle
from math import ceil
from cs50 import SQL
# configure application
app = Flask(__name__)
JSGlue(app)
# ensure responses aren't cached
if app.config["DEBUG"]:
@app.after_request
def after_request(response):
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Expires"] = 0
response.headers["Pragma"] = "no-cache"
return response
# configure CS50 Library to use SQLite database
db = SQL("sqlite:///doodles.db")
@app.route("/")
def index():
return render_template("index.html")
@app.route("/basic")
def basic():
return render_template("basic.html")
@app.route("/customize")
def doodle():
return render_template("customize.html")
@app.route("/multiple")
def multiple():
return render_template("multiple.html")
@app.route("/data")
def data():
category = request.args.get('categories')
return theme(json.loads(category))
@app.route("/categories")
def categories():
all_categories = []
file = open("categories.txt", "r")
data = file.read();
i = 0;
j = 0;
category = ""
while i<len(data):
if(data[i] == '\n'):
all_categories.append(category)
category = ""
j = j+1
else:
category += data[i]
i = i+1
file.close()
return jsonify(all_categories);
def theme(categories):
category_size = ceil(1200/len(categories))
data = []
for category in categories:
with open('./files/json/' + category + '.json') as json_data:
d = json.load(json_data)
for j in range(0, category_size):
data.append(d[randint(0, len(d)-1)])
shuffle(data)
return jsonify(data)
@app.route("/suggest")
def suggest():
q = request.args.get('q') + '%'
suggestions = db.execute("SELECT name FROM categories WHERE name LIKE :q", q = q)
# if you get less than 5 suggestions, check the words in the dictionary for similar suggestions
# check the synonyms of these matches to see if they are the synonyms of any categories
# store these categories in a list and append them to suggestions if they are not already present
# flag = 0 # if flag is 1, it indicates that synonym suggestions have been searched for already
# if(len(suggestions) < 5 and flag == 0):
# # temp = r"'^' + request.args.get('q') + '[a-z]*'"
# flag = 1
# temp = r"^{}[a-z]*".format(request.args.get('q'))
# pattern = re.compile(temp)
# file = open("nouns", "r")
# nouns = word_tokenize(file.read())
# file.close()
# matches = []
# for noun in nouns:
# if pattern.match(noun):
# matches.append(noun)
# file = open("categories.txt", "r")
# categories = word_tokenize(file.read())
# file.close()
# suggests = set()
# for match in matches:
# synonyms = set()
# for synset in wn.synsets(match):
# for lemma in synset.lemmas():
# synonyms.add(lemma.name())
# for synonym in synonyms:
# if synonym in categories:
# suggests.add(synonym)
# suggestions.extend(suggests)
return jsonify(suggestions)