-
Notifications
You must be signed in to change notification settings - Fork 32
/
metadata.py
176 lines (137 loc) · 6.59 KB
/
metadata.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
#!/usr/bin/env python
import subprocess
from manipulate import Manipulate
from teimanipulate import TeiManipulate
__author__ = "Martin Paul Eve, Dulip Withanage"
__email__ = "[email protected]"
class Metadata(Manipulate):
def __init__(self, gv):
self.gv = gv
self.metadata_items = {}
self.metadata = []
self.authors = []
self.debug = self.gv.debug
self.dom_to_load = self.gv.input_metadata_file_path
self.dom_temp_file = self.gv.input_metadata_file_path
self.mod_name = 'Metadata'
Manipulate.__init__(self, gv)
def attach_metadata(self):
cmd = ["java", "-classpath", self.gv.java_class_path,
"-Dxml.catalog.files=" + self.gv.runtime_catalog_path,
"net.sf.saxon.Transform",
"-x", "org.apache.xml.resolver.tools.ResolvingXMLReader",
"-y", "org.apache.xml.resolver.tools.ResolvingXMLReader",
"-r", "org.apache.xml.resolver.tools.CatalogResolver",
"-o", self.gv.nlm_file_path,
self.gv.nlm_temp_file_path,
self.gv.metadata_style_sheet_path,
'metadataFile="' + self.gv.input_metadata_file_path + '"'
]
return ' '.join(cmd)
def run(self):
java_command = self.attach_metadata()
subprocess.call(java_command, stdin=None, shell=True)
# copy back to the temp file for debug purposes
Manipulate.update_tmp_file(self.gv.nlm_file_path, self.gv.nlm_temp_file_path)
self.debug.print_debug(self, u'Running metadata transform')
def extract_metadata_fields(self):
# load the metadata file for reading
tree = self.load_dom_read()
metadata = []
# attempt to find identifiers
count = 0
ids = tree.xpath('//article-id | //tei:article-id', namespaces={'tei': 'http://www.tei-c.org/ns/1.0'})
for id in ids:
text = self.get_stripped_text(id).strip()
self.metadata_items['ID{0}'.format(count)] = text
self.metadata.append(text)
count += 1
self.debug.print_debug(self, u'Extracted an article ID: "{0}" from metadata'.format(text))
self.metadata_items['IDs_count'] = count
# attempt to find article titles
count = 0
titles = tree.xpath('//article-title | //tei:article-title', namespaces={'tei': 'http://www.tei-c.org/ns/1.0'})
for title in titles:
text = self.get_stripped_text(title).strip()
self.metadata_items['title{0}'.format(count)] = text
self.metadata.append(text)
count += 1
self.debug.print_debug(self, u'Extracted an article title: "{0}" from metadata'.format(text))
self.metadata_items['titles_count'] = count
# attempt to find journal titles
count = 0
titles = tree.xpath('//journal-id | //tei:journal-title', namespaces={'tei': 'http://www.tei-c.org/ns/1.0'})
for title in titles:
text = self.get_stripped_text(title).strip()
self.metadata_items['journal_title{0}'.format(count)] = text
self.metadata.append(text)
count += 1
self.debug.print_debug(self, u'Extracted a journal title: "{0}" from metadata'.format(text))
self.metadata_items['journal_titles_count'] = count
# attempt to find authors
count = 0
titles = tree.xpath('//contrib/name | //tei:contrib/tei:name',
namespaces={'tei': 'http://www.tei-c.org/ns/1.0'})
authors = []
for title in titles:
components = []
for component in title:
text = self.get_stripped_text(component).strip()
self.debug.print_debug(self, u'Extracted a name component: "{0}" from metadata'.format(text))
components.append(text)
self.metadata_items['contrib_name{0}'.format(count)] = components
self.authors.append(components)
count += 1
self.metadata_items['contrib_names_count'] = count
# attempt to find affiliations
count = 0
titles = tree.xpath('//aff | //tei:aff', namespaces={'tei': 'http://www.tei-c.org/ns/1.0'})
for title in titles:
text = self.get_stripped_text(title).strip()
self.metadata_items['aff{0}'.format(count)] = text
self.metadata.append(text)
count += 1
self.debug.print_debug(self, u'Extracted an affiliation: "{0}" from metadata'.format(text))
self.metadata_items['affs_count'] = count
def pre_clean(self):
self.extract_metadata_fields()
manipulate = TeiManipulate(self.gv)
tree = manipulate.load_dom_tree()
# get all elements in the body
section = tree.xpath('//tei:body//*', namespaces={'tei': 'http://www.tei-c.org/ns/1.0'})
items_to_match = ['{http://www.tei-c.org/ns/1.0}head', '{http://www.tei-c.org/ns/1.0}p',
'{http://www.tei-c.org/ns/1.0}cit']
count = 0
matched_authors = []
for item in section:
if count > 2:
break
if item.tag in items_to_match:
count += 1
text = self.get_stripped_text(item)
processed = False
for author in self.authors:
if not author in matched_authors:
has_all = True
for component in author:
if not component in text:
has_all = False
break
if has_all:
# found a metadata line
matched_authors.append(author)
count -= 1
item.getparent().remove(item)
self.debug.print_debug(self, u'Removed line "{0}" '
u'because it appears to be author metadata'.format(text))
processed = True
break
if not processed:
for metadata in self.metadata:
if metadata in text:
# found a metadata line
count -= 1
item.getparent().remove(item)
self.debug.print_debug(self, u'Removed line "{0}" '
u'because it appears to be duplicated metadata'.format(text))
manipulate.save_tree(tree)