"""
Created on Wed May 3 12:58:30 2017
https://zhuanlan.zhihu.com/p/24767513 参考链接
@author: chuc
"""
import networkx as nx
import matplotlib.pyplot as plt
import jieba
import codecs
import jieba.posseg as pseg
names = {}
relationships = {}
lineNames = []
jieba.load_userdict("some/person.txt")
with codecs.open("some/people.txt", "r") as f:
for line in f.readlines():
poss = pseg.cut(line)
lineNames.append([])
for w in poss:
if w.flag != "nr" or len(w.word) < 2:
continue
lineNames[-1].append(w.word)
if names.get(w.word) is None:
names[w.word] = 0
relationships[w.word] = {}
names[w.word] += 1
for line in lineNames:
for name1 in line:
for name2 in line:
if name1 == name2:
continue
if relationships[name1].get(name2) is None:
relationships[name1][name2]= 1
else:
relationships[name1][name2] = relationships[name1][name2]+ 1
with codecs.open("some/person_edge.txt", "a+", "utf-8") as f:
for name, edges in relationships.items():
for v, w in edges.items():
if w > 20:
f.write(name + " " + v + " " + str(w) + "\r\n")
a = []
f = open('some/person_edge.txt','r',encoding='utf-8')
line = f.readline()
while line:
a.append(line.split())
line = f.readline()
f.close()
G = nx.Graph()
G.add_weighted_edges_from(a)
nx.draw(G,with_labels=True,font_size=12,node_size=1000,node_color='g')
plt.show()