-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_raw_data_2.py
318 lines (255 loc) · 11.3 KB
/
parse_raw_data_2.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
309
310
311
312
313
314
315
316
from __future__ import print_function
import sys
import os
import zipfile,io,gzip
import pandas as pd
from lxml import etree
import multiprocessing as mp
#from pathos import multiprocessing as mp
import numpy as np
import time
import glob
import datetime
import logging
from functools import partial
try:
import cPickle as pickle
except ImportError:
import pickle
from tqdm import tqdm as tq
"""
import lxml.etree as etree
x = etree.parse("filename")
print(etree.tostring(x, pretty_print = True))
"""
years = np.arange(1950,2016,1).astype(str)
basedir = 'Z:/DSSHU_ANNUAL_1950-2015/'
#basedir = '/webofscience/diego/backup_13_01_17/WoS_XML/xdata/data/'
output_dir = 'P:/Projects/WoS/WoS/parsed/'
#output_dir = '/backup/home/jared/storage/wos/parsed/'
#raw_data_path = '/backup/home/jared/storage/wos/raw/'
raw_data_path = 'P:/Projects/WoS/'
do_logging = False
allowed_filetypes = ['metadata','references','authors','subjects','keywords','abstracts']
filetypes = ['authors']
import time,datetime
class timed(object):
def __init__(self,desc='command',pad='',**kwargs):
self.desc = desc
self.kwargs = kwargs
self.pad = pad
def __enter__(self):
self.start = time.time()
print('{} started...'.format(self.desc))
def __exit__(self, type, value, traceback):
if len(self.kwargs)==0:
#logger.info('{}{} complete in {}{}'.format(self.pad,self.desc,str(datetime.timedelta(seconds=time.time()-self.start)),self.pad))
print('{}{} complete in {}{}'.format(self.pad,self.desc,str(datetime.timedelta(seconds=time.time()-self.start)),self.pad))
else:
#logger.info('{}{} complete in {} ({}){}'.format(self.pad,self.desc,str(datetime.timedelta(seconds=time.time()-self.start)),','.join(['{}={}'.format(*kw) for kw in self.kwargs.iteritems()]),self.pad))
print('{}{} complete in {} ({}){}'.format(self.pad,self.desc,str(datetime.timedelta(seconds=time.time()-self.start)),','.join(['{}={}'.format(*kw) for kw in self.kwargs.iteritems()]),self.pad))
if __name__!='__main__':
if 'authors' in filetypes:
with timed('prepping author data dict'):
dpath = raw_data_path+'dais_dict.pkl'
if os.path.exists(dpath):
#logger.info('loading existing dict')
author_dict = pickle.load(open(dpath,'rb'))
else:
#logger.info('generating dict')
author_dict = {}
with gzip.open(raw_data_path+'dais_data.gz') as f:
for line in f:
line = line.strip().split('|')
uid = line[0]
author_id = line[1]
seq = line[2]
if uid in author_dict:
author_dict[uid][seq] = author_id
else:
author_dict[uid] = {seq:author_id}
pickle.dump(author_dict,open(dpath,'wb'),protocol=2)
def reader(files):
for fname in files:
chunk = ''
with gzip.open(fname, "r") as f:
for line in f:
line = line.strip()
if '<REC' in line or ('<REC' in chunk and line != '</REC>'):
chunk += line
continue
if line == '</REC>':
chunk += line
yield chunk
chunk = ''
def zipreader(year):
with zipfile.ZipFile("{}{}_DSSHU.zip".format(basedir,year), 'r') as archive:
for name in archive.namelist():
if name.endswith('.xml.gz'):
bfn = archive.read(name)
bfi = io.BytesIO(bfn)
f = gzip.GzipFile(fileobj=bfi,mode='rb')
chunk = ''
for line in f:
line = line.strip()
if '<REC' in line or ('<REC' in chunk and line != '</REC>'):
chunk += line
continue
if line == '</REC>':
chunk += line
yield chunk
chunk = ''
def find_text(query):
if query is not None:
if query.text is not None:
return query.text
return ''
def process(record,handles,year):
paper = etree.fromstring(record)
uid = paper.find(".//UID").text
if 'metadata' in handles:
pub_info = paper.find('.//pub_info')
basic_data = pub_info.attrib
date = basic_data.get('sortdate','')
pubtype = basic_data.get('pubtype','')
volume = basic_data.get('vol','')
issue = basic_data.get('issue','')
pages = find_text(pub_info.find('page'))
doctype = '|'.join([find_text(doctype) for doctype in paper.find('.//doctypes')])
source_title = find_text(paper.find(".//title[@type='source']"))
paper_title = find_text(paper.find(".//title[@type='item']"))
#### NEED CONFERENCE / JOURNAL / PUBLISHER INFO
handles['metadata'].write(('\t'.join([uid,date,pubtype,volume,issue,pages,paper_title,source_title,doctype])+'\n').replace("'","").replace('"','').encode('utf8'))
if 'abstracts' in handles:
abstracts = paper.findall('.//abstract_text')
if len(abstracts)>1:
print(uid,year)
raise('multi-abstract?')
for a in abstracts:
all_p = a.findall('.//p')
handles['abstracts'].write(('\t'.join([uid,'|'.join([p.text for p in all_p])])+'\n').encode('utf8'))
if 'authors' in handles:
all_authors = []
all_author_names = []
author_add_idx = []
addresses = paper.findall('.//fullrecord_metadata/addresses/address_name/address_spec')
all_addresses = [a.find('full_address').text.replace('\\','') for a in addresses]
address_numbers = [a.attrib['addr_no'] for a in addresses]
mapping = {n:i for i,n in enumerate(address_numbers)}
for author in paper.findall('.//summary/names/name'):
basic_data = author.attrib
#dais = basic_data.get('dais_id','')
#role = basic_data.get('role','')
addr_no = basic_data.get('addr_no',None)
if addr_no is not None:
addr_no = [mapping.get(a,-1) for a in addr_no.split()]
else:
addr_no = [-1]
author_add_idx.append(','.join(map(str,addr_no)))
fullname = author.find('full_name').text
if fullname is not None:
fullname = fullname.replace('|','').replace('\\','') # kludges
if not fullname:
fullname = '?'
try:
seq = author.attrib['seq_no']
author_id = author_dict[uid][seq]
except KeyError:
author_id = '-1'
all_authors.append(author_id)
all_author_names.append(fullname)
handles['authors'].write("{}\t{}\t{}\t{}\t{}\n".format(uid,'|'.join(all_authors),'|'.join(all_author_names),'|'.join(all_addresses),'|'.join(author_add_idx)).encode('utf8'))
#for address in paper.findall('.//addresses/address_name/address_spec'):
# pass
if 'subjects' in handles:
heading = find_text(paper.find('.//heading'))
subheading = find_text(paper.find('.//subheading'))
categories = '|'.join([cat.text for cat in paper.findall(".//subject[@ascatype='traditional']")])
handles['subjects'].write("{}\t{}\t{}\t{}\n".format(uid,heading,subheading,categories).encode('utf8'))
if 'references' in handles:
references = []
no_uid = 0
for ref in paper.find(".//references"):
ref_uid = ref.find('.//uid')
if ref_uid is not None:
references.append(ref_uid.text)
else:
no_uid += 1
handles['references'].write("{}\t{}\t{}\t{}\n".format(uid,len(references),'|'.join(references),no_uid).encode('utf8'))
if 'keywords' in handles:
keywords = paper.findall('.//keyword')
if len(keywords) > 0:
keyword_text = [k.text.lower() for k in keywords]
handles['keywords'].write("{}\t{}\t{}\n".format(uid,len(keyword_text),'|'.join(keyword_text)).encode('utf8'))
def go(year,fromzip = False):
year_start = time.time()
if fromzip:
records = zipreader(year)
else:
filelist = [f for f in glob.glob(basedir+'*') if f[f.rfind('/'):][4:8]==year]
records = reader(filelist)
records_logged = 0
files = ['{}{}/{}.txt.gz'.format(output_dir,kind,year) for kind in filetypes]
handles = dict(zip(filetypes,[gzip.open(f,'wb') for f in files]))
for record in records:
process(record,handles,year)
records_logged += 1
#if records_logged % 10000 == 0:
# logger.info("{} --> {} records complete".format(year,records_logged))
for handle in handles.values():
handle.close()
td = str(datetime.timedelta(seconds=time.time()-year_start))
#logger.info("{} --> ALL {} records logged in {}".format(year,records_logged,td))
print("{} --> ALL {} records logged in {}".format(year,records_logged,td))
return records_logged
#N = mp.cpu_count()
N=12
if __name__ == '__main__':
overall_start = time.time()
if do_logging:
now = datetime.datetime.now()
logpath = now.strftime('%Y%m%d_%H%M%S.log')
logger = logging.getLogger('WoS processing')
hdlr = logging.FileHandler(logpath)
formatter = logging.Formatter('%(asctime)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)
#filetypes = sys.argv[2:]
# else:
# filetypes = sys.argv[1:]
# for f in filetypes:
# if f not in allowed_filetypes:
# raise("Not a valid filetype")
# dname = 'parsed/{}'.format(f)
# if not os.path.exists(dname):
# os.mkdir(dname)
# if 'authors' in filetypes:
# with timed('prepping author data dict'):
# dpath = raw_data_path+'dais_dict.pkl'
# if os.path.exists(dpath):
# #logger.info('loading existing dict')
# author_dict = pickle.load(open(dpath))
# else:
# #logger.info('generating dict')
# author_dict = {}
# with gzip.open(raw_data_path+'dais_data.gz') as f:
# for line in tq(f):
# line = line.strip().split('|')
# uid = line[0]
# author_id = line[1]
# seq = line[2]
# if uid in author_dict:
# author_dict[uid][seq] = author_id
# else:
# author_dict[uid] = {seq:author_id}
# pickle.dump(author_dict,open(dpath,'wb'),protocol=2)
pool = mp.Pool(N)
#func_partial = partial(go,filetypes=filetypes,fromzip=True)
func_partial = partial(go,fromzip=True)
record_count = pool.map(func_partial,years[::-1])
#record_count = pool.map(go,years[::-1])
#pool.close()
td = str(datetime.timedelta(seconds=time.time()-overall_start))
#logger.info("Parsing complete: {} total records processed in {}".format(sum(record_count),td))
print("Parsing complete: {} total records processed in {}".format(sum(record_count),td))