|
| 1 | +from flask import Flask, render_template, request, url_for, Markup |
| 2 | +import os |
| 3 | +import pandas as pd |
| 4 | +import numpy as np |
| 5 | +from random import randrange |
| 6 | + |
| 7 | +import nltk |
| 8 | +nltk.download('vader_lexicon') |
| 9 | + |
| 10 | +from nltk.sentiment.vader import SentimentIntensityAnalyzer |
| 11 | + |
| 12 | +app = Flask(__name__) |
| 13 | + |
| 14 | +# load quotes in memory |
| 15 | +BASE_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 16 | + |
| 17 | +# declare global variable |
| 18 | +quotes = None |
| 19 | + |
| 20 | + |
| 21 | +@app.before_request |
| 22 | +def prepare_sentiment_quote_stash(): |
| 23 | + global quotes |
| 24 | + |
| 25 | + # load the quote stash |
| 26 | + quotes = pd.read_csv(os.path.join(BASE_DIR, 'quotes.csv')) |
| 27 | + |
| 28 | + sid = SentimentIntensityAnalyzer() |
| 29 | + |
| 30 | + all_compounds = [] |
| 31 | + for sentence in quotes['quote']: |
| 32 | + ss = sid.polarity_scores(sentence) |
| 33 | + for k in sorted(ss): |
| 34 | + if k == 'compound': |
| 35 | + all_compounds.append(ss[k]) |
| 36 | + |
| 37 | + |
| 38 | + # add sentiment to the data |
| 39 | + quotes['sentiment_score'] = all_compounds |
| 40 | + |
| 41 | + # create ladder index |
| 42 | + quotes = quotes.sort_values('sentiment_score') |
| 43 | + quotes['index'] = [ix for ix in range(0, len(quotes))] |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +def gimme_a_quote(direction = None, current_index = None, max_index_value = 0): |
| 48 | + rand_index = randrange(max_index_value) |
| 49 | + darker = None |
| 50 | + brighter = None |
| 51 | + |
| 52 | + |
| 53 | + # New session visit |
| 54 | + if current_index is None: |
| 55 | + brighter = rand_index |
| 56 | + |
| 57 | + if direction == 'brighter': |
| 58 | + brighter = current_index |
| 59 | + else: |
| 60 | + darker = current_index |
| 61 | + |
| 62 | + if darker is not None: |
| 63 | + try: |
| 64 | + current_index = int(darker) |
| 65 | + except ValueError: |
| 66 | + # somebody is gaming the system |
| 67 | + current_index = rand_index |
| 68 | + |
| 69 | + |
| 70 | + if current_index > 0: |
| 71 | + # try for a lesser value than current one |
| 72 | + rand_index = randrange(0, current_index) |
| 73 | + else: |
| 74 | + # already at lowest point so assign a new random of full set |
| 75 | + rand_index = rand_index |
| 76 | + |
| 77 | + |
| 78 | + elif brighter is not None: |
| 79 | + try: |
| 80 | + current_index = int(brighter) |
| 81 | + except ValueError: |
| 82 | + # somebody is gaming the system |
| 83 | + current_index = rand_index |
| 84 | + |
| 85 | + # try for a higher value than current one |
| 86 | + if current_index < max_index_value -1: |
| 87 | + rand_index = randrange(current_index, max_index_value) |
| 88 | + else: |
| 89 | + # already at highest point so assign a new random of full set |
| 90 | + rand_index = rand_index |
| 91 | + else: |
| 92 | + # grab a random value |
| 93 | + rand_index = rand_index |
| 94 | + |
| 95 | + return (rand_index) |
| 96 | + |
| 97 | + |
| 98 | +@app.route("/") |
| 99 | +def quote_me(): |
| 100 | + quote_stash_tmp = quotes.copy() |
| 101 | + max_index_value = np.max(quote_stash_tmp['index'].values) |
| 102 | + rand_index_value = randrange(max_index_value) |
| 103 | + |
| 104 | + darker = request.args.get("darker") |
| 105 | + brighter = request.args.get("brighter") |
| 106 | + |
| 107 | + if darker is not None: |
| 108 | + |
| 109 | + try: |
| 110 | + current_index = int(darker) |
| 111 | + except ValueError: |
| 112 | + # somebody is gaming the system |
| 113 | + current_index = randrange(max_index_value) |
| 114 | + |
| 115 | + new_index = gimme_a_quote(direction = 'darker', current_index = current_index, max_index_value = max_index_value) |
| 116 | + |
| 117 | + elif brighter is not None: |
| 118 | + |
| 119 | + try: |
| 120 | + current_index = int(brighter) |
| 121 | + except ValueError: |
| 122 | + # somebody is gaming the system |
| 123 | + current_index = rand_index_value |
| 124 | + |
| 125 | + new_index = gimme_a_quote(direction = 'brighter', current_index = current_index, max_index_value = max_index_value) |
| 126 | + |
| 127 | + |
| 128 | + else: |
| 129 | + # grab a random value |
| 130 | + new_index = randrange(max_index_value) |
| 131 | + |
| 132 | + random_quote = quote_stash_tmp.iloc[new_index] |
| 133 | + |
| 134 | + |
| 135 | + # get a random integer between 0 and max_index_value |
| 136 | + quote=random_quote['quote'] |
| 137 | + author = random_quote['author'] |
| 138 | + current_id = random_quote['index'] |
| 139 | + |
| 140 | + return render_template("quote.html", |
| 141 | + quote=quote, |
| 142 | + author=author, |
| 143 | + current_id=current_id,) |
| 144 | + |
| 145 | + |
| 146 | +if __name__ == '__main__': |
| 147 | + app.run() |
0 commit comments