Forked from FilipDominec/Abbreviate Journal Names in Bibtex Database.py
Last active
January 28, 2021 07:22
-
-
Save peci1/4e67f3d0521ce014fc952bcca664b37d 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 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 -*- | |
import sys, os | |
import re | |
try: bibtexdb = open(sys.argv[1]).read() | |
except: print "Error: specify the file to be processed!" | |
if not os.path.isfile('journalList.txt'): | |
import urllib | |
urllib.urlretrieve("https://raw.githubusercontent.com/JabRef/jabref/master/src/main/resources/journals/journalList.txt", | |
filename="journalList.txt") | |
rulesfile = open('journalList.txt') | |
for rule in rulesfile.readlines()[::-1]: ## reversed alphabetical order matches extended journal names first | |
pattern1, pattern2 = rule.strip().split(" = ") | |
if pattern1 != pattern1.upper() and (' ' in pattern1): ## avoid mere abbreviations | |
#bibtexdb = bibtexdb.replace(pattern1.strip(), pattern2.strip()) ## problem - this is case sensitive | |
repl = re.compile(re.escape(pattern1), re.IGNORECASE) ## this is more robust, although ca. 10x slower | |
(bibtexdb, num_subs) = repl.subn(pattern2, bibtexdb) | |
if num_subs > 0: | |
print "Replacing '%s' FOR '%s'" % (pattern1, pattern2) | |
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