Update modules/text_analysis/discourse_analysis.py
Browse files
modules/text_analysis/discourse_analysis.py
CHANGED
@@ -1,72 +1,67 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import spacy
|
3 |
-
import networkx as nx
|
4 |
-
import matplotlib.pyplot as plt
|
5 |
-
import pandas as pd
|
6 |
-
import numpy as np
|
7 |
-
from .semantic_analysis import (
|
8 |
-
create_concept_graph,
|
9 |
-
visualize_concept_graph,
|
10 |
-
identify_key_concepts,
|
11 |
-
POS_COLORS,
|
12 |
-
POS_TRANSLATIONS,
|
13 |
-
ENTITY_LABELS
|
14 |
-
)
|
15 |
-
|
16 |
-
def compare_semantic_analysis(text1, text2, nlp, lang):
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
with col2:
|
69 |
-
with st.expander(t['doc2_title'], expanded=True):
|
70 |
-
st.pyplot(analysis_result['graph2'])
|
71 |
-
st.subheader(t['key_concepts'])
|
72 |
-
st.table(analysis_result['table2'])
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import spacy
|
3 |
+
import networkx as nx
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import pandas as pd
|
6 |
+
import numpy as np
|
7 |
+
from .semantic_analysis import (
|
8 |
+
create_concept_graph,
|
9 |
+
visualize_concept_graph,
|
10 |
+
identify_key_concepts,
|
11 |
+
POS_COLORS,
|
12 |
+
POS_TRANSLATIONS,
|
13 |
+
ENTITY_LABELS
|
14 |
+
)
|
15 |
+
|
16 |
+
def compare_semantic_analysis(text1, text2, nlp, lang):
|
17 |
+
"""
|
18 |
+
Realiza el análisis semántico comparativo entre dos textos
|
19 |
+
"""
|
20 |
+
doc1 = nlp(text1)
|
21 |
+
doc2 = nlp(text2)
|
22 |
+
|
23 |
+
# Identificar conceptos clave para ambos documentos
|
24 |
+
key_concepts1 = identify_key_concepts(doc1)
|
25 |
+
key_concepts2 = identify_key_concepts(doc2)
|
26 |
+
|
27 |
+
# Crear grafos de conceptos para ambos documentos
|
28 |
+
G1 = create_concept_graph(doc1, key_concepts1)
|
29 |
+
G2 = create_concept_graph(doc2, key_concepts2)
|
30 |
+
|
31 |
+
# Visualizar los grafos de conceptos
|
32 |
+
fig1 = visualize_concept_graph(G1, lang)
|
33 |
+
fig2 = visualize_concept_graph(G2, lang)
|
34 |
+
|
35 |
+
# Remover los títulos superpuestos
|
36 |
+
fig1.suptitle("")
|
37 |
+
fig2.suptitle("")
|
38 |
+
|
39 |
+
return fig1, fig2, key_concepts1, key_concepts2
|
40 |
+
|
41 |
+
def create_concept_table(key_concepts):
|
42 |
+
"""
|
43 |
+
Crea una tabla de conceptos clave con sus frecuencias
|
44 |
+
"""
|
45 |
+
df = pd.DataFrame(key_concepts, columns=['Concepto', 'Frecuencia'])
|
46 |
+
df['Frecuencia'] = df['Frecuencia'].round(2)
|
47 |
+
return df
|
48 |
+
|
49 |
+
def perform_discourse_analysis(text1, text2, nlp, lang):
|
50 |
+
"""
|
51 |
+
Realiza el análisis completo del discurso
|
52 |
+
"""
|
53 |
+
graph1, graph2, key_concepts1, key_concepts2 = compare_semantic_analysis(text1, text2, nlp, lang)
|
54 |
+
|
55 |
+
# Crear tablas de conceptos clave
|
56 |
+
table1 = create_concept_table(key_concepts1)
|
57 |
+
table2 = create_concept_table(key_concepts2)
|
58 |
+
|
59 |
+
return {
|
60 |
+
'graph1': graph1,
|
61 |
+
'graph2': graph2,
|
62 |
+
'key_concepts1': key_concepts1,
|
63 |
+
'key_concepts2': key_concepts2,
|
64 |
+
'table1': table1,
|
65 |
+
'table2': table2,
|
66 |
+
'success': True
|
67 |
+
}
|
|
|
|
|
|
|
|
|
|