@@ -201,67 +201,6 @@ def topic_what(text, topic):
201
201
prompt = f'这是一段文字:{ text } 。我想知道这记忆里有什么关于{ topic } 的话题,帮我总结成一句自然的话,可以包含时间和人物。只输出这句话就好'
202
202
return prompt
203
203
204
- def visualize_graph (memory_graph : Memory_graph , color_by_memory : bool = False ):
205
- # 设置中文字体
206
- plt .rcParams ['font.sans-serif' ] = ['SimHei' ] # 用来正常显示中文标签
207
- plt .rcParams ['axes.unicode_minus' ] = False # 用来正常显示负号
208
-
209
- G = memory_graph .G
210
-
211
- # 保存图到本地
212
- nx .write_gml (G , "memory_graph.gml" ) # 保存为 GML 格式
213
-
214
- # 根据连接条数或记忆数量设置节点颜色
215
- node_colors = []
216
- nodes = list (G .nodes ()) # 获取图中实际的节点列表
217
-
218
- if color_by_memory :
219
- # 计算每个节点的记忆数量
220
- memory_counts = []
221
- for node in nodes :
222
- memory_items = G .nodes [node ].get ('memory_items' , [])
223
- if isinstance (memory_items , list ):
224
- count = len (memory_items )
225
- else :
226
- count = 1 if memory_items else 0
227
- memory_counts .append (count )
228
- max_memories = max (memory_counts ) if memory_counts else 1
229
-
230
- for count in memory_counts :
231
- # 使用不同的颜色方案:红色表示记忆多,蓝色表示记忆少
232
- if max_memories > 0 :
233
- intensity = min (1.0 , count / max_memories )
234
- color = (intensity , 0 , 1.0 - intensity ) # 从蓝色渐变到红色
235
- else :
236
- color = (0 , 0 , 1 ) # 如果没有记忆,则为蓝色
237
- node_colors .append (color )
238
- else :
239
- # 使用原来的连接数量着色方案
240
- max_degree = max (G .degree (), key = lambda x : x [1 ])[1 ] if G .degree () else 1
241
- for node in nodes :
242
- degree = G .degree (node )
243
- if max_degree > 0 :
244
- red = min (1.0 , degree / max_degree )
245
- blue = 1.0 - red
246
- color = (red , 0 , blue )
247
- else :
248
- color = (0 , 0 , 1 )
249
- node_colors .append (color )
250
-
251
- # 绘制图形
252
- plt .figure (figsize = (12 , 8 ))
253
- pos = nx .spring_layout (G , k = 1 , iterations = 50 )
254
- nx .draw (G , pos ,
255
- with_labels = True ,
256
- node_color = node_colors ,
257
- node_size = 200 ,
258
- font_size = 10 ,
259
- font_family = 'SimHei' ,
260
- font_weight = 'bold' )
261
-
262
- title = '记忆图谱可视化 - ' + ('按记忆数量着色' if color_by_memory else '按连接数量着色' )
263
- plt .title (title , fontsize = 16 , fontfamily = 'SimHei' )
264
- plt .show ()
265
204
266
205
267
206
def visualize_graph_lite (memory_graph : Memory_graph , color_by_memory : bool = False ):
@@ -280,7 +219,7 @@ def visualize_graph_lite(memory_graph: Memory_graph, color_by_memory: bool = Fal
280
219
memory_items = H .nodes [node ].get ('memory_items' , [])
281
220
memory_count = len (memory_items ) if isinstance (memory_items , list ) else (1 if memory_items else 0 )
282
221
degree = H .degree (node )
283
- if memory_count < 5 or degree < 2 : # 改为小于2而不是小于等于2
222
+ if memory_count < 3 or degree < 2 : # 改为小于2而不是小于等于2
284
223
nodes_to_remove .append (node )
285
224
286
225
H .remove_nodes_from (nodes_to_remove )
@@ -291,7 +230,7 @@ def visualize_graph_lite(memory_graph: Memory_graph, color_by_memory: bool = Fal
291
230
return
292
231
293
232
# 保存图到本地
294
- nx .write_gml (H , "memory_graph.gml" ) # 保存为 GML 格式
233
+ # nx.write_gml(H, "memory_graph.gml") # 保存为 GML 格式
295
234
296
235
# 计算节点大小和颜色
297
236
node_colors = []
@@ -315,21 +254,23 @@ def visualize_graph_lite(memory_graph: Memory_graph, color_by_memory: bool = Fal
315
254
memory_count = len (memory_items ) if isinstance (memory_items , list ) else (1 if memory_items else 0 )
316
255
# 使用指数函数使变化更明显
317
256
ratio = memory_count / max_memories
318
- size = 500 + 5000 * (ratio ** 2 ) # 使用平方函数使差异更明显
257
+ size = 500 + 5000 * (ratio ) # 使用1.5次方函数使差异不那么明显
319
258
node_sizes .append (size )
320
259
321
260
# 计算节点颜色(基于连接数)
322
261
degree = H .degree (node )
323
262
# 红色分量随着度数增加而增加
324
- red = min (1.0 , degree / max_degree )
263
+ r = (degree / max_degree ) ** 0.3
264
+ red = min (1.0 , r )
325
265
# 蓝色分量随着度数减少而增加
326
- blue = 1.0 - red
327
- color = (red , 0 , blue )
266
+ blue = max (0.0 , 1 - red )
267
+ # blue = 1
268
+ color = (red , 0.1 , blue )
328
269
node_colors .append (color )
329
270
330
271
# 绘制图形
331
272
plt .figure (figsize = (12 , 8 ))
332
- pos = nx .spring_layout (H , k = 1.5 , iterations = 50 ) # 增加k值使节点分布更开
273
+ pos = nx .spring_layout (H , k = 1 , iterations = 50 ) # 增加k值使节点分布更开
333
274
nx .draw (H , pos ,
334
275
with_labels = True ,
335
276
node_color = node_colors ,
@@ -339,7 +280,7 @@ def visualize_graph_lite(memory_graph: Memory_graph, color_by_memory: bool = Fal
339
280
font_weight = 'bold' ,
340
281
edge_color = 'gray' ,
341
282
width = 0.5 ,
342
- alpha = 0.7 )
283
+ alpha = 0.9 )
343
284
344
285
title = '记忆图谱可视化 - 节点大小表示记忆数量,颜色表示连接数'
345
286
plt .title (title , fontsize = 16 , fontfamily = 'SimHei' )
0 commit comments