camel_tools.sentiment

This module contains the CAMeL Tools sentiment analyzer component.

Classes

class camel_tools.sentiment.SentimentAnalyzer(model_path, use_gpu=True)

CAMeL Tools sentiment analysis component.

Parameters:
  • model_path (str) – The path to the fine-tuned model.
  • use_gpu (bool, optional) – The flag to use a GPU or not. Defaults to True.
static labels()

Get the list of possible sentiment labels returned by predictions.

Returns:List of sentiment labels.
Return type:list of str
predict(sentences, batch_size=32)

Predict the sentiment labels of a list of sentences.

Parameters:
  • sentences (list of str) – Input sentences.
  • batch_size (int) – The batch size.
Returns:

The predicted sentiment labels for given sentences.

Return type:

list of str

predict_sentence(sentence)

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, use_gpu=True)

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.
  • use_gpu (bool, optional) – The flag to use a GPU or not. Defaults to True.
Returns:

Instance with loaded pre-trained model.

Return type:

SentimentAnalyzer

Examples

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)