Source code for langchain_community.utilities.bibtex
"""Util that calls bibtexparser."""importloggingfromtypingimportAny,Dict,List,Mappingfromlangchain_core.pydantic_v1importBaseModel,root_validatorlogger=logging.getLogger(__name__)OPTIONAL_FIELDS=["annotate","booktitle","editor","howpublished","journal","keywords","note","organization","publisher","school","series","type","doi","issn","isbn",]
[docs]classBibtexparserWrapper(BaseModel):"""Wrapper around bibtexparser. To use, you should have the ``bibtexparser`` python package installed. https://bibtexparser.readthedocs.io/en/master/ This wrapper will use bibtexparser to load a collection of references from a bibtex file and fetch document summaries. """classConfig:extra="forbid"@root_validator(pre=True)defvalidate_environment(cls,values:Dict)->Dict:"""Validate that the python package exists in environment."""try:importbibtexparser# noqaexceptImportError:raiseImportError("Could not import bibtexparser python package. ""Please install it with `pip install bibtexparser`.")returnvalues
[docs]defload_bibtex_entries(self,path:str)->List[Dict[str,Any]]:"""Load bibtex entries from the bibtex file at the given path."""importbibtexparserwithopen(path)asfile:entries=bibtexparser.load(file).entriesreturnentries
[docs]defget_metadata(self,entry:Mapping[str,Any],load_extra:bool=False)->Dict[str,Any]:"""Get metadata for the given entry."""publication=entry.get("journal")orentry.get("booktitle")if"url"inentry:url=entry["url"]elif"doi"inentry:url=f'https://doi.org/{entry["doi"]}'else:url=Nonemeta={"id":entry.get("ID"),"published_year":entry.get("year"),"title":entry.get("title"),"publication":publication,"authors":entry.get("author"),"abstract":entry.get("abstract"),"url":url,}ifload_extra:forfieldinOPTIONAL_FIELDS:meta[field]=entry.get(field)return{k:vfork,vinmeta.items()ifvisnotNone}