import numpy as np from scipy.cluster.hierarchy import linkage, fcluster, dendrogram import matplotlib.pyplot as plt def passage_outline(matrix,sentences): # matrix = np.array([[1.0, 0.8, 0.2, 0.1], # [0.8, 1.0, 0.3, 0.2], # [0.2, 0.3, 1.0, 0.9], # [0.1, 0.2, 0.9, 1.0]]) # sentences = ["主题句子1", "主题句子2", "主题句子3", "主题句子4"] Z = linkage(matrix, method="average") labels = fcluster(Z, t=0.5, criterion="distance") # 根据簇标签和主题句子生成文章结构 structure = {} for label, sentence in zip(labels, sentences): if label not in structure: structure[label] = [] structure[label].append(sentence) outline = "" outline_list = [] for key in sorted(structure.keys()): outline_list.append(f"主题{key}:") outline = outline+f"主题{key}:\n" for sentence in structure[key]: outline_list.append(sentence) outline = outline+f"- {sentence}\n" return outline,outline_list