代码源于需求~
第一步,python构建出共现矩阵
中文分词、去停用词
数据存储
第二步,pajek结合ucinet可视化,可vosviewer
上图
python实现共现矩阵
代码见:
https://www.cnblogs.com/Cookie-Jing/p/13837525.html点击原文链接也可
数据预处理部分与之前的主题模型——lda差不多
import numpy as npimport pandas as pdfrom pprint import pprintimport xlrd #读取excel数据import reimport jieba #使用结巴进行中文分词path = r"D:\01研\01大四\2020.3.13-国家突发卫生事件\20201008\lda.xlsx" #修改路径data = xlrd.open_workbook(path)sheet_1_by_index = data.sheet_by_index(0) #读取表一title = sheet_1_by_index.col_values(1) #第二列n_of_rows = sheet_1_by_index.nrowsdoc_set = [] #空列表for i in range(1,n_of_rows): #逐行读取 doc_set.append(title[i])
#从文件导入停用词表def stopwordslist(filepath): stopwords=[line.strip() for line in open(filepath,'r',encoding='utf-8').readlines()] return stopwordsstopwords=stopwordslist(r"D:\01研\01大四\2020.3.13-国家突发卫生事件\20201008\stopwords.txt")texts = []#每篇文章关键词word_set = []#每篇文章关键词不重复set_word = []#所有关键词的集合stpwrdlst2 = ['和', '等', '对', '的', '不','与', '一','化','三要','二要']#去停用词2自编,这里是我自己觉得需要去掉的词for doc in doc_set: #只保留中文 cleaned_doc = ''.join(re.findall(r'[\u4e00-\u9fa5]', doc)) #分词 doc_cut = jieba.lcut(cleaned_doc) #去停用词 text_list0 = [word for word in doc_cut if word not in stopwords and len(word)>1] text_list1 = [word for word in text_list0 if word not in stpwrdlst2] #最终处理好的结果存放于text[]中 texts.append(text_list1) for word in texts: word_new = list(set(word))#去除一维数组中相同的词 word_set.append(word_new) for subword in word_set: for word in subword: if word not in set_word: set_word.append(word)#统计所有出现的词
使用ucinet转化excel存储的共现矩阵
存储为.##d,.##h格式
关键词太多,进行阈值的抽取,保存为.net格式
可视化
使用netdraw打开,图形不太美观
pajek打开.net格式文件
对图要求高一点的话,加上.vec,自己制作