Skip to main content

Add your description here

Project description


Sentimotion

A repository containing the source code for the Sentimotion Python Package. The package can be used for undertaking a comparative analysis of Emotion and Sentiment modelling techniques, including visualisation over time.
Read the paper »

About The Project

Sentimotion is a Python package used to assist with undertaking a Sentiment & Emotion analysis of a large, unlabelled corpora. The package uses a series of widely used classifiers, LLM's and custom implementations of Lexicons, such as NRC. We have used this package to undertake Dynamic Emotion and Dynamic Sentiment Analysis of Twitter datasets with in excess of 1 million Tweets.

The source code is optimised and will run on CPU-only systems; however, please bear in mind that performance is likely to be extremely limited.

Requirements

  • Python 3.13
  • Kaleido
  • NLTK
  • Numpy
  • Pandas
  • Plotly
  • Tensorflow
  • TextBlob
  • PyTorch
  • TQDM
  • Transformers

Usage

Installation

Development

For local development, we have utilised the Rust-based package and environment manager, UV by Astral. You are of course free to use whichever package manager you prefer, however, instructions have been provided for UV below for ease of use. The distributable version on this repository only supports the CPU-based version of PyTorch. You may wish to install the relevant GPU packages for your system.

  1. Clone the repository locally
git clone https://github.com/KieronHolmes/Sentimotion.git
  1. Change your current working directory to the Sentimotion directory
cd Sentimotion
  1. Create a virtual environment and install dependencies
uv venv
uv sync

Setup

Initiating Sentimotion

To use the Sentimotion package, you must initiate the Sentimotion class, passing values for emotion_model and sentiment_model. An example of how this can be done is as follows:

from sentimotion import Sentimotion

sentimotionmodel = Sentimotion(
    language="english",
    emotion_model=emotion_model, # Substitute this with your emotion model of choice
    sentiment_model=sentiment_model, # Substitute this with your emotion model of choice
    verbose=True,
)

Calculating Sentiment/Emotion

To calculate the Sentiment/Emotion of input content, you must provide a Python list to the calculate method in Sentimotion. An example of how this can be done is as follows:

documents_input = [
    "Lorem ipsum dolor sit amet",
    "Lorem ipsum dolor sit amet",
    "Lorem ipsum dolor sit amet"
]
sentimotionmodel.calculate(documents=documents_input)

Getting Document Info

Once the calculate method has been executed, you can access the calculated Sentiment and Emotion by using the get_document_info method, passing document info to the class. An example of the code to undertake this is as follows:

df = sentimotionmodel.get_document_info(documents=documents_input)

This is an instance of the pandas.DataFrame class, so you are free to interrogate or export the data as you wish, using functions such as .to_csv().

Visualisations

All visualisations return an instance of the plotly.graph_objects class. You are able to visualise and inspect these using all common functions such as fig.write_image() and fig.write_html()

Sentiment - Pie Chart
fig = sentimotionmodel.visualise_sentiment_piechart()
Emotion - Pie Chart
fig = sentimotionmodel.visualise_emotion_piechart()
Sentiment & Emotion - Barchart
fig = sentimotionmodel.visualise_emotion_sentiment_barchart()
Sentiment Over Time

This visualisation has two required parameters:

  • nr_bins: The number of timing bins to group data into. This should be a relatively low number for large datasets to speed up generation.
  • timestamps: Provide timestamps correlating to the documents input when training the model.
fig = sentimotionmodel.visualise_sentiment_over_time(nr_bins=20, timestamps=timestamps_input)
Emotion Over Time

This visualisation has two required parameters:

  • nr_bins: The number of timing bins to group data into. This should be a relatively low number for large datasets to speed up generation.
  • timestamps: Provide timestamps correlating to the documents input when training the model.
fig = sentimotionmodel.visualise_emotion_over_time(nr_bins=20, timestamps=timestamps_input)

Available Classifiers

Emotion

This package supports the following Emotion Classifier Models:

CardiffNLP

from sentimotion.emotion import CardiffNLP as CardiffNLPEmotion

emotion_model = CardiffNLPEmotion()

This classifier has two required parameters during instantiation:

  • batch_size: (default: 250) The number of input documents to include in each batch when running parallel or on GPU.
  • model_name: (default: "cardiffnlp/twitter-roberta-base-emotion-multilabel-latest") The named huggingface transformer to use.

GoEmotions

from sentimotion.emotion import GoEmotions

emotion_model = GoEmotions()

This classifier has two required parameter during instantiation:

  • batch_size: (default: 250) The number of input documents to include in each batch when running parallel or on GPU.
  • model_name: (default: "SamLowe/roberta-base-go_emotions") The named huggingface transformer to use.

NRC

from sentimotion.emotion import NRC

emotion_model = NRC()

This classifier has one required parameter during instantiation:

  • lexicon_file: (default: "./nrc-lexicons/NRC-Emotion-Lexicon-Wordlevel-v0.92.txt") The filepath to the NRC Lexicon.

OpenAI (API)

from sentimotion.emotion import OpenAI as OpenAIEmotion

emotion_model = OpenAIEmotion()

This classifier has one required parameter during instantiation:

  • openai_client: (default: None) An instance of the OpenAI class.
  • model_name: (default: "gpt-4o-mini") The OpenAI model to use for inference.

Deepseek R1 1.5B (Local)

from sentimotion.emotion import Deepseek as DeepseekEmotion

emotion_model = DeepseekEmotion()

This classifier has two required parameters during instantiation:

  • batch_size: (default: 250) The number of input documents to include in each batch when running parallel or on GPU.
  • model_name: (default: "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B") The named huggingface transformer to use.

Sentiment

This package supports the following Sentiment Classifier Models:

CardiffNLP

from sentimotion.sentiment import CardiffNLP as CardiffNLPSentiment

sentiment_model = CardiffNLPSentiment()

This classifier has two required parameters during instantiation:

  • batch_size: (default: 250) The number of input documents to include in each batch when running parallel or on GPU.
  • model_name: (default: "cardiffnlp/twitter-roberta-base-emotion-multilabel-latest") The named huggingface transformer to use.

SentiWordNet

from sentimotion.sentiment import SentiWordNet

sentiment_model = SentiWordNet()

This classifier has no required parameters.

VaderSentiment

from sentimotion.sentiment import VaderSentiment

sentiment_model = VaderSentiment()

This classifier has no required parameters.

OpenAI (API)

from sentimotion.sentiment import OpenAI as OpenAISentiment

sentiment_model = OpenAISentiment()

This classifier has one required parameter during instantiation:

  • openai_client: (default: None) An instance of the OpenAI class.
  • model_name: (default: "gpt-4o-mini") The OpenAI model to use for inference.

Deepseek R1 1.5B (Local)

from sentimotion.sentiment import Deepseek as DeepseekSentiment

sentiment_model = DeepseekSentiment()

This classifier has two required parameters during instantiation:

  • batch_size: (default: 250) The number of input documents to include in each batch when running parallel or on GPU.
  • model_name: (default: "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B") The named huggingface transformer to use.

Example Code

# Import Packages
from sentimotion import Sentimotion
from sentimotion.emotion import CardiffNLP as CardiffNLPEmotion
from sentimotion.sentiment import CardiffNLP as CardiffNLPSentiment

# Input Documents and Timestamps
documents_input = [
    "Lorem ipsum dolor sit amet",
    "Lorem ipsum dolor sit amet",
    "Lorem ipsum dolor sit amet",
    "Lorem ipsum dolor sit amet",
    "Lorem ipsum dolor sit amet"
]

timestamps_input = [
    "2024-01-01 00:00:00",
    "2024-01-02 00:00:00",
    "2024-01-03 00:00:00",
    "2024-01-04 00:00:00",
    "2024-01-05 00:00:00"
]

# Calculate Emotion/Sentiment
sentimotionmodel.calculate(documents=documents_input)

# Get Sentiment and Emotion for each document and output to csv
df = sentimotionmodel.get_document_info(documents=documents_input)
df.to_csv(f"./output/emotion-sentiment-output.csv")

# Undertake Dynamic Sentiment Analysis and output to svg
fig = sentimotionmodel.visualise_sentiment_over_time(nr_bins=20, timestamps=timestamps_input)
fig.write_image(f"./output/sentiment-over-time-svg.svg")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sentimotion-0.1.0.tar.gz (435.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sentimotion-0.1.0-py3-none-any.whl (452.6 kB view details)

Uploaded Python 3

File details

Details for the file sentimotion-0.1.0.tar.gz.

File metadata

  • Download URL: sentimotion-0.1.0.tar.gz
  • Upload date:
  • Size: 435.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for sentimotion-0.1.0.tar.gz
Algorithm Hash digest
SHA256 25e2a801adab437a777e4cbf81c2dfde27c4ee353c839df2497dc08de1b85fe5
MD5 0d6830d6496d7f7172085240a1a1d2bc
BLAKE2b-256 6db4321316cdc4a12204acf31d03f9ee68e670b38ab0584a901f3549f86d4c7c

See more details on using hashes here.

File details

Details for the file sentimotion-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: sentimotion-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 452.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for sentimotion-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c5d91538ffc3293387b822cd0abda2849122b859ad737a96c338240dd3b78a09
MD5 7f73e38b049ddd1e5a93d531180f4087
BLAKE2b-256 2d85b1142a4757b1e4fda15b6208724262d6b321c9206cd8cdbb8ed5ebb0757d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page