Update: new post and source code here.
So last weekend, I’d almost finished editing a Word manuscript draft to submit to a particular journal, except for one final item on the todo list: converting the existing list of references (inserted and formatted as a plain text list) to another style.
I planned on using Word’s built-in reference manager to add all 40+ references to the bibliography, but even in Word’s latest incarnation, the “Here are 20 text boxes squished together, have fun!”-interface remains a pain to use.
So then, switching to BibTeX first. This involves hunting down BibTeX entries for all references, putting them in a tool like Jabref, and then exporting the list to a — properly formatted — plain text list, which can be pasted back into Word.
After doing this for three or so reference entries, feelings of boredom and frustration welled up inside me, and my mind drifted off pondering the greater questions of life…
Are we alone in the universe? … Is there a god? … Why can’t I convert a list of plain text references to a BibTeX list automatically?
The latter questions really isn’t that ridiculous. With sources like DBLP and Web of Science allowing plain text search, it shouldn’t be too hard to automagically get this task done, right?
So I had a look at Endnote. Quote this page:
EndNote: Cannot import bibliography not created with EndNote Unfortunately, EndNote was not designed to import information in a bibliography format. EndNote can import text files that are in a tagged data format, as well as tab-delimited files that are in the exact right format. Some customers have had success at converting an existing text file into a tagged data file that can be imported into EndNote, however the process is difficult and may take more time than finding another source for the references, or even copying and pasting the text into new references by hand. More information on preparing text files for importing can be found in the “Importing Reference Data” section of the Help File.
That’s just great. One of these “some customers” can be found at this blog… Ten steps, including a bunch of arcane editing and Linux commands… There has to be a better way. If Mendeley can parse information from a PDF (not perfect, but still), a plain text reference list should be convertable to a BibTeX entry, right?
So instead of spending two hours creating a BibTeX file manually, I spent two days coming up with some automatic way.
Hour 1: Finding a Source
The first hour of this experiment was spent finding a literature corpus source where I’d be able to download a structured reference file from. Web of Science looks like a good candidate, but their website is hellish to parse, requires juggling with cookies and viewstates, and going through my university’s proxy to gain access, something I’ve all experimented with in the past and didn’t want to go through again.
Next possibility: Google Scholar. A few test runs with some edited queries came up with the right articles alright, and Google Scholar allows to export Endnote, BibTeX and other files. Even though the quality of the entry is not always perfect, this seemed like a good starting point.
Hours 2-3: Finding an API
First issue: Google Scholar does not provide an API. Luckily, there’s scholar.py
on GitHub which seems to be working fine (as long as you don’t hammer Google’s servers too much):
macuyiko$ python scholar.py -c 3 --author "vanden broucke" --txt "artificial"
ID PktWS3Llix8J
Title Improved artificial negative event generation to enhance process event logs
URL http://link.springer.com/chapter/10.1007/978-3-642-31095-9_17
Citations 4
Versions 5
Citations list http://scholar.google.com/scholar?cites=2273162715991526206&as_sdt=2005&sciodt=1,5&hl=en&num=3
Versions list http://scholar.google.com/scholar?cluster=2273162715991526206&hl=en&num=3&as_sdt=1,5
Cite URL http://scholar.google.com/scholar?q=info:PktWS3Llix8J:scholar.google.com/&output=cite&scirp=0&hl=en
Related list http://scholar.google.com/scholar?q=related:PktWS3Llix8J:scholar.google.com/&hl=en&num=3&as_sdt=1,5
Year 2012
ID 1_9CU_-bcNEJ
Title An Improved Process Event Log Artificial Negative Event Generator
URL http://papers.ssrn.com/sol3/papers.cfm?abstract_id=2165204
Citations 0
Versions 0
Citations list None
Versions list None
Cite URL http://scholar.google.com/scholar?q=info:1_9CU_-bcNEJ:scholar.google.com/&output=cite&scirp=0&hl=en
Related list None
Year 2012
ID EE_4eP8NmncJ
Title Third International Business Process Intelligence Challenge (BPIC'13): Volvo IT Belgium VINST
URL http://ceur-ws.org/Vol-1052/paper3.pdf
Citations 0
Versions 0
Citations list None
Versions list None
Cite URL http://scholar.google.com/scholar?q=info:EE_4eP8NmncJ:scholar.google.com/&output=cite&scirp=0&hl=en
Related list http://scholar.google.com/scholar?q=related:EE_4eP8NmncJ:scholar.google.com/&hl=en&num=3&as_sdt=1,5
Year None
Hours 4-5: Getting Citation Results
Second issue, scholar.py
does not provide support to retrieve citation entries from Google Scholar (i.e. what you see when you click the “Cite” link under an article).
Getting this information is a little bit tricky, because we first have to retrieve the Google Scholar ID for the article, and then perform a second HTTP request using a unique token in the URL. I’ve modified scholar.py
to look as follows (now also using the requests
library instead of the whole urllib
soup:
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 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 |
|
So now, we are able to get this:
macuyiko$ python scholar.py -c 1 --cite 1 --author "vanden broucke" --txt "artificial" && cat end.enw
------------Article #1:------------
ID PktWS3Llix8J
Title Improved artificial negative event generation to enhance process event logs
URL http://link.springer.com/chapter/10.1007/978-3-642-31095-9_17
Citations 4
Versions 5
Citations list http://scholar.google.com/scholar?cites=2273162715991526206&as_sdt=2005&sciodt=1,5&hl=en&num=1
Versions list http://scholar.google.com/scholar?cluster=2273162715991526206&hl=en&num=1&as_sdt=1,5
Cite URL http://scholar.google.com/scholar?q=info:PktWS3Llix8J:scholar.google.com/&output=cite&scirp=0&hl=en
Related list http://scholar.google.com/scholar?q=related:PktWS3Llix8J:scholar.google.com/&hl=en&num=1&as_sdt=1,5
Year 2012
%0 Conference Proceedings
%T Improved artificial negative event generation to enhance process event logs
%A vanden Broucke, Seppe KLM
%A De Weerdt, Jochen
%A Baesens, Bart
%A Vanthienen, Jan
%B Advanced Information Systems Engineering
%P 254-269
%@ 364231094X
%D 2012
%I Springer
Hours 6-11: Wasting Time with Google’s Appengine
Almost done. The final part which remains is to automate iterating through the plain references list, finding a way to extract a good search query for each entry, and getting out the Endnote (or BibTeX from Google Scholar).
In an impulse, I decided to try to whip up a Google Appengine page to provide this great service to the world. After creating a basic index.html
with some jQuery on top off it I created a quick Flask
app with the “magic” part looking something like this (disclaimer: not really that magical):
@app.route('/guess', methods=['POST'])
def guess():
f = string.ascii_lowercase + ' '
q = request.form['t']
q = q.lower()
q = q.replace('-', ' ')
q = ''.join(c for c in q if c in f)
q = q.split(' ')
q = ' '.join(c for c in q if len(c) >= 3)
# Use q as the Google Scholar search query
Really simple: convert to lowercase, leave only text and spaces, filter out all words smaller then three characters. This approached seemed to work well in initial tests.
What didn’t work well, however, was getting scholar.py
to work on Google Appengine. The library works fine, but Google Scholar is very picky towards handling requests coming from Google Appengine, due to reasons which are actually pretty obvious.
Hour 12: Let’s Just Finish This
After contemplating whether I should host this on my own server, I just decided to move on and built a quick and dirty script to do the work, as the likelihood of me getting popular/rich/famous/insert-attribute-here by a service like this one is small anyway, especially since Google’s banhammer would strike quickly.
So the final “convertor” script looks like this:
import sys
import os.path
import scholar
import string
import re
import codecs
import requests
references = """Bang-Jensen, J., & Gutin, G. (2010) Diagraphs: theory, algorithms and applications. Springer-Verlag.
Chandy, K., & Schulte, W. (2010) Event Processing - Designing IT Systems for Agile Companies, McGraw-Hill.
<OTHER REFERENCES>"""
def make_query(reference, l = 3):
q = reference
q = q.lower()
q = q.replace('-', ' ')
q = ''.join(c for c in q if c in f)
q = q.split(' ')
q = ' '.join(c for c in q if len(c) >= l)
return q
def make_query_year(reference, l = 3):
q = reference
id_re = re.compile(r'(\d\d\d\d)')
id = id_re.findall(q)
year = id[0] if len(id) > 0 else '('
q = q.split(year)
q = q[1:]
q = ''.join(q)
q = q.lower()
q = q.replace('-', ' ')
q = ''.join(c for c in q if c in f)
q = q.split(' ')
q = ' '.join(c for c in q if len(c) >= l)
if year != '(':
q = year + ' ' + q
return q
myFile = codecs.open('bibliography.bib', 'w','utf-8')
for reference in references.split('\n'):
reference = reference.strip()
print "\n---------------------------------------------------------"
print "Doing reference:", reference
f = string.ascii_lowercase + ' '
q = make_query(reference)
print "Query used:", q
r = scholar.articles(q, '', 1)
if len(r) == 0 or r[0]['id'] is None:
q = make_query_year(reference)
print "Trying again with:", q
r = scholar.articles(q, '', 1)
print r[0].as_txt(), '\n'
querier = scholar.CiteQuerier(r[0]['id'])
req = requests.get(querier.parser.export['BibTeX'], headers={'User-Agent': querier.USER_AGENT}, cookies=scholar.CookieJar.COOKIE_JAR)
myFile.write('\n\n%%% '+r[0]['id']+' '+r[0]['title']+'\n')
myFile.write(req.text)
myFile.close()
Note that I had to include two querying strategies: make_query
and make_query_year
. The latter combines the year with the title but leaves out authors, this is to retrieve some problematic articles where the author name was formatted differently in Google Scholar than in my list. A hacky approach which could be improved, but it gets the point across.
Anyway, running this script returns a BibTeX list like this:
macuyiko$ cat ./bibliography.bib
%%% mr4JHn2auF8J Theory, algorithms and applications
@article{bang2007theory,
title={Theory, algorithms and applications},
author={Bang-Jensen, J{\o}rgen and Gutin, Gregory},
journal={Springer Monographs in Mathematics, Springer-Verlag London Ltd., London},
year={2007},
publisher={Springer}
}
%%% Odfz1prcWxEJ Event Processing: Designing IT Systems for Agile Companies
@book{chandy2009event,
title={Event Processing: Designing IT Systems for Agile Companies},
author={Chandy, K and Schulte, W},
year={2009},
publisher={McGraw-Hill, Inc.}
}
Which is sufficient to import in a reference manager and edit/correct the remaining issues without much hassle.
By the Way…
I realize I might be an idiot and there might exist some free or expensive reference management software providing this functionality already. If you know about such magnificent tool, feel free to point this out in the comments or on Twitter (both the software name, not the fact that I’m an idiot, please).
Anyway, I hope this proves to be useful to anyone encountering the same problem. Also, fellow researchers, can we agree on the fact that the functionality offered by reference management software and scholarly websites is still pretty much utter crap? We live in the future, this should not be painful anymore.