Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save georg-wolflein/bf503388dfe34d642c98225301e1c24e to your computer and use it in GitHub Desktop.
Save georg-wolflein/bf503388dfe34d642c98225301e1c24e to your computer and use it in GitHub Desktop.
Convert a list of authors from bibtex format to paperpile format
def convert_bibtex_author_list_to_paperpile(authors, last_name_indicators=("el", "van", "vaan", "de")):
"""
Convert a list of authors in bibtex format to paperpile format.
:param authors: list of authors in bibtex format
:param last_name_indicators: list of words that indicate the start of a last name
:return: list of authors in paperpile format
Example:
>>> bibtex_authors = "Georg Wölflein and Ludwig van Beethoven and Johann Sebastian Bach"
>>> convert_bibtex_author_list_to_paperpile(bibtex_authors)
'Wölflein, Georg; van Beethoven, Ludwig; Bach, Johann Sebastian'
Note that "van" is correctly interpreted as part of the last name.
"""
authors = [
a.strip().split(" ") for a in authors.split(" and ")
] # list of lists; each list is a name split into words
# split into first and last names
last_name_start_index = [
max(i if x.lower() in last_name_indicators else -1 for i, x in enumerate(a)) for a in authors
]
first_names = [" ".join(a[: last_name_start_index[i]]) for i, a in enumerate(authors)]
last_names = [" ".join(a[last_name_start_index[i] :]) for i, a in enumerate(authors)]
return "; ".join(f"{last_name}, {first_name}" for last_name, first_name in zip(last_names, first_names))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment