Last active
August 30, 2019 20:10
-
-
Save prrao87/b3d32a2fead0ea1210efbe798489cf51 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class VaderSentiment(Base): | |
"""Predict fine-grained sentiment classes using Vader.""" | |
def __init__(self, model_file: str=None) -> None: | |
super().__init__() | |
from nltk.sentiment.vader import SentimentIntensityAnalyzer | |
self.vader = SentimentIntensityAnalyzer() | |
def score(self, text: str) -> float: | |
return self.vader.polarity_scores(text)['compound'] | |
def predict(self, train_file: None, test_file: str, lower_case: bool) -> pd.DataFrame: | |
"Return DataFrame with a new column of predicted labels" | |
df = self.read_data(test_file, lower_case) | |
df['score'] = df['text'].apply(self.score) | |
# Convert float score to category based on binning | |
df['pred'] = pd.cut(df['score'], bins=5, labels=[1, 2, 3, 4, 5]) | |
df = df.drop('score', axis=1) | |
return df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment