1"""Build paper/references.bib from literature/references_verified.csv (only verified entries)."""2import re, pandas as pd3from src.common import ROOT, get_logger4log = get_logger("bib")56def tex(s): return str(s).replace("&", "&").replace("\\&", "&").replace("&", "\\&") if isinstance(s, str) else ""78def main():9 df = pd.read_csv(ROOT / "literature" / "references_verified.csv"); df = df[df.verified == True]10 out = []11 for _, r in df.iterrows():12 title = tex(r.crossref_title if isinstance(r.crossref_title, str) and r.crossref_title else r.title).replace("‐", "-")13 authors = r.authors if isinstance(r.authors, str) and r.authors else r.author14 authors = " and ".join(a.strip() for a in str(authors).split(";") if a.strip()) if ";" in str(authors) else str(authors)15 yr = int(r.pub_year) if pd.notna(r.pub_year) else int(r.year)16 typ = str(r.type) if isinstance(r.type, str) else "journal-article"17 if typ == "report" or r.key == "competitionbureau2023":18 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")19 continue20 if typ == "posted-content": # preprint / working paper (SSRN): cite as unpublished work with DOI21 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")22 continue23 if typ in ("book", "monograph"):24 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")25 continue26 fields = [f" author = {{{authors}}}", f" title = {{{title}}}", f" journal = {{{tex(r.journal)}}}", f" year = {{{yr}}}"]27 if pd.notna(r.volume) and str(r.volume) != "nan": fields.append(f" volume = {{{str(r.volume).replace('.0', '')}}}")28 if pd.notna(r.issue) and str(r.issue) != "nan": fields.append(f" number = {{{str(r.issue).replace('.0', '')}}}")29 if pd.notna(r.pages) and str(r.pages) != "nan": fields.append(f" pages = {{{str(r.pages).replace('-', '--')}}}")30 if isinstance(r.doi, str) and r.doi: fields.append(f" doi = {{{r.doi}}}")31 elif isinstance(r.note, str) and "jstor" in r.note: fields.append(f" note = {{{re.search(r'https?://[^ ]+', r.note).group(0)}}}")32 out.append(f"@article{{{r.key},\n" + ",\n".join(fields) + "\n}\n")33 (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))34 log.info("wrote %d entries", len(out))3536if __name__ == "__main__":37 main()38