-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
executable file
·54 lines (41 loc) · 1.54 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
import os
from flask import Flask, render_template, session, redirect, url_for, flash
from flask_bootstrap import Bootstrap
from flask_wtf import FlaskForm
from wtforms import SubmitField, TextAreaField
from wtforms.validators import DataRequired
import vaderSentiment
app = Flask(__name__)
app.config['SECRET_KEY'] = 'hard to guess string'
# Bind to PORT if defined, otherwise default to 5000.
port = int(os.environ.get('PORT', 5000))
bootstrap = Bootstrap(app)
class NameForm(FlaskForm):
name = TextAreaField('Enter Text', validators=[DataRequired()])
submit = SubmitField('Clasify')
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
@app.errorhandler(500)
def internal_server_error(e):
return render_template('500.html'), 500
@app.route('/', methods=['GET', 'POST'])
def index():
form = NameForm()
if form.validate_on_submit():
score = vaderSentiment.SentimentIntensityAnalyzer().polarity_scores(form.name.data)['compound']
if score > 0.05:
sentiment = 'Positive'
category = 'success'
elif score < -0.05:
sentiment = 'Negative'
category = 'danger'
else:
sentiment = 'Neutral'
category = 'info'
flash('Score = ' + str(score) + ' (' + sentiment + ')', category)
session['name'] = form.name.data
return redirect(url_for('index'))
return render_template('index.html', form=form, name=session.get('name'))
if __name__ == '__main__':
app.run(host='0.0.0.0', port=port)