-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
75 lines (60 loc) · 2.3 KB
/
app.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
# app.py
import os
from flask import Flask, render_template, request
from scripts import parse_file, markov_tokenise, markov_walk
app = Flask(__name__)
#PARSE INDIVIDUAL SCRIPTS AND COLOR CODE THEM FOR OUR OUTPUT
parsed_text = parse_file('Quotes1') #black default
markov = markov_tokenise(parsed_text, '')
parsed_text = parse_file('BrainyQuote') #blue
markov = markov_tokenise(parsed_text, 'blue', markov)
parsed_text = parse_file('Funny') #pink
markov = markov_tokenise(parsed_text, 'pink', markov)
parsed_text = None
@app.route('/')
def index():
"""Return homepage"""
wordlist = []
colored_wordlist = []
words = request.args.get('num')
num_words = int(words) if (words != None and words != "" and int(words)>8) else 8
used_words = 0
look_for_STOP = False
end = False
data = None
phrase = ""
color = ""
while end is False:
if used_words == num_words:
look_for_STOP = True
#Generate word based on needs
if used_words == 0:
data = markov_walk("START START", markov, False)
word = data[0] #grab both words
color = data[1]
used_words += 1 #add one ADDITIONAL here, as we add one each cycle
else:
data = markov_walk(phrase, markov, look_for_STOP)
word = data[0].split(" ")[1]
color = data[1]
phrase = data[0] #store our phrase to be used in the next loop
used_words += 1
if color == 'red':
color = "text-danger"
elif color == 'blue':
color = "text-primary"
elif color == 'pink':
color = "text-info"
else:
color = ""
if word == '888' or word == 'STOP':
end = True
break #if random word doesn't return another connection, 888 is returned to end the chain prematurely
else:
wordlist.append(word)
colored_wordlist.append( [word, color])
#print (f'\nwordlist length = {used_words} looking for stop = {look_for_STOP} end = {end}')
#print (f' phrase = {phrase}, \n "{wordlist}"')
return render_template('index.html', f_wordlist = colored_wordlist, wordlist = wordlist)
if app.name == '__main__':
app.run(debug=True, host='0.0.0.0', port=os.environ.get('PORT', 5000))