Skip to content

Commit 6806d63

Browse files
authored
Add files via upload
1 parent cd176d3 commit 6806d63

File tree

3 files changed

+206
-0
lines changed

3 files changed

+206
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# NLP-Sentiment-analysis-using-Machine-Learning-and-flask
2+
It analyses everyday language that we used in communication and produces number of tokens in the language,keywords,polarity and subjectivity of the context.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from flask import Flask, render_template,request,url_for
2+
from flask_bootstrap import Bootstrap
3+
4+
5+
# NLP Packages
6+
from textblob import TextBlob,Word
7+
import random
8+
import time
9+
10+
app = Flask(__name__)
11+
Bootstrap(app)
12+
13+
@app.route('/')
14+
def index():
15+
return render_template('index.html')
16+
17+
18+
@app.route('/analyse',methods=['POST'])
19+
def analyse():
20+
start = time.time()
21+
if request.method == 'POST':
22+
rawtext = request.form['rawtext']
23+
#NLP Stuff
24+
blob = TextBlob(rawtext)
25+
received_text2 = blob
26+
blob_sentiment,blob_subjectivity = blob.sentiment.polarity ,blob.sentiment.subjectivity
27+
number_of_tokens = len(list(blob.words))
28+
# Extracting Main Points
29+
nouns = list()
30+
for word, tag in blob.tags:
31+
if tag == 'NN':
32+
nouns.append(word.lemmatize())
33+
len_of_words = len(nouns)
34+
rand_words = random.sample(nouns,len(nouns))
35+
final_word = list()
36+
for item in rand_words:
37+
word = Word(item).pluralize()
38+
final_word.append(word)
39+
summary = final_word
40+
end = time.time()
41+
final_time = end-start
42+
43+
44+
return render_template('index.html',received_text = received_text2,number_of_tokens=number_of_tokens,blob_sentiment=blob_sentiment,blob_subjectivity=blob_subjectivity,summary=summary,final_time=final_time)
45+
46+
47+
48+
49+
50+
51+
if __name__ == '__main__':
52+
app.run(debug=True)
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
{% extends "bootstrap/base.html" %}
2+
3+
{% block content %}
4+
5+
<style type="text/css">
6+
body{
7+
font:15px/1.5 Arial, Helvetica,sans-serif;
8+
}
9+
.spinner-1:before{
10+
content: "";
11+
box-sizing: border-box;
12+
position: absolute;
13+
top:50%;
14+
left: 50%;
15+
height: 60px;
16+
width: 60px;
17+
margin-top: -30px;
18+
margin-left: -30px;
19+
border-radius: 50%;
20+
border:6px solid transparent;
21+
border-top-color: blue;
22+
animation: spinner 0.7s linear infinite;
23+
}
24+
.jumbotron text-center{
25+
background-color:green;
26+
text-color:white;
27+
}
28+
@keyframes spinner {
29+
to {
30+
transform: rotate(360deg);
31+
}
32+
33+
}
34+
li { background-color:#BDBDBD; }
35+
li:nth-child(odd) { background-color:green; }
36+
</style>
37+
38+
39+
<div class="container">
40+
<div class="jumbotron text-center">
41+
<h3><b>SENTIMENT ANALYSIS USING NLP<b></h3>
42+
<p>Text Summarization Tool</p>
43+
</div>
44+
</div>
45+
46+
<div class="container">
47+
<form method="POST" action="{{ url_for('analyse')}}" id="myForm">
48+
49+
50+
<label ><strong>Enter Your Text Below<strong></label><br>
51+
<textarea class="form-control" rows="20" cols="40" name="rawtext"></textarea><br>
52+
53+
<input type="submit" onclick="myAnalyser()" value="Submit" class="btn btn-primary ">
54+
<input type="button" onclick="myFunction()" value="Clear" class="btn btn-outline-dark">
55+
56+
<a href="{{ url_for('index')}}" type="button" class="btn btn-danger" > Reset</a>
57+
58+
</form>
59+
60+
</div>
61+
<br/>
62+
<hr/>
63+
<div class="main">
64+
<div class="container">
65+
<div class="card">
66+
<div class="card-header">
67+
Main Points
68+
</div>
69+
<div class="card-body">
70+
<h5 class="card-title"><div class="alert alert-primary" role="alert">
71+
This text has {{number_of_tokens}} tokens with {{len_of_words}} important point
72+
</div> </h5>
73+
<div class="card-text">
74+
<h5>Your Text</h5>
75+
<p style="color:#0091EA;font-family:sans-serif;">{{ received_text }}</p>
76+
<hr/>
77+
<br/>
78+
<p>Time Elapsed: <span style="color:#0091EA;">{{ final_time }} </span> seconds to analyse</p>
79+
<p>This text is about:</p>
80+
{% for i in summary %}
81+
<ul class="list-group ">
82+
<li class="list-group-item list-group-item-info"><span style="color:black">{{i}}</span>
83+
<a href="http://www.dictionary.com/browse/{{i}}?s=" target="_blank" type="button" class="btn btn-outline-primary btn-sm" style="float:right;font-size:9px;color:#fff;">View</a>
84+
85+
</li>
86+
</ul>
87+
88+
89+
{% endfor %}
90+
</div>
91+
<div class="card-footer text-muted">
92+
<table class="table table-striped table-dark" >
93+
<thead>
94+
<tr>
95+
<th scope="col">Sentiment</th>
96+
<th scope="col">Polarity</th>
97+
<th scope="col">Subjectivity</th>
98+
</tr>
99+
</thead>
100+
<tbody>
101+
<tr>
102+
<th scope="row">Score:</th>
103+
<td>{{blob_sentiment}}</td>
104+
<td>{{blob_subjectivity}}</td>
105+
</tr>
106+
</tbody></table>
107+
108+
</div>
109+
110+
111+
</div>
112+
</div>
113+
114+
115+
{% endblock %}
116+
117+
118+
119+
120+
121+
122+
123+
124+
125+
<!-- Scripts starts here -->
126+
{% block scripts %}
127+
128+
{{ super() }}
129+
130+
<script>
131+
function myFunction() {
132+
document.getElementById("myForm").reset();
133+
}
134+
</script>
135+
<script>
136+
function myAnalyser() {
137+
document.querySelector('.main div').style.display = 'none';
138+
//Hide the main division
139+
document.querySelector('.main').classList.add('spinner-1');
140+
// Server request
141+
setTimeout(() => {
142+
document.querySelector('.main').classList.remove('spinner-1');
143+
//Remove the animation
144+
document.querySelector('.main div').style.display = 'block';
145+
//Show the main division
146+
},5000);//Number of seconds to last
147+
}
148+
</script>
149+
150+
<!-- Prevent it from being overwritten -->
151+
152+
{% endblock %}

0 commit comments

Comments
 (0)