DBLP学者合作关系数据集的制作

DBLP是一个计算机领域的文献数据库,网站上的所有文献数据保存在一个xml文件中,下载地址https://dblp.org/xml/。这个文件每几天会更新一次,我下载的是2019.10.30日更新的版本,解压后是一个2.53g的xml文件。使用python对这个文件解析,得到一个描述学者合作关系的图结构。

1.dblp.xml的标签

一篇文献的标签结构如下图所示
在这里插入图片描述
article是文献的类型,代表期刊文献。DBLP将文献分为8中类型。

  • article:期刊
  • proceeding:论文集
  • inproceeding:论文集中的一篇(应该指的是会议论文)
  • book:出版的书籍
  • incollection:书籍中的一章
  • www:网页
  • phdthesis:博士学位论文
  • masterthesis:硕士学位论文

这里我们只关注article和inproceeding

一篇文献下的标签代表这篇文献的相关信息。author标签指的是作者姓名,其他的相关信息比如发表时间、出版社、题目都有不同的标签表示。

除论文外,这个xml中还有其它的信息,比如作者、会议。具体内容和标签格式可参考文献 Michael Ley, “DBLP — Some Lessons Learned”

2.xml的解析

用python自带的sax库对xml文件解析,使用方法可看python 菜鸟教程的介绍。我对文件解析两次,第一次提取全部的作者并为每个作者分配一个id,这也就是图的顶点。将 id-作者姓名 对应关系输出到一个文本文件中。

第二次解析建立图中的边。若id为 x 的作者和id为 y 的作者合作过文章,则图中应有边(x,y)。要注意三种情况:

  1. 边(x,x)不应该出现
  2. x,y)和(y,x)只记录一次、
  3. xy 有过多次合作,(x,y)只记录一次

最终将所有的边输出到一个文本文件中,每个数对 x y 占一行

完整代码如下:

#use xml.sax parse dblp.xml. Give each author an ID and put them in a file.
#find collaboration relations of all authors and output them to a file. One relation's format is like (id1,id2)

import xml.sax
class authorHandler(xml.sax.ContentHandler):   #extract all authors
	def __init__(self):
		self.CurrentData=""     #tag's name
		self.dict={}   #save all authors. The key is an author's name, the value is his id
		self.name=""   #the name of an author
		self.id=0      #the ID of an author
	def startElement(self, tag, attributes):   
		self.CurrentData = tag
		self.name = ""

	def endElement(self, tag):
		if self.CurrentData == 'author':    #this tag is author, save it in the dict
			exist = self.dict.get(self.name, -1)    
			if exist == -1:     #if this author have not been added into dict
				self.dict[self.name] = self.id
				self.id = self.id + 1
				self.name=""

	def characters(self, content):
		if self.CurrentData == 'author':
			self.name += content


class collabrationHandler(xml.sax.ContentHandler):    #extract all collaboration relations
	def __init__(self, dict, file):
		self.CurrentData=""     #tag's name
		self.dict = dict   #the author-ID dict
		self.name=""   #the name of an author
		self.id=0      #the ID of an author
		self.paper=False   #if the tag is article or inproceeding, paper = True
		self.author=[]  #all authors' id in one <article> or <inproceeding>
		self.file = file   #Output collaboration relation to file
		self.edge = set()   #Edge's set
	def startElement(self, tag, attributes):   
		self.CurrentData = tag
		if tag == 'article' or tag == 'inproceeding':
			self.author.clear()   #start processing a new paper, old collaboration neen to be deleted
			self.paper = True

	def endElement(self, tag):
		if (tag == 'article' or tag == 'inproceeding') and self.paper == True:   #One paper's tag close
			self.paper = False
			for i in self.author:
				for j in self.author:
					if i < j and (i,j) not in self.edge:    #edge
						self.file.write(str(i) + ' ' + str(j) + '\n')
						self.edge.add((i,j))

	def characters(self, content):
		if self.paper == True:
			self.name = content
			isAuthor = self.dict.get(self.name, -1)   # isAuthor == -1 means that this content is not an author's name
			if isAuthor != -1:
				self.author.append(self.dict[self.name])    #add this author's id 

#set xml parser
parser = xml.sax.make_parser()
parser.setFeature(xml.sax.handler.feature_namespaces, 0)
handler1 = authorHandler()
parser.setContentHandler(handler1)
parser.parse('D:\\dataset\\dblp demo.xml')

with open('author.txt','w') as f:
	for k,v in handler1.dict.items():
		f.write(str(v))
		f.write(' '+k)
		f.write('\n')
f.close()


with open('collaboration.txt', 'w') as f:
	handler2 = collabrationHandler(handler1.dict, f)
	parser.setContentHandler(handler2)
	parser.parse('D:\\dataset\\dblp demo.xml')
f.close()
3.运行程序

整个处理过程大约需要几分钟,比我想象的要快。占用内存1G+,也还可以接受。

最终得到2377820个作者(顶点),9004876条合作关系(边)。

  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值