-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.py
40 lines (31 loc) · 940 Bytes
/
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
from flask import Flask, request, json
from nltk.sentiment.vader import SentimentIntensityAnalyzer
import pdb
import re
app = Flask(__name__)
sid = SentimentIntensityAnalyzer()
# compile regex for airport codes, i.e. "LAX"
airport = re.compile('\\b[A-Z0-9]{3}\\b')
# only need one route for determining words
@app.route('/parse', methods=['POST'])
def parse():
# get the content
# request.headers['Content-Type'] == 'application/json'
text = request.form['text']
global sid
# call sentiment analysis on text
scores = sid.polarity_scores(text)
global airport
#find airport codes in text
codes = re.findall(airport, text)
#remove duplicates
codes = list(set(codes))
#define response dict
response = {
'sentiment': json.dumps(scores),
'codes': codes
}
# return json response
return json.dumps(response)
if __name__ == '__main__':
app.run(debug=True)