forked from h2oai/driverlessai-recipes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vader_text_sentiment_transformer.py
39 lines (29 loc) · 1.25 KB
/
vader_text_sentiment_transformer.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
"""Extract sentiment from text using lexicon and rule-based sentiment analysis tool called VADER"""
# https://github.com/cjhutto/vaderSentiment
# https://medium.com/analytics-vidhya/simplifying-social-media-sentiment-analysis-using-vader-in-python-f9e6ec6fc52f
import importlib
from h2oaicore.transformer_utils import CustomTransformer
import datatable as dt
import numpy as np
import pandas as pd
class VaderSentimentTransformer(CustomTransformer):
_unsupervised = True
_modules_needed_by_name = ['vaderSentiment']
@staticmethod
def do_acceptance_test():
return True
@staticmethod
def get_default_properties():
return dict(col_type="text", min_cols=1, max_cols=1, relative_importance=1)
def __init__(self, **kwargs):
super().__init__(**kwargs)
@staticmethod
def sentimentAnalysis(s):
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
analyzer = SentimentIntensityAnalyzer()
return analyzer.polarity_scores(s)['compound']
def fit_transform(self, X: dt.Frame, y: np.array = None):
return self.transform(X)
def transform(self, X: dt.Frame):
return X.to_pandas().astype(str).iloc[:, 0].apply(
lambda x: self.sentimentAnalysis(x))