Solution: spaCy and Transformers
Let's look at the solutions to the previous exercises.
We'll cover the following...
Press + to interact
import spacyfrom transformers import pipeline# Load spaCy's pre-trained model for sentiment analysisnlp = spacy.load("en_core_web_trf")# Load transformers' sentiment analysis pipelinesentiment_analyzer = pipeline("sentiment-analysis")# The sentence to analyzesentence = "I really enjoy using this product. It's amazing!"# Process the sentence with spaCydoc = nlp(sentence)# Get the text of the sentence without any stopwordsclean_text = " ".join(token.text for token in doc if not token.is_stop)# Analyze the sentiment of the cleaned text using transformers' sentiment analysis pipelinesentiment = sentiment_analyzer(clean_text)[0]# Print the sentiment label and scoreprint("Sentiment:", sentiment["label"])print("Score:", sentiment["score"])