Last active
October 15, 2023 08:13
-
-
Save FilipDominec/9ff081952dbc4aae1df657a56c3db4ea 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 -*- | |
# Supporting Python 3 | |
import sys, os, 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'" |
I am glad you found it useful. Should I review your changes and possibly
sync them with the original script?
…On Thu, 28 Jan 2021 at 08:47, Shun Arahata ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
This tool is really useful. I fix some parts of the code in order to run
my Python 3 environment.
https://gist.github.com/arahatashun/2d27d5e2694c3f1f25aa5cd624defe5f
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/9ff081952dbc4aae1df657a56c3db4ea#gistcomment-3610691>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAD47TTZ3VPD2WU4GU6UYHLS4EJAHANCNFSM4KIYT7JQ>
.
I found @trevismd's python3 version of code after I posted my code so I deleted mine.
Anyway, providing python3 code explicitly will be helpful for people who come up with this gist.
By the way, what is journalList.txt based on? A journal I try to submit to requires to follow ISSN List of Title Word Abbreviations so I want to know that.
It seems the list is based on JabRef.
@FilipDominec, feel free to reuse any part of my contribution.
For my own use, I took the code posted by @arahatashun and updated it to pull the latest version of the JabRef lists, which have moved to abbrv.jabref.org and switched to CSV files. It can be found here, in case anyone is interested.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am happy that you find this little script useful. Perhaps should I somehow sync all the improvements in at least 4 modified forks?