-
Notifications
You must be signed in to change notification settings - Fork 5
/
xmlFormat.py
executable file
·832 lines (722 loc) · 39.1 KB
/
xmlFormat.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
# -*- coding: utf-8 -*-
import xml.etree.cElementTree as ET
from collections import OrderedDict
from datetime import datetime
from flask import current_app
from textwrap import fill
import re
from geotext import GeoText
from csv import reader
from exportsrv.formatter.format import Format
from exportsrv.utils import get_eprint
from exportsrv.formatter.strftime import strftime
# This class accepts JSON object created by Solr and can reformat it
# for the XML Export formats we are supporting.
# 1- To get Dublin Core XML use
# dublinXML = XMLFormat(jsonFromSolr).get_dublincore_xml()
# 2- To get Reference XML without Abstract use
# referenceXML = XMLFormat(jsonFromSolr).get_reference_xml()
# 3- To get Reference XML with Abstract use
# referenceXML = XMLFormat(jsonFromSolr).get_reference_xml(True)
# 4- To get JATS XML use
# referenceXML = XMLFormat(jsonFromSolr).get_jats_xml(True)
class XMLFormat(Format):
EXPORT_FORMAT_REF_XML = 'ReferenceXML'
EXPORT_FORMAT_REF_ABS_XML = 'ReferenceAbsXML'
EXPORT_FORMAT_DUBLIN_XML = 'DublinXML'
EXPORT_FORMAT_JATS_XML = 'JATSXML'
EXPORT_SERVICE_RECORDS_SET_XML_REF = [('xmlns', 'http://ads.harvard.edu/schema/abs/1.1/references'),
('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'),
('xsi:schemaLocation', 'http://ads.harvard.edu/schema/abs/1.1/references http://ads.harvard.edu/schema/abs/1.1/references.xsd')]
EXPORT_SERVICE_RECORDS_SET_XML_REF_ABS = [('xmlns', 'http://ads.harvard.edu/schema/abs/1.1/abstracts'),
('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'),
('xsi:schemaLocation', 'http://ads.harvard.edu/schema/abs/1.1/abstracts http://ads.harvard.edu/schema/abs/1.1/abstracts.xsd')]
EXPORT_SERVICE_RECORDS_SET_XML_DUBLIN = [('xmlns', 'http://ads.harvard.edu/schema/abs/1.1/dc'),
('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'),
('xmlns:dc', 'http://purl.org/dc/elements/1.1/'),
('xsi:schemaLocation', 'http://ads.harvard.edu/schema/abs/1.1/dc http://ads.harvard.edu/schema/abs/1.1/dc.xsd')]
EXPORT_SERVICE_RECORDS_SET_XML_JATS = [('xmlns', 'http://ads.harvard.edu/schema/abs/1.1/dc'),
('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'),
('xmlns:dc', 'http://purl.org/dc/elements/1.1/'),
('xsi:schemaLocation', 'http://ads.harvard.edu/schema/abs/1.0/jats http://ads.harvard.edu/schema/abs/1.0/jats.xsd')]
re_year = re.compile(r'([12]+[09]\d\d)')
# partial list of known publishers,
# once this field is populated in solr, rely on solr,
# for now this is only used for JATS format
re_publisher_names = re.compile(r"([A-Z]+[A-Za-z\s\-:]+ University Press|[A-Z]+[A-Za-z\s\-:,']+ Press|Springer- .*|Elsevier|University of [A-Z]+[A-Za-z\s-]+ Press|University of [A-Z]+[A-Za-z\s-]+|[\w-]+,\s*\w+\s*:[\s\w]+|Springer\s+([A-Z]+[A-Za-z\s-]+)+|Springer Nature|Springer, Cham|Springer Fachmedien Wiesbaden GmbH, DE|Springer-Verlag GmbH Deutschland|Springer-Verlag Berlin Heidelberg|Springer-Verlag|Springer|Cambridge, J. Wilson and son, University press|Cambridge, The University press|Oxford university press|Loyola university press|Cambridge, University press|Harvard university press|Cambridge, Eng., The University press|Louisiana state university press|Des Moines, Iowa, University press|Cambridge [Eng.] The University press|Edinburgh University Press|Cambridge University Press|Yale University Press|Brigham Young University Press|University Press of Virginia|Erevan University Press|Artemis Press|Laval University Press|Columbia University Press|Rutgers University Press|University Press of America|Johns Hopkins University Press|Sydney University Press|Yerevan University press|McGill-Queen's University Press|Innsbruck University Press|University of Arizona Press|Atlantis Press|Ilia State University Press|Ziti press|The University of Chicago Press|Princeton University Press|eXamen.press|T rculo Press|Duke University Press|Kyoto University Press|Imperial College Press|Heron Press Ltd|Kyiv University Press|Sole Logistics Press|BrownWalker Press|Joseph Henry Press|National Radio Astronomy Press|SPIE Press|Kyriakidis Press|St. Martin's Press|Huntington Library and University of Washington Press|Microcosm Press|Free Press \(Simon and Schuster\)|Cambridge Univ. Press|Templeton Foundation Press|IEEE Press|Heron Press|AIP Press|Pergamon Press|Boydell Press|Baltic Astronomy 6 and L. Davis Press|West Virginia University Press|ACM Press Books|State University of New York \(SUNY\) Press|Clarendon Press|Universal Academic Press Inc|SPIE Optical Engineering Press|Presses universitaires de France|Ginn Press|ABELexpress|SPC Press|CRC Press|Plenum Press|Pedagogical Univ. Press|The MIT \(Massachusetts Institute of Technology\) Press|Yourdon Press Computing Series|L. Davis Press|Ivy Press Books|Moscow Univ. Press|Cambridge Univ. Press|Presses du CNRS|Presses de l'Ecole nationale des ponts et chaussees|Academic Press Inc|Cambridge UniversityPress|Vantage Press Inc|Massachusettes Institute of Technology \(MIT\) Press|IAP Press|Academic Press and OHM|IEEE Comput. Soc. Press|The Weizmann Science Press of Israel|MIT Press|Process Press|Blandford Press|Science Press|University of Tasmania Press|Vantage Press|Arno Press|Academic Press|University of Massachusetts Press|Delacorte Press/E. Friede|The Macmillan Press Ltd|Peebles Press|Anchor Press/Doubleday|Smithsonian Institution Press|Anchor Press / Doubleday|FAN Press|Univ. Calif. Press|Presses de la Cite|Ballena Press|Pica Press|University of Texas Press|Optosonic Press|University of Missouri Press|University of Alabama Press|Nauka Press|Exposition Press|Presses universitaire de France|Viking Press|Priory Press|Chemical Rubber Co. Press|Books for Libraries Press|Pragopress|Fundamental Research Press|University of California Press|University of Michigan Press|University of New Mexico Press|University of London Press|Natural History Press|Lenin Belorussian State University Press|Crowell-Collier Press|Greenwood Press|NEO Press|M.I.T. Press|Univ. Wisconsin Press|University of Chicago Press|University of Colorado Press|Brockhampton Press|Golden Press|Lutterworth Press|Trident Press Book|Beacon Press|St Martin's Press|Pageant Press|M. I. T. Press|Orion Press|The Univesrity of Chicago Press|Museum Press|Citadel Press|Pegasus Press|Childrens Press|Ronald Press Co|Majestic Press|Westernlore Press|The Science press printing company|The Technical press ltd|The Florida Bible institute press|The Sheldon press|Pacific Science Press|The Theosophical press|The Clarendon press|The Pilgrim press|The Hispanic Society of America and The De Vinne Press|Press of E. W. Stephen|The Nichols press|Press of T. P. Nichols|Press of J. Wilson and son|Roy. Acad. press)")
def __format_date(self, solr_date, export_format):
"""
:param solr_date:
:param export_format:
:return:
"""
# solr_date has the format 2017-12-01
dateTime = datetime.strptime(solr_date.replace('-00', '-01'), '%Y-%m-%d')
formats = {self.EXPORT_FORMAT_DUBLIN_XML: '%Y-%m-%d', self.EXPORT_FORMAT_REF_XML: '%b %Y', self.EXPORT_FORMAT_REF_ABS_XML: '%b %Y'}
return strftime(dateTime, formats[export_format])
def __format_line_wrapped(self, text):
"""
:param text:
:return:
"""
# 3/2 for now do not wrap text, it is way too slow
return text
return fill(text, width=72, break_on_hyphens=False)
def __add_author_list(self, a_doc, parent, tag):
"""
format authors
:param a_doc:
:param parent:
:param tag:
:return:
"""
if 'author' not in a_doc:
return
for author in a_doc['author']:
ET.SubElement(parent, tag).text = author
def __add_affiliation_list(self, a_doc, parent, field):
"""
format affilation
:param a_doc:
:param parent:
:param field:
:return:
"""
if ('aff') not in a_doc:
return ''
counter = self.generate_counter_id(len(a_doc['aff']))
separator = ', '
affiliation_list = ''
for affiliation, i in zip(a_doc['aff'], range(len(a_doc['aff']))):
if (affiliation != '-'):
affiliation_list += counter[i] + '(' + affiliation + ')' + separator
# do not need the last separator
if (len(affiliation_list) > len(separator)):
affiliation_list = affiliation_list[:-len(separator)]
# if no affiliation was defined
if (len(affiliation_list) > 0):
ET.SubElement(parent, field).text = self.__format_line_wrapped(affiliation_list)
def __add_doc_a_link(self, parent, link_type, link_name, link_url, count='', access=''):
"""
add a link to xml structure
:param parent:
:param link_type:
:param link_name:
:param link_url:
:param count:
:param access:
:return:
"""
record = ET.SubElement(parent, "link")
record.set('type', link_type)
if (len(access) > 0):
record.set('access', access)
ET.SubElement(record, 'name').text = link_name
ET.SubElement(record, 'url').text = link_url
if (len(count) > 0):
ET.SubElement(record, 'count').text = count
def __add_doc_links_property(self, a_doc, parent):
"""
format links that are defined in the property field
:param a_doc:
:param parent:
:return:
"""
link_url_format = current_app.config['EXPORT_SERVICE_RESOLVE_URL'] + '/{bibcode}/{link_type}'
bibcode = a_doc.get('bibcode', '')
link_dict = OrderedDict([
('TOC', ['TOC', 'Table of Contents']),
('LIBRARYCATALOG', ['LIBRARY', 'Library Entry']),
])
for link in link_dict:
if link in a_doc.get('property', ''):
self.__add_doc_a_link(parent=parent, link_type=link_dict[link][0], link_name=link_dict[link][1],
link_url=link_url_format.format(bibcode=bibcode, link_type=link.lower()))
def __add_doc_links_esource(self, a_doc, parent):
"""
format links that are defined in the esource field
:param a_doc:
:param parent:
:return:
"""
link_url_format = current_app.config['EXPORT_SERVICE_RESOLVE_URL'] + '/{bibcode}/{link_type}'
bibcode = a_doc.get('bibcode', '')
link_dict = OrderedDict([
# (link type:[name, access])
('eprint_html', ['arXiv Article', '']),
('author_html', ['Author Article', '']),
('pub_html', ['Publisher Article', '']),
('ads_scan', ['Scanned Article (GIF)', '']),
('eprint_pdf', ['arXiv PDF', 'eprint_openaccess']),
('author_pdf', ['Author PDF', 'author_openaccess']),
('pub_pdf', ['Publisher PDF', 'pub_openaccess']),
('ads_pdf', ['ADS PDF', 'ads_openaccess']),
])
for link in link_dict:
if link.upper() in a_doc.get('esources', ''):
self.__add_doc_a_link(parent=parent, link_type=link.upper(), link_name=link_dict[link][0],
link_url=link_url_format.format(bibcode=bibcode, link_type=link.upper()),
access='open' if link_dict[link][1].upper() in a_doc.get('property', '') else '')
def __add_doc_links_data(self, a_doc, parent):
"""
format links that are defined in the data field
:param a_doc:
:param parent:
:return:
"""
link_url_format = current_app.config['EXPORT_SERVICE_RESOLVE_URL'] + '/{bibcode}/{link_type}'
bibcode = a_doc.get('bibcode', '')
link_dict = OrderedDict([
('ARI', 'Astronomisches Rechen-Institut'),
('SIMBAD', 'SIMBAD Database at the CDS'),
('NED', 'NASA/IPAC Extragalactic Database'),
('CDS', 'Strasbourg Astronomical Data Center'),
('Vizier', 'VizieR Catalog Service'),
('GCPD', 'The General Catalogue of Photometric Data'),
('Author', 'Author Hosted Dataset'),
('PDG', 'Particle Data Group'),
('MAST', 'Mikulski Archive for Space Telescopes'),
('HEASARC', '''NASA's High Energy Astrophysics Science Archive Research Center'''),
('INES', 'IUE Newly Extracted Spectra'),
('IBVS', 'Information Bulletin on Variable Stars'),
('Astroverse', 'CfA Dataverse'),
('ESA', 'ESAC Science Data Center'),
('NExScI', 'NASA Exoplanet Archive'),
('PDS', 'The NASA Planetary Data System'),
('AcA', 'Acta Astronomica Data Files'),
('ISO', 'Infrared Space Observatory'),
('ESO', 'European Southern Observatory'),
('Chandra', 'Chandra X-Ray Observatory'),
('NOAO', 'National Optical Astronomy Observatory'),
('XMM', 'XMM Newton Science Archive'),
('Spitzer', 'Spitzer Space Telescope'),
('PASA', 'Publication of the Astronomical Society of Australia Datasets'),
('ATNF', 'Australia Telescope Online Archive'),
('KOA', 'Keck Observatory Archive'),
('Herschel', 'Herschel Science Center'),
('GTC', 'Gran Telescopio CANARIAS Public Archive'),
('BICEP2', 'BICEP/Keck Data'),
('ALMA', 'Atacama Large Millimeter/submillimeter Array'),
('CADC', 'Canadian Astronomy Data Center'),
('Zenodo', 'Zenodo Archive'),
('TNS', 'Transient Name Server'),
('IRSA', 'NASA/IPAC Infrared Science Archive'),
('Github', 'Git Repository Hosting Service'),
('Dryad', 'International Repository of Research Data'),
('Figshare', 'Online Open Access Repository'),
('JWST', 'JWST Proposal Info'),
('PANGAEA', 'Digital Data Library and a Data Publisher for Earth System Science'),
('protocols', 'Collaborative Platform and Preprint Server for Science Methods and Protocols'),
('BAVJ', 'Data of the German Association for Variable Stars'),
])
data_dict = {}
for d in a_doc.get('data', ''):
data_dict[d.split(':')[0]] = int(d.split(':')[1])
for link in link_dict:
if link in data_dict.keys():
self.__add_doc_a_link(parent=parent, link_type=link, link_name=link_dict[link],
link_url=link_url_format.format(bibcode=bibcode, link_type=link),
count=str(data_dict[link]))
def __add_doc_links(self, a_doc, parent):
"""
format links
:param a_doc:
:param parent:
:return:
"""
link_url_format = current_app.config['EXPORT_SERVICE_RESOLVE_URL'] + '/{bibcode}/{link_type}'
bibcode = a_doc.get('bibcode', '')
link_dict = OrderedDict([
#(link type:[include if, name, has count])
('abstract', [len(a_doc.get('abstract', '')), 'abstract', False]),
('citations', [a_doc.get('num_citations', 0), 'Citations to the Article', 'citations', True]),
('reference', [a_doc.get('num_references', 0), 'References in the Article', 'references', True]),
('coreads', [a_doc.get('read_count', 0), 'Co-Reads', False]),
])
for link in link_dict:
if (link_dict[link][0] > 0):
self.__add_doc_a_link(parent=parent, link_type=link, link_name=link_dict[link][1],
link_url=link_url_format.format(bibcode=bibcode, link_type=link),
count=str(link_dict[link][0]) if link_dict[link][2] else '')
self.__add_doc_links_property(a_doc, parent)
self.__add_doc_links_esource(a_doc, parent)
self.__add_doc_links_data(a_doc, parent)
def __add_keywords(self, a_doc, parent, export_format):
"""
format keyword
:param a_doc:
:param parent:
:param export_format:
:return:
"""
if 'keyword' not in a_doc:
return
if (export_format == self.EXPORT_FORMAT_REF_ABS_XML):
record = ET.SubElement(parent, "keywords")
for keyword in a_doc['keyword']:
ET.SubElement(record, 'keyword').text = keyword
elif (export_format == self.EXPORT_FORMAT_DUBLIN_XML):
ET.SubElement(parent, 'dc:subject').text = self.__format_line_wrapped(', '.join(a_doc.get('keyword', '')))
def __add_clean_pub_raw(self, a_doc):
"""
parse pub_raw and eliminate tags
:param a_doc:
:return:
"""
pub_raw = ''.join(a_doc.get('pub_raw', ''))
# proceed only if necessary
if ('<' in pub_raw) and ('>' in pub_raw):
for key in self.REGEX_PUB_RAW:
pub_raw = key.sub(self.REGEX_PUB_RAW[key], pub_raw)
return pub_raw
def __add_pub_raw(self, a_doc, parent, field, export_format):
"""
format pub_raw
:param a_doc:
:param parent:
:param field:
:param export_format:
:return:
"""
if 'pub_raw' not in a_doc:
return
pub_raw = self.__add_clean_pub_raw(a_doc)
if (export_format == self.EXPORT_FORMAT_REF_XML) or (export_format == self.EXPORT_FORMAT_REF_ABS_XML):
ET.SubElement(parent, field).text = pub_raw
elif (export_format == self.EXPORT_FORMAT_DUBLIN_XML):
# for dublin both types of pub_raw are exported
# we could have something like this is Solr
# "pub_raw":"Sensing and Imaging, Volume 18, Issue 1, article id. #17, <NUMPAGES>12</NUMPAGES> pp."
# where we remove the tag and output it, or something like this
# "pub_raw":"eprint arXiv:astro-ph/0003081"
# that gets output as is
pub_raw = self.REGEX_REMOVE_TAGS_PUB_RAW.sub('', pub_raw)
ET.SubElement(parent, field).text = self.__format_line_wrapped(pub_raw)
def __get_fields(self, export_format):
"""
from solr to each types' tags
:param export_format:
:return:
"""
if (export_format == self.EXPORT_FORMAT_REF_XML):
fields = [('bibcode', 'bibcode'), ('title', 'title'), ('author', 'author'),
('pub_raw', 'journal'), ('pubdate', 'pubdate'), ('link', 'link'),
('url', 'url'), ('', 'score'), ('num_citations', 'citations'),
('doi', 'DOI'), ('eprintid', 'eprintid')]
elif (export_format == self.EXPORT_FORMAT_REF_ABS_XML):
fields = [('bibcode', 'bibcode'), ('title', 'title'), ('author', 'author'),
('aff', 'affiliation'), ('pub_raw', 'journal'), ('volume', 'volume'),
('pubdate', 'pubdate'), ('page', 'page'), ('page_range', 'lastpage'),
('keyword', 'keywords'), ('', 'origin'), ('copyright', 'copyright'),
('link', 'link'), ('url', 'url'), ('comment', 'comment'),
('', 'score'), ('num_citations', 'citations'), ('abstract', 'abstract'),
('doi', 'DOI'), ('eprintid', 'eprintid')]
elif (export_format == self.EXPORT_FORMAT_DUBLIN_XML):
fields = [('bibcode', 'dc:identifier'), ('title', 'dc:title'), ('author', 'dc:creator'),
('pub_raw', 'dc:source'), ('pubdate', 'dc:date'), ('keyword', 'dc:subject'),
('copyright', 'dc:rights'), ('url', 'dc:relation'), ('num_citations', 'dc:relation'),
('abstract', 'dc:description'), ('doi', 'dc:identifier')]
elif (export_format == self.EXPORT_FORMAT_JATS_XML):
fields = [('doctype', ''), ('author', ''), ('year', ''), ('title', ''),
('pub', ''), ('pub_raw', ''), ('volume', 'volume'), ('issue', 'issue'),
('editor', ''), ('publisher', ''), ('page', ''), ('page_range', ''), ('doi', '')]
else:
fields = []
return OrderedDict(fields)
def __get_doi(self, doi):
"""
:param doi:
:return:
"""
if len(doi) > 0:
return 'doi:' + doi
return ''
def __get_citation(self, num_citations, export_format):
"""
:param num_citations:
:return:
"""
if num_citations != 0:
if (export_format == self.EXPORT_FORMAT_REF_XML) or (export_format == self.EXPORT_FORMAT_REF_ABS_XML):
return str(num_citations)
if (export_format == self.EXPORT_FORMAT_DUBLIN_XML):
return 'citations:' + str(num_citations)
return ''
def __add_page(self, a_doc, parent, field):
"""
:return:
"""
if 'page_range' not in a_doc or 'page' not in a_doc:
return
page = ''.join(a_doc.get('page_range', ''))
if len(page) > 0:
if '-' in page:
page,lastpage = page.split('-')
else:
lastpage = ''
else:
page = ''.join(a_doc.get('page', ''))
lastpage = ''
if (field == 'page'):
self.__add_in(parent, field, page)
elif (field == 'lastpage'):
self.__add_in(parent, field, lastpage)
def __add_in(self, parent, field, value):
"""
add the value into the return structure, only if a value was defined in Solr
:param parent:
:param field:
:param value:
:return:
"""
if (len(value) > 0):
ET.SubElement(parent, field).text = value
def __get_attrib(self, export_format):
"""
:param export_format:
:return:
"""
if (export_format == self.EXPORT_FORMAT_REF_XML):
return OrderedDict(self.EXPORT_SERVICE_RECORDS_SET_XML_REF)
if (export_format == self.EXPORT_FORMAT_REF_ABS_XML):
return OrderedDict(self.EXPORT_SERVICE_RECORDS_SET_XML_REF_ABS)
if (export_format == self.EXPORT_FORMAT_DUBLIN_XML):
return OrderedDict(self.EXPORT_SERVICE_RECORDS_SET_XML_DUBLIN)
return OrderedDict([])
def __get_num_citations(self):
"""
:return:
"""
num_citations = 0
for a_doc in self.from_solr['response'].get('docs'):
num_citations += int(a_doc.get('num_citations', '0'))
return num_citations
def __get_doc_dublin_xml(self, index, parent):
"""
for each document from Solr, get the fields, and format them accordingly for Dublin format
:param index:
:param parent:
:return:
"""
a_doc = self.from_solr['response'].get('docs')[index]
fields = self.__get_fields(self.EXPORT_FORMAT_DUBLIN_XML)
record = ET.SubElement(parent, "record")
for field in fields:
if (field == 'bibcode') or (field == 'copyright'):
self.__add_in(record, fields[field], a_doc.get(field, ''))
elif (field == 'title'):
self.__add_in(record, fields[field], ''.join(a_doc.get(field, '')))
elif (field == 'author'):
self.__add_author_list(a_doc, record, fields[field])
elif (field == 'pub_raw'):
self.__add_pub_raw(a_doc, record, fields[field], self.EXPORT_FORMAT_DUBLIN_XML)
elif (field == 'pubdate'):
self.__add_in(record, fields[field], self.__format_date(a_doc.get(field, ''), self.EXPORT_FORMAT_DUBLIN_XML))
elif (field == 'keyword'):
self.__add_keywords(a_doc, record, self.EXPORT_FORMAT_DUBLIN_XML)
elif (field == 'url'):
self.__add_in(record, fields[field], current_app.config.get('EXPORT_SERVICE_FROM_BBB_URL') + '/' + a_doc.get('bibcode', ''))
elif (field == 'abstract'):
self.__add_in(record, fields[field], self.__format_line_wrapped(a_doc.get(field, '')))
elif (field == 'doi'):
self.__add_in(record, fields[field], self.__get_doi(''.join(a_doc.get(field, ''))))
elif (field == 'num_citations'):
self.__add_in(record, fields[field], self.__get_citation(int(a_doc.get(field, 0)), self.EXPORT_FORMAT_DUBLIN_XML))
def __get_doc_reference_xml(self, index, parent, export_format):
"""
for each document from Solr, get the fields, and format them accordingly for Reference format
:param index:
:param parent:
:param export_format:
:return:
"""
a_doc = self.from_solr['response'].get('docs')[index]
fields = self.__get_fields(export_format)
record = ET.SubElement(parent, "record")
if (export_format == self.EXPORT_FORMAT_REF_ABS_XML):
property = a_doc.get('property', [])
if 'REFEREED' in property:
record.set('refereed', 'true')
if 'ARTICLE' in property:
record.set('article', 'true')
record.set('type', a_doc.get('doctype', ''))
for field in fields:
if (field == 'bibcode') or (field == 'pub') or (field == 'volume') or \
(field == 'copyright'):
self.__add_in(record, fields[field], a_doc.get(field, ''))
elif (field == 'title') or (field == 'doi'):
self.__add_in(record, fields[field], ''.join(a_doc.get(field, '')))
elif (field == 'author'):
self.__add_author_list(a_doc, record, fields[field])
elif (field == 'aff'):
self.__add_affiliation_list(a_doc, record, fields[field])
elif (field == 'pubdate'):
self.__add_in(record, fields[field], self.__format_date(a_doc.get(field, ''), export_format))
elif (field == 'pub_raw'):
self.__add_pub_raw(a_doc, record, fields[field], export_format)
elif (field == 'page') or (field == 'page_range'):
self.__add_page(a_doc, record, fields[field])
elif (field == 'keyword'):
self.__add_keywords(a_doc, record, export_format)
elif (field == 'url'):
self.__add_in(record, fields[field], current_app.config.get('EXPORT_SERVICE_FROM_BBB_URL') + '/' + a_doc.get('bibcode', ''))
elif (field == 'num_citations'):
self.__add_in(record, fields[field], self.__get_citation(int(a_doc.get(field, 0)), export_format))
elif (field == 'abstract'):
self.__add_in(record, fields[field], self.__format_line_wrapped(a_doc.get(field, '')))
elif (field == 'link'):
self.__add_doc_links(a_doc, record)
elif (field == 'eprintid'):
self.__add_in(record, fields[field], get_eprint(a_doc))
def __add_person_group_jats_xml(self, person_list, record, person_group_type):
"""
add author or editors for JATS xml format
:param record:
:param person_group_type:
:return:
"""
if person_list:
# add outter tag
person_group_record = ET.SubElement(record, 'person-group')
person_group_record.set('person-group-type', person_group_type)
# now add inner tag
for person in person_list:
separate = person.split(',')
# author might not have first name
if len(separate) >= 1:
person_record = ET.SubElement(person_group_record, 'string-name')
ET.SubElement(person_record, 'surname').text = separate[0].strip()
if len(separate) == 2:
ET.SubElement(person_record, 'given-names').text = '%s.'%separate[1].strip()[0]
# add role tag if this is editor type
if (person_group_type == 'editor'):
ET.SubElement(record, 'role').text = 'Eds.'
def __add_title_jats_xml(self, title, record, publication_type, lookahead):
"""
format title basded on JATS publication type
:param title:
:param record:
:param publication_type:
:param lookahead:
:return:
"""
title = ';'.join(title)
# <article-title>title</article-title>.
if publication_type in ['journal', 'report']:
title_record = ET.SubElement(record, 'article-title')
title_record.text = title
title_record.tail = '.\n'
# <article-title>title</article-title>,
elif publication_type == 'confproc':
title_record = ET.SubElement(record, 'article-title')
title_record.text = title
title_record.tail = ',\n'
# book: <source><italic>title</italic></source>
# book with editor: <source><italic>title</italic></source>;
elif publication_type == 'book':
title_record = ET.SubElement(record, 'title')
ET.SubElement(title_record, 'italic').text = title
# is set to true if the record has editor and needs to have semicolon at the end
if lookahead:
title_record.tail = ';\n'
# <source>Ph.D. thesis</source>
elif publication_type == 'thesis':
ET.SubElement(record, 'source').text = 'Ph.D. thesis'
# <source>title</source>.
elif publication_type in ['software', 'review', 'other']:
ET.SubElement(record, 'source').text = title
def __add_conf_proc_info_jats_xml(self, pub_raw, record):
"""
for confproc publication type, jats format the following four tags are needed to be filled
<conf-name>usually the first or second substring</conf-name>,
<conf-loc>city/country usually appears following conference name</conf-loc>,
<month>most records do not have the month of conference so for now ignore</month>
<year>year of the conference usually appears in pub_raw</year>.
:param pub_raw:
:param record:
:return:
"""
# see if the year appears in pub_raw
year = None
match = self.re_year.search(pub_raw)
if match:
year = match.group(1)
# see if the location is in pub_raw
location = ''
places = GeoText(pub_raw)
if places.cities:
location = places.cities
if places.countries:
if location:
location += ', '
location += places.countries
# now split the pub_raw and try to see if conference name can be inferred
conference = [s for s in list(reader([pub_raw]))[0] if 'conference' in s.lower()]
if conference:
ET.SubElement(record, 'conf-name').text = conference[0]
if location:
ET.SubElement(record, 'conf-loc').text = location
if year:
ET.SubElement(record, 'year').text = location
def __add_book_publisher_info_jats_xml(self, pub_raw, record):
"""
for book publication type, jats format the following four tags are needed to be filled
<publisher-loc>in pub_raw</publisher-loc>:
<publisher-name>in pub_raw</publisher-name>.
:param pub_raw:
:param record:
:return:
"""
# see if the location is in pub_raw
location = ''
places = GeoText(pub_raw)
if places.cities:
location = places.cities
if places.countries:
if location:
location += ', '
location += places.countries
publisher = ''
match = self.re_publisher_names.search(pub_raw, re.IGNORECASE)
if match:
publisher = match.group(1)
if publisher:
if location:
location_record = ET.SubElement(record, 'publisher-loc')
location_record.text = location
location_record.tail = ': '
ET.SubElement(record, 'publisher-name').text = publisher
def __get_doc_jats_xml(self, index, parent):
"""
for each document from Solr, get the fields, and format them accordingly for JATS format
:param index:
:param parent:
:return:
"""
ads_to_jats_doctype_mapping = {
'book': 'book', 'inproceedings': 'book', 'inbook': 'book',
'proceedings':'confproc',
'article': 'journal', 'abstract': 'journal', 'eprint': 'journal',
'phdthesis': 'thesis', 'mastersthesis': 'thesis',
'software': 'software',
'techreport': 'report',
'bookreview': 'review',
'circular': 'other', 'editorial': 'other', 'erratum': 'other', 'misc': 'other', 'catalog': 'other',
'newsletter': 'other', 'obituary': 'other', 'pressrelease': 'other', 'proposal': 'other', 'talk': 'other',
}
a_doc = self.from_solr['response'].get('docs')[index]
fields = self.__get_fields(self.EXPORT_FORMAT_JATS_XML)
# add outter tag and label for this reference
ref = ET.SubElement(parent, 'ref', id='CIT%03d'%(index+1))
ET.SubElement(ref, 'label').text = '%d.'%(index+1)
publication_type = ''
for field in fields:
if not a_doc.get(field, None):
continue
if (field == 'doctype'):
publication_type = ads_to_jats_doctype_mapping[a_doc.get(field, '')]
record = ET.SubElement(ref, 'mixed-citation')
record.set('publication-type', publication_type)
elif (field == 'author') or (field == 'editor'):
self.__add_person_group_jats_xml(a_doc.get(field, []), record, field)
elif (field == 'year'):
# year appears in parenthesis, so need to find the last element and add open parenthesis
if record:
record[-1].tail = '\n('
else:
record.text = '\n('
year = ET.SubElement(record, 'year')
year.text = a_doc.get(field, '')
# now add the close parenthesis
year.tail = ')\n'
elif (field == 'title'):
self.__add_title_jats_xml(a_doc.get(field, ''), record, publication_type, a_doc.get('editor', None))
elif (field == 'pub'):
if (publication_type == 'journal'):
source_record = ET.SubElement(record, 'source')
ET.SubElement(source_record, 'italic').text = a_doc.get(field, '')
elif (field == 'pub_raw'):
if (publication_type == 'confproc'):
self.__add_conf_proc_info_jats_xml(a_doc.get(field, ''), record)
# TODO: once solr contains publisher info need to remove extracting publisher from pub_raw
elif (publication_type == 'book') or (publication_type == 'report'):
self.__add_book_publisher_info_jats_xml(a_doc.get(field, ''), record)
elif (field == 'volume'):
ET.SubElement(record, 'volume').text = a_doc.get(field, '')
elif (field == 'issue'):
# issue appears in parenthesis, so need to find the last element and add open parenthesis
record[-1].tail = '(\n'
issue = ET.SubElement(record, 'issue')
issue.text = a_doc.get(field, '')
# now add the close parenthesis followed by colon
issue.tail = '):'
elif (field == 'page'):
ET.SubElement(record, 'fpage').text = ''.join(a_doc.get(field, ''))
elif (field == 'page_range'):
pages = ''.join(a_doc.get('page_range', '')).split('-')
if len(pages) == 2:
# need to insert a dash before lastpage
record[-1].tail = ' #x2013;'
ET.SubElement(record, 'lpage').text = pages[1]
# insert a dot after page info
record[-1].tail = '.\n'
elif (field == 'doi'):
# need to add `doi:` before tag
record[-1].tail = ' doi:'
doi = ET.SubElement(record, 'pub-id')
doi.set('pub-id-type', 'doi')
doi.text = ''.join(a_doc.get(field, ''))
def __get_xml(self, export_format):
"""
setup the outer xml structure
:param export_format:
:return:
"""
num_docs = 0
format_xml = ''
if (self.status == 0):
num_docs = self.get_num_docs()
records = ET.Element("records")
attribs = self.__get_attrib(export_format)
for attrib in attribs:
records.set(attrib, attribs[attrib])
records.set('retrieved', str(num_docs))
records.set('start', str(1))
records.set('selected', str(num_docs))
num_citations = self.__get_num_citations()
if num_citations > 0:
records.set('citations', str(num_citations))
if (export_format == self.EXPORT_FORMAT_REF_XML) or (export_format == self.EXPORT_FORMAT_REF_ABS_XML):
for index in range(num_docs):
self.__get_doc_reference_xml(index, records, export_format)
elif (export_format == self.EXPORT_FORMAT_DUBLIN_XML):
for index in range(num_docs):
self.__get_doc_dublin_xml(index, records)
elif (export_format == self.EXPORT_FORMAT_JATS_XML):
for index in range(num_docs):
self.__get_doc_jats_xml(index, records)
format_xml = ET.tostring(records, encoding='utf8', method='xml')
format_xml = (b'>\n<'.join(format_xml.split(b'><')))
format_xml = format_xml.replace(b'</record>', b'</record>\n')
result_dict = {}
result_dict['msg'] = 'Retrieved {} abstracts, starting with number 1.'.format(num_docs)
result_dict['export'] = format_xml.decode('utf8')
return result_dict
def get_reference_xml(self, include_abs=False):
"""
:param include_abs:
:return: reference xml format with or without abstract
"""
if include_abs:
return self.__get_xml(self.EXPORT_FORMAT_REF_ABS_XML)
return self.__get_xml(self.EXPORT_FORMAT_REF_XML)
def get_dublincore_xml(self):
"""
:return: dublin xml format
"""
return self.__get_xml(self.EXPORT_FORMAT_DUBLIN_XML)
def get_jats_xml(self):
"""
:return: jats xml format
"""
return self.__get_xml(self.EXPORT_FORMAT_JATS_XML)