A TextBlob sentiment analysis pipeline compponent for spaCy
Project description
spaCyTextBlob
A TextBlob sentiment analysis pipeline compponent for spaCy.
Version 3.0 is a major version update providing support for spaCy 3.0's new interface for adding pipeline components. As a result, it is not backwards compatible with previous versions of spaCyTextBlob. For compatability with spaCy 2.0 please use pip install spacytextblob==0.1.7
.
Note that version 1.0, and 2.0 have been skipped. The numbering has been aligned with spaCy's version numbering in the hopes of making it easier to compar.
Table of Contents
Install
Install spaCyTextBlob from pypi.
pip install spacytextblob
TextBlob also requires some data to be downloaded before getting started.
python -m textblob.download_corpora
spaCy requires that you download a model to get started.
python -m spacy download en_core_web_sm
Quick Start
spaCyTextBlob allows you to access all of the attributes created by TextBlob sentiment method but within the spaCy framework. The code below will demonstrate how to use spaCyTextBlob on a simple string.
text = "I had a really horrible day. It was the worst day ever! But every now and then I have a really good day that makes me happy."
Using spaCyTextBlob
:
import spacy
from spacytextblob.spacytextblob import SpacyTextBlob
nlp = spacy.load('en_core_web_sm')
nlp.add_pipe("spacytextblob")
doc = nlp(text)
print('Polarity:', doc._.polarity)
Polarity: -0.125
print('Sujectivity:', doc._.subjectivity)
Sujectivity: 0.9
print('Assessments:', doc._.assessments)
Assessments: [(['really', 'horrible'], -1.0, 1.0, None), (['worst', '!'], -1.0, 1.0, None), (['really', 'good'], 0.7, 0.6000000000000001, None), (['happy'], 0.8, 1.0, None)]
Using TextBlob
:
from textblob import TextBlob
blob = TextBlob(text)
print(blob.sentiment_assessments.polarity)
-0.125
print(blob.sentiment_assessments.subjectivity)
0.9
print(blob.sentiment_assessments.assessments)
[(['really', 'horrible'], -1.0, 1.0, None), (['worst', '!'], -1.0, 1.0, None), (['really', 'good'], 0.7, 0.6000000000000001, None), (['happy'], 0.8, 1.0, None)]
Quick Reference
spaCyTextBlob performs sentiment analysis using the TextBlob library. Adding spaCyTextBlob to a spaCy nlp pipeline provides access to three new extension attributes.
._.polarity
._.subjectivity
._.assessments
These extension attributes can be accessed at the Doc
, Span
, or Token
level.
Polarity is a float within the range [-1.0, 1.0], subjectivity is a float within the range [0.0, 1.0] where 0.0 is very objective and 1.0 is very subjective, and assessments is a list of polarity and subjectivity scores for the assessed tokens.
Reference and Attribution
- TextBlob
- negspaCy (for inpiration in writing pipeline and organizing repo)
- spaCy custom components
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for spacytextblob-3.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 97830e18da00cf8e8b42ff690cfcfa83056f97e5ec56aa5164608f13e3377add |
|
MD5 | e681cf6942c59f3c64a0c9696eea65ee |
|
BLAKE2b-256 | cb954f1b0db09e33a2b7884db17be01f33aa611398bec64bcf1ae2d05330716f |