A TextBlob sentiment analysis pipeline compponent for spaCy.
Project description
spaCyTextBlob
A TextBlob sentiment analysis pipeline compponent for spaCy.
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.1.tar.gz
(3.7 kB
view hashes)
Built Distribution
Close
Hashes for spacytextblob-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a1e5eb5d68f938f3c65599f87def412ce7c4ffc638a4a4e9ac7d3626169f5439 |
|
MD5 | fdc5916f9ebaca0448ead0918ccdecf4 |
|
BLAKE2b-256 | 6412fc4ab3cd40213cb3efe0689582d8df8d12e875f92ea57aad4b2861c67038 |