This module contains the CAMeL Tools sentiment analyzer component.


class camel_tools.sentiment.SentimentAnalyzer(model_path)

A class for running a fine-tuned sentiment analysis model to predict the sentiment of given sentences.

static labels()

Get the list of possible sentiment labels returned by predictions.

Returns:List of sentiment labels.
Return type:list of str

Predict the sentiment labels of a list of sentences.

Parameters:sentences (list of str) – Input sentences.
Returns:The predicted sentiment labels for given sentences.
Return type:list of str

Predict the sentiment label of a single sentence.

Parameters:sentence (str) – Input sentence.
Returns:The predicted sentiment label for given sentence.
Return type:str
static pretrained(model_name=None)

Load a pre-trained model provided with camel_tools.

Parameters:model_name (str, optional) – Name of pre-trained model to load. Two models are available: ‘arabert’ and ‘mbert’. If None, the default model (‘arabert’) will be loaded. Defaults to None.
Returns:Instance with loaded pre-trained model.
Return type:SentimentAnalyzer


Below is an example of how to load and use the default pre-trained model.

from camel_tools.sentiment import SentimentAnalyzer

sa = SentimentAnalyzer.pretrained()

# Predict the sentiment of a single sentence
sentiment = sa.predict_sentence('أنا بخير')

# Predict the sentiment of multiple sentences
sentences = [
    'أنا بخير',
    'أنا لست بخير'
sentiments = sa.predict(sentences)