Spaces:
Running
Running
| # modules/text_analysis/discourse_analysis.py | |
| import streamlit as st | |
| import spacy | |
| import networkx as nx | |
| import matplotlib.pyplot as plt | |
| import pandas as pd | |
| import numpy as np | |
| import logging | |
| logger = logging.getLogger(__name__) | |
| from .semantic_analysis import ( | |
| create_concept_graph, | |
| visualize_concept_graph, | |
| identify_key_concepts, | |
| get_stopwords, | |
| POS_COLORS, | |
| POS_TRANSLATIONS, | |
| ENTITY_LABELS | |
| ) | |
| def compare_semantic_analysis(text1, text2, nlp, lang): | |
| """ | |
| Realiza el análisis semántico comparativo entre dos textos | |
| Args: | |
| text1: Primer texto a analizar | |
| text2: Segundo texto a analizar | |
| nlp: Modelo de spaCy cargado | |
| lang: Código de idioma | |
| Returns: | |
| tuple: (fig1, fig2, key_concepts1, key_concepts2) | |
| """ | |
| try: | |
| # Procesar los textos | |
| doc1 = nlp(text1) | |
| doc2 = nlp(text2) | |
| # Identificar conceptos clave con parámetros específicos | |
| key_concepts1 = identify_key_concepts(doc1, min_freq=2, min_length=3) | |
| key_concepts2 = identify_key_concepts(doc2, min_freq=2, min_length=3) | |
| # Crear y visualizar grafos | |
| G1 = create_concept_graph(doc1, key_concepts1) | |
| G2 = create_concept_graph(doc2, key_concepts2) | |
| fig1 = visualize_concept_graph(G1, lang) | |
| fig2 = visualize_concept_graph(G2, lang) | |
| # Limpiar títulos | |
| fig1.suptitle("") | |
| fig2.suptitle("") | |
| return fig1, fig2, key_concepts1, key_concepts2 | |
| except Exception as e: | |
| logger.error(f"Error en compare_semantic_analysis: {str(e)}") | |
| raise | |
| def create_concept_table(key_concepts): | |
| """ | |
| Crea una tabla de conceptos clave con sus frecuencias | |
| Args: | |
| key_concepts: Lista de tuplas (concepto, frecuencia) | |
| Returns: | |
| pandas.DataFrame: Tabla formateada de conceptos | |
| """ | |
| try: | |
| df = pd.DataFrame(key_concepts, columns=['Concepto', 'Frecuencia']) | |
| df['Frecuencia'] = df['Frecuencia'].round(2) | |
| return df | |
| except Exception as e: | |
| logger.error(f"Error en create_concept_table: {str(e)}") | |
| raise | |
| def perform_discourse_analysis(text1, text2, nlp, lang): | |
| """ | |
| Realiza el análisis completo del discurso | |
| Args: | |
| text1: Primer texto a analizar | |
| text2: Segundo texto a analizar | |
| nlp: Modelo de spaCy cargado | |
| lang: Código de idioma | |
| Returns: | |
| dict: Resultados del análisis | |
| """ | |
| try: | |
| # Realizar análisis comparativo | |
| fig1, fig2, key_concepts1, key_concepts2 = compare_semantic_analysis( | |
| text1, text2, nlp, lang | |
| ) | |
| # Crear tablas de resultados | |
| table1 = create_concept_table(key_concepts1) | |
| table2 = create_concept_table(key_concepts2) | |
| return { | |
| 'graph1': fig1, | |
| 'graph2': fig2, | |
| 'key_concepts1': key_concepts1, | |
| 'key_concepts2': key_concepts2, | |
| 'table1': table1, | |
| 'table2': table2, | |
| 'success': True | |
| } | |
| except Exception as e: | |
| logger.error(f"Error en perform_discourse_analysis: {str(e)}") | |
| return { | |
| 'success': False, | |
| 'error': str(e) | |
| } |