messageanalyzer.sentiment_analysis

Functions

analyze_sentiment(→ List[Dict[str, Union[str, float, ...)

This function analyzes the sentiment of a list of given messages

Module Contents

messageanalyzer.sentiment_analysis.analyze_sentiment(messages: List[str], model: str = 'Default') List[Dict[str, str | float | bool]][source]

This function analyzes the sentiment of a list of given messages and returns the sentiment scores and labels for each messange and prints alert message if it’s highly negative.

Parameters:
  • messages (List[str]) – The messages to analyze.

  • model (str, optional) – The model to use for sentiment analysis. The “Default” model is TextBlob.

Returns:

A list of dictionaries, where each dictionary contains: - “messages”: The original message. - “score”: The sentiment polarity score. - “label”: The sentiment category (“positive”, “negative”, “neutral”). - “alert” (optional): True if the message is highly negative. Alert will be printed if some messages are highly negative, and these messages will be displayed.

Return type:

List[Dict[str, Union[str, float, bool]]]

Raises:
  • TypeError – If messages is not a list of strings.

  • ValueError – If an unrecognized sentiment analysis model is provided.

Example

>>> messages = ["I love this!", "This is terrible."]
>>> analyze_sentiment(messages, "Default")
[{'message': 'I love this!', 'score': 0.5, 'label': 'positive'},
 {'message': 'This is terrible.', 'score': -1.0, 'label': 'negative', 'alert': True}]