Skip to content

Instantly share code, notes, and snippets.

@alonsoir
Created January 22, 2025 12:30
Show Gist options
  • Save alonsoir/39947b3ab1e4cf942f04b77a0d9aa52d to your computer and use it in GitHub Desktop.
Save alonsoir/39947b3ab1e4cf942f04b77a0d9aa52d to your computer and use it in GitHub Desktop.

(attendance-system-py3.10) ┌<▸> ~/g/attendance_system └➤ poetry add textblob

The following packages are already present in the pyproject.toml and will be skipped:

  • textblob

If you want to update it to the latest compatible version, you can use poetry update package. If you prefer to upgrade it to the latest available version, you can use poetry add package@latest.

Nothing to add. (attendance-system-py3.10) ┌<▸> ~/g/attendance_system └➤ poetry run python -m textblob.download_corpora

[nltk_data] Downloading package brown to /Users/aironman/nltk_data... [nltk_data] Unzipping corpora/brown.zip. [nltk_data] Downloading package punkt_tab to [nltk_data] /Users/aironman/nltk_data... [nltk_data] Unzipping tokenizers/punkt_tab.zip. [nltk_data] Downloading package wordnet to [nltk_data] /Users/aironman/nltk_data... [nltk_data] Downloading package averaged_perceptron_tagger_eng to [nltk_data] /Users/aironman/nltk_data... [nltk_data] Unzipping taggers/averaged_perceptron_tagger_eng.zip. [nltk_data] Downloading package conll2000 to [nltk_data] /Users/aironman/nltk_data... [nltk_data] Unzipping corpora/conll2000.zip. [nltk_data] Downloading package movie_reviews to [nltk_data] /Users/aironman/nltk_data... [nltk_data] Unzipping corpora/movie_reviews.zip. Finished. (attendance-system-py3.10) ┌<▸> ~/g/attendance_system └➤ poetry run python sentiment_analysis.py

Sentimiento: Polaridad = 0.0, Subjetividad = 0.0 Traducción al inglés: I love programming, but sometimes it's frustrating. Sentimiento del texto traducido: Polaridad = 0.04999999999999999, Subjetividad = 0.75 Corrección: Ll cabin climatico es un problems global. Frases nominales: ['encanta programar', 'veces es frustrante'] 2025-01-22 13:29:45.023 Python[8565:327244] +[IMKClient subclass]: chose IMKClient_Modern 2025-01-22 13:29:45.023 Python[8565:327244] +[IMKInputSession subclass]: chose IMKInputSession_Modern

import matplotlib.pyplot as plt
from textblob import TextBlob
from deep_translator import GoogleTranslator
# Lista de textos para análisis de sentimientos
texts = [
"I love programming!",
"Python is an amazing language.",
"I hate bugs in my code.",
"Debugging is fun but challenging."
]
# Análisis de sentimientos
polaridades = [TextBlob(text).sentiment.polarity for text in texts]
# Texto original en español
texto_original = "Me encanta programar, pero a veces es frustrante."
# Análisis de sentimientos con TextBlob
blob = TextBlob(texto_original)
sentimiento = blob.sentiment
print(f"Sentimiento: Polaridad = {sentimiento.polarity}, Subjetividad = {sentimiento.subjectivity}")
# Traducción usando deep-translator
traduccion = GoogleTranslator(source='auto', target='en').translate(texto_original)
print(f"Traducción al inglés: {traduccion}")
# Análisis de sentimientos en el texto traducido
blob_traducido = TextBlob(traduccion)
sentimiento_traducido = blob_traducido.sentiment
print(f"Sentimiento del texto traducido: Polaridad = {sentimiento_traducido.polarity}, Subjetividad = {sentimiento_traducido.subjectivity}")
# Corrección ortográfica
texto_erroneo = "El cambió climatico es un problema global."
blob_erroneo = TextBlob(texto_erroneo)
print("Corrección:", blob_erroneo.correct())
# Extracción de frases nominales
print("Frases nominales:", blob.noun_phrases)
# Visualización del análisis de sentimientos
plt.barh(texts, polaridades, color='skyblue')
plt.xlabel('Polaridad')
plt.title('Análisis de Sentimientos')
plt.axvline(0, color='red', linestyle='--') # Línea para indicar neutralidad
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment