-
Notifications
You must be signed in to change notification settings - Fork 599
/
SentimentAnalysis.py
52 lines (40 loc) · 1.39 KB
/
SentimentAnalysis.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
from textblob import TextBlob
import nltk
from nltk.sentiment.vader import SentimentIntensityAnalyzer
from tabpy.models.utils import setup_utils
import ssl
_ctx = ssl._create_unverified_context
ssl._create_default_https_context = _ctx
nltk.download("vader_lexicon")
nltk.download("punkt")
def SentimentAnalysis(_arg1, library="nltk"):
"""
Sentiment Analysis is a procedure that assigns a score from -1 to 1
for a piece of text with -1 being negative and 1 being positive. For
more information on the function and how to use it please refer to
tabpy-tools.md
"""
if not (isinstance(_arg1[0], str)):
raise TypeError
supportedLibraries = {"nltk", "textblob"}
library = library.lower()
if library not in supportedLibraries:
raise ValueError
scores = []
if library == "nltk":
sid = SentimentIntensityAnalyzer()
for text in _arg1:
sentimentResults = sid.polarity_scores(text)
score = sentimentResults["compound"]
scores.append(score)
elif library == "textblob":
for text in _arg1:
currScore = TextBlob(text)
scores.append(currScore.sentiment.polarity)
return scores
if __name__ == "__main__":
setup_utils.deploy_model(
"Sentiment Analysis",
SentimentAnalysis,
"Returns a sentiment score between -1 and 1 for " "a given string",
)