Skip to content

Instantly share code, notes, and snippets.

@raven4752
Last active September 12, 2018 06:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raven4752/10cb1cd3a6ce657dfa369f7acbc45c3b to your computer and use it in GitHub Desktop.
Save raven4752/10cb1cd3a6ce657dfa369f7acbc45c3b to your computer and use it in GitHub Desktop.
turn bibtex string into apa formatted citation string. the authors' names are concatenated by commas. First name and mid name are shortened.
import bibtexparser
from bibtexparser.bparser import BibTexParser
from bibtexparser.customization import convert_to_unicode
def parse_author(auth_str):
auth_str_authors = auth_str.split(" and ")
authors = []
for author in auth_str_authors:
if "," in author:
author_parts = author.split(", ")
last_name = author_parts.pop(0)
new_name = []
for name in author_parts:
if '-' in name:
sub_name = name.split('-')
new_sub_name = []
for s in sub_name:
new_sub_name.append(s[0].upper() + '.')
new_name.append('-'.join(new_sub_name))
elif ' ' in name:
sub_name = name.split(' ')
new_sub_name = []
for s in sub_name:
new_sub_name.append(s[0].upper() + '.')
new_name.append(' '.join(new_sub_name))
else:
new_name.append(name[0].upper())
new_name.append(last_name)
authorfirstlast = " ".join(new_name)
authors.append(authorfirstlast)
else:
if author != 'other' and author != 'others':
authors.append(author)
return ', '.join(authors)
def parse_title(data):
entrytype = data['ENTRYTYPE']
if entrytype != 'phdthesis':
return data['title']
else:
if 'school' in data:
school = ', ' + data['school']
else:
school = ''
title = data['title'] + ' (Doctoral dissertation' + school + ')'
return title
def bib_to_apa(bib_database):
parser = BibTexParser()
parser.customization = convert_to_unicode
bib_data = bibtexparser.loads(bib_database, parser=parser)
bib_database = bib_data.entries[0]
try:
year = ' (' + bib_database['year'] + ')'
except:
year = ''
authors = parse_author(bib_database['author'])
title = parse_title(bib_database)
try:
pages = bib_database['pages']
except:
pages = ''
try:
number = '(' + bib_database['number'] + ')'
except:
number = ''
try:
volume = bib_database['volume']
except:
volume = ''
vol_num= volume + number
try:
paper_from = 'In ' + bib_database['booktitle']
except:
paper_from = None
if paper_from is None:
try:
paper_from = bib_database['journal']
except:
paper_from = ''
detail_list = []
if paper_from != '':
detail_list.append(paper_from)
if vol_num != '':
detail_list.append(vol_num)
if pages != '' and 'booktitle' not in bib_database:
detail_list.append(pages)
if len(detail_list) > 0:
detail = ', '.join(detail_list)
else:
detail = ''
if pages != '' and 'booktitle' in bib_database:
detail+=' (pp. '+pages+')'
field_list = [authors + year, title]
if detail != '':
field_list.append(detail)
field_list.append('')
return '. '.join(field_list)
if __name__ == '__main__':
print(bib_to_apa("""@article{yang2018target,
title={Target guiding self-avoiding random walk with intersection algorithm for minimum exposure path problem in wireless sensor networks},
author={Yang, Tinghong and Jiang, Dali and Fang, Haiyang and Tan, Mian and Xie, Li and Zhao, Jing},
journal={EURASIP Journal on Wireless Communications and Networking},
volume={2018},
number={1},
pages={33},
year={2018},
publisher={Nature Publishing Group}
}
"""))
print(bib_to_apa("""@inproceedings{duong2017temporal,
title={Temporal non-volume preserving approach to facial age-progression and age-invariant face recognition},
author={Duong, Chi Nhan and Quach, Kha Gia and Luu, Khoa and Le, T Hoang Ngan and Savvides, Marios},
booktitle={Computer Vision (ICCV), 2017 IEEE International Conference on},
pages={3755--3763},
year={2017},
organization={IEEE}
}
"""))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment