...

/

Solution: spaCy and Transformers

Solution: spaCy and Transformers

Let's look at the solutions to the previous exercises.

Exercise 1: Solution

Here is the solution to Exercise 1: Using transformers to perform sentiment analysis: 

Press + to interact
import spacy
from transformers import pipeline
# Load spaCy's pre-trained model for sentiment analysis
nlp = spacy.load("en_core_web_trf")
# Load transformers' sentiment analysis pipeline
sentiment_analyzer = pipeline("sentiment-analysis")
# The sentence to analyze
sentence = "I really enjoy using this product. It's amazing!"
# Process the sentence with spaCy
doc = nlp(sentence)
# Get the text of the sentence without any stopwords
clean_text = " ".join(token.text for token in doc if not token.is_stop)
# Analyze the sentiment of the cleaned text using transformers' sentiment analysis pipeline
sentiment = sentiment_analyzer(clean_text)[0]
# Print the sentiment label and score
print("Sentiment:", sentiment["label"])
print("Score:", sentiment["score"])

Solution explanation

    ...