-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
106 lines (83 loc) · 3.73 KB
/
api.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
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from flask import Flask
from flask_restful import Resource, Api
from flask_cors import CORS
from flask import render_template, make_response
from LyricsAnalyzer import LyricsAnalyzer
import nltk
app = Flask(__name__)
api = Api(app)
CORS(app)
def encode_mostCommon(mostCommonWords):
"""
Encode a most common words results to json
Input: [("word1",count1),("word2",count2),...,("wordn",countn),]
Output:{"mostCommon": [{"word": word1, "count": count1},..]}
"""
wordList = []
for pair in mostCommonWords:
word, count = pair
wordList.append({'word': word, 'count': count})
return {'mostCommon': wordList}
class WordTokensBase(Resource):
def get(self,lyrics):
analyzer = LyricsAnalyzer(lyrics)
return {'wordTokens': analyzer.wordTokenize()}
class WordTokensNoStops(Resource):
def get(self,lyrics):
analyzer = LyricsAnalyzer(lyrics)
return {'wordTokens': analyzer.wordTokenizeWithoutStopwords()}
class MostCommonBase(Resource):
def get(self,lyrics, num):
analyzer = LyricsAnalyzer(lyrics)
#Get the most common words from the analyzer, and encode them to json
return encode_mostCommon(analyzer.mostCommon(num))
class MostCommonBaseDefault(Resource):
def get(self,lyrics):
analyzer = LyricsAnalyzer(lyrics)
#Get the most common words from the analyzer, and encode them to json
return encode_mostCommon(analyzer.mostCommon(10))
class MostCommonFiltered(Resource):
def get(self,lyrics, num):
analyzer = LyricsAnalyzer(lyrics)
return encode_mostCommon(analyzer.mostCommonFiltered(num))
class MostCommonFilteredDefault(Resource):
def get(self,lyrics):
analyzer = LyricsAnalyzer(lyrics)
return encode_mostCommon(analyzer.mostCommonFiltered(10))
class GetSentimentVADER(Resource):
def get(self,lyrics):
return LyricsAnalyzer(lyrics).getSentiment()
class GetAnalysisDefault(Resource):
def get(self,lyrics):
analyzer = LyricsAnalyzer(lyrics)
mostCommon, sentiment = analyzer.getAnalysis(10)
return {'words':encode_mostCommon(mostCommon), 'sentiment': sentiment}
class GetAnalysis(Resource):
def get(self,lyrics,num):
analyzer = LyricsAnalyzer(lyrics)
mostCommon, sentiment = analyzer.getAnalysis(num)
return {'words':encode_mostCommon(mostCommon), 'sentiment': sentiment}
class Documentation(Resource):
def get(self):
headers = {'Content-Type': 'text/html'}
return make_response(render_template('documentation.html'),200,headers)
#Tokenizing a text into words
api.add_resource(WordTokensBase,'/tokens/base/<string:lyrics>')
api.add_resource(WordTokensNoStops,'/tokens/noStopwords/<string:lyrics>')
#Find the most common words in the text
api.add_resource(MostCommonBase,'/mostCommon/base/<string:lyrics>/<int:num>')
api.add_resource(MostCommonBaseDefault,'/mostCommon/base/<string:lyrics>')
api.add_resource(MostCommonFiltered,'/mostCommon/filtered/<string:lyrics>/<int:num>')
api.add_resource(MostCommonFilteredDefault,'/mostCommon/filtered/<string:lyrics>')
#Find the sentiment of a text, using the VADER algorithm:
api.add_resource(GetSentimentVADER,'/sentiment/vader/<string:lyrics>')
#Do an analysis of a text, providing most common words and sentiment
api.add_resource(GetAnalysisDefault,'/analysis/<string:lyrics>')
api.add_resource(GetAnalysis,'/analysis/<string:lyrics>/<int:num>')
#Documentation
api.add_resource(Documentation, '/documentation')
if __name__ == '__main__':
#First, download the necessary resources
nltk.download('vader_lexicon')
nltk.download('popular')
app.run(host='0.0.0.0', port=80)