Skip to content
Switch branches/tags
Go to file
Cannot retrieve contributors at this time
"""Extract sentiment from text using pretrained models from TextBlob"""
import importlib
from h2oaicore.transformer_utils import CustomTransformer
import datatable as dt
import numpy as np
class TextSentimentTransformer(CustomTransformer):
_testing_can_skip_failure = False # ensure tested as if shouldn't fail
_modules_needed_by_name = ['nltk==3.4.3', 'textblob']
def get_default_properties():
return dict(col_type="text", min_cols=1, max_cols=1, relative_importance=1)
def __init__(self, **kwargs):
def sentimentAnalysis(s):
from textblob import TextBlob
analysis = TextBlob(s)
return analysis.sentiment[0]
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))