-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
143 lines (102 loc) · 3.68 KB
/
main.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from flask import Flask, render_template, request, url_for, Markup
import os
import pandas as pd
import numpy as np
from random import randrange
import nltk
nltk.download('vader_lexicon')
from nltk.sentiment.vader import SentimentIntensityAnalyzer
app = Flask(__name__)
# load quotes in memory
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
# declare global variable
quotes = None
@app.before_request
def prepare_sentiment_quote_stash():
global quotes
# load the quote stash
quotes = pd.read_csv(os.path.join(BASE_DIR, 'quotes.csv'))
sid = SentimentIntensityAnalyzer()
all_compounds = []
for sentence in quotes['quote']:
ss = sid.polarity_scores(sentence)
for k in sorted(ss):
if k == 'compound':
all_compounds.append(ss[k])
# add sentiment to the data
quotes['sentiment_score'] = all_compounds
# create ladder index
quotes = quotes.sort_values('sentiment_score')
quotes['index'] = [ix for ix in range(0, len(quotes))]
def gimme_a_quote(direction = None, current_index = None, max_index_value = 0):
rand_index = randrange(max_index_value)
darker = None
brighter = None
# New session visit
if current_index is None:
brighter = rand_index
if direction == 'brighter':
brighter = current_index
else:
darker = current_index
if darker is not None:
try:
current_index = int(darker)
except ValueError:
# somebody is gaming the system
current_index = rand_index
if current_index > 0:
# try for a lesser value than current one
rand_index = randrange(0, current_index)
else:
# already at lowest point so assign a new random of full set
rand_index = rand_index
elif brighter is not None:
try:
current_index = int(brighter)
except ValueError:
# somebody is gaming the system
current_index = rand_index
# try for a higher value than current one
if current_index < max_index_value -1:
rand_index = randrange(current_index, max_index_value)
else:
# already at highest point so assign a new random of full set
rand_index = rand_index
else:
# grab a random value
rand_index = rand_index
return (rand_index)
@app.route("/")
def quote_me():
quote_stash_tmp = quotes.copy()
max_index_value = np.max(quote_stash_tmp['index'].values)
rand_index_value = randrange(max_index_value)
darker = request.args.get("darker")
brighter = request.args.get("brighter")
if darker is not None:
try:
current_index = int(darker)
except ValueError:
# somebody is gaming the system
current_index = randrange(max_index_value)
new_index = gimme_a_quote(direction = 'darker', current_index = current_index, max_index_value = max_index_value)
elif brighter is not None:
try:
current_index = int(brighter)
except ValueError:
# somebody is gaming the system
current_index = rand_index_value
new_index = gimme_a_quote(direction = 'brighter', current_index = current_index, max_index_value = max_index_value)
else:
# grab a random value
new_index = randrange(max_index_value)
random_quote = quote_stash_tmp.iloc[new_index]
# get a random integer between 0 and max_index_value
quote=random_quote['quote']
author = random_quote['author']
current_id = random_quote['index']
return render_template("index.html",
quote=quote,
author=author,
current_id=current_id,)