AIdeaText commited on
Commit
957c8b2
1 Parent(s): d02bfcd

Update modules/text_analysis/discourse_analysis.py

Browse files
modules/text_analysis/discourse_analysis.py CHANGED
@@ -3,52 +3,50 @@ import spacy
3
  import networkx as nx
4
  import matplotlib.pyplot as plt
5
  from collections import defaultdict
6
- from .semantic_analysis import visualize_semantic_relations, create_semantic_graph, POS_COLORS, POS_TRANSLATIONS
 
 
 
 
 
 
 
7
 
8
- ##################################################################################################################
9
  def compare_semantic_analysis(text1, text2, nlp, lang):
10
  doc1 = nlp(text1)
11
  doc2 = nlp(text2)
12
-
13
- G1, pos_counts1 = create_semantic_graph(doc1, lang)
14
- G2, pos_counts2 = create_semantic_graph(doc2, lang)
15
-
16
- # Create two separate figures with a smaller size
17
- fig1, ax1 = plt.subplots(figsize=(18, 13))
18
- fig2, ax2 = plt.subplots(figsize=(18, 13))
19
-
20
- # Draw the first graph
21
- pos1 = nx.spring_layout(G1, k=0.7, iterations=50)
22
- nx.draw(G1, pos1, ax=ax1, node_color=[POS_COLORS.get(G1.nodes[node]['pos'], '#CCCCCC') for node in G1.nodes()],
23
- with_labels=True, node_size=4000, font_size=10, font_weight='bold',
24
- arrows=True, arrowsize=20, width=2, edge_color='gray')
25
- nx.draw_networkx_edge_labels(G1, pos1, edge_labels=nx.get_edge_attributes(G1, 'label'), font_size=8, ax=ax1)
26
-
27
- # Draw the second graph
28
- pos2 = nx.spring_layout(G2, k=0.7, iterations=50)
29
- nx.draw(G2, pos2, ax=ax2, node_color=[POS_COLORS.get(G2.nodes[node]['pos'], '#CCCCCC') for node in G2.nodes()],
30
- with_labels=True, node_size=4000, font_size=10, font_weight='bold',
31
- arrows=True, arrowsize=20, width=2, edge_color='gray')
32
- nx.draw_networkx_edge_labels(G2, pos2, edge_labels=nx.get_edge_attributes(G2, 'label'), font_size=8, ax=ax2)
33
-
34
- ax1.set_title("Documento 1: Relaciones Semánticas Relevantes", fontsize=14, fontweight='bold')
35
- ax2.set_title("Documento 2: Relaciones Semánticas Relevantes", fontsize=14, fontweight='bold')
36
-
37
- ax1.axis('off')
38
- ax2.axis('off')
39
-
40
- # Add legends
41
- legend_elements = [plt.Rectangle((0,0),1,1,fc=POS_COLORS.get(pos, '#CCCCCC'), edgecolor='none',
42
- label=f"{POS_TRANSLATIONS[lang].get(pos, pos)}")
43
- for pos in ['NOUN', 'VERB']]
44
- ax1.legend(handles=legend_elements, loc='upper left', bbox_to_anchor=(0, 1), fontsize=8)
45
- ax2.legend(handles=legend_elements, loc='upper left', bbox_to_anchor=(0, 1), fontsize=8)
46
-
47
- plt.tight_layout()
48
-
49
- return fig1, fig2
50
 
51
- ##################################################################################################################
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  def perform_discourse_analysis(text1, text2, nlp, lang):
53
- graph1, graph2 = compare_semantic_analysis(text1, text2, nlp, lang)
54
- return graph1, graph2
 
 
 
 
 
 
 
 
 
 
 
 
3
  import networkx as nx
4
  import matplotlib.pyplot as plt
5
  from collections import defaultdict
6
+ from .semantic_analysis import (
7
+ create_concept_graph,
8
+ visualize_concept_graph,
9
+ identify_and_contextualize_entities,
10
+ POS_COLORS,
11
+ POS_TRANSLATIONS,
12
+ ENTITY_LABELS
13
+ )
14
 
 
15
  def compare_semantic_analysis(text1, text2, nlp, lang):
16
  doc1 = nlp(text1)
17
  doc2 = nlp(text2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
+ # Identificar entidades y conceptos clave para ambos documentos
20
+ entities1, key_concepts1 = identify_and_contextualize_entities(doc1, lang)
21
+ entities2, key_concepts2 = identify_and_contextualize_entities(doc2, lang)
22
+
23
+ # Crear grafos de conceptos para ambos documentos
24
+ concepts1 = [concept for concept, _ in key_concepts1]
25
+ concepts2 = [concept for concept, _ in key_concepts2]
26
+ G1 = create_concept_graph(text1, concepts1)
27
+ G2 = create_concept_graph(text2, concepts2)
28
+
29
+ # Visualizar los grafos de conceptos
30
+ fig1 = visualize_concept_graph(G1, lang)
31
+ fig2 = visualize_concept_graph(G2, lang)
32
+
33
+ # Añadir títulos específicos para cada documento
34
+ fig1.suptitle("Documento 1: Relaciones Conceptuales", fontsize=16, fontweight='bold')
35
+ fig2.suptitle("Documento 2: Relaciones Conceptuales", fontsize=16, fontweight='bold')
36
+
37
+ return fig1, fig2, entities1, entities2, key_concepts1, key_concepts2
38
+
39
  def perform_discourse_analysis(text1, text2, nlp, lang):
40
+ graph1, graph2, entities1, entities2, key_concepts1, key_concepts2 = compare_semantic_analysis(text1, text2, nlp, lang)
41
+
42
+ # Aquí puedes añadir más análisis de discurso si lo necesitas
43
+ # Por ejemplo, podrías comparar las entidades y conceptos clave entre los dos textos
44
+
45
+ return {
46
+ 'graph1': graph1,
47
+ 'graph2': graph2,
48
+ 'entities1': entities1,
49
+ 'entities2': entities2,
50
+ 'key_concepts1': key_concepts1,
51
+ 'key_concepts2': key_concepts2
52
+ }