Python基于共现提取《釜山行》人物关系

转载博客:https://blog.csdn.net/qq_32400847/article/details/60467433

本文转载自Python基于共现提取《釜山行》人物关系 
《釜山行》是一部丧尸灾难片,其人物少、关系简单,非常适合我们学习文本处理。这个项目将介绍共现在关系中的提取,使用python编写代码实现对《釜山行》文本的人物关系提取,最终利用Gephi软件对提取的人物关系绘制人物关系图。实体间的共现是一种基于统计的信息提取。关系紧密的人物往往会在文本中多段内同时出现,可以通过识别文本中已确定的实体(人名),计算不同实体共同出现的次数和比率。当比率大于某一阈值,我们认为两个实体间存在某种联系。这种联系可以具体细化,但提取过程也更加复杂。因此在此课程只介绍最基础的共现网络。 
1.开发环境 
剧本 
字典 
gephi 
Python2+jieba库 
2.实验过程 
开始编写我们的代码。

import os, sys
import jieba, codecs, math
import jieba.posseg as pseg

names = {}
relationships = {}
lineNames = []
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

字典类型names保存人物,该字典的键为人物名称,值为该人物在全文中出现的次数。字典类型relationships保存人物关系的有向边,该字典的键为有向边的起点,值为一个字典edge,edge的键是有向边的终点,值是有向边的权值,代表两个人物之间联系的紧密程度。lineNames是一个缓存变量,保存对每一段分词得到当前段中出现的人物名称,lineNames[i]是一个列表,列表中存储第i段中出现过的人物。

jieba.load_userdict("dict.txt")
with codecs.open("to_train.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) < 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

在具体实现过程中,读入剧本的每一行,对其做分词。提取该行中出现的人物集存入lineNames中。之后对出现的人物,更新他们在names中的出现次数。

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

对于lineNames中每一行,我们为该行中出现的所有人物两两相连。如果两个人物之间尚未有边建立,则将新建的边权值设为1,否则将已存在的边的权值加1。这种方法将产生很多的冗余边,这些冗余边将在最后处理。

with codecs.open("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("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 > 3:
                f.write(name + " " + v + " " + str(w) + "\r\n")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

将已经建好的names和relationships输出到文本,以方便gephi可视化处理。输出边的过程中可以过滤可能是冗余的边,这里假设共同出现次数少于3次的是冗余边,则在输出时跳过这样的边。 
完整的代码如下。

# last edit date: 2016/10/19
# author: Forec
# LICENSE
# Copyright (c) 2015-2017, Forec <forec@bupt.edu.cn>

# Permission to use, copy, modify, and/or distribute this code for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.

# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

import os, sys
import jieba, codecs, math
import jieba.posseg as pseg

names = {}
relationships = {}
lineNames = []

# count names
jieba.load_userdict("dict.txt")
with codecs.open("to_train.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) < 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

# 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

# output
with codecs.open("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("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 > 3:
                f.write(name + " " + v + " " + str(w) + "\r\n")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63

运行得到节点集合node.txt,边集合edge.txt。 
这里写图片描述 
下面使用gephi这个软件来将人物关系可视化。启动gephi,分别选择节点表格和边表格导入上面代码中生成的两个文件,分隔符选择空格,编码选择GB2312。 
这里写图片描述 
这里写图片描述 
可以在最上方的数据资料选项卡中查看图中所有的边和节点,对于分词不准确导致的噪音可以手动删除。分别点击右侧统计栏中平均度和模块化运行计算。模块化运算时Resolution值填写0.5。 
这里写图片描述
点击左上角外观中节点第一个选项卡,选择数值设定,选择Modularity Class,点击应用。 
这里写图片描述 
点击左上角外观中节点第二个选项卡,选择数值设定,选择连入度,最小尺寸填10,最大尺寸填40,点击应用。 
这里写图片描述 
选择左下角布局中的Force Atlas,斥力强度填写20000.0,吸引强度填写 1.0。点击运行,稍后点击停止。 
这里写图片描述
点染色根据模块化计算结果不定,但染色效果大致相同。点击最上方的预览按钮,选中左侧节点标签中显示标签选项,并选择一种字体。 
这里写图片描述 
点击刷新按钮,右侧显示最终的人物关系图。为了优化显示的效果,还可以调整左边的参数。 
这里写图片描述


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值