/
CnkiParser.py
308 lines (280 loc) · 10.7 KB
/
CnkiParser.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/python
# -*- coding: utf-8 -*-
# author: zhongxin1990@gmail.com
# CnkiParser.py parse local files to db
import os
import re
from bs4 import BeautifulSoup
import MySQLdb
import sys
from time import time
import time
reload(sys)
sys.setdefaultencoding( "utf-8" )
class Parser():
def __init__(self):
pass
# parse one file !
def parseKeyword(self,dir,filename):
f = open(dir+"/"+filename)
soup = BeautifulSoup(f)
f.close()
keywordList = []
for keyword in soup.find_all(href = re.compile(u'中文关键词')):
keywordList.append((keyword.get_text()))
return keywordList
'''
# convert list to json and wirte json in file!
with open('keywordReuslt','w') as outfile:
json.dump(keywordList,outfile)
outfile.close()
# read json from file and convert to list!
jsonfile = open('keywordReuslt')
keywords = json.loads(jsonfile.read())
jsonfile.close()
return keywords
'''
def parsePaper(self,dir,filename):
f = open(dir+"/"+filename)
words = filename.split("_")
expertID = words[0]
soup = BeautifulSoup(f)
f.close()
papers = []
paperID = 0
for title in soup.find_all(class_='fz14'):
paperID += 1
paper = Paper(expertID=expertID,paperID=paperID)
tr = title.parent.parent
# titles:
text = title.get_text().split("'")
paper.setTitle(text[1])
# authors:
for author in tr.find_all(target="knet"):
paper.addAuthor(author.get_text())
paper.authorsList = "|".join(paper.authors)
# origin:
origins = tr.find_all(text=re.compile("IPFD,CPFD,HBRD,MTRD,SCPD,SOPD,SCSF,SCHF,SCSD,SOSD,SNAD"))
for origin in origins:
paper.setOrigin(origin.split('"')[3])
# pubtime database citation:
pdc = []
for pdcItem in tr.find_all(class_="tdrigtxt"):
pdc.append(pdcItem.get_text().strip())
paper.setPubtime(pdc[0])
paper.setDatabase(pdc[1])
if pdc[2] != '':
paper.setCitation(pdc[2])
papers.append(paper)
return papers
class DB():
def __init__(self):
self.conn = MySQLdb.connect(host="localhost",user="root",passwd="123456")
cursor = self.conn.cursor()
# cursor.execute('Create database if not exists experts DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;')
self.conn.select_db('experts')
self.cursor = self.conn.cursor()
self.cursor.execute('Create table if not exists GeniusExpert(Expert_ID BIGINT(20),Expert_name varchar(100),Expert_keywords varchar(2000) );')
self.cursor.execute('Create table if not exists Paper(Expert_ID BIGINT(20),Paper_ID BIGINT(4),Paper_Title varchar(2000),Paper_Authorslist varchar(2000), Paper_Url varchar(1000),Paper_Origin varchar(1000),Paper_Pubtime varchar(200), Paper_Database varchar(1000), Paper_Citation BIGINT(5)) ;')
self.cursor.execute("SET NAMES utf8")
self.cursor.execute("SET CHARACTER_SET_CLIENT=utf8")
self.cursor.execute("SET CHARACTER_SET_RESULTS=utf8")
self.conn.commit()
def close(self):
self.cursor.close()
self.conn.close()
def isExpertExist(self,expert):
sql = "select * from GeniusExpert where Expert_ID = '%s'" %(expert.expertID)
#print sql
count = self.cursor.execute(sql)
if count > 0:
return True
else:
return False
def isPaperExist(self,paper):
sql = "select * from Paper where Expert_ID = '%s' and Paper_ID='%s' " %(paper.expertID,paper.paperID)
#print sql
count = self.cursor.execute(sql)
if count > 0:
return True
else:
return False
def insertExpert(self,expert):
sql = "insert into GeniusExpert(Expert_ID,Expert_name,Expert_keywords) values('%s','%s','%s')" %(expert.expertID,expert.name,str(expert.keywordsList))
try:
#print sql
self.cursor.execute(sql)
except Exception, e:
f = open("ExpertError.log","a")
f.write(str(e)+"\n")
f.write(sql+"\n")
f.write(time.strftime('%m-%d %H:%M:%S',time.localtime(time.time())))
f.close()
return True
def insertPaper(self,paper):
sql = "insert into Paper(Expert_ID,Paper_ID,Paper_Title,Paper_Authorslist,Paper_Url,Paper_Origin,Paper_Pubtime,Paper_Database,Paper_Citation) values('%s','%s','%s','%s','%s','%s','%s','%s','%s')" %(paper.expertID,paper.paperID,paper.title,paper.authorsList,paper.url,paper.origin,paper.pubtime,paper.database,paper.citation)
try:
#print sql
self.cursor.execute(sql)
except Exception, e:
f = open("PaperError.log","a")
f.write(str(e)+"\n")
f.write(sql+"\n")
f.write(time.strftime('%m-%d %H:%M:%S',time.localtime(time.time())))
f.close()
return True
class Paper():
#def __init__(self,id,title,authors,origin,pubtime,database,citation,url):
def __init__(self,expertID,paperID):
self.expertID = expertID
self.paperID = paperID
self.title = ""
self.authors = []
self.authorsList = "" # a|b|c
self.origin = ""
self.pubtime = ""
self.database = ""
self.citation = 0
self.url = ""
'''
self.title = title
self.authors = authors
self.origin = origin
self.pubtime = pubtime
self.database = database
self.citation = citation
'''
def convertToDict(self):
paper = {
"expertID" : self.expertID,
"paperID":self.paperID,
"title" : self.title,
"authors" : self.authors,
"origin" : self.origin,
"pubtime" : self.pubtime,
"database" : self.database,
"citation" : self.citation,
"url":self.url,
}
return paper
def listPaper(self):
print "expertID:",self.expertID
print "paperID:",self.paperID
print "title:",self.title
print "authorsList",self.authorsList
'''
print "authors:"
for author in self.authors:
print author,
print ""
'''
print "origin:",self.origin
print "pubtime:",self.pubtime
print "database:",self.database
print "citation:",self.citation
print "url:",self.url
def setPaperID(self,paperID):
self.paperID = paperID
def setTitle(self,title):
self.title = title
def addAuthor(self,author):
self.authors.append(author)
def setOrigin(self,origin):
self.origin = origin
def setPubtime(self,pubtime):
self.pubtime = pubtime
def setDatabase(self,database):
self.database = database
def setCitation(self,citation):
self.citation = citation
def setUrl(self,url):
self.url = url
class Expert():
def __init__(self,expertID,name,department):
self.expertID = expertID
self.name = name
self.department = department
self.keywords = []
self.papers = []
self.keywordsList = ""
def ChangeKeywordsToString(self):
self.keywordsList = " ".join(self.keywords)
def getName(self):
return self.name
def getDepartment(self):
return self.department
def setKeyword(self,keywords):
self.keywords = keywords
def addPapers(self,paper):
self.papers.append(paper)
def setPapers(self,papers):
self.papers = papers
def listPerson(self):
print "*"*80
print 'ExpertID:',self.expertID
print 'name:' + self.name+ 'department:' + self.department
print "keywords:"
for keyword in self.keywords:
print keyword,
print ""
print "papers:"
for paper in self.papers:
paper.listPaper()
print "-"*80
def InsertKeyWordToDB(fromSubDir,toSubDir):
db = DB()
parser = Parser()
for index in range(fromSubDir,toSubDir):
for root,dirs,files in os.walk('test/keyword/'+str(index)+"/"):
#each subdir: 1000record
start = time.time()
for afile in files:
if afile == '.DS_Store':
continue
words = afile.split('_')
aExpert = Expert(words[0].strip(),words[1].strip(),words[2].replace(".html","").strip())
aExpert.setKeyword(parser.parseKeyword(root,afile))
aExpert.ChangeKeywordsToString()
#print aExpert.keywordsList
if not db.isExpertExist(aExpert):
db.insertExpert(aExpert)
end = time.time()
db.conn.commit()
print ("KeywordSubDir %d is Done!"%index),
print time.strftime('%m-%d %H:%M:%S',time.localtime(time.time())),"total:",end-start
f = open("KeywordsToDB.log","a")
f.write(time.strftime('%m-%d %H:%M:%S',time.localtime(time.time()))+" keywordSubDir"+str(index)+" is Done! "+"total"+str(end-start) )
f.close()
db.close()
def InsertPaperToDB(fromSubDir,toSubDir):
db = DB()
parser = Parser()
for index in range(fromSubDir,toSubDir):
for root,dirs,files in os.walk('test/paper/'+str(index)+"/"):
n = 1000*index
start = time.time()
for afile in files:
if afile == '.DS_Store':
continue
words = afile.split('_')
papers = (parser.parsePaper(root,afile))
for eachPapaer in papers:
if not db.isPaperExist(eachPapaer):
db.insertPaper(eachPapaer)
print "n:",n,
print "Expert_ID %s is done"%words[0]
n = n + 1
db.conn.commit()
end = time.time()
print ("PaperSubDir %d is Done!"%index),
print time.strftime('%m-%d %H:%M:%S',time.localtime(time.time())),"time:",end-start,
f = open("PaperToDB.log","a")
f.write(time.strftime('%m-%d %H:%M:%S',time.localtime(time.time()))+" paperSubDir"+str(index)+" is Done! "+"total"+str(end-start) )
f.close()
db.close()
def main():
#print time.strftime('%m-%d %H:%M:%S',time.localtime(time.time()))
InsertKeyWordToDB(0,1)
InsertPaperToDB(0,1)
if __name__ == '__main__':
main()