Update modules/semantic_analysis.py
Browse files- modules/semantic_analysis.py +22 -10
modules/semantic_analysis.py
CHANGED
|
@@ -139,10 +139,25 @@ def visualize_context_graph(doc, lang):
|
|
| 139 |
|
| 140 |
return plt
|
| 141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
def visualize_semantic_relations(doc, lang):
|
| 143 |
-
|
| 144 |
-
# con algunas modificaciones para enfocarse en relaciones sem谩nticas
|
| 145 |
-
G, word_colors = create_syntax_graph(doc, lang)
|
| 146 |
|
| 147 |
plt.figure(figsize=(24, 18))
|
| 148 |
pos = nx.spring_layout(G, k=0.9, iterations=50)
|
|
@@ -150,11 +165,8 @@ def visualize_semantic_relations(doc, lang):
|
|
| 150 |
node_colors = [data['color'] for _, data in G.nodes(data=True)]
|
| 151 |
node_sizes = [data['size'] for _, data in G.nodes(data=True)]
|
| 152 |
|
| 153 |
-
nx.draw(G, pos,
|
| 154 |
-
arrowsize=20, width=2, edge_color='gray')
|
| 155 |
-
|
| 156 |
-
nx.draw_networkx_labels(G, pos, {node: data['label'] for node, data in G.nodes(data=True)},
|
| 157 |
-
font_size=10, font_weight='bold')
|
| 158 |
|
| 159 |
edge_labels = nx.get_edge_attributes(G, 'label')
|
| 160 |
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=8)
|
|
@@ -164,8 +176,8 @@ def visualize_semantic_relations(doc, lang):
|
|
| 164 |
plt.axis('off')
|
| 165 |
|
| 166 |
legend_elements = [plt.Rectangle((0,0),1,1, facecolor=color, edgecolor='none',
|
| 167 |
-
label=f"{POS_TRANSLATIONS[lang][pos]} ({
|
| 168 |
-
for pos, color in POS_COLORS.items() if pos in
|
| 169 |
plt.legend(handles=legend_elements, loc='center left', bbox_to_anchor=(1, 0.5), fontsize=12)
|
| 170 |
|
| 171 |
return plt
|
|
|
|
| 139 |
|
| 140 |
return plt
|
| 141 |
|
| 142 |
+
def create_semantic_graph(doc, lang):
|
| 143 |
+
G = nx.Graph()
|
| 144 |
+
pos_counts = count_pos(doc)
|
| 145 |
+
|
| 146 |
+
for token in doc:
|
| 147 |
+
if token.pos_ != 'PUNCT':
|
| 148 |
+
G.add_node(token.text,
|
| 149 |
+
pos=token.pos_,
|
| 150 |
+
color=POS_COLORS.get(token.pos_, '#FFFFFF'),
|
| 151 |
+
size=pos_counts[token.pos_] * 100)
|
| 152 |
+
|
| 153 |
+
for token in doc:
|
| 154 |
+
if token.dep_ != "ROOT":
|
| 155 |
+
G.add_edge(token.head.text, token.text, label=token.dep_)
|
| 156 |
+
|
| 157 |
+
return G
|
| 158 |
+
|
| 159 |
def visualize_semantic_relations(doc, lang):
|
| 160 |
+
G = create_semantic_graph(doc, lang)
|
|
|
|
|
|
|
| 161 |
|
| 162 |
plt.figure(figsize=(24, 18))
|
| 163 |
pos = nx.spring_layout(G, k=0.9, iterations=50)
|
|
|
|
| 165 |
node_colors = [data['color'] for _, data in G.nodes(data=True)]
|
| 166 |
node_sizes = [data['size'] for _, data in G.nodes(data=True)]
|
| 167 |
|
| 168 |
+
nx.draw(G, pos, node_color=node_colors, node_size=node_sizes, with_labels=True,
|
| 169 |
+
font_size=8, font_weight='bold', arrows=True, arrowsize=20, width=2, edge_color='gray')
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
edge_labels = nx.get_edge_attributes(G, 'label')
|
| 172 |
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=8)
|
|
|
|
| 176 |
plt.axis('off')
|
| 177 |
|
| 178 |
legend_elements = [plt.Rectangle((0,0),1,1, facecolor=color, edgecolor='none',
|
| 179 |
+
label=f"{POS_TRANSLATIONS[lang][pos]} ({pos_counts[pos]})")
|
| 180 |
+
for pos, color in POS_COLORS.items() if pos in pos_counts]
|
| 181 |
plt.legend(handles=legend_elements, loc='center left', bbox_to_anchor=(1, 0.5), fontsize=12)
|
| 182 |
|
| 183 |
return plt
|