-
Notifications
You must be signed in to change notification settings - Fork 54
/
bibtex2md.py
429 lines (350 loc) · 15.5 KB
/
bibtex2md.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#!/usr/bin/env python
# -*- coding: utf-8 -*-
################################################################################
# Copyright (c) 2020 ContinualAI #
# Copyrights licensed under the MIT License. #
# See the accompanying LICENSE file for terms. #
# #
# Date: 1-05-2020 #
# Author(s): Vincenzo Lomonaco #
# E-mail: [email protected] #
# Website: www.continualai.org #
################################################################################
""" Simple script to generate the papers.md file loading references from
a bibtex. """
# Python 2-3 compatible
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
import bibtexparser
import random
from bibtexparser.customization import convert_to_unicode
from bibtexparser.bparser import BibTexParser
import copy
import os
random.seed(1)
showbib_template = """
<script>
function [PAPERID][SECTION]Function() {
var moreText = document.getElementById("[PAPERID][SECTION]_more");
var moreText2 = document.getElementById("[PAPERID][SECTION]_more2");
var moreText3 = document.getElementById("[PAPERID][SECTION]_more3");
var btnText = document.getElementById("[PAPERID][SECTION]_btt");
if (moreText.style.display === "none") {
btnText.innerHTML = "Bib";
moreText.style.display = "inline";
} else {
btnText.innerHTML = "Bib";
moreText.style.display = "none";
}
moreText2.style.display = "none";
moreText3.style.display = "none";
}
</script>
<script>
function [PAPERID][SECTION]Function2() {
var moreText = document.getElementById("[PAPERID][SECTION]_more2");
var moreText1 = document.getElementById("[PAPERID][SECTION]_more");
var moreText3 = document.getElementById("[PAPERID][SECTION]_more3");
var btnText = document.getElementById("[PAPERID][SECTION]_btt2");
if (moreText.style.display === "none") {
btnText.innerHTML = "Abstract";
moreText.style.display = "inline";
} else {
btnText.innerHTML = "Abstract";
moreText.style.display = "none";
}
moreText1.style.display = "none";
moreText3.style.display = "none";
}
</script>
<script>
function [PAPERID][SECTION]Function3() {
var moreText = document.getElementById("[PAPERID][SECTION]_more3");
var moreText1 = document.getElementById("[PAPERID][SECTION]_more");
var moreText2 = document.getElementById("[PAPERID][SECTION]_more2");
var btnText = document.getElementById("[PAPERID][SECTION]_btt3");
if (moreText.style.display === "none") {
btnText.innerHTML = "Notes";
moreText.style.display = "inline";
} else {
btnText.innerHTML = "Notes";
moreText.style.display = "none";
}
moreText1.style.display = "none";
moreText2.style.display = "none";
}
</script>
<button style="font-size:75%; line-height:15px" onclick="[PAPERID][SECTION]Function()" id="[PAPERID][SECTION]_btt">Bib</button>
<button style="font-size:75%; line-height:15px" onclick="[PAPERID][SECTION]Function2()" id="[PAPERID][SECTION]_btt2">Abstract</button>
<button style="font-size:75%; line-height:15px" onclick="[PAPERID][SECTION]Function3()" id="[PAPERID][SECTION]_btt3">Notes</button>
<p style="background-color: #2980b929; font-size:75%; line-height:15px"><span id="[PAPERID][SECTION]_more" style="display: none">
[BIBTEX]
</span></p>
<p style="background-color: #2980b929; font-size:75%; line-height:15px"><span id="[PAPERID][SECTION]_more2" style="display: none">
[ABSTRACT]
</span></p>
<p style="background-color: #2980b929; font-size:75%; line-height:15px"><span id="[PAPERID][SECTION]_more3" style="display: none">
[NOTE]
</span></p>
<span id="[PAPERID][SECTION]_year" class="yearSpan" style="display: none">[YEAR]</span>
"""
def generate_hsl():
H = random.randint(0, 359)
S = random.randint(60, 100)
L = random.randint(40, 80)
color_string = "HSL({}, {}%, {}%)".format(H, S, L)
return color_string
def build_tags_string(tag2c):
output = ""
for tagname, color in tag2c.items():
output += create_colored_tag(tagname, color)
output += " "
return output
def create_colored_tag(tagname, tagcolor):
output = "<span " \
"style='background-color:{}; padding: " \
"2px; border-radius:4px; border: 1px " \
"solid black;'>[{}]" \
"</span> ".format(tagcolor, tagname)
return output
def count_current_papers(bibtex_path, main_bib_path):
with open(os.path.join(bibtex_path, main_bib_path), 'r') as f:
papers = bibtexparser.load(f)
return len(papers.entries)
def remove_mendeley_notice_from_files(filename):
with open(filename, 'r') as fin:
data = fin.read().splitlines(True)
if data[0].startswith("Automatically generated"):
with open(filename, 'w') as fout:
fout.writelines(data[5:])
def extract_bibtex(bib_database, id):
# print("bib_database.entries: ", bib_database.entries)
pos = None
for i, entry in enumerate(bib_database.entries):
if entry['ID'] == id:
pos = i
# print(entry['ID'])
bib_db = copy.deepcopy(bib_database)
# print(id)
# print("pos:", pos)
del bib_db.entries[pos+1:]
del bib_db.entries[:pos]
str = bibtexparser.dumps(bib_db)
return str
def bibtex_string2html(str, remove_abstract=True):
lines = str.split("\n")
final_str = ""
# print(lines)
for i, line in enumerate(lines):
if remove_abstract and line.strip().startswith("abstract"):
continue
if line == "":
continue
if i == 0:
final_line = line + "<br>"
else:
final_line = line + "<br>"
final_str += final_line
# print(final_str)
return final_str
def journal_or_booktitle(item):
if "journal" in item.keys():
return "*" + item["journal"] + "*"
elif "booktitle" in item.keys():
return "*" + item["booktitle"] + "*"
elif item["ENTRYTYPE"] == "book":
return "*" + item["publisher"] + "*"
else:
print("WARNING: venue missing in '" + str(item["title"]) + "'!!!")
return ""
def pages_or_void(item):
if "pages" in item.keys():
return ", " + item["pages"]
else:
return ""
def get_author(item):
authors_list = item['author'].split(" and ")
str = ""
for i, aut in enumerate(authors_list):
# print(aut)
try:
surname, name = aut.split(", ")
except ValueError:
surname, name = aut.split(" ")
authors_list[i] = name + " " + surname
if i == len(authors_list) - 1:
str += " and " + name + " " + surname
elif i == 0:
str += name + " " + surname
else:
str += ', ' + name + " " + surname
return str
def get_title(item):
title = item['title'].replace("{", "").replace("}", "")
if "url" in item.keys():
return "[" + title + "](" + item["url"] + ")"
else:
return title
# settings ---------------------------------------------------------------------
bibtex_path = "bibtex"
full_bib_db = "Continual Learning Papers.bib"
full_bib_db_path = full_bib_db
template_file_path = "papers_template.md"
tag2fill = "<TAG>"
papercount2fill = "<PAPER_COUNT>"
taglist2fill = "<TAGLIST>"
output_filename = "papers.md"
# this respect also the order of the sections
bib_files = [
"Continual Learning Papers-Applications.bib",
"Continual Learning Papers-Architectural Methods.bib",
"Continual Learning Papers-Benchmarks.bib",
"Continual Learning Papers-Bioinspired Methods.bib",
"Continual Learning Papers-Catastrophic Forgetting Studies.bib",
"Continual Learning Papers-Classics.bib",
"Continual Learning Papers-Continual Few Shot Learning.bib",
"Continual Learning Papers-Continual Meta Learning.bib",
"Continual Learning Papers-Continual Reinforcement Learning.bib",
"Continual Learning Papers-Continual Sequential Learning.bib",
"Continual Learning Papers-Dissertation and Theses.bib",
"Continual Learning Papers-Generative Replay Methods.bib",
"Continual Learning Papers-Hybrid Methods.bib",
"Continual Learning Papers-Meta Continual Learning.bib",
"Continual Learning Papers-Metrics and Evaluations.bib",
"Continual Learning Papers-Neuroscience.bib",
"Continual Learning Papers-Others.bib",
"Continual Learning Papers-Regularization Methods.bib",
"Continual Learning Papers-Rehearsal Methods.bib",
"Continual Learning Papers-Review Papers and Books.bib",
"Continual Learning Papers-Robotics.bib"
]
sec_descriptions = [
"In this section we maintain a list of all applicative papers "
"produced on continual learning and related topics.",
"In this section we collect all the papers introducing a continual "
"learning strategy employing some architectural methods.",
"In this section we list all the papers related to new benchmarks "
"proposals for continual learning and related topics. ",
"In this section we list all the papers related to bioinspired continual "
"learning approaches.",
"In this section we list all the major contributions trying to understand "
"catastrophic forgetting and its implication in machines that learn "
"continually.",
"In this section you'll find pioneering and classic continual learning "
"papers. We recommend to read all the papers in this section for a "
"good background on current continual deep learning developments.",
"Here we list the papers related to Few-Shot continual and incremental learning.",
"In this section we list all the papers related to the continual "
"meta-learning.",
"In this section we list all the papers related to the continual "
"Reinforcement Learning.",
"Here we maintain a list of all the papers related to the continual "
"learning at the intersection with sequential learning.",
"In this section we maintain a list of all the dissertation and thesis "
"produced on continual learning and related topics.",
"In this section we collect all the papers introducing a continual "
"learning strategy employing some generative replay methods.",
"In this section we collect all the papers introducing a continual "
"learning strategy employing some hybrid methods, mixing different strategies.",
"In this section we list all the papers related to the meta-continual "
"learning.",
"In this section we list all the papers related to the continual learning "
"evalution protocols and metrics.",
"In this section we maintain a list of all Neuroscience papers "
"that can be related (and useful) for continual machine learning.",
"In this section we list all the other papers not appearing in at least "
"one of the above sections.",
"In this section we collect all the papers introducing a continual "
"learning strategy employing some regularization methods.",
"In this section we collect all the papers introducing a continual "
"learning strategy employing some rehearsal methods.",
"In this section we collect all the main review papers and books on "
"continual learning and related subjects. These may constitute a solid "
"starting point for continual learning newcomers.",
"In this section we maintain a list of all Robotics papers "
"that can be related to continual learning."
]
with open('tags.csv', 'r') as f:
tags_list = [line.split(',')[0].strip() for line in f][1:] # get all tags
n_tags = len(tags_list)
print("Read " + str(n_tags) + " tags.")
# generate random HSL colors for tags (light colors only)
colors = [generate_hsl() for _ in range(n_tags)]
tags2color = dict(zip(tags_list, colors))
# ------------------------------------------------------------------------------
remove_mendeley_notice_from_files(os.path.join(bibtex_path, full_bib_db))
papers_count = {}
for bib_f in bib_files:
papers_count[bib_f] = str(count_current_papers(bibtex_path, bib_f))
with open(os.path.join(bibtex_path, full_bib_db)) as bibtex_file:
parser = BibTexParser()
parser.customization = convert_to_unicode
full_bib_db = bibtexparser.load(bibtex_file, parser=parser)
str2inject = ""
#rst_end_str = ""
for i, bibfile in enumerate(bib_files):
sec_title = bibfile.split("-")[1][:-4]
with open(os.path.join(bibtex_path, bibfile)) as bibtex_file:
parser = BibTexParser()
parser.customization = convert_to_unicode
bib_database = bibtexparser.load(bibtex_file, parser=parser)
with open(template_file_path) as rf:
template_str = rf.read()
str2inject += "### " + sec_title + "\n\n**" + \
papers_count[bibfile] + " papers**" + "\n\n" + \
sec_descriptions[i] + "\n\n"
for item in sorted(
bib_database.entries, key=lambda j: j['year'], reverse=True):
# print(item)
str2inject_tags = ""
if "keywords" in item.keys():
# print(item["mendeley-tags"])
str_tags = item["keywords"].replace(";", "").replace("[", "")
str_tags = str_tags.replace(",", "")
cur_tags = str_tags.replace(" ", "").split("]")
del cur_tags[-1]
# print(cur_tags)
for tag in cur_tags:
str2inject_tags += create_colored_tag(tag, tags2color[tag])
str2inject += "- " + get_title(item) + \
" by " + get_author(item) + \
". " + journal_or_booktitle(item) + \
pages_or_void(item) + \
", " + item['year'] + ". " + \
str2inject_tags + "\n"
# Add bib file button
bib_str = showbib_template.replace(
"[BIBTEX]", bibtex_string2html(
extract_bibtex(full_bib_db, item["ID"])
)
)
bib_str = bib_str.replace("[PAPERID]", item["ID"].replace("-", ""))
if "abstract" in item.keys():
bib_str = bib_str.replace("[ABSTRACT]", item["abstract"])
else:
bib_str = bib_str.replace("[ABSTRACT]", "N.A.")
if "annote" in item.keys():
bib_str = bib_str.replace("[NOTE]", item["annote"].replace(
"\n", "\n\t\t"))
else:
bib_str = bib_str.replace("[NOTE]", "N.A.")
bib_str = bib_str.replace("[SECTION]", sec_title.replace(" ", "_"))
bib_str = bib_str.replace("[YEAR]", item["year"])
str2inject += bib_str
#rst_end_str += bib_str
if i != len(os.listdir(bibtex_path)) - 1:
str2inject += "\n"
else:
str2inject = str2inject[:-1]
template_str = template_str.replace(papercount2fill,
"**Search among " +
str(count_current_papers(bibtex_path,
full_bib_db_path)) + " papers!**"
)
template_str = template_str.replace(taglist2fill,
build_tags_string(tags2color)
)
template_str = template_str.replace(tag2fill, str2inject) #+ rst_end_str
with open(output_filename, "w") as wf:
wf.write(template_str)