A TextBlob sentiment analysis pipeline compponent for spaCy.
Project description
spaCyTextBlob
A TextBlob sentiment analysis pipeline compponent for spaCy.
Repo: https://github.com/SamEdwardes/spaCyTextBlob
PyPi: https://pypi.org/project/spacytextblob/
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
Usage
How to load the package in spaCy pipeline
import spacy
from spacytextblob.textblob import SpacyTextBlob
nlp = spacy.load('en_core_web_sm')
spacy_text_blob = SpacyTextBlob()
nlp.add_pipe(spacy_text_blob)
# pipeline contains component name
print(nlp.pipe_names)
['tagger', 'parser', 'ner', 'spaCyTextBlob']
How to use the pipeline
By adding SpacyTextBlob
into the pipeline sentiment analysis is perofmed on the doc everytime you call nlp
.
text = "I had a really horrible day. It was the worst day ever!"
doc = nlp(text)
print('Polarity:', doc._.polarity)
print('Sujectivity:', doc._.subjectivity)
print('Assessments:', doc._.assessments)
Polarity: -1.0
Sujectivity: 1.0
Assessments: [(['really', 'horrible'], -1.0, 1.0, None), (['worst', '!'], -1.0, 1.0, None)]
text = "Wow I had just the best day ever today!"
doc = nlp(text)
print('Polarity:', doc._.polarity)
print('Sujectivity:', doc._.subjectivity)
print('Assessments:', doc._.assessments)
Polarity: 0.55
Sujectivity: 0.65
Assessments: [(['wow'], 0.1, 1.0, None), (['best', '!'], 1.0, 0.3, None)]
API
To make the usage simpler spacy provides custom extensions which a library can use. This makes it easier for the user to get the desired data. The below tables summaries the extensions.
spacy.Doc
extensions
Extension | Type | Description | Default |
---|---|---|---|
doc._.polarity | Float |
The polarity of the document. The polarity score is a float within the range [-1.0, 1.0]. | None |
doc._.sujectivity | Float |
The subjectivity of the document. The subjectivity is a float within the range [0.0, 1.0] where 0.0 is very objective and 1.0 is very subjective. | None |
doc._.assessments | tuple |
Return a tuple of form (polarity, subjectivity, assessments ) where 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. | None |
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
spacytextblob-0.1.2.tar.gz
(3.9 kB
view hashes)
Built Distribution
Close
Hashes for spacytextblob-0.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 17f4ed4ac93eee7a50d92849468befac4387b094bec69713caa02b0d4353c755 |
|
MD5 | 2672ae047b426a61687e0ba61f141e6b |
|
BLAKE2b-256 | 6534cca77e239b09d5e00bc060435ec6c46823b63ceabc338b04c89904b5429f |