function get_bibtext
Retrieves and parses BibTeX citation data for a given DOI (Digital Object Identifier), extracting the title and formatted BibTeX string.
/tf/active/vicechatdev/offline_parser_docstore.py
54 - 69
moderate
Purpose
This function fetches bibliographic information in BibTeX format for academic papers using their DOI. It calls an external function get_bib() to retrieve the raw BibTeX data, then parses it using bibtexparser to extract the title and return both the formatted BibTeX string and the paper title. It handles errors gracefully by returning 'Not available' and 'NA' when the resource is not found or parsing fails.
Source Code
def get_bibtext(doi):
#print("doi in ",doi)
bibtex=get_bib(doi)
if bibtex=='Resource not found.':
return 'Not available','NA'
#print("bibtex out ",bibtex)
try:
bibtex = bibtexparser.parse_string(bibtex)
#print(bibtex.entries)
title=bibtex.entries[0].get('title').value
bibtex = bibtexparser.write_string(bibtex)
return bibtex,title.replace("'","`")
except Exception as e:
print(e)
return 'Not available','NA'
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
doi |
- | - | positional_or_keyword |
Parameter Details
doi: A string containing the Digital Object Identifier (DOI) for an academic publication. This is a unique alphanumeric string assigned to academic papers (e.g., '10.1234/example.doi'). The function uses this to retrieve bibliographic information.
Return Value
Returns a tuple of two strings: (bibtex_string, title). The first element is the formatted BibTeX citation string, and the second is the paper title with single quotes replaced by backticks. If the resource is not found or an error occurs, returns ('Not available', 'NA').
Dependencies
bibtexparser
Required Imports
import bibtexparser
Usage Example
# Assuming get_bib() function is available
import bibtexparser
# Example usage
doi = '10.1038/nature12373'
bibtex_citation, paper_title = get_bibtext(doi)
if bibtex_citation != 'Not available':
print(f'Title: {paper_title}')
print(f'BibTeX:\n{bibtex_citation}')
else:
print('Citation not available for this DOI')
Best Practices
- Always check if the returned values are 'Not available' and 'NA' before using them in downstream processing
- The function replaces single quotes with backticks in titles to avoid string formatting issues - be aware of this transformation
- Depends on an external get_bib() function that must be implemented separately to fetch BibTeX data from DOI
- Error handling is broad (catches all exceptions) - consider logging specific errors for debugging
- The function assumes bibtex.entries[0] exists after parsing - ensure the BibTeX string contains at least one entry
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_bib 74.7% similar
-
function parse_references_section 49.4% similar
-
class ReferenceManager_v3 40.4% similar
-
class ReferenceManager_v2 40.3% similar
-
class ReferenceManager_v4 39.9% similar