Python Advanced Series

5. Evaluar tono de retroalimentaciones

5. Evaluate Feedback Sentiment

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.

⚠️ Advertencia de ciberseguridad, datos y producción ⚠️ Cybersecurity, data, and production warning

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.

Objetivo del ejercicio

Exercise objective

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.

Datos sintéticos

Synthetic data

IDComentario
1The new system is slow and confusing.
2I am very happy with the new process.
3El servicio fue rápido y profesional.
4La espera fue demasiado larga.
5Everything worked perfectly today.

Ejemplo base

Base example

from transformers import pipeline

nlp = pipeline("sentiment-analysis")
resultado = nlp("¡El nuevo sistema es lento! / The new system is slow!")
print(resultado)

Script completo

Complete script

# ==========================================================
# 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}")

Salida esperada en consola

Expected console output

   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

Reporte CSV generado

Generated CSV report

idcommentsentiment_labelsentiment_score
1The new system is slow and confusing.NEGATIVE0.9981
2I am very happy with the new process.POSITIVE0.9997
3The staff was helpful and professional.POSITIVE0.9996
4The wait time was too long.NEGATIVE0.9969
5Everything worked perfectly today.POSITIVE0.9998

Aplicaciones reales

Real-world applications

  • Analizar encuestas de estudiantes o clientes.
  • Priorizar comentarios negativos.
  • Crear KPIs de satisfacción por periodo.
  • Resumir miles de textos sin leerlos uno por uno.
  • Analyze student or customer surveys.
  • Prioritize negative comments.
  • Create satisfaction KPIs by period.
  • Summarize thousands of texts without reading one by one.

Recomendaciones

Recommendations

  • Validar resultados con revisión humana.
  • Usar modelos multilingües si hay español e inglés.
  • No tomar decisiones disciplinarias solo con el modelo.
  • Guardar comentarios originales y puntuaciones para auditoría.
  • Validate results with human review.
  • Use multilingual models when Spanish and English are present.
  • Do not make disciplinary decisions based only on the model.
  • Keep original comments and scores for auditability.

Valor del tópico

Topic value

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.