Update modules/discourse_analysis.py
Browse files- modules/discourse_analysis.py +25 -11
modules/discourse_analysis.py
CHANGED
|
@@ -9,25 +9,39 @@ def compare_semantic_analysis(text1, text2, nlp, lang):
|
|
| 9 |
doc1 = nlp(text1)
|
| 10 |
doc2 = nlp(text2)
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
|
| 15 |
# Create a new figure with two subplots side by side
|
| 16 |
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(72, 27))
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
ax2.set_title("Documento 2: Relaciones Semánticas Relevantes", fontsize=24, fontweight='bold')
|
|
|
|
|
|
|
| 29 |
ax2.axis('off')
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
plt.tight_layout()
|
| 32 |
|
| 33 |
return fig
|
|
|
|
| 9 |
doc1 = nlp(text1)
|
| 10 |
doc2 = nlp(text2)
|
| 11 |
|
| 12 |
+
G1, pos_counts1 = create_semantic_graph(doc1, lang)
|
| 13 |
+
G2, pos_counts2 = create_semantic_graph(doc2, lang)
|
| 14 |
|
| 15 |
# Create a new figure with two subplots side by side
|
| 16 |
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(72, 27))
|
| 17 |
|
| 18 |
+
# Draw the first graph
|
| 19 |
+
pos1 = nx.spring_layout(G1, k=0.7, iterations=50)
|
| 20 |
+
nx.draw(G1, pos1, ax=ax1, node_color=[POS_COLORS.get(G1.nodes[node]['pos'], '#CCCCCC') for node in G1.nodes()],
|
| 21 |
+
with_labels=True, node_size=5000, font_size=12, font_weight='bold',
|
| 22 |
+
arrows=True, arrowsize=20, width=2, edge_color='gray')
|
| 23 |
+
nx.draw_networkx_edge_labels(G1, pos1, edge_labels=nx.get_edge_attributes(G1, 'label'), font_size=10, ax=ax1)
|
| 24 |
|
| 25 |
+
# Draw the second graph
|
| 26 |
+
pos2 = nx.spring_layout(G2, k=0.7, iterations=50)
|
| 27 |
+
nx.draw(G2, pos2, ax=ax2, node_color=[POS_COLORS.get(G2.nodes[node]['pos'], '#CCCCCC') for node in G2.nodes()],
|
| 28 |
+
with_labels=True, node_size=5000, font_size=12, font_weight='bold',
|
| 29 |
+
arrows=True, arrowsize=20, width=2, edge_color='gray')
|
| 30 |
+
nx.draw_networkx_edge_labels(G2, pos2, edge_labels=nx.get_edge_attributes(G2, 'label'), font_size=10, ax=ax2)
|
| 31 |
+
|
| 32 |
+
ax1.set_title("Documento 1: Relaciones Semánticas Relevantes", fontsize=24, fontweight='bold')
|
| 33 |
ax2.set_title("Documento 2: Relaciones Semánticas Relevantes", fontsize=24, fontweight='bold')
|
| 34 |
+
|
| 35 |
+
ax1.axis('off')
|
| 36 |
ax2.axis('off')
|
| 37 |
|
| 38 |
+
# Add legends
|
| 39 |
+
legend_elements = [plt.Rectangle((0,0),1,1,fc=POS_COLORS.get(pos, '#CCCCCC'), edgecolor='none',
|
| 40 |
+
label=f"{POS_TRANSLATIONS[lang].get(pos, pos)}")
|
| 41 |
+
for pos in ['NOUN', 'VERB']]
|
| 42 |
+
ax1.legend(handles=legend_elements, loc='upper left', bbox_to_anchor=(0, 1), fontsize=12)
|
| 43 |
+
ax2.legend(handles=legend_elements, loc='upper left', bbox_to_anchor=(0, 1), fontsize=12)
|
| 44 |
+
|
| 45 |
plt.tight_layout()
|
| 46 |
|
| 47 |
return fig
|