-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
66 lines (55 loc) · 2.03 KB
/
Copy pathapp.py
File metadata and controls
66 lines (55 loc) · 2.03 KB
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
#!/usr/bin/env python
import flask
from flask import Flask, jsonify, abort, make_response, request, render_template
import json
import pickle
import re
import tweepy
from textblob import TextBlob
import warnings
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
def clean_tweet(tweet):
'''
Utility function to clean tweet text by removing links, special characters
using simple regex statements.
'''
return ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)", " ", tweet).split())
def get_tweet_sentiment(tweet):
'''
Utility function to classify sentiment of passed tweet
using textblob's sentiment method
'''
# create TextBlob object of passed tweet text
analysis = TextBlob(clean_tweet(tweet))
# set sentiment
if analysis.sentiment.polarity > 0:
return ['positive',float(analysis.sentiment.polarity)]
elif analysis.sentiment.polarity == 0:
return ['neutral',float(analysis.sentiment.polarity)]
else:
return ['negative',float(analysis.sentiment.polarity)]
@app.route('/result',methods = ['POST'])
def execute_xgboost():
input_map = dict(request.form.to_dict())
search_query=input_map['Enter the input text']
print('calling sentence analyzer')
sentiment_metadata=get_tweet_sentiment(search_query)
final_dic={}
final_dic["Sentiment detected"]=sentiment_metadata[0]
final_dic["Confidence score"]=sentiment_metadata[1]
final_dic["Algorithm Used"]="Naive Bayes Classifier"
#final_dic = flask.Response(json.dumps(final_dic))
print(final_dic)
return render_template("result.html",result=final_dic)
@app.errorhandler(404)
def not_found(error):
resp = flask.Response()
resp.headers['Access-Control-Allow-Origin'] = '*'
return make_response(jsonify({'error': 'Not found'}), 404)
@app.route('/',methods=['GET', 'POST'])
def index():
return (render_template('index.html'))
if __name__ == '__main__':
app.run(debug=True,use_reloader=False)