← Back to home

Natural Language Processing with Transformers

A basic first step into Natural Language Processing

About the Transformers Library

The transformers library provides an interface to interact with models.

In this post, I’ll share how we can set up a simple sentiment analysis of text.

Setting things up

pip install transformers
from transformers import pipeline

Sentiment Analysis

Positive, Negative

classifier = pipeline("sentiment-analysis", model = "distilbert/distilbert-base-uncased-finetuned-sst-2-english")
data = ["This product is good", "I don't like this product"]
classifier(data)

[{'label': 'POSITIVE', 'score': 0.9998680353164673},
 {'label': 'NEGATIVE', 'score': 0.9953964352607727}]

Emotion Detection

classifier = pipeline("sentiment-analysis", model = "SamLowe/roberta-base-go_emotions", top_k=None)
data = [ "It was perfect for the first two days and then it was awful"]
results = classifier(data)
print(results)

[[{'label': 'disgust', 'score': 0.7053156495094299}, 
{'label': 'annoyance', 'score': 0.12410394847393036}, 
{'label': 'approval', 'score': 0.0792454406619072}, 
{'label': 'disapproval', 'score': 0.050548385828733444}, 
{'label': 'neutral', 'score': 0.04294545575976372}, 
{'label': 'disappointment', 'score': 0.03646270930767059},
{'label': 'admiration', 'score': 0.018407044932246208}, 
{'label': 'embarrassment', 'score': 0.016334818676114082}, 
{'label': 'anger', 'score': 0.01377098634839058}, 
{'label': 'realization', 'score': 0.01139384787529707}, 
{'label': 'fear', 'score': 0.01103982049971819}, 
{'label': 'love', 'score': 0.00794794037938118}, 
{'label': 'sadness', 'score': 0.007545181084424257}, 
{'label': 'gratitude', 'score': 0.005044595338404179}, 
{'label': 'desire', 'score': 0.0035985559225082397}, 
{'label': 'amusement', 'score': 0.0033641094341874123}, 
{'label': 'confusion', 'score': 0.003023572964593768}, 
{'label': 'excitement', 'score': 0.0023223150055855513}, 
{'label': 'curiosity', 'score': 0.002252183621749282}, 
{'label': 'optimism', 'score': 0.002141808858141303}, 
{'label': 'joy', 'score': 0.0018355769570916891}, 
{'label': 'remorse', 'score': 0.0016548713902011514}, 
{'label': 'surprise', 'score': 0.0016250367043539882}, 
{'label': 'caring', 'score': 0.001429390744306147}, 
{'label': 'nervousness', 'score': 0.0010988765861839056}, 
{'label': 'relief', 'score': 0.0010947092669084668}, 
{'label': 'pride', 'score': 0.0010576138738542795}, 
{'label': 'grief', 'score': 0.0008262579794973135}]]