AIdeaText commited on
Commit
3b2551a
1 Parent(s): eece828

Update modules/semantic_analysis.py

Browse files
Files changed (1) hide show
  1. modules/semantic_analysis.py +24 -12
modules/semantic_analysis.py CHANGED
@@ -135,6 +135,7 @@ def extract_entities(doc, lang):
135
  return entities
136
 
137
  #####################################################################################################################
 
138
  def visualize_context_graph(doc, lang):
139
  G = nx.Graph()
140
  entities = extract_entities(doc, lang)
@@ -153,25 +154,30 @@ def visualize_context_graph(doc, lang):
153
  G.add_edge(sent_entities[i].text, sent_entities[j].text)
154
 
155
  # Visualize
156
- plt.figure(figsize=(20, 15))
157
- pos = nx.spring_layout(G, k=0.5, iterations=50)
158
 
159
  node_colors = [color_map[G.nodes[node]['category']] for node in G.nodes()]
160
 
161
- nx.draw(G, pos, node_color=node_colors, with_labels=True, node_size=5000,
162
- font_size=12, font_weight='bold')
 
 
 
 
163
 
164
  # Add a legend
165
  legend_elements = [plt.Rectangle((0,0),1,1,fc=color, edgecolor='none', label=category)
166
  for category, color in color_map.items()]
167
- plt.legend(handles=legend_elements, loc='upper left', bbox_to_anchor=(1, 1))
168
 
169
- plt.title("Análisis del Contexto" if lang == 'es' else "Context Analysis" if lang == 'en' else "Analyse du Contexte", fontsize=20)
170
  plt.axis('off')
171
 
172
  return plt
173
 
174
  ############################################################################################################################################
 
175
  def visualize_semantic_relations(doc, lang):
176
  G = nx.Graph()
177
  word_freq = Counter(token.text.lower() for token in doc if token.pos_ not in ['PUNCT', 'SPACE'])
@@ -185,29 +191,35 @@ def visualize_semantic_relations(doc, lang):
185
  if token.text.lower() in top_words and token.head.text.lower() in top_words:
186
  G.add_edge(token.text, token.head.text, label=token.dep_)
187
 
188
- plt.figure(figsize=(24, 18))
189
- pos = nx.spring_layout(G, k=0.9, iterations=50)
190
 
191
  node_colors = [POS_COLORS.get(G.nodes[node]['pos'], '#CCCCCC') for node in G.nodes()]
192
 
193
  nx.draw(G, pos, node_color=node_colors, with_labels=True,
194
- font_size=10, font_weight='bold', arrows=True, arrowsize=20, width=2, edge_color='gray')
 
 
 
 
 
 
195
 
196
  edge_labels = nx.get_edge_attributes(G, 'label')
197
- nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=8)
198
 
199
  title = {
200
  'es': "Relaciones Semánticas Relevantes",
201
  'en': "Relevant Semantic Relations",
202
  'fr': "Relations Sémantiques Pertinentes"
203
  }
204
- plt.title(title[lang], fontsize=20, fontweight='bold')
205
  plt.axis('off')
206
 
207
  legend_elements = [plt.Rectangle((0,0),1,1, facecolor=POS_COLORS.get(pos, '#CCCCCC'), edgecolor='none',
208
  label=f"{POS_TRANSLATIONS[lang].get(pos, pos)}")
209
  for pos in set(nx.get_node_attributes(G, 'pos').values())]
210
- plt.legend(handles=legend_elements, loc='center left', bbox_to_anchor=(1, 0.5), fontsize=12)
211
 
212
  return plt
213
 
 
135
  return entities
136
 
137
  #####################################################################################################################
138
+
139
  def visualize_context_graph(doc, lang):
140
  G = nx.Graph()
141
  entities = extract_entities(doc, lang)
 
154
  G.add_edge(sent_entities[i].text, sent_entities[j].text)
155
 
156
  # Visualize
157
+ plt.figure(figsize=(30, 22)) # Increased figure size
158
+ pos = nx.spring_layout(G, k=0.7, iterations=50) # Adjusted layout
159
 
160
  node_colors = [color_map[G.nodes[node]['category']] for node in G.nodes()]
161
 
162
+ nx.draw(G, pos, node_color=node_colors, with_labels=True,
163
+ node_size=10000, # Increased node size
164
+ font_size=18, # Increased font size
165
+ font_weight='bold',
166
+ width=2, # Increased edge width
167
+ arrowsize=30) # Increased arrow size
168
 
169
  # Add a legend
170
  legend_elements = [plt.Rectangle((0,0),1,1,fc=color, edgecolor='none', label=category)
171
  for category, color in color_map.items()]
172
+ plt.legend(handles=legend_elements, loc='upper left', bbox_to_anchor=(1, 1), fontsize=16) # Increased legend font size
173
 
174
+ plt.title("Análisis del Contexto" if lang == 'es' else "Context Analysis" if lang == 'en' else "Analyse du Contexte", fontsize=24) # Increased title font size
175
  plt.axis('off')
176
 
177
  return plt
178
 
179
  ############################################################################################################################################
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_ not in ['PUNCT', 'SPACE'])
 
191
  if token.text.lower() in top_words and token.head.text.lower() in top_words:
192
  G.add_edge(token.text, token.head.text, label=token.dep_)
193
 
194
+ plt.figure(figsize=(36, 27)) # Increased figure size
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()]
198
 
199
  nx.draw(G, pos, node_color=node_colors, with_labels=True,
200
+ node_size=10000, # Increased node size
201
+ font_size=16, # Increased font size
202
+ font_weight='bold',
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
+ plt.title(title[lang], fontsize=24, fontweight='bold') # Increased title font size
217
  plt.axis('off')
218
 
219
  legend_elements = [plt.Rectangle((0,0),1,1, facecolor=POS_COLORS.get(pos, '#CCCCCC'), edgecolor='none',
220
  label=f"{POS_TRANSLATIONS[lang].get(pos, pos)}")
221
  for pos in set(nx.get_node_attributes(G, 'pos').values())]
222
+ plt.legend(handles=legend_elements, loc='center left', bbox_to_anchor=(1, 0.5), fontsize=16) # Increased legend font size
223
 
224
  return plt
225