Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save yoavram/888dee944346f3a26d22fd328db3c868 to your computer and use it in GitHub Desktop.

Select an option

Save yoavram/888dee944346f3a26d22fd328db3c868 to your computer and use it in GitHub Desktop.

Revisions

  1. Yoav Ram revised this gist Sep 25, 2017. 1 changed file with 7 additions and 4 deletions.
    11 changes: 7 additions & 4 deletions Abbreviate Journal Names in Bibtex Database.py
    Original file line number Diff line number Diff line change
    @@ -4,12 +4,15 @@
    import sys, os
    import re

    try: bibtexdb = open(sys.argv[1]).read()
    except: print "Error: specify the file to be processed!"
    try:
    bibtexdb = open(sys.argv[1]).read()
    except IndexError:
    print("Error: specify the file to be processed!")
    exit()

    if not os.path.isfile('journalList.txt'):
    import urllib
    urllib.urlretrieve("https://raw.githubusercontent.com/JabRef/jabref/master/src/main/resources/journals/journalList.txt",
    import urllib.request
    urllib.request.urlretrieve("https://raw.githubusercontent.com/JabRef/jabref/master/src/main/resources/journals/journalList.txt",
    filename="journalList.txt")
    rulesfile = open('journalList.txt')

  2. Yoav Ram revised this gist Sep 25, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Abbreviate Journal Names in Bibtex Database.py
    Original file line number Diff line number Diff line change
    @@ -20,8 +20,8 @@
    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)
    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'"
    print("Bibtex database with abbreviated files saved into 'abbreviated.bib'")
  3. @peci1 peci1 revised this gist May 10, 2016. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions Abbreviate Journal Names in Bibtex Database.py
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@
    #-*- coding: utf-8 -*-

    import sys, os
    import re

    try: bibtexdb = open(sys.argv[1]).read()
    except: print "Error: specify the file to be processed!"
    @@ -15,10 +16,12 @@
    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
    print "Replacing '%s' FOR '%s'" % (pattern1, pattern2)
    #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 = repl.sub(pattern2, bibtexdb)
    (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'"
  4. @FilipDominec FilipDominec revised this gist Apr 26, 2016. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions Abbreviate Journal Names in Bibtex Database.py
    Original file line number Diff line number Diff line change
    @@ -14,8 +14,11 @@

    for rule in rulesfile.readlines()[::-1]: ## reversed alphabetical order matches extended journal names first
    pattern1, pattern2 = rule.strip().split(" = ")
    print "Replacing '%s' FOR '%s'" % (pattern1, pattern2)
    bibtexdb = bibtexdb.replace(pattern1.strip(), pattern2.strip())
    if pattern1 != pattern1.upper() and (' ' in pattern1): ## avoid mere abbreviations
    print "Replacing '%s' FOR '%s'" % (pattern1, pattern2)
    #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 = repl.sub(pattern2, bibtexdb)
    with open('abbreviated.bib', 'w') as outfile:
    outfile.write(bibtexdb)
    print "Bibtex database with abbreviated files saved into 'abbreviated.bib'"
  5. @FilipDominec FilipDominec revised this gist Apr 7, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Abbreviate Journal Names in Bibtex Database.py
    Original file line number Diff line number Diff line change
    @@ -18,4 +18,4 @@
    bibtexdb = bibtexdb.replace(pattern1.strip(), pattern2.strip())
    with open('abbreviated.bib', 'w') as outfile:
    outfile.write(bibtexdb)
    print "Bibtex database with abbreviated files aved into abbreviated.bib"
    print "Bibtex database with abbreviated files saved into 'abbreviated.bib'"
  6. @FilipDominec FilipDominec revised this gist Apr 7, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Abbreviate Journal Names in Bibtex Database.py
    Original file line number Diff line number Diff line change
    @@ -18,4 +18,4 @@
    bibtexdb = bibtexdb.replace(pattern1.strip(), pattern2.strip())
    with open('abbreviated.bib', 'w') as outfile:
    outfile.write(bibtexdb)
    print "Bibtex database with abbreviated files aved into abbreviated.bib
    print "Bibtex database with abbreviated files aved into abbreviated.bib"
  7. @FilipDominec FilipDominec revised this gist Apr 7, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Abbreviate Journal Names in Bibtex Database.py
    Original file line number Diff line number Diff line change
    @@ -17,5 +17,5 @@
    print "Replacing '%s' FOR '%s'" % (pattern1, pattern2)
    bibtexdb = bibtexdb.replace(pattern1.strip(), pattern2.strip())
    with open('abbreviated.bib', 'w') as outfile:
    outfile.write(bibtexfile)
    outfile.write(bibtexdb)
    print "Bibtex database with abbreviated files aved into abbreviated.bib
  8. @FilipDominec FilipDominec created this gist Apr 7, 2016.
    21 changes: 21 additions & 0 deletions Abbreviate Journal Names in Bibtex Database.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    #!/usr/bin/env python
    #-*- coding: utf-8 -*-

    import sys, os

    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(" = ")
    print "Replacing '%s' FOR '%s'" % (pattern1, pattern2)
    bibtexdb = bibtexdb.replace(pattern1.strip(), pattern2.strip())
    with open('abbreviated.bib', 'w') as outfile:
    outfile.write(bibtexfile)
    print "Bibtex database with abbreviated files aved into abbreviated.bib