Skip to main content

Spacy extension for sentiment polarity

Project description

spacy-polarity

Python Version
License

Measure sentiment polarity of sentences and documents with spaCy

spacy-polarity is a spaCy pipeline component that adds sentiment polarity analysis to your pipeline. It supports both TextBlob-based polarity scoring and transformer-based sentiment analysis.

Installation

Install spacy-polarity via pip:

pip install spacy-polarity

You’ll also need a spaCy language model (e.g., en_core_web_sm):

python -m spacy download en_core_web_sm

For transformer support, ensure you have transformers installed:

pip install "spacy-polarity[transformers]" 

Usage

Using TextBlob for Polarity

Analyze sentiment polarity with TextBlob’s lightweight algorithm:

import spacy
import spacy_polarity

# Load spaCy model and add the polarity component
nlp = spacy.load("en_core_web_sm")
nlp.add_pipe("spacy_polarity")

# Process text
doc = nlp("The financial markets are performing well, bringing good returns to investors. The stock markets in USA grew by 5% this year.")

# Polarity for each sentence
for sent in doc.sents:
    print(f"Sentence: {sent.text}")
    print(f"Polarity: {sent._.polarity:.3f}\n")

# Polarity for the entire document
print(f"Document Polarity: {doc._.polarity:.3f}")

Using Transformers

Leverage transformer models for more advanced sentiment analysis:

import spacy
import spacy_polarity

# Load spaCy model and add the polarity component with transformers
nlp = spacy.load("en_core_web_sm")
nlp.add_pipe("spacy_polarity", config={"use_transformer": True})

# Process text
doc = nlp("The financial markets are performing well, bringing good returns to investors. The stock markets in USA grew by 5% this year.")

# Polarity for each sentence
for sent in doc.sents:
    print(f"Sentence: {sent.text}")
    print(f"Polarity: {sent._.polarity:.3f}\n")

# Polarity for the entire document
print(f"Document Polarity: {doc._.polarity:.3f}")

Performance Note

This package processes documents individually and does not batch inference across multiple documents. For high-volume analysis or GPU optimization, consider a custom solution using the transformers library directly.

Development

Style and Linting

Ensure code quality with ruff:

ruff check
ruff format

Testing

Install test dependencies and run tests:

pip install ".[test]"
pytest -vvv

ARM Installation

For ARM architectures (e.g., Raspberry Pi, Apple M1), use the following:

apt install python3-dev
pip install wheel
BLIS_ARCH="generic" pip install spacy --no-binary blis
pip install spacy-polarity

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

spacy_polarity-0.1.3.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

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

spacy_polarity-0.1.3-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file spacy_polarity-0.1.3.tar.gz.

File metadata

  • Download URL: spacy_polarity-0.1.3.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for spacy_polarity-0.1.3.tar.gz
Algorithm Hash digest
SHA256 cbb1550bb0c9387654d73f61cdbc8d2c15e890c61e4a7476182153e975f1e715
MD5 4bb32521eb32375f432ae54d38ba0185
BLAKE2b-256 3d3e61ffa06b776c9dc929e717317d7449b370ef3fe2d0f4b335e68547a3752b

See more details on using hashes here.

File details

Details for the file spacy_polarity-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: spacy_polarity-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for spacy_polarity-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 95d0426cabcb65b87d720791c6fa43063e77b8117a4dff54b1b98ded98122dce
MD5 c0c195e1ae02e3a151421d1b5562b3be
BLAKE2b-256 ba5863eca7cdeb1be67680a3f481421ea0d7a85465a1f40adecc38085c7debbc

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