-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthorparse.py
More file actions
315 lines (245 loc) · 10.2 KB
/
Copy pathauthorparse.py
File metadata and controls
315 lines (245 loc) · 10.2 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 10 10:35:54 2023
@author: repa
"""
from pyparsing import oneOf, Word, alphas, OneOrMore, ZeroOrMore, \
Literal, Regex, ParserElement, LineEnd, Opt, ParseException
import sys
# newlines are significant for parsing multi-line author lists
ParserElement.setDefaultWhitespaceChars(' \t')
# ensure we parse most accented names
unicodePrintables = u''.join(chr(c) for c in range(512)
if chr(c).isalpha() or chr(c) == "'" or
chr(c) == '-' or chr(c) == '_')
def dprint(*argv, **argkw):
# print(*argv, **argkw)
pass
# functions to assemble different parts
def setFirst(toks):
toks['firstname'] = ' '.join(toks)
dprint(f"firstname {toks}")
def setLast(toks):
toks['lastname'] = ' '.join(toks)
dprint(f"lastname {toks}")
def setLastCap(toks):
toks['lastname'] = toks[0].capitalize()
dprint(f"lastname {toks[0].capitalize()}")
def setTitle(toks):
toks['titlepre'] = toks[0]
dprint(f"titlepre {toks}")
def setTitle2(toks):
toks['titlepost'] = toks[0]
dprint(f"titlepost {toks}")
def setAffiliation(toks):
toks['affiliation'] = ' '.join(toks)
dprint(f"rest {toks}")
def completeAuthor(toks):
toks['author'] = Author(toks)
dprint(f"complete {toks} {list(toks.keys())}")
# parsing tokens and language
titlepre = oneOf(('Dr', 'Dr.', 'Prof', 'Prof.', 'dr.', 'dr.ir.', 'Lt',
'Lt Col', 'Lt. Col.', 'Prof. Dr.-Ing.', 'Professor',
'Mr', 'Mr.', 'Mrs.')).set_parse_action(setTitle)
titlepost = Literal(',') + \
oneOf(('MSc', 'MSc.', 'PHD', 'Phd.', 'Ed.D', 'Ph.D.', 'MSC',
'MS')).set_parse_action(setTitle2)
firstname = (Word(unicodePrintables, asKeyword=True) +
ZeroOrMore(Regex(r'[A-Z]\.'))).set_parse_action(setFirst)
nickname = (Literal('(') + Word(unicodePrintables, asKeyword=True) +
Literal(')')).set_parse_action(lambda toks: ''.join(toks))
initial = Regex(r'([A-Z]\.)+')
initials = (
(OneOrMore(initial) +
Opt(nickname) +
ZeroOrMore(initial)).set_parse_action(setFirst) |
(ZeroOrMore(initial) +
Opt(nickname) +
OneOrMore(initial)).set_parse_action(setFirst))
lastname = OneOrMore(Word(unicodePrintables, asKeyword=True)
).set_parse_action(setLast)
lastcaps = Word(alphas.upper(), min=2,
asKeyword=True).set_parse_action(setLastCap)
author = (lastcaps + firstname + Opt(titlepost)) | \
(Opt(titlepre) + (initials | firstname) + lastname + Opt(titlepost)) | \
(lastname + Literal(',') + firstname)
separator = Literal('&') | Literal('\n') | Literal(';')
author_line = (author + Opt(Literal(',') +
Regex(r'[^\n]*').set_parse_action(setAffiliation))
)
author_lines = author_line + OneOrMore(LineEnd() + author_line)
author_list = author + ZeroOrMore(separator + author)
def printattr(o, attrib, pre='', post=' ', default=''):
if hasattr(o, attrib):
return pre + str(getattr(o, attrib)) + post
return default
def daysort(ses):
_dayvalue = dict(wed=300, thu=400, fri=500, sat=600)
try:
return _dayvalue[ses[:3]] + 10*int(ses[4]) + \
((len(ses) == 6) and (ord(ses[5])-ord('a')) or 0)
except Exception:
return 0
class Author:
_members = ('orcid', 'affiliation', 'email',
'picture', 'biography')
def __new__(cls, *argv, **argkw):
if len(argv) == 2 and hasattr(argv[0], 'keys'):
return cls._from_dict(argv[0], argv[1])
elif len(argv) == 3:
return cls._from_iterable(argv[0], argv[1], argv[2])
raise ValueError(f"Cannot make author from {argv}")
@classmethod
def _from_parts(cls, firstname, lastname, orcid, program):
if firstname.upper() == firstname and '.' not in firstname:
firstname, lastname = lastname, firstname
try:
return program.authors[(lastname, firstname, orcid)]
except KeyError:
if orcid is None:
# is there an author def with orcid in there?
for k, a in program.authors.items():
if k[:2] == (lastname, firstname):
print(f"Matching {lastname}, {firstname} to orcid={k[2]}",
file=sys.stderr)
return a
else:
# is the name there, but without orcid? Appropriate
a = program.authors.get((lastname, firstname, None), False)
if a:
print(f"Matching orcid={orcid} to {lastname}, {firstname}",
file=sys.stderr)
del program.authors[(lastname, firstname, None)]
program.authors[(lastname, firstname, orcid)] = a
return a
# apparently nothing found, create a new author
obj = super().__new__(cls)
obj.lastname = lastname
obj.firstname = firstname
obj.orcid = orcid
obj._items = []
obj._chairing = []
program.authors[obj.key()] = obj
return obj
def addChairRole(self, event):
self._chairing.append(event)
@classmethod
def _from_dict(cls, data, program):
obj = cls._from_parts(data.get('firstname'),
data.get('lastname', 'Anonymous'),
data.get('orcid', None),
program)
for k, v in data.items():
setattr(obj, k, v)
return obj
@classmethod
def _from_iterable(cls, row, data, program):
obj = SingleAuthor(data['author'], program)[0]
# these are directly coupled
for m in cls._members:
setattr(obj, m, data[m])
# if applicable, remove the un-orcided one, and install with orcid
if obj.orcid:
try:
del program.authors[(obj.lastname, obj.firstname, None)]
program.authors[(obj.lastname, obj.firstname, obj.orcid)] = obj
# print(f"Adding orcid to author {str(obj)}")
except KeyError:
pass
return obj
def __str__(self):
return f'{self.lastname}, {self.firstname}'
def __eq__(self, o):
return self.lastname == o.lastname and self.firstname == o.firstname
def __lt__(self, o):
if self.lastname < o.lastname:
return True
if self.firstname < o.firstname:
return True
def __hash__(self):
return hash((self.lastname, self.firstname))
@classmethod
def find(cls, **kw):
try:
return cls._authors((kw['lastname'], kw['firstname'],
kw.get('orcid', None)))
except KeyError:
pass
try:
for k, o in cls._authors.items():
if k[2] == kw['orcid']:
return o
except KeyError:
raise KeyError("Wrong arguments to find author")
raise KeyError(f"Cannot find author from {kw}")
def key(self):
return (self.lastname, self.firstname, self.orcid)
def nameLastFirst(self):
return f'{self.lastname}, {printattr(self, "titlepre")}{self.firstname}{printattr(self, "titlepost", pre=", ", post="")}'
def getEventCodes(self):
res = []
for it in self._items:
res.extend(it.getEvents())
for ch in self._chairing:
res.extend((f"{ch.event} (chair)",))
return sorted(res, key=daysort)
class AuthorList(list):
def __init__(self, text, program):
self.program = program
text = text.strip()
if '\n' in text:
al = author_line.copy().set_parse_action(self.complete)
parser = al + OneOrMore(LineEnd() + al)
else:
au = author.copy().set_parse_action(self.complete)
parser = au + ZeroOrMore(separator + au)
parser.parseString(text)
def complete(self, toks):
dprint(f"list addition {dict(toks.items())}")
self.append(Author(toks, self.program))
class SingleAuthor(list):
def __init__(self, text, program):
self.program = program
parser = author_line.copy().set_parse_action(self.complete)
try:
parser.parseString(text.strip())
except ParseException:
raise ParseException(f"Cannot read single author from {text}")
def complete(self, toks):
self.append(Author(toks, self.program))
if __name__ == '__main__':
print(initial.parseString(' A. '))
print(initial.parseString(' A.J. '))
print(initial.parseString('B. '))
print(initials.parseString('J.J. '))
print(initials.parseString('J.J. (Rowan)'))
print(initials.parseString('N. D. '))
for test in (
'Dr Amy Irwin',
'Lt. Col. Pedro Piedade',
'René van Paassen',
'OKINAWA John'
):
res = author.parseString(test)
print(res)
res = author_lines.parseString('''Lt Nicholas Armendariz, MSC, USN, Naval Aerospace Medical Institute
J. J. Walcutt, Ph.D., Clay Strategic Designs
Christina Parker, Ed.D, Education and Technology Branch, Army Aviation Center of Excellence
Shelbi Kuhlmann, Ph.D., School of Education, UNC-Chapel Hill''')
for test in (
'Hannah Rennich, Dr. Michael Miller, Dr. John McGuirl, Dr. Timothy Fry',
'Michael Vidulich & Pamela Tsang',
'Nejc Sedlar, Dr Amy Irwin, Prof Amelia Hunt',
'Samantha N. Emerson, Maria Chaparro Osman, Cait Rizzardo, Kent C. Halverson, Steve Ellis, Don Haley',
'Lynne Martin, Lauren Roberts, Joey Mercer, Yasmin Arbab, Charles Walter, William McCarty, Charles Sheehe III',
'Ivo Stuldreher, Erik Van der Burg, Wietse Ledegang, Mark Houben, Yvonne Fonken & Eric Groen',
'''Lt Nicholas Armendariz, MSC, USN, Naval Aerospace Medical Institute
J.J. Walcutt, Ph.D., Clay Strategic Designs
Christina Parker, Ed.D, Education and Technology Branch, Army Aviation Center of Excellence
Shelbi Kuhlmann, Ph.D., School of Education, UNC-Chapel Hill'''):
if '\n' in test:
res = author_lines.parseString(test)
else:
res = author_list.parseString(test)
print(res)