Natural Language Processing (NLP) in Python
Fouad Trad
Machine Learning Engineer




from transformers import pipelineclassification_pipeline = pipeline(task="sentiment-analysis", # or text-classificationmodel="distilbert/distilbert-base-uncased-finetuned-sst-2-english" )result = classification_pipeline("I really liked the movie!!")print(result)
[{'label': 'POSITIVE', 'score': 0.9998093247413635}]
texts = ["I really liked the movie!!", "Great job ruining my day.", "This product exceeded my expectations.", "Wow, just what I needed... another problem.", "Absolutely fantastic experience!"]results = classification_pipeline(texts)print(results)
[{'label': 'POSITIVE', 'score': 0.9998093247413635},
{'label': 'NEGATIVE', 'score': 0.8666700124740601},
{'label': 'POSITIVE', 'score': 0.998874843120575},
{'label': 'POSITIVE', 'score': 0.98626708984375},
{'label': 'POSITIVE', 'score': 0.9998812675476074}]
texts = ["I really liked the movie!!", "Great job ruining my day.", "This product exceeded my expectations.", "Wow, just what I needed... another problem.", "Absolutely fantastic experience!"]true_labels = ["POSITIVE", "NEGATIVE", "POSITIVE", "NEGATIVE", "POSITIVE"]results = classification_pipeline(texts)predicted_labels = [result['label'] for result in results]
from sklearn.metrics import accuracy_score accuracy = accuracy_score(true_labels, predicted_labels)print(f"Accuracy: {accuracy}")
Accuracy: 0.80
Natural Language Processing (NLP) in Python