Skip to content

Instantly share code, notes, and snippets.

@prrao87
Created August 31, 2019 01:27
Show Gist options
  • Select an option

  • Save prrao87/645c42dc771acd1949154fd2f2adb0b7 to your computer and use it in GitHub Desktop.

Select an option

Save prrao87/645c42dc771acd1949154fd2f2adb0b7 to your computer and use it in GitHub Desktop.
class FastTextSentiment(Base):
"""Predict fine-grained sentiment scores using FastText"""
def __init__(self, model_file: str=None) -> None:
super().__init__()
import fasttext
self.model = fasttext.load_model(model_file)
def score(self, text: str) -> int:
# Predict just the top label (hence 1 index below)
labels, probabilities = self.model.predict(text, 1)
pred = int(labels[0][-1])
return pred
def predict(self, train_file: None, test_file: str, lower_case: bool) -> pd.DataFrame:
df = self.read_data(test_file, lower_case)
df['pred'] = df['text'].apply(self.score)
return df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment