Update modules/semantic_analysis.py
Browse files- modules/semantic_analysis.py +17 -16
modules/semantic_analysis.py
CHANGED
|
@@ -180,18 +180,19 @@ def visualize_context_graph(doc, lang):
|
|
| 180 |
|
| 181 |
def visualize_semantic_relations(doc, lang):
|
| 182 |
G = nx.Graph()
|
| 183 |
-
word_freq = Counter(token.text.lower() for token in doc if token.pos_
|
| 184 |
-
top_words = [word for word, _ in word_freq.most_common(20)] # Top 20 most frequent
|
| 185 |
|
| 186 |
for token in doc:
|
| 187 |
-
if token.text.lower() in top_words:
|
| 188 |
G.add_node(token.text, pos=token.pos_)
|
| 189 |
|
| 190 |
for token in doc:
|
| 191 |
-
if token.
|
| 192 |
-
|
|
|
|
| 193 |
|
| 194 |
-
plt.
|
| 195 |
pos = nx.spring_layout(G, k=0.7, iterations=50) # Adjusted layout
|
| 196 |
|
| 197 |
node_colors = [POS_COLORS.get(G.nodes[node]['pos'], '#CCCCCC') for node in G.nodes()]
|
|
@@ -203,25 +204,26 @@ def visualize_semantic_relations(doc, lang):
|
|
| 203 |
arrows=True,
|
| 204 |
arrowsize=30, # Increased arrow size
|
| 205 |
width=3, # Increased edge width
|
| 206 |
-
edge_color='gray'
|
|
|
|
| 207 |
|
| 208 |
edge_labels = nx.get_edge_attributes(G, 'label')
|
| 209 |
-
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=14) # Increased edge label font size
|
| 210 |
|
| 211 |
title = {
|
| 212 |
'es': "Relaciones Semánticas Relevantes",
|
| 213 |
'en': "Relevant Semantic Relations",
|
| 214 |
'fr': "Relations Sémantiques Pertinentes"
|
| 215 |
}
|
| 216 |
-
|
| 217 |
-
|
| 218 |
|
| 219 |
-
legend_elements = [plt.Rectangle((0,0),1,1,
|
| 220 |
label=f"{POS_TRANSLATIONS[lang].get(pos, pos)}")
|
| 221 |
-
for pos in
|
| 222 |
-
|
| 223 |
|
| 224 |
-
return plt
|
| 225 |
|
| 226 |
|
| 227 |
############################################################################################################################################
|
|
@@ -233,6 +235,5 @@ def perform_semantic_analysis(text, nlp, lang):
|
|
| 233 |
for ent in doc.ents:
|
| 234 |
print(f"{ent.text} - {ent.label_}")
|
| 235 |
|
| 236 |
-
context_graph = visualize_context_graph(doc, lang)
|
| 237 |
relations_graph = visualize_semantic_relations(doc, lang)
|
| 238 |
-
return
|
|
|
|
| 180 |
|
| 181 |
def visualize_semantic_relations(doc, lang):
|
| 182 |
G = nx.Graph()
|
| 183 |
+
word_freq = Counter(token.text.lower() for token in doc if token.pos_ in ['NOUN', 'VERB'])
|
| 184 |
+
top_words = [word for word, _ in word_freq.most_common(20)] # Top 20 most frequent nouns and verbs
|
| 185 |
|
| 186 |
for token in doc:
|
| 187 |
+
if token.pos_ in ['NOUN', 'VERB'] and token.text.lower() in top_words:
|
| 188 |
G.add_node(token.text, pos=token.pos_)
|
| 189 |
|
| 190 |
for token in doc:
|
| 191 |
+
if token.pos_ in ['NOUN', 'VERB'] and token.text.lower() in top_words:
|
| 192 |
+
if token.head.pos_ in ['NOUN', 'VERB'] and token.head.text.lower() in top_words:
|
| 193 |
+
G.add_edge(token.text, token.head.text, label=token.dep_)
|
| 194 |
|
| 195 |
+
fig, ax = plt.subplots(figsize=(36, 27)) # Create a figure and axis
|
| 196 |
pos = nx.spring_layout(G, k=0.7, iterations=50) # Adjusted layout
|
| 197 |
|
| 198 |
node_colors = [POS_COLORS.get(G.nodes[node]['pos'], '#CCCCCC') for node in G.nodes()]
|
|
|
|
| 204 |
arrows=True,
|
| 205 |
arrowsize=30, # Increased arrow size
|
| 206 |
width=3, # Increased edge width
|
| 207 |
+
edge_color='gray',
|
| 208 |
+
ax=ax) # Draw on the axis
|
| 209 |
|
| 210 |
edge_labels = nx.get_edge_attributes(G, 'label')
|
| 211 |
+
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=14, ax=ax) # Increased edge label font size
|
| 212 |
|
| 213 |
title = {
|
| 214 |
'es': "Relaciones Semánticas Relevantes",
|
| 215 |
'en': "Relevant Semantic Relations",
|
| 216 |
'fr': "Relations Sémantiques Pertinentes"
|
| 217 |
}
|
| 218 |
+
ax.set_title(title[lang], fontsize=24, fontweight='bold') # Set title on the axis
|
| 219 |
+
ax.axis('off')
|
| 220 |
|
| 221 |
+
legend_elements = [plt.Rectangle((0,0),1,1,fc=POS_COLORS.get(pos, '#CCCCCC'), edgecolor='none',
|
| 222 |
label=f"{POS_TRANSLATIONS[lang].get(pos, pos)}")
|
| 223 |
+
for pos in ['NOUN', 'VERB']]
|
| 224 |
+
ax.legend(handles=legend_elements, loc='center left', bbox_to_anchor=(1, 0.5), fontsize=16) # Add legend to the axis
|
| 225 |
|
| 226 |
+
return fig # Return the figure instead of plt
|
| 227 |
|
| 228 |
|
| 229 |
############################################################################################################################################
|
|
|
|
| 235 |
for ent in doc.ents:
|
| 236 |
print(f"{ent.text} - {ent.label_}")
|
| 237 |
|
|
|
|
| 238 |
relations_graph = visualize_semantic_relations(doc, lang)
|
| 239 |
+
return relations_graph # Ahora solo devuelve un único gráfico
|