Forked from trevismd/Abbreviate Journal Names in Bibtex Database.py
Created
August 3, 2020 03:26
-
-
Save chasemc/9a94db6082c62f06cf53cbc38f6c17b1 to your computer and use it in GitHub Desktop.
Using the translation table from the Jabref program, finds and replaces all scientific journal names to their standardized abbreviated form. First argument is the file to be processed; outputs safely to 'abbreviated.bib'
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#-*- coding: utf-8 -*- | |
# Supporting Python 3 and JabRef csv file | |
import os | |
import re | |
import sys | |
try: | |
bibtexdb = open(sys.argv[1]).read() | |
except IndexError: | |
print("Error: specify the file to be processed!") | |
exit(1) | |
except FileNotFoundError: | |
print("Error: Provided filename could not be loaded!") | |
exit(1) | |
if not os.path.isfile('journalList.csv'): | |
import urllib.request | |
urllib.request.urlretrieve( | |
"https://raw.githubusercontent.com/JabRef/jabref/master/src/main/" | |
"resources/journals/journalList.csv", | |
filename="journalList.csv") | |
rulesfile = open('journalList.csv') | |
for rule in rulesfile.readlines(): | |
patterns = rule.split(";")[:2] | |
pattern1, pattern2 = map(lambda pattern: "Journal = {%s}" % pattern, | |
patterns) | |
# avoid mere abbreviations | |
if pattern1 != pattern1.upper() and (' ' in pattern1): | |
repl = re.compile(re.escape(pattern1), re.IGNORECASE) | |
(bibtexdb, num_subs) = repl.subn(pattern2, bibtexdb) | |
if num_subs > 0: | |
print("Replaced (%ix) '%s' FOR '%s'" % (num_subs, *patterns)) | |
with open('abbreviated.bib', 'w') as outfile: | |
outfile.write(bibtexdb) | |
print("Bibtex database with abbreviated files saved into 'abbreviated.bib'") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment