-
Notifications
You must be signed in to change notification settings - Fork 1
/
arxiv_to_solr.py
executable file
·141 lines (113 loc) · 4.9 KB
/
arxiv_to_solr.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#------------------------------------------------------------------------------
# Copyright (c) 2015 University of Helsinki
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#------------------------------------------------------------------------------
from __future__ import print_function
from __future__ import division
import lxml.etree as ET
import sys
from datetime import datetime
#------------------------------------------------------------------------------
namespaces = {
'oai': 'http://www.openarchives.org/OAI/2.0/',
'oai_dc' : 'http://www.openarchives.org/OAI/2.0/oai_dc/',
'dc' : 'http://purl.org/dc/elements/1.1/'
}
# things not parsed yet
# <dc:date>2007-03-30</dc:date>
# <dc:date>2008-12-13</dc:date>
# <dc:type>text</dc:type>
def log(text):
print("[" + str(datetime.now()) + "]", text, file=sys.stderr)
#------------------------------------------------------------------------------
def process_file(filename, solr_update):
log("Processing file {} ...".format(filename))
tree = ET.parse(filename)
root = tree.getroot()
import_warning = False
for record in root.iterfind('./oai:ListRecords/oai:record', namespaces):
doc_node = ET.SubElement(solr_update, 'doc')
def field_node(name, text):
n = ET.SubElement(doc_node, 'field', attrib = { 'name': name })
n.text = text
if text == '':
print('ERROR: empty text for '+ name, file=sys.stderr)
sys.exit(1)
def field_nodes(name, text_nodes):
for tn in text_nodes:
field_node(name, tn.text)
def get_text_nodes(path):
return record.findall('.' + path, namespaces)
def get_text(path):
return record.find('.' + path, namespaces).text
rid = get_text('/oai:header/oai:identifier')
field_node('id', rid)
field_node('last_modified', get_text('/oai:header/oai:datestamp') + \
'T00:00:00Z')
field_node('set', get_text('/oai:header/oai:setSpec'))
mdns = '/oai:metadata/oai_dc:dc'
field_node('title', get_text(mdns + '/dc:title'))
field_nodes('author', get_text_nodes(mdns + '/dc:creator'))
field_nodes('subject', get_text_nodes(mdns + '/dc:subject'))
desc_nodes = get_text_nodes(mdns + '/dc:description')
ldn = len(desc_nodes)
if ldn < 1 or ldn > 2:
print('ERROR [{0}]: desc_nodes length {1}'.format(rid, ldn), file=sys.stderr)
sys.exit(1)
abstract = ''
for dn in desc_nodes:
txt = dn.text
if txt.startswith('Comment: '):
txt = txt.replace('Comment: ', '')
field_node('comment', txt)
else:
field_node('abstract', txt)
abstract = txt
try:
from topia.termextract import extract
if abstract != '':
extractor = extract.TermExtractor()
res = extractor(abstract)
for kw_triple in res:
kw = kw_triple[0]
if len(kw) > 3:
field_node('keyword', kw)
except ImportError:
if not import_warning:
print('WARNING: Unable to import topia.termextract for keyword extraction!',
file=sys.stderr)
import_warning = True
field_node('url', get_text(mdns + '/dc:identifier'))
#------------------------------------------------------------------------------
if __name__ == '__main__':
if len(sys.argv) <= 1:
print("Usage: {0} XML_FILE(S)".format(sys.argv[0]))
sys.exit(1)
solr_update = ET.Element('add')
for f in sys.argv[1:]:
process_file(f, solr_update)
log("Generating solr.xml ...")
tree = ET.ElementTree(solr_update)
tree.write('solr.xml', pretty_print=True)