-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolver.py
executable file
·74 lines (61 loc) · 1.99 KB
/
resolver.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
#!/usr/bin/env python
# encoding: utf-8
"""
Resolve bibitem text to an ADS bibcode.
TOTALLY HACKING
e.g.
0912.5387
http://arxiv.org/abs/0912.5387
"""
def extract_bibitem_bibkey(bibitem):
"""Parse the bibkey in a bibkey"""
counter = -1
outer_counter = 0
bibkey = ""
for i, c in enumerate(bibitem):
if c == u'{':
counter += 1
if counter == 0:
outer_counter += 1
elif c == u'}':
counter -= 1
print i, c, counter, outer_counter
if outer_counter == 2:
bibkey += c
bibkey = bibkey.strip('{}')
return bibkey
def resolve_bibitem(bibitem, referenced_pubs):
"""Returns the publication object for the first referenced publication
whose bibliographic data matches to text in the bibitem.
.. note:: This is pretty hacky
Parameters
----------
bibitem : unicode
The bibitem string (e.g., \bibitem text)
referenced_pubs : object
List of publications referenced by the base document.
"""
for pub in referenced_pubs:
if len(pub.authors) > 3:
authors = pub.authors[:3]
else:
authors = pub.authors
for author in authors:
# author last names need to be in bibtem
if author[0] not in bibitem:
continue
return pub
def main():
from starlit.bib.adsdb import ADSBibDB
from starlit.bib.adscache import ADSCacheDB
cachedb = ADSCacheDB(host='localhost', port=27017, ads_db=ADSBibDB())
adsdb = ADSBibDB(cache=cachedb)
# adsdb = ADSBibDB()
# base_pub = adsdb['2007ApJ...667L..49D']
# print(base_pub.reference_bibcodes)
bibitem = "\bibitem[{{Fall} \& {Efstathiou}(1980)}]{FalEfs80}{Fall}, S.~M. \& {Efstathiou}, G. 1980, \mnras, 193, 189"
# ref_pub = resolve_bibitem(bibitem, base_pub.references)
print("bibkey", extract_bibitem_bibkey(bibitem))
# print("Matched", ref_pub.bibcode, ref_pub.authors)
if __name__ == '__main__':
main()