File size: 6,423 Bytes
8fcd0f0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pdf2bib"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def extract_metadata(file_path):\n",
" pdfextractdata = pdf2bib.pdf2bib(file_path)\n",
" #st.write(pdfextractdata)\n",
" pdfextractdata_metadata = {} if pdfextractdata.get('metadata', {}) is None else pdfextractdata.get('metadata', {})\n",
"\n",
" return pdfextractdata_metadata"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"[pdf2bib]: Trying to extract data to generate the BibTeX entry for the file: boiteau-et-al-2024-relating-molecular-properties-to-the-persistence-of-marine-dissolved-organic-matter-with-liquid.pdf\n",
"[pdf2bib]: Calling pdf2doi...\n",
"[pdf2doi]: Trying to retrieve a DOI/identifier for the file: boiteau-et-al-2024-relating-molecular-properties-to-the-persistence-of-marine-dissolved-organic-matter-with-liquid.pdf\n",
"[pdf2doi]: Method #1: Looking for a valid identifier in the document infos...\n",
"[pdf2doi]: Validating the possible DOI 10.1021/acs.est.3c08245 via a query to dx.doi.org...\n",
"[pdf2doi]: The DOI 10.1021/acs.est.3c08245 is validated by dx.doi.org.\n",
"[pdf2doi]: A valid DOI was found in the document info labelled '/prism:doi'.\n",
"[pdf2bib]: pdf2doi found a valid identifier for this paper.\n",
"[pdf2bib]: Parsing the info returned by dx.doi.org...\n",
"[pdf2bib]: A valid BibTeX entry was generated.\n"
]
}
],
"source": [
"a = pdf2bib.pdf2bib(\"boiteau-et-al-2024-relating-molecular-properties-to-the-persistence-of-marine-dissolved-organic-matter-with-liquid.pdf\")"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'@article{boiteau2024relating,\\n\\ttitle = {Relating Molecular Properties to the Persistence of Marine Dissolved Organic Matter with Liquid Chromatography–Ultrahigh-Resolution Mass Spectrometry},\\n\\tpublisher = {American Chemical Society (ACS)},\\n\\turl = {http://dx.doi.org/10.1021/acs.est.3c08245},\\n\\tdoi = {10.1021/acs.est.3c08245},\\n\\tjournal = {Environmental Science & Technology},\\n\\tyear = {2024},\\n\\tmonth = {2},\\n\\tauthor = {Rene M. Boiteau and Yuri E. Corilo and William R. Kew and Christian Dewey and Maria Cristina Alvarez Rodriguez and Craig A. Carlson and Tim M. Conway}\\n}'"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.get(\"bibtex\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"import bibtexparser"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"parser = bibtex.Parser()"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
"parser = bibtexparser.bparser.BibTexParser(common_strings=True)\n",
"bib_database = bibtexparser.loads(a.get(\"bibtex\"), parser=parser)"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
"def format_author_names(authors_str):\n",
" authors = authors_str.split(' and ')\n",
" formatted_authors = []\n",
" for author in authors:\n",
" parts = author.split()\n",
" if len(parts) == 2: # Simple case: First Last\n",
" last, first = parts[1], parts[0]\n",
" formatted_authors.append(f\"{last}, {first[0]}.\")\n",
" elif len(parts) > 2: # Handling middle names or initials\n",
" last = parts[-1]\n",
" initials = ''.join(f\"{part[0]}.\" for part in parts[:-1])\n",
" formatted_authors.append(f\"{last}, {initials}\")\n",
" if len(formatted_authors) > 1:\n",
" formatted_authors_str = ', '.join(formatted_authors[:-1]) + ', & ' + formatted_authors[-1]\n",
" else:\n",
" formatted_authors_str = formatted_authors[0]\n",
" return formatted_authors_str"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"def format_apa(entry):\n",
" author = format_author_names(entry.get('author', ''))\n",
" year = entry.get('year', '')\n",
" title = entry.get('title', '')\n",
" journal = entry.get('journal', '')\n",
" volume = entry.get('volume', '')\n",
" issue = entry.get('issue', '')\n",
" pages = entry.get('page', '').replace('-', '–') # En dash for page range\n",
" doi = entry.get('doi', '')\n",
" \n",
" # Constructing the citation\n",
" apa_citation = f\"{author} {title}. {journal} {volume}, {pages} ({year}). https://doi.org/{doi}\"\n",
" return apa_citation"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Boiteau, R.M., Corilo, Y.E., Kew, W.R., Dewey, C., Rodriguez, M.C.A., Carlson, C.A., & Conway, T.M. Relating Molecular Properties to the Persistence of Marine Dissolved Organic Matter with Liquid Chromatography–Ultrahigh-Resolution Mass Spectrometry. Environmental Science & Technology , (2024). https://doi.org/10.1021/acs.est.3c08245'"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"format_apa(bib_database.entries[0])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|