-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsort_bib.py
36 lines (30 loc) · 1.06 KB
/
sort_bib.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
import biblib.bib as bbl
import numpy as np
import sys
def sort_refs(bibfile,ofile):
parser = bbl.Parser()
parsed = parser.parse(open(bibfile,'r'))
unsorted = parsed.get_entries()
bibkeys = np.array([k for k in unsorted.keys()])
surnames = []
for key in bibkeys:
entry = unsorted[key]
firstauth = entry['author'].split('and')[0]
if ',' in firstauth:
surnames.append(firstauth.split(',')[0])
elif firstauth.startswith('{') and firstauth.endswith('}'):
toadd = firstauth.split(' ')[0].lstrip('{')
if toadd.lower() == 'the':
toadd = firstauth.split(' ')[1].rstrip('}')
surnames.append(toadd)
else:
surnames.append(firstauth.split(' ')[-1])
keyorder = bibkeys[np.argsort(surnames)]
with open(ofile,'w') as f:
for k in keyorder:
f.write(unsorted[k].to_bib())
f.write('\n')
if __name__ == '__main__':
bibfile = sys.argv[1] + '.bib'
ofile = sys.argv[1] + '_sort.bib'
sort_refs(bibfile,ofile)