AIdeaText commited on
Commit
5203a97
1 Parent(s): b7ade80

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
- doc1 = nlp(text1)
18
- doc2 = nlp(text2)
19
-
20
- # Identificar conceptos clave para ambos documentos
21
- key_concepts1 = identify_key_concepts(doc1)
22
- key_concepts2 = identify_key_concepts(doc2)
23
-
24
- # Crear grafos de conceptos para ambos documentos
25
- G1 = create_concept_graph(doc1, key_concepts1)
26
- G2 = create_concept_graph(doc2, key_concepts2)
27
-
28
- # Visualizar los grafos de conceptos
29
- fig1 = visualize_concept_graph(G1, lang)
30
- fig2 = visualize_concept_graph(G2, lang)
31
-
32
- # Remover los títulos superpuestos
33
- fig1.suptitle("")
34
- fig2.suptitle("")
35
-
36
- return fig1, fig2, key_concepts1, key_concepts2
37
-
38
- def create_concept_table(key_concepts):
39
- df = pd.DataFrame(key_concepts, columns=['Concepto', 'Frecuencia'])
40
- df['Frecuencia'] = df['Frecuencia'].round(2)
41
- return df
42
-
43
- def perform_discourse_analysis(text1, text2, nlp, lang):
44
- graph1, graph2, key_concepts1, key_concepts2 = compare_semantic_analysis(text1, text2, nlp, lang)
45
-
46
- # Crear tablas de conceptos clave
47
- table1 = create_concept_table(key_concepts1)
48
- table2 = create_concept_table(key_concepts2)
49
-
50
- return {
51
- 'graph1': graph1,
52
- 'graph2': graph2,
53
- 'key_concepts1': key_concepts1,
54
- 'key_concepts2': key_concepts2
55
- }
56
-
57
- def display_discourse_analysis_results(analysis_result, lang_code):
58
- t = get_translations(lang_code)
59
-
60
- col1, col2 = st.columns(2)
61
-
62
- with col1:
63
- with st.expander(t['doc1_title'], expanded=True):
64
- st.pyplot(analysis_result['graph1'])
65
- st.subheader(t['key_concepts'])
66
- st.table(analysis_result['table1'])
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
+ }