在之前分词的基础上进一步分析人物的关系。
首先先把每一行小说里面的人物名字统计出来,只需在之前统计各人物出场次数的基础上增加一点代码即可:
sentence = []
for line in f.readlines():
seg_list = jieba.cut(line,cut_all=False)
unique_list = []
linenames.append([])
for i in seg_list:
if i not in STOPWORD:
unique_list.append(i)
if i in all_names:
linenames[-1].append(i)
if names.get(i) is None:
names[i] = 0
relationships[i] = {
}
names[i] += 1
sentence.append(unique_list)
在linenames
这个列表里存储的即为每一行出现的各人物名字,同时给每一个人物添加一个关系元组,用来分析其他人物与该人物的亲密关系。
for line in linenames:
for name1 in line:
for name2 in line:
if name1 in relationships:
if name1 == name2:
continue
if relationships[name1].get(name2) is None:
relationships[name1][name2]=1
else:
relationships[name1][name2] += 1
根据每一行小说里某一人物与其他人物同时出现的次数来分析该人物与其他人物的亲密关系:
for line in linenames:
for name1 in line:
for name2 in line:
if name1 in relationships:
if name1 == name2:
continue
if relationships[name1].get(name2) is None:
relationships[name1][name2]=1
else