参考http://www.jianshu.com/p/3bd06f8816d7

 
项目原理:
  实验基于简单共现关系,编写 Python 代码从纯文本中提取出人物关系网络,并用Gephi
将生成的网络可视化。下面介绍共现网络的基本原理。(共现网络简单的英文介绍
 
共现网络的基本原理:
  
实体间的共现是是一种基于统计信息的提取,关系密切的人物往往会在文中的多段连续出现,通过文中以出现的实体(人名),计算不同实体共同出现的比率和次数,设定一个阈值,大于该阈值认为实体间存在某种联系。
 
准备:
  1. 环境 windows Python3.6
  2. 模块jieba  https://github.com/fxsjy/jieba
  3. jephi软件

人名字典 
http://labfile.oss.aliyuncs.com/courses/677/dict.txt 

《釜山行》中文剧本 

http://labfile.oss.aliyuncs.com/courses/677/busan.txt


 

代码:

# -*- coding: utf-8 -*-
import

os, sys
import jieba, codecs, math
import jieba.posseg as pseg names = {} # 姓名字典
relationships = {} # 关系字典
lineNames = [] # 每段内人物关系 # count names
jieba.load_userdict("D:\\ResearchContent\\Exercise_Programm\\PythonExercise\\Python\\dict.txt")

# 加载字典
with

codecs.open("D:\\ResearchContent\\Exercise_Programm\\PythonExercise\\Python\\fushan.txt", "r", "utf8") 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) 

< :
continue # 当分词长度小于2或该词词性不为nr时认为该词不为人名

lineNames[

-

].append(w.word)      

# 为当前段的环境增加一个人物
if

names.get(w.word) 

is None:

names[w.word] = 

0

relationships[w.word] = {}
names[w.word]

+= 1

# 该人物出现次数加 1

# explore relationships
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

# 两人共同出现次数加 1

# output
with

codecs.open("busan_node.txt", "w", "gbk") as f

:

f.write("Id Label Weight\r\n")
for name, times in names.items()

:

f.write(name 

+ " " +

name 

+ " " + str

(times) 

+ "

\r\n")

with codecs.open("busan_edge.txt", "w", "gbk") as f

:

f.write("Source Target Weight\r\n")
for name, edges in relationships.items()

:
for

v, w in edges.items()

:
if

w 

> :

f.write(name 

+ " " +

v 

+ " " + str

(w) 

+ "

\r\n")

参考:

共线网络简单英文介绍https://forec.github.io/2016/10/03/co-occurrence-structure-capture/

Python中文分词:结巴分词http://www.cnblogs.com/kaituorensheng/p/3595879.html

import as 解释:https://www.zhihu.com/question/20871904

修改2

最新文章

  1. Java 集合系列08之 List总结(LinkedList, ArrayList等使用场景和性能分析)
  2. Android -- 通知栏的使用
  3. How to enable logging
  4. 安装pgadmin3
  5. div里面的内容超出自身高度时,显示省略号
  6. Mono for Android (4)-- 图片转为二进制,二进制转回图片
  7. 计算机视觉和人工智能的状态:我们已经走得很远了 The state of Computer Vision and AI: we are really, really far away.
  8. iOS多线程总结(二)NSOperation
  9. 如何优化你的JS脚本来减少reflow/repaint?
  10. 委托、Lambda和事件
  11. poj2378(树的dfs)
  12. 8. Python自定义模块humansize
  13. hive新特性reflect函数介绍
  14. WebSocket(5)---多人聊天系统
  15. JAVA集合类兄妹List和Set
  16. 聊天机器人開發好消息!!DIALOGFLOW與微信的天作之合!!
  17. Ionic 入门与实战之第二章第一节:Ionic 环境搭建之开发环境配置
  18. 巧用ELK快速实现网站流量监控可视化
  19. 标准标签、&lt;jsp:include&gt;、&lt;jsp:forward&gt;
  20. testNG 学习笔记 Day2 配置testNG自带的监听器

热门文章

  1. Microsoft Dynamics CRM 解决数据大于5000时,页面上只能导出5000+数据。
  2. java中IO流异常处理
  3. MYSQL基础操作之单表的增删改查
  4. Eclipse for Java EE软件操作集锦(二)
  5. OpenSceneGraph 编译 error LNK2019:unresolved external symbol 错误
  6. C#语言基础——集合(ArrayList集合)
  7. SQLite使用(三)&amp;&amp;核心API使用
  8. ASP.NET MVC Filters 4种默认过滤器的使用【附示例】
  9. SQL SERVER 2008复制数据库时发生执行SQL Server代理作业错误
  10. Python Socket 网络编程