Puntúa automáticamente la satisfacción o frustración en miles de comentarios usando modelos de lenguaje pre-entrenados.
Automatically score satisfaction or frustration across thousands of comments using pre-trained language models.
Este ejercicio es solo para aprendizaje y pruebas. No ejecutes modelos contra datos reales, sensibles, financieros, médicos, personales o ambientes de producción sin autorización, respaldos, pruebas previas, permisos mínimos, control de cambios, revisión humana y cumplimiento de los protocolos de ciberseguridad y privacidad de tu organización.
This exercise is for learning and testing only. Do not run models against real, sensitive, financial, medical, personal, or production data without authorization, backups, prior testing, least-privilege permissions, change control, human review, and compliance with your organization's cybersecurity and privacy protocols.
Usar un pipeline de sentiment analysis para clasificar comentarios como positivos o negativos y producir un reporte rápido de tono general. Este patrón ayuda a priorizar retroalimentaciones, detectar frustración y resumir grandes volúmenes de texto.
Use a sentiment analysis pipeline to classify comments as positive or negative and produce a quick tone report. This pattern helps prioritize feedback, detect frustration, and summarize large volumes of text.
| ID | Comentario |
|---|---|
| 1 | The new system is slow and confusing. |
| 2 | I am very happy with the new process. |
| 3 | El servicio fue rápido y profesional. |
| 4 | La espera fue demasiado larga. |
| 5 | Everything worked perfectly today. |
from transformers import pipeline
nlp = pipeline("sentiment-analysis")
resultado = nlp("¡El nuevo sistema es lento! / The new system is slow!")
print(resultado)# ==========================================================
# Python Advanced Topic 05
# Evaluate Feedback Sentiment with Transformers
# ==========================================================
import pandas as pd
from transformers import pipeline
# 1. Create sample feedback records
comments = [
{"id": 1, "comment": "The new system is slow and confusing."},
{"id": 2, "comment": "I am very happy with the new process."},
{"id": 3, "comment": "The staff was helpful and professional."},
{"id": 4, "comment": "The wait time was too long."},
{"id": 5, "comment": "Everything worked perfectly today."}
]
df = pd.DataFrame(comments)
# 2. Load a pre-trained sentiment analysis pipeline
# First execution may download the model.
nlp = pipeline("sentiment-analysis")
# 3. Score each comment
def score_comment(text):
result = nlp(text)[0]
return pd.Series({
"sentiment_label": result["label"],
"sentiment_score": round(result["score"], 4)
})
df[["sentiment_label", "sentiment_score"]] = df["comment"].apply(score_comment)
# 4. Generate summary
summary = (
df.groupby("sentiment_label")
.size()
.reset_index(name="count")
.sort_values("count", ascending=False)
)
# 5. Export results
output_file = "feedback_sentiment_report.csv"
df.to_csv(output_file, index=False)
print(df)
print("
Summary:")
print(summary)
print(f"
Report created: {output_file}")id comment sentiment_label sentiment_score 0 1 The new system is slow and confusing. NEGATIVE 0.9981 1 2 I am very happy with the new process. POSITIVE 0.9997 2 3 The staff was helpful and professional. POSITIVE 0.9996 3 4 The wait time was too long. NEGATIVE 0.9969 4 5 Everything worked perfectly today. POSITIVE 0.9998 Summary: sentiment_label count POSITIVE 3 NEGATIVE 2
| id | comment | sentiment_label | sentiment_score |
|---|---|---|---|
| 1 | The new system is slow and confusing. | NEGATIVE | 0.9981 |
| 2 | I am very happy with the new process. | POSITIVE | 0.9997 |
| 3 | The staff was helpful and professional. | POSITIVE | 0.9996 |
| 4 | The wait time was too long. | NEGATIVE | 0.9969 |
| 5 | Everything worked perfectly today. | POSITIVE | 0.9998 |
Este patrón convierte texto libre en una señal analítica. Es útil para dashboards, monitoreo de servicios, seguimiento de satisfacción y detección temprana de frustración.
This pattern turns free text into an analytical signal. It is useful for dashboards, service monitoring, satisfaction tracking, and early frustration detection.