35
loading...
This website collects cookies to deliver better user experience
# sentiment.py
import nltk
text = "This is a very nice day"
nltk.download('vader_lexicon')
from nltk.sentiment.vader import SentimentIntensityAnalyzer
sid = SentimentIntensityAnalyzer()
score = ((sid.polarity_scores(str(text))))['compound']
if(score > 0):
label = 'This sentence is positive'
elif(score == 0):
label = 'This sentence is neutral'
else:
label = 'This sentence is negative'
print(label)
python sentiment.py
main.py
- This is where the flask server and the VADER is initialisedtemplates/index.html
- We can use custom HTML files along with flask to give the final webpage a nice look.# main.py
import nltk
from flask import request
from flask import jsonify
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def my_form():
return render_template('index.html')
@app.route('/', methods=['POST'])
def my_form_post():
text = request.form['text']
nltk.download('vader_lexicon')
from nltk.sentiment.vader import SentimentIntensityAnalyzer
sid = SentimentIntensityAnalyzer()
score = ((sid.polarity_scores(str(text))))['compound']
if(score > 0):
label = 'This sentence is positive'
elif(score == 0):
label = 'This sentence is neutral'
else:
label = 'This sentence is negative'
return(render_template('index.html', variable=label))
if __name__ == "__main__":
app.run(port='8088', threaded=False, debug=True)
// index.html
<!DOCTYPE html>
<html>
<head>
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
crossorigin="anonymous"
/>
<script
src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
crossorigin="anonymous"
></script>
<style>
.centered {
position: relative;
text-align: center;
display: grid;
place-items: center;
color: white;
}
.header {
color: black;
}
p {
color: #111;
font-family: 'Helvetica Neue', sans-serif;
font-size: 14px;
line-height: 24px;
margin: 0 0 24px;
text-justify: inter-word;
}
h1 {
color: #111;
font-family: 'Helvetica Neue', sans-serif;
font-size: 36px;
font-weight: bold;
letter-spacing: -1px;
line-height: 1;
text-align: center;
}
#demo {
color: #111;
font-family: 'Helvetica Neue', sans-serif;
font-size: 22px;
line-height: 24px;
margin: 0 0 24px;
text-justify: inter-word;
}
.form-div {
display: flex;
justify-content: space-around;
align-items: center;
}
</style>
</head>
<body>
<div class="container mt-5">
<div class="centered">
<div class="text-center text-white">
<title>sentiment analysis app</title>
</div>
<h1 class="header">Sentiment Analysis App</h1>
<p class="desc">
Type a sentence, click on the submit button and wait for your prediction.
</p>
<form method="POST" onSubmit="return validate();" class="form">
<div class="form-div">
<input type="text" name="text" class="form-control mr-2" />
<button type="submit" class="btn btn-outline-primary" onclick="this.form.submit();">
Submit
</button>
</div>
</form>
<br />
<br />
<p id="demo">{{ variable }}</p>
</div>
</div>
</body>
</html>