Skip to content

TextBlob Extensions

Usage

TextBlob supports adding custom models and new languages through “extensions”. Check out the TextBlob docs (https://textblob.readthedocs.io/en/dev/extensions.html#extensions) for more details.

spacytextblob also supports the use of TextBlob extensions. To use a TextBlob extension you need to pass some additional information to the config parameter when initializing the spacytextblob pipeline component.

import spacy
from spacytextblob.spacytextblob import SpacyTextBlob
from textblob import Blobber
from textblob_fr import PatternTagger, PatternAnalyzer # 

text = u"Quelle belle matinée"

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

nlp_fr = spacy.load("fr_core_news_sm")

nlp_fr.add_pipe(
    "spacytextblob", # 
    config={ # 
        "custom_blob": {"@misc": "spacytextblob.fr_blob"} # 
    }
)

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)

Extensions

The following extensions have been tested and are supported. Other extensions may work, but have not been tested.

textblob-fr

textblob-fr is a TextBlob extension that enables French language support for TextBlob (https://github.com/sloria/textblob-fr).

pip install textblob-fr

To use it with spacytextblob First install a spaCy model that supports French (https://spacy.io/models/fr):

python -m spacy download fr_core_news_sm

The code below demonstrates how you can then use and access textblob-fr within spacytextblob.

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)

textblob-de

Warning

textblob-de is not supported. As of spacytextblob 4.1.0. The textblob-de library depends on a Google Translate feature that no longer works. More details can be found in this issue https://github.com/markuskiller/textblob-de/issues/24.

textblob-aptagger

Warning

textblob-aptagger is not supported. As of TextBlob 0.11.0, TextBlob uses NLTK's averaged perceptron tagger by default. This package is no longer necessary (https://github.com/sloria/textblob-aptagger).