Last active
May 25, 2023 20:02
-
-
Save bbuenz/ad07236578508bb6fc4865d01099f49d to your computer and use it in GitHub Desktop.
Script to convert bib entries to matching cryptobib entries
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 bibtexparser | |
from bibtexparser.bibdatabase import BibDatabase | |
from pybtex.database import parse_file | |
from pybtex.database import BibliographyData, Entry | |
from pybtex.utils import OrderedCaseInsensitiveDict | |
import re | |
if __name__ == '__main__': | |
# with open('bitcoin.bib') as bibfile: | |
# bib_database : BibDatabase=bibtexparser.load(bibfile) | |
crypto_data: BibliographyData = parse_file("crypto.bib", bib_format="bibtex") | |
crypto_entries: OrderedCaseInsensitiveDict = crypto_data.entries | |
titleKeyMap = {} | |
for key in crypto_entries: | |
if "title" not in crypto_entries[key].fields: | |
print(crypto_entries[key]) | |
continue | |
title = crypto_entries[key].fields["title"] | |
newtitle = re.sub(r'\W+', '', title.lower()) | |
#titleprefix = ' '.join(title.split()[0:3]) | |
if newtitle in titleKeyMap: | |
old_entry: Entry = crypto_entries[titleKeyMap[newtitle]] | |
new_entry: Entry = crypto_entries[key] | |
#prefer journal > conference > eprint | |
if old_entry.type == "article" or (old_entry.type == "inproceedings" and new_entry.type != "article"): | |
continue | |
titleKeyMap[newtitle] = key | |
bitcoin_data: BibliographyData = parse_file("bitcoin.bib", bib_format="bibtex") | |
bitcoin_entries = bitcoin_data.entries | |
commandlist=[] | |
for key in bitcoin_entries: | |
title = bitcoin_entries[key].fields["title"] | |
newtitle = re.sub(r'\W+', '', title.lower()) | |
if newtitle in titleKeyMap: | |
#print(bitcoin_entries[key].fields["title"]) | |
#print(crypto_entries[titleKeyMap[title]].fields["title"]) | |
command = "%s/" + key + "/" + titleKeyMap[newtitle] + "/ge" | |
commandlist.append(command) | |
print(command) | |
print(' | '.join(commandlist)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment