Skip to main content

moodswing

PyPI Version Downloads from PyPI Test Status

Track changes in sentiment over the course of a narrative text. Inspired by Matthew Jockers’ syuzhet R package, moodswing brings sentiment trajectory analysis to Python with both dictionary-based and neural approaches.

moodswing helps you:

  • 📊 Visualize emotional arcs in novels, memoirs, and other narratives

  • 📚 Use proven lexicons (Syuzhet, AFINN, Bing, NRC) optimized for different text types

  • 🧠 Leverage spaCy models for context-aware sentiment analysis

  • 🔬 Apply DCT smoothing to reveal underlying narrative structure

  • 🎨 Create publication-ready plots with flexible visualization options

Based on research in computational literary analysis, this package makes it easy to uncover the emotional patterns that shape stories.

Quick Start

Install from PyPI:

pip install moodswing

For spaCy support (optional):

pip install moodswing
python -m spacy download en_core_web_sm

Example: Analyze a Novel

from moodswing import (
    DictionarySentimentAnalyzer,
    Sentencizer,
    DCTTransform,
    prepare_trajectory,
    plot_trajectory,
)
from moodswing.data import load_sample_text

# Load a sample novel
doc_id, text = load_sample_text("madame_bovary")

# Split into sentences
sentences = Sentencizer().split(text)

# Score each sentence
analyzer = DictionarySentimentAnalyzer()
scores = analyzer.sentence_scores(sentences, method="syuzhet")

# Create smoothed trajectory
trajectory = prepare_trajectory(
    scores,
    rolling_window=int(len(scores) * 0.1),
    dct_transform=DCTTransform(low_pass_size=10, output_length=200, scale_range=True)
)

# Plot the emotional arc
plot_trajectory(trajectory, title="Madame Bovary: Sentiment Trajectory")

Features

Four Sentiment Lexicons
  • Syuzhet: Optimized for narrative analysis

  • AFINN: Includes slang and informal language

  • Bing: Binary positive/negative classification

  • NRC: Multi-dimensional emotions (joy, fear, anger, etc.) with multilingual support

Two Analysis Approaches
  • Dictionary-based: Fast, transparent word lookup

  • spaCy-based: Context-aware neural models (handles negation, sarcasm)

Advanced Smoothing
  • Rolling mean: Local trend smoothing

  • DCT (Discrete Cosine Transform): Reveals overall narrative shape

  • Compatible with R’s syuzhet package for reproducible research

Flexible Visualization
  • Built-in plotting with customizable colors and components

  • Export to pandas DataFrame for seaborn, plotly, or custom plots

  • Publication-ready output with figure size/DPI control

Documentation

Full documentation with tutorials, examples, and API reference:

📖 https://browndw.github.io/moodswing/

Citation

If you use this package in your research, please cite:

@software{moodswing2025,
  author = {Brown, David},
  title = {moodswing: Sentiment Trajectory Analysis for Python},
  year = {2025},
  url = {https://github.com/browndw/moodswing}
}

This package is based on Matthew Jockers’ syuzhet R package:

@article{jockers2015syuzhet,
  title={Revealing sentiment and plot arcs with the Syuzhet package},
  author={Jockers, Matthew L},
  year={2015}
}

License

Code licensed under MIT License.

See LICENSE file.

Metadata

Release files for moodswing 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for moodswing 0.1.0
File Size Uploaded Provenance Actions
moodswing-0.1.0.tar.gz 3.2 MB With provenance Details for moodswing-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for moodswing 0.1.0
File Interpreter ABI Platform Actions
moodswing-0.1.0-py3-none-any.whl Python 3 none any Details for moodswing-0.1.0-py3-none-any.whl

Total release size: 6.5 MB

Release files / moodswing-0.1.0.tar.gz

Download URL moodswing-0.1.0.tar.gz
Size 3.2 MB
Tags Source
SHA-256 checksum
How to use checksums
1d4664e729e267599dd7280ccf314d1505061456af795be2458bff967e6cc6c0
BLAKE2b-256 checksum
How to use checksums
16902b83ac12a03bed9c92a7dc4369164ddb59632d5154466c0757be95f15d72
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 6, 2026.

Transparency log

Release files / moodswing-0.1.0-py3-none-any.whl

Download URL moodswing-0.1.0-py3-none-any.whl
Size 3.3 MB
Tags Python 3
SHA-256 checksum
How to use checksums
4bba070fa30a97c09b1ae903adbbe6b6f0f219e68666b2430aeb10b40c25f03d
BLAKE2b-256 checksum
How to use checksums
140823644ce5a2949738d5b00dfb08481fc09c5f93a4d218ea5914eb4da84793
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 6, 2026.

Transparency log

Security for moodswing 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for moodswing-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 6, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page