Create discourse_analysis.py
Browse files
modules/text_analysis/discourse_analysis.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
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
|