Update modules/text_analysis/discourse_analysis.py
Browse files
modules/text_analysis/discourse_analysis.py
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
-
modules/text_analysis/discourse_analysis.py
|
2 |
-
|
3 |
import streamlit as st
|
4 |
import spacy
|
5 |
import networkx as nx
|
@@ -10,7 +9,6 @@ import logging
|
|
10 |
|
11 |
logger = logging.getLogger(__name__)
|
12 |
|
13 |
-
|
14 |
from .semantic_analysis import (
|
15 |
create_concept_graph,
|
16 |
visualize_concept_graph,
|
@@ -24,60 +22,74 @@ from .semantic_analysis import (
|
|
24 |
def compare_semantic_analysis(text1, text2, nlp, lang):
|
25 |
"""
|
26 |
Realiza el análisis semántico comparativo entre dos textos
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
"""
|
28 |
-
|
29 |
-
|
|
|
|
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
fig1.suptitle("")
|
45 |
-
fig2.suptitle("")
|
46 |
|
47 |
-
|
|
|
|
|
48 |
|
49 |
def create_concept_table(key_concepts):
|
50 |
"""
|
51 |
Crea una tabla de conceptos clave con sus frecuencias
|
|
|
|
|
|
|
|
|
52 |
"""
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
56 |
|
57 |
def perform_discourse_analysis(text1, text2, nlp, lang):
|
58 |
"""
|
59 |
Realiza el análisis completo del discurso
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
"""
|
61 |
try:
|
62 |
-
#
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
# Identificar conceptos clave
|
67 |
-
key_concepts1 = identify_key_concepts(doc1, min_freq=2, min_length=3)
|
68 |
-
key_concepts2 = identify_key_concepts(doc2, min_freq=2, min_length=3)
|
69 |
|
70 |
-
# Crear
|
71 |
-
G1 = create_concept_graph(doc1, key_concepts1)
|
72 |
-
G2 = create_concept_graph(doc2, key_concepts2)
|
73 |
-
|
74 |
-
# Visualizar grafos
|
75 |
-
fig1 = visualize_concept_graph(G1, lang)
|
76 |
-
fig2 = visualize_concept_graph(G2, lang)
|
77 |
-
fig1.suptitle("")
|
78 |
-
fig2.suptitle("")
|
79 |
-
|
80 |
-
# Crear tablas
|
81 |
table1 = create_concept_table(key_concepts1)
|
82 |
table2 = create_concept_table(key_concepts2)
|
83 |
|
@@ -92,8 +104,8 @@ def perform_discourse_analysis(text1, text2, nlp, lang):
|
|
92 |
}
|
93 |
|
94 |
except Exception as e:
|
95 |
-
logger.error(f"Error en
|
96 |
return {
|
97 |
'success': False,
|
98 |
'error': str(e)
|
99 |
-
}
|
|
|
1 |
+
# modules/text_analysis/discourse_analysis.py
|
|
|
2 |
import streamlit as st
|
3 |
import spacy
|
4 |
import networkx as nx
|
|
|
9 |
|
10 |
logger = logging.getLogger(__name__)
|
11 |
|
|
|
12 |
from .semantic_analysis import (
|
13 |
create_concept_graph,
|
14 |
visualize_concept_graph,
|
|
|
22 |
def compare_semantic_analysis(text1, text2, nlp, lang):
|
23 |
"""
|
24 |
Realiza el análisis semántico comparativo entre dos textos
|
25 |
+
Args:
|
26 |
+
text1: Primer texto a analizar
|
27 |
+
text2: Segundo texto a analizar
|
28 |
+
nlp: Modelo de spaCy cargado
|
29 |
+
lang: Código de idioma
|
30 |
+
Returns:
|
31 |
+
tuple: (fig1, fig2, key_concepts1, key_concepts2)
|
32 |
"""
|
33 |
+
try:
|
34 |
+
# Procesar los textos
|
35 |
+
doc1 = nlp(text1)
|
36 |
+
doc2 = nlp(text2)
|
37 |
|
38 |
+
# Identificar conceptos clave con parámetros específicos
|
39 |
+
key_concepts1 = identify_key_concepts(doc1, min_freq=2, min_length=3)
|
40 |
+
key_concepts2 = identify_key_concepts(doc2, min_freq=2, min_length=3)
|
41 |
|
42 |
+
# Crear y visualizar grafos
|
43 |
+
G1 = create_concept_graph(doc1, key_concepts1)
|
44 |
+
G2 = create_concept_graph(doc2, key_concepts2)
|
45 |
|
46 |
+
fig1 = visualize_concept_graph(G1, lang)
|
47 |
+
fig2 = visualize_concept_graph(G2, lang)
|
48 |
+
|
49 |
+
# Limpiar títulos
|
50 |
+
fig1.suptitle("")
|
51 |
+
fig2.suptitle("")
|
52 |
|
53 |
+
return fig1, fig2, key_concepts1, key_concepts2
|
|
|
|
|
54 |
|
55 |
+
except Exception as e:
|
56 |
+
logger.error(f"Error en compare_semantic_analysis: {str(e)}")
|
57 |
+
raise
|
58 |
|
59 |
def create_concept_table(key_concepts):
|
60 |
"""
|
61 |
Crea una tabla de conceptos clave con sus frecuencias
|
62 |
+
Args:
|
63 |
+
key_concepts: Lista de tuplas (concepto, frecuencia)
|
64 |
+
Returns:
|
65 |
+
pandas.DataFrame: Tabla formateada de conceptos
|
66 |
"""
|
67 |
+
try:
|
68 |
+
df = pd.DataFrame(key_concepts, columns=['Concepto', 'Frecuencia'])
|
69 |
+
df['Frecuencia'] = df['Frecuencia'].round(2)
|
70 |
+
return df
|
71 |
+
except Exception as e:
|
72 |
+
logger.error(f"Error en create_concept_table: {str(e)}")
|
73 |
+
raise
|
74 |
|
75 |
def perform_discourse_analysis(text1, text2, nlp, lang):
|
76 |
"""
|
77 |
Realiza el análisis completo del discurso
|
78 |
+
Args:
|
79 |
+
text1: Primer texto a analizar
|
80 |
+
text2: Segundo texto a analizar
|
81 |
+
nlp: Modelo de spaCy cargado
|
82 |
+
lang: Código de idioma
|
83 |
+
Returns:
|
84 |
+
dict: Resultados del análisis
|
85 |
"""
|
86 |
try:
|
87 |
+
# Realizar análisis comparativo
|
88 |
+
fig1, fig2, key_concepts1, key_concepts2 = compare_semantic_analysis(
|
89 |
+
text1, text2, nlp, lang
|
90 |
+
)
|
|
|
|
|
|
|
91 |
|
92 |
+
# Crear tablas de resultados
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
table1 = create_concept_table(key_concepts1)
|
94 |
table2 = create_concept_table(key_concepts2)
|
95 |
|
|
|
104 |
}
|
105 |
|
106 |
except Exception as e:
|
107 |
+
logger.error(f"Error en perform_discourse_analysis: {str(e)}")
|
108 |
return {
|
109 |
'success': False,
|
110 |
'error': str(e)
|
111 |
+
}
|