Emotion classifier¶
This module contains the EmotionClassifiers class, which is designed to categorise input texts into emotion categories. It uses a Huggingface transformer model trained on Bert_Large by default. The EmotionClassifiers class has the following method: @method:recognize_emotion: Adding an emotion category. @method:recognize_average_emotion_from_multiple_models: Adding an average emotion category or the most likely emotion category using multiple models.
- class soika.src.risks.emotion_classifier.EmotionRecognizer(model_name=Text.Bert_Tiny, device='cpu')[исходный код]¶
This class is designed to categorize input texts into emotion categories.
Attributes:
model: This attribute holds the model used for emotion recognition. It defaults to HuggingFaceModel.Text.Bert_Large, but can be set to any other compatible model during the instantiation of the class.
device: The device to use for inference. It automatically selects „cuda“ (GPU) if a compatible GPU is available and CUDA is enabled, otherwise, it falls back to „cpu“.
text: The text to be analyzed.
df: The DataFrame containing the text to be analyzed.
text_column: The name of the column containing the text to be analyzed.
- recognize_average_emotion_from_multiple_models(df, text_column, models=None, average=True)[исходный код]¶
Calculate the prevailing emotion using multiple models for a DataFrame column.
- recognize_emotion(text)[исходный код]¶
Return the emotion for a given text.
Example¶
df = pd.read_csv('data.csv')
recognizer = EmotionRecognizer()
df['emotion'] = df['text'].apply(recognizer.recognize_emotion)