Metadata-Version: 2.2
Name: spacy-polarity
Version: 0.1.3
Summary: Spacy extension for sentiment polarity
Author: sondalex
License: MIT License
        
        Copyright (c) 2025 sondalex
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: homepage, https://github.com/sondalex/spacy-polarity
Project-URL: documentation, https://github.com/sondalex/spacy-polarity
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: spacy>=3.1.0
Requires-Dist: textblob>=0.19.0
Requires-Dist: numpy<2.0.0
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Provides-Extra: docs
Requires-Dist: sphinx; extra == "docs"
Provides-Extra: transformers
Requires-Dist: transformers; extra == "transformers"
Requires-Dist: torch; extra == "transformers"

# spacy-polarity

![Python Version](https://img.shields.io/badge/python-3.10+-blue.svg)  
![License](https://img.shields.io/badge/license-MIT-green.svg)  

**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:

```bash
pip install spacy-polarity
```

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

```bash
python -m spacy download en_core_web_sm
```

For transformer support, ensure you have `transformers` installed:

```bash
pip install "spacy-polarity[transformers]" 
```

## Usage

### Using TextBlob for Polarity

Analyze sentiment polarity with TextBlob’s lightweight algorithm:

```python
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:

```python
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`:

```bash
ruff check
ruff format
```

### Testing

Install test dependencies and run tests:

```bash
pip install ".[test]"
pytest -vvv
```

### ARM Installation

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

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

