"""Build paper/references.bib from literature/references_verified.csv (only verified entries).""" import re, pandas as pd from src.common import ROOT, get_logger log = get_logger("bib") def tex(s): return str(s).replace("&", "&").replace("\\&", "&").replace("&", "\\&") if isinstance(s, str) else "" def main(): df = pd.read_csv(ROOT / "literature" / "references_verified.csv"); df = df[df.verified == True] out = [] for _, r in df.iterrows(): title = tex(r.crossref_title if isinstance(r.crossref_title, str) and r.crossref_title else r.title).replace("‐", "-") authors = r.authors if isinstance(r.authors, str) and r.authors else r.author authors = " and ".join(a.strip() for a in str(authors).split(";") if a.strip()) if ";" in str(authors) else str(authors) yr = int(r.pub_year) if pd.notna(r.pub_year) else int(r.year) typ = str(r.type) if isinstance(r.type, str) else "journal-article" if typ == "report" or r.key == "competitionbureau2023": out.append(f"@techreport{{{r.key},\n author = {{{authors}}},\n title = {{{title}}},\n institution = {{Competition Bureau Canada}},\n year = {{{yr}}},\n url = {{https://competition-bureau.canada.ca}}\n}}\n") continue if typ == "posted-content": # preprint / working paper (SSRN): cite as unpublished work with DOI out.append(f"@unpublished{{{r.key},\n author = {{{authors}}},\n title = {{{title}}},\n note = {{Working paper, SSRN}},\n year = {{{yr}}},\n doi = {{{r.doi}}}\n}}\n") continue if typ in ("book", "monograph"): out.append(f"@book{{{r.key},\n author = {{{authors}}},\n title = {{{title}}},\n publisher = {{{tex(r.publisher)}}},\n year = {{{yr}}}" + (f",\n doi = {{{r.doi}}}" if isinstance(r.doi, str) else "") + "\n}\n") continue fields = [f" author = {{{authors}}}", f" title = {{{title}}}", f" journal = {{{tex(r.journal)}}}", f" year = {{{yr}}}"] if pd.notna(r.volume) and str(r.volume) != "nan": fields.append(f" volume = {{{str(r.volume).replace('.0', '')}}}") if pd.notna(r.issue) and str(r.issue) != "nan": fields.append(f" number = {{{str(r.issue).replace('.0', '')}}}") if pd.notna(r.pages) and str(r.pages) != "nan": fields.append(f" pages = {{{str(r.pages).replace('-', '--')}}}") if isinstance(r.doi, str) and r.doi: fields.append(f" doi = {{{r.doi}}}") elif isinstance(r.note, str) and "jstor" in r.note: fields.append(f" note = {{{re.search(r'https?://[^ ]+', r.note).group(0)}}}") out.append(f"@article{{{r.key},\n" + ",\n".join(fields) + "\n}\n") (ROOT / "paper" / "references.bib").write_text("% Generated from literature/references_verified.csv — every entry verified via Crossref or a stable URL\n" + "\n".join(out)) log.info("wrote %d entries", len(out)) if __name__ == "__main__": main()