forked from msindev/Sentiment-Analysis-Flask-API
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·40 lines (29 loc) · 988 Bytes
/
main.py
File metadata and controls
executable file
·40 lines (29 loc) · 988 Bytes
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
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#|r|e|d|a|n|d|g|r|e|e|n|.|c|o|.|u|k|
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
from flask import Flask, jsonify
from flask_restful import Resource, Api, reqparse
import nltk
from nltk.sentiment.vader import SentimentIntensityAnalyzer
app = Flask(__name__)
api = Api(app)
output = {}
# argument parsing
parser = reqparse.RequestParser()
parser.add_argument('q',help='Pass a sentence to analyse')
class SentAnalysis(Resource):
def get(self):
# use parser and find the user's query
args = parser.parse_args()
sentence = args['q']
nltk.download("vader_lexicon")
sid = SentimentIntensityAnalyzer()
score = sid.polarity_scores(sentence)["compound"]
if score > 0:
output["sentiment"] = "Positive"
else:
output["sentiment"] = "Negative"
return jsonify(output)
api.add_resource(SentAnalysis, '/')
if __name__ == "__main__":
app.run(debug=True)