Update modules/semantic_analysis.py
Browse files- modules/semantic_analysis.py +25 -14
modules/semantic_analysis.py
CHANGED
|
@@ -119,10 +119,12 @@ def visualize_context_graph(doc, lang):
|
|
| 119 |
|
| 120 |
# Add edges
|
| 121 |
for sent in doc.sents:
|
| 122 |
-
sent_entities = [ent
|
| 123 |
-
for
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
| 126 |
|
| 127 |
# Visualize
|
| 128 |
plt.figure(figsize=(20, 15))
|
|
@@ -137,7 +139,7 @@ def visualize_context_graph(doc, lang):
|
|
| 137 |
legend_elements = [plt.Rectangle((0,0),1,1,fc=color, edgecolor='none') for color in color_map.values()]
|
| 138 |
plt.legend(legend_elements, color_map.keys(), loc='upper left', bbox_to_anchor=(1, 1))
|
| 139 |
|
| 140 |
-
plt.title("Análisis
|
| 141 |
plt.axis('off')
|
| 142 |
|
| 143 |
return plt
|
|
@@ -160,27 +162,36 @@ def create_semantic_graph(doc, lang):
|
|
| 160 |
return G, pos_counts
|
| 161 |
|
| 162 |
def visualize_semantic_relations(doc, lang):
|
| 163 |
-
G
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
plt.figure(figsize=(24, 18))
|
| 166 |
pos = nx.spring_layout(G, k=0.9, iterations=50)
|
| 167 |
|
| 168 |
-
node_colors = [G.nodes[node]
|
| 169 |
-
node_sizes = [G.nodes[node].get('size', 100) for node in G.nodes()]
|
| 170 |
|
| 171 |
-
nx.draw(G, pos, node_color=node_colors,
|
| 172 |
-
font_size=
|
| 173 |
|
| 174 |
edge_labels = nx.get_edge_attributes(G, 'label')
|
| 175 |
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=8)
|
| 176 |
|
| 177 |
-
plt.title("
|
| 178 |
fontsize=20, fontweight='bold')
|
| 179 |
plt.axis('off')
|
| 180 |
|
| 181 |
-
legend_elements = [plt.Rectangle((0,0),1,1, facecolor=
|
| 182 |
-
label=f"{POS_TRANSLATIONS[lang].get(pos, pos)}
|
| 183 |
-
for pos
|
| 184 |
plt.legend(handles=legend_elements, loc='center left', bbox_to_anchor=(1, 0.5), fontsize=12)
|
| 185 |
|
| 186 |
return plt
|
|
|
|
| 119 |
|
| 120 |
# Add edges
|
| 121 |
for sent in doc.sents:
|
| 122 |
+
sent_entities = [ent for ent in sent.ents if ent.text in G.nodes()]
|
| 123 |
+
person = next((ent for ent in sent_entities if ent.label_ == "PER"), None)
|
| 124 |
+
if person:
|
| 125 |
+
for ent in sent_entities:
|
| 126 |
+
if ent != person:
|
| 127 |
+
G.add_edge(person.text, ent.text)
|
| 128 |
|
| 129 |
# Visualize
|
| 130 |
plt.figure(figsize=(20, 15))
|
|
|
|
| 139 |
legend_elements = [plt.Rectangle((0,0),1,1,fc=color, edgecolor='none') for color in color_map.values()]
|
| 140 |
plt.legend(legend_elements, color_map.keys(), loc='upper left', bbox_to_anchor=(1, 1))
|
| 141 |
|
| 142 |
+
plt.title("Análisis del Contexto" if lang == 'es' else "Context Analysis" if lang == 'en' else "Analyse du Contexte", fontsize=20)
|
| 143 |
plt.axis('off')
|
| 144 |
|
| 145 |
return plt
|
|
|
|
| 162 |
return G, pos_counts
|
| 163 |
|
| 164 |
def visualize_semantic_relations(doc, lang):
|
| 165 |
+
G = nx.Graph()
|
| 166 |
+
word_freq = Counter(token.text.lower() for token in doc if token.pos_ not in ['PUNCT', 'SPACE'])
|
| 167 |
+
top_words = [word for word, _ in word_freq.most_common(20)] # Top 20 most frequent words
|
| 168 |
+
|
| 169 |
+
for token in doc:
|
| 170 |
+
if token.text.lower() in top_words:
|
| 171 |
+
G.add_node(token.text, pos=token.pos_)
|
| 172 |
+
|
| 173 |
+
for token in doc:
|
| 174 |
+
if token.text.lower() in top_words and token.head.text.lower() in top_words:
|
| 175 |
+
G.add_edge(token.text, token.head.text, label=token.dep_)
|
| 176 |
|
| 177 |
plt.figure(figsize=(24, 18))
|
| 178 |
pos = nx.spring_layout(G, k=0.9, iterations=50)
|
| 179 |
|
| 180 |
+
node_colors = [POS_COLORS.get(G.nodes[node]['pos'], '#CCCCCC') for node in G.nodes()]
|
|
|
|
| 181 |
|
| 182 |
+
nx.draw(G, pos, node_color=node_colors, with_labels=True,
|
| 183 |
+
font_size=10, font_weight='bold', arrows=True, arrowsize=20, width=2, edge_color='gray')
|
| 184 |
|
| 185 |
edge_labels = nx.get_edge_attributes(G, 'label')
|
| 186 |
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=8)
|
| 187 |
|
| 188 |
+
plt.title("Relaciones Semánticas Relevantes" if lang == 'es' else "Relevant Semantic Relations" if lang == 'en' else "Relations Sémantiques Pertinentes",
|
| 189 |
fontsize=20, fontweight='bold')
|
| 190 |
plt.axis('off')
|
| 191 |
|
| 192 |
+
legend_elements = [plt.Rectangle((0,0),1,1, facecolor=POS_COLORS.get(pos, '#CCCCCC'), edgecolor='none',
|
| 193 |
+
label=f"{POS_TRANSLATIONS[lang].get(pos, pos)}")
|
| 194 |
+
for pos in set(nx.get_node_attributes(G, 'pos').values())]
|
| 195 |
plt.legend(handles=legend_elements, loc='center left', bbox_to_anchor=(1, 0.5), fontsize=12)
|
| 196 |
|
| 197 |
return plt
|