Skip to content

Instantly share code, notes, and snippets.

@prrao87
Created August 29, 2019 21:37
Show Gist options
  • Save prrao87/94b47978c9167ecdf5f83270c0323969 to your computer and use it in GitHub Desktop.
Save prrao87/94b47978c9167ecdf5f83270c0323969 to your computer and use it in GitHub Desktop.
class TextBlobSentiment(Base):
"""Predict fine-grained sentiment classes using TextBlob."""
def __init__(self, model_file: str=None) -> None:
super().__init__()
def score(self, text: str) -> float:
# pip install textblob
from textblob import TextBlob
return TextBlob(text).sentiment.polarity
def predict(self, train_file: None, test_file: str, lower_case: bool) -> pd.DataFrame:
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