赞
踩
在知识网络研究中应用普遍,最为常见的就是利用论文关键词及其共现关系构建共词矩阵,进而映射为共词网络并可视化,从而来揭示某一学科某一领域某一主题的研究热点与趋势、知识结构与演化等。
引自:共词网络的结构与演化-概念与理论进展。
其基本含义:在大规模语料中,若两个词经常共同出现(共现)在截取的同一单元(如一定词语间隔/一句话/一篇文档等)中,则认为这两个词在语义上是相互关联的,而且,共现的频率越高,其相互间的关联越紧密。
图片来自:CiteSpace关键词共现图谱含义详细解析
两个词共同出现的次数越多,网络图中两个词语节点连线越粗,也就是共现的次数为边上的权值
其次,单个词出现的次数越多,在网络图中节点越大,若一个词与许多词均有联系,则这个词会在网络图的中心区域。
在文本挖掘中,有共现矩阵的概念,如下::
- ·I like deep learning.
- ·I like NLP.
- ·I enjoy modeling.
数据采用的还是 大江大河2弹幕数据
已经对数据做了文本去噪、去重、过滤等清洗
- import pandas as pd
- import numpy as np
- import os
- import jieba
-
- def my_cut(text):
-
- my_words = ['大江大河']
- for i in my_words:
- jieba.add_word(i)
-
- # 加载停用词
- stop_words = []
- with open(r"C:\\Users\\Administrator\\Desktop\\停用词.txt", 'r',encoding='utf-8') as f:
- lines = f.readlines()
- for line in lines:
- stop_words.append(line.strip())
- # stop_words[:10]
-
- return [w for w in jieba.cut(text) if w not in stop_words and len(w)>1]
-
-
-
- def str2csv(filePath, s, x):
- '''
- 将字符串写入到本地csv文件中
- :param filePath: csv文件路径
- :param s: 待写入字符串(逗号分隔格式)
- '''
- if x=='node':
- with open(filePath, 'w', encoding='gbk') as f:
- f.write("Label,Weight\r")
- f.write(s)
- print('写入文件成功,请在'+filePath+'中查看')
- else:
- with open(filePath, 'w', encoding='gbk') as f:
- f.write("Source,Target,Weight\r")
- f.write(s)
- print('写入文件成功,请在'+filePath+'中查看')
-
-
-
- def sortDictValue(dict, is_reverse):
- '''
- 将字典按照value排序
- :param dict: 待排序的字典
- :param is_reverse: 是否按照倒序排序
- :return s: 符合csv逗号分隔格式的字符串
- '''
- # 对字典的值进行倒序排序,items()将字典的每个键值对转化为一个元组,key输入的是函数,item[1]表示元组的第二个元素,reverse为真表示倒序
- tups = sorted(dict.items(), key=lambda item: item[1], reverse=is_reverse)
- s = ''
- for tup in tups: # 合并成csv需要的逗号分隔格式
- s = s + tup[0] + ',' + str(tup[1]) + '\n'
- return s
-
-
- def build_matrix(co_authors_list, is_reverse):
- '''
- 根据共同列表,构建共现矩阵(存储到字典中),并将该字典按照权值排序
- :param co_authors_list: 共同列表
- :param is_reverse: 排序是否倒序
- :return node_str: 三元组形式的节点字符串(且符合csv逗号分隔格式)
- :return edge_str: 三元组形式的边字符串(且符合csv逗号分隔格式)
- '''
- node_dict = {} # 节点字典,包含节点名+节点权值(频数)
- edge_dict = {} # 边字典,包含起点+目标点+边权值(频数)
- # 第1层循环,遍历整表的每行信息
- for row_authors in co_authors_list:
- row_authors_list = row_authors.split(' ') # 依据','分割每行,存储到列表中
- # 第2层循环
- for index, pre_au in enumerate(row_authors_list): # 使用enumerate()以获取遍历次数index
- # 统计单个词出现的频次
- if pre_au not in node_dict:
- node_dict[pre_au] = 1
- else:
- node_dict[pre_au] += 1
- # 若遍历到倒数第一个元素,则无需记录关系,结束循环即可
- if pre_au == row_authors_list[-1]:
- break
- connect_list = row_authors_list[index+1:]
- # 第3层循环,遍历当前行词后面所有的词,以统计两两词出现的频次
- for next_au in connect_list:
- A, B = pre_au, next_au
- # 固定两两词的顺序
- # 仅计算上半个矩阵
- if A==B:
- continue
- if A > B:
- A, B = B, A
- key = A+','+B # 格式化为逗号分隔A,B形式,作为字典的键
- # 若该关系不在字典中,则初始化为1,表示词间的共同出现次数
- if key not in edge_dict:
- edge_dict[key] = 1
- else:
- edge_dict[key] += 1
- # 对得到的字典按照value进行排序
- node_str = sortDictValue(node_dict, is_reverse) # 节点
- edge_str = sortDictValue(edge_dict, is_reverse) # 边
- return node_str, edge_str
-
-
- if __name__ == '__main__':
- os.chdir(r'C:\Users\Administrator\Desktop')
- filePath1 = r'C:\Users\Administrator\Desktop\node.csv'
- filePath2 = r'C:\Users\Administrator\Desktop\edge.csv'
- # 读取csv文件获取数据并存储到列表中
- df = pd.read_excel('处理好的弹幕数据.xlsx')
- df_ = [w for w in df['弹幕'] if len(w)>20]
- co_ist = [ " ".join(my_cut(w)) for w in df_]
- # 根据共同词列表, 构建共现矩阵(存储到字典中), 并将该字典按照权值排序
- node_str, edge_str = build_matrix(co_ist, is_reverse=True)
- #print(edge_str)
- # 将字符串写入到本地csv文件中
- str2csv(filePath1,node_str,'node')
- str2csv(filePath2,edge_str,'edge')
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
继续处理,这里只要 Weight 大于 3 的数据
- import pandas as pd
- edge_str = pd.read_csv('edge.csv',encoding='gbk')
- edge_str.shape
-
- edge_str1 = edge_str[edge_str['Weight']>3]
- edge_str1.shape
-
- Source = edge_str1['Source'].tolist()
- Target = edge_str1['Target'].tolist()
- co = Source + Target
- co =list(set(co))
-
- node_str = pd.read_csv('node.csv',encoding='gbk')
- #node_str
-
- node_str=node_str[node_str['Label'].isin(co)]
- node_str['id']=node_str['Label']
- node_str = node_str[['id','Label','Weight']] # 调整列顺序
- #node_str
-
- node_str.to_csv(path_or_buf="node.txt", index=False) # 写入csv文件
- edge_str1.to_csv(path_or_buf="edge.txt", index=False) # 写入csv文件
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
最终得到的数据
制作网络图的过程 可参见 【绘制关系网络图】Gephi 入门使用
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。