-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdblp_to_md.py
144 lines (112 loc) · 3.64 KB
/
dblp_to_md.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
import re
BIBTEX_ENTRIES = {'@inproceedings', '@article'}
SKIP_LIST = {'a', 'an', 'the', 'on', 'to', 'for', 'in'}
def extract_ref(entry):
for b in BIBTEX_ENTRIES:
entry = entry.replace(b, "")
return remove_parenthesis(entry)
def remove_parenthesis(token):
return token.strip("{}")
def invert_author(author):
#
# very crude heuristics
names = author.split(' ')
first_name = [names[0]]
for n in names[1:]:
if '.' in n:
first_name.append(n)
else:
break
# return names[-1] + ", " + " ".join(names[:-1])
return " ".join([n for n in names if n not in first_name]) + ", " + " ".join(first_name)
def process_authors(token):
token = remove_parenthesis(token)
authors = token.split(" and\n")
authors = [invert_author(a.strip()) for a in authors]
return authors
def process_title(token):
token = token.replace("\n", "")
token = remove_parenthesis(token)
token = re.sub(' +', ' ', token)
return token
def process_year(token):
token = token.replace("\n", "")
token = remove_parenthesis(token)
return token
def process_venue(venue, year):
venue = venue.replace("\n", "")
venue = remove_parenthesis(venue)
return "{} {}".format(venue, year)
def get_first_word(words):
for w in words:
if w.lower() not in SKIP_LIST:
return w
return ""
def process_ref(authors, year, title):
first_author = authors.split(",")[0].strip().lower()
first_author = re.sub(' +', '', first_author)
first_word = get_first_word(title.split(" ")).strip().lower()
return "{}{}{}".format(first_author, year, first_word)
def replace_ref(bibtex, new_ref):
tokens = bibtex.split(',')
entry = tokens[0]
ref = extract_ref(str(entry))
tokens[0] = entry.replace(ref, new_ref)
return ','.join(tokens)
def replace_authors(bibtex, old_authors, new_authors):
return bibtex.replace(old_authors, f"{{{new_authors}}}")
def break_tokens(bibtex):
bibtex = bibtex.strip()
tokens = bibtex.split(',')
entry = tokens[0]
#
# discard first
tokens = tokens[1:]
tokens = [t.split("=") for t in tokens]
data = {k.strip():v.strip() for k, v in tokens}
old_authors = data["author"]
data["author"] = " and ".join(process_authors(data["author"]))
data["title"] = process_title(data["title"])
data["year"] = process_year(data["year"])
if "booktitle" in data:
data["venue"] = process_venue(data["booktitle"], data["year"])
else:
data["venue"] = process_venue(data["journal"], data["year"])
data["ref"] = process_ref(data["author"], data["year"], data["title"])
bibtex = replace_ref(bibtex, data["ref"])
bibtex = replace_authors(bibtex, old_authors, data["author"])
bibtex = bibtex.replace('\n', '<br/>')
return data, bibtex
def gen_markdown(data, bibtex, id):
md = """---
layout: paper
ref: "{}"
title: "{}"
date: {}-08-{} 00:00
tags: ""
image: ""
authors: "{}"
pdf: ""
venue: "{}"
abstract: ""
bibtex: "{}"
---
""".format(data["ref"], data["title"], data["year"], id, data["author"], data["venue"], bibtex)
return md
if __name__ == "__main__":
a = """
@inproceedings{DBLP:conf/aiia/MauroVE15,
author = {Nicola Di Mauro and
Antonio Vergari and
Floriana Esposito},
title = {Learning Accurate Cutset Networks by Exploiting Decomposability},
booktitle = {AI*IA},
series = {Lecture Notes in Computer Science},
volume = {9336},
pages = {221--232},
publisher = {Springer},
year = {2015}
}
"""
data, bibtex = break_tokens(a)
print(gen_markdown(data, bibtex, 12))