获取数据
大概思路:
1.将人物名字加载到数组中
2.加载文本
3.由于计算机没有思考能力来分析那两个人物是有关系的,这里就按照段落建立联系,每段出现的各人物就有了关系
4.根据名字数组判断每个段落人物的联系
5.将关系写入到字典中
6.将关系写入到文本中
import jieba,codecs
from pyecharts import options as opts
from pyecharts.charts import Graph
names={}
relationships={}
lineNames=[]
with codecs.open("悲惨世界 id.txt","r",encoding='utf-8') as p:
linename=[]
linenames=[]
nameline=p.readlines()
for nml in nameline:
if nml =="\r\n":
continue
else:
linename.append( nml.replace('\"','') )
for kg in linename:
linenames.append( kg.replace('\r\n',''))
with codecs.open("悲惨世界.txt",'r','utf-8') as f:
for line in f.readlines():
wordlist=jieba.lcut(line)
lineNames.append([])
for w in wordlist:
if w not in linenames:
continue
lineNames[-1].append(w)
if names.get(w) is None:
names[w] = 0
relationships[w] = {}
names[w] +=1
for line in lineNames:
for ln1 in line:
for ln2 in line:
if ln1==ln2:
continue
if relationships[ln1].get(ln2) is None:
relationships[ln1][ln2] = 1
else:
relationships[ln1][ln2] = relationships[ln1][ln2]+1
with open('node.csv','w',encoding='utf-8') as p:
for name,edgs in relationships.items():
for v,m in edgs.items():
p.write("{},{},{}\n".format(name,v,m))
绘制关系图
1.加载数据
先打开文件 —>导入电子表格—>开始gephi绘制
2.设置格式
3.导出
文件链接
链接:https://pan.baidu.com/s/1zo_gQjZYwtUYXdAriDgStw
提取码:1111