Skip to content

API Reference

Custom attributes

When you add spacytextblob into your spaCy pipeline it exposes a custom attribute ._.blob. This attribute is available for for the Doc, Span, and Token classes from spaCy.

  • Doc._.blob
  • Span._.blob
  • Token._.blob

The section below outlines commonly accessed ._.blob attributes and methods. See the textblob docs for the complete listing of all attributes and methods that are available in ._.blob.

Attributes

Name Type Description
doc._.blob.polarity Float The polarity of the document. The polarity score is a float within the range [-1.0, 1.0].
doc._.blob.subjectivity 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.
doc._.blob.sentiment_assessments.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.

Methods

doc._.blob.ngrams

Name Type Description
n int The number of words to include in the ngram. By default 3.
RETURNS List[WordLists]

Config

When adding spacytextblob to your spaCy pipeline you can optionally pass additional parameters into the config parameter:

Name Type Description
custom_blob Dict[str, str] The "custom_blob" key should be assigned to a dictionary that tells spaCy what function to replace textblob.TextBlob with. In this case, we want to replace it with TextBlobDE. The key of the dictionary is "@misc". This tells spaCy to look into the misc section of the spaCy register. The value should be the string name of a function that you have registered with spaCy. See the TextBlob extensions section for more details.
import spacy
from spacytextblob.spacytextblob import SpacyTextBlob

nlp = spacy.load("de_core_news_sm")

nlp.add_pipe( "spacytextblob", config={
    "custom_blob": ...    # Dict[str, str]
})

Examples

Using spacytextblob without an extension:

import spacy

from spacytextblob.spacytextblob import SpacyTextBlob  # noqa: F401

nlp = spacy.load("en_core_web_sm")
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."
nlp.add_pipe("spacytextblob")
doc = nlp(text)

print(doc._.blob.polarity)
# -0.125

print(doc._.blob.subjectivity)
# 0.9

print(doc._.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)]

print(doc._.blob.ngrams())
# [WordList(['I', 'had', 'a']), WordList(['had', 'a', 'really']), WordList(['a', 'really', 'horrible']), WordList(['really', 'horrible', 'day']), WordList(['horrible', 'day', 'It']), WordList(['day', 'It', 'was']), WordList(['It', 'was', 'the']), WordList(['was', 'the', 'worst']), WordList(['the', 'worst', 'day']), WordList(['worst', 'day', 'ever']), WordList(['day', 'ever', 'But']), WordList(['ever', 'But', 'every']), WordList(['But', 'every', 'now']), WordList(['every', 'now', 'and']), WordList(['now', 'and', 'then']), WordList(['and', 'then', 'I']), WordList(['then', 'I', 'have']), WordList(['I', 'have', 'a']), WordList(['have', 'a', 'really']), WordList(['a', 'really', 'good']), WordList(['really', 'good', 'day']), WordList(['good', 'day', 'that']), WordList(['day', 'that', 'makes']), WordList(['that', 'makes', 'me']), WordList(['makes', 'me', 'happy'])]

Using spacytextblob with an extension:

import spacy
from textblob import Blobber
from textblob_fr import PatternAnalyzer, PatternTagger

from spacytextblob.spacytextblob import SpacyTextBlob  # noqa: F401


@spacy.registry.misc("spacytextblob.fr_blob")
def create_fr_blob():
    tb = Blobber(pos_tagger=PatternTagger(), analyzer=PatternAnalyzer())
    return tb


config = {"custom_blob": {"@misc": "spacytextblob.fr_blob"}}

nlp_fr = spacy.load("fr_core_news_sm")
nlp_fr.add_pipe("spacytextblob", config=config)
text = "Quelle belle matinée"
doc = nlp_fr(text)

print(doc)
# Quelle belle matinée
print(doc._.blob)
# Quelle belle matinée
print(doc._.blob.sentiment)
# (0.8, 0.8)