|
| 1 | +from collections import defaultdict |
| 2 | +from typing import Dict, List |
| 3 | + |
| 4 | +import bibtexparser |
| 5 | + |
| 6 | +# Define the categories to be printed in the README |
| 7 | +VALID_CATEGORIES = ["overview", "software", "paper", "uncategorized"] |
| 8 | + |
| 9 | +README_HEADER = """ |
| 10 | +Welcome to the Awesome Amortized Inference repository! |
| 11 | +This is a curated list of resources, including overviews, software, papers, and other resources related to amortized inference. |
| 12 | +Feel free to explore the entries below and use the provided BibTeX information for citation purposes. |
| 13 | +Contributioons always welcome, this shall be a community-driven project. |
| 14 | +Contribution guide will follow ASAP. |
| 15 | +""" |
| 16 | + |
| 17 | + |
| 18 | +# Define Entry class |
| 19 | +class Entry: |
| 20 | + def __init__(self, title: str, authors: str, url: str, category: str, bibtex: str): |
| 21 | + self.title = title |
| 22 | + self.authors = authors |
| 23 | + self.url = url |
| 24 | + self.category = category |
| 25 | + self.bibtex = bibtex |
| 26 | + |
| 27 | + @classmethod |
| 28 | + def from_bibtex(cls, entry: dict) -> "Entry": |
| 29 | + title = entry.get("title", "No title").replace("{", "").replace("}", "") |
| 30 | + authors = entry.get("author", "No author").replace(" and ", ", ") |
| 31 | + url = entry.get("url", "") |
| 32 | + category = entry.get("category", "uncategorized").lower() |
| 33 | + bibtex = cls.format_bibtex(entry) |
| 34 | + return cls(title, authors, url, category, bibtex) |
| 35 | + |
| 36 | + @staticmethod |
| 37 | + def format_bibtex(entry: dict) -> str: |
| 38 | + bibtex_type = entry.get("ENTRYTYPE", "misc") |
| 39 | + bibtex_key = entry.get("ID", "unknown") |
| 40 | + bibtex_fields = [ |
| 41 | + f" {key} = {{{value}}}" |
| 42 | + for key, value in entry.items() |
| 43 | + if key not in ["ENTRYTYPE", "ID"] |
| 44 | + ] |
| 45 | + bibtex_str = ( |
| 46 | + f"@{bibtex_type}{{{bibtex_key},\n " + ",\n ".join(bibtex_fields) + "\n}" |
| 47 | + ) |
| 48 | + return bibtex_str |
| 49 | + |
| 50 | + def to_string(self) -> str: |
| 51 | + entry_str = f"- **{self.title}**\n {self.authors}\n" |
| 52 | + if self.url: |
| 53 | + entry_str += f" [Link]({self.url})\n" |
| 54 | + entry_str += ( |
| 55 | + f" <details>\n" |
| 56 | + f" <summary>Show BibTeX</summary>\n" |
| 57 | + f""" <button onclick="var btn=this; navigator.clipboard.writeText(`{self.bibtex}`).then(function() {{ |
| 58 | + btn.textContent='Copied!'; |
| 59 | + setTimeout(function() {{ |
| 60 | + btn.textContent='Copy BibTeX to Clipboard'; |
| 61 | + }}, 2000); |
| 62 | + }});">Copy BibTeX</button>\n""" |
| 63 | + f"<pre><code>\n" |
| 64 | + f"{self.bibtex}\n" |
| 65 | + f"</code></pre>\n" |
| 66 | + f"</details>\n" |
| 67 | + ) |
| 68 | + entry_str += "\n" |
| 69 | + return entry_str |
| 70 | + |
| 71 | + |
| 72 | +def organize_entries(bib_database) -> Dict[str, List[Entry]]: |
| 73 | + entries_by_category: Dict[str, List[Entry]] = defaultdict(list) |
| 74 | + for entry in bib_database.entries: |
| 75 | + entry_obj = Entry.from_bibtex(entry) |
| 76 | + entries_by_category[entry_obj.category].append(entry_obj) |
| 77 | + return entries_by_category |
| 78 | + |
| 79 | + |
| 80 | +def create_readme(entries_by_category: Dict[str, List[Entry]]) -> str: |
| 81 | + readme_content = "# Awesome Amortized Inference\n\n" |
| 82 | + readme_content += README_HEADER |
| 83 | + |
| 84 | + for category in VALID_CATEGORIES: |
| 85 | + if category in entries_by_category: |
| 86 | + readme_content += f"## {category.capitalize()}\n\n" |
| 87 | + readme_content += "\n".join( |
| 88 | + [entry.to_string() for entry in entries_by_category[category]] |
| 89 | + ) |
| 90 | + readme_content += "\n" |
| 91 | + return readme_content |
| 92 | + |
| 93 | + |
| 94 | +def main(): |
| 95 | + try: |
| 96 | + with open("data.bib") as bibtex_file: |
| 97 | + bib_database = bibtexparser.load(bibtex_file) |
| 98 | + except FileNotFoundError: |
| 99 | + print("The data.bib file was not found.") |
| 100 | + exit(1) |
| 101 | + |
| 102 | + entries_by_category = organize_entries(bib_database) |
| 103 | + readme_content = create_readme(entries_by_category) |
| 104 | + |
| 105 | + with open("README.md", "w") as readme_file: |
| 106 | + readme_file.write(readme_content) |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == "__main__": |
| 110 | + main() |
0 commit comments