Last active
April 3, 2024 13:16
-
-
Save arahatashun/2d27d5e2694c3f1f25aa5cd624defe5f to your computer and use it in GitHub Desktop.
Abbreviate Journal Names in Bibtex Database.py
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
import sys, os | |
import re | |
try: | |
bibtexdb = open(sys.argv[1]).read() | |
except: | |
print("Error: specify the file to be processed!") | |
FILE_NAME = "journalList.txt" | |
url = "https://gist.githubusercontent.com/FilipDominec/6df14b3424e335c4a47a96640f7f0df9/raw/74876d2d5df9ed60492ef3a14dc3599a6a6a9cfc/journalList.txt" | |
if not os.path.isfile(FILE_NAME): | |
import urllib.request | |
urllib.request.urlretrieve(url, FILE_NAME) | |
rulesfile = open(FILE_NAME) | |
for rule in rulesfile.readlines()[::-1]: ## reversed alphabetical order matches extended journal names first | |
pattern1, pattern2 = rule.strip().split(" = ") | |
# 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, 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