forked from GenSI-THUAIR/GenSI-THUAIR.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder.py
More file actions
209 lines (173 loc) · 5.03 KB
/
Copy pathbuilder.py
File metadata and controls
209 lines (173 loc) · 5.03 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
# /// script
# requires-python = ">=3.10"
# dependencies = [bibtexparser, pyyaml]
# ///
"""
/
- pub.bib
YML:
papers:
entries: # list[dict[str, str]]
- author
venue
title
year
paper
abstract
bib
[code]
[data]
[slides]
[video]
[poster]
tags: # dict[str, list[int]]
recent: [] # list[int]
other_tag:
"""
import os.path as osp
from datetime import datetime
# import json
# import shutil
import bibtexparser # bibtexparser
import yaml # pyyaml
ROOT = osp.dirname(__file__)
RECENT_YEAR = 2
PUB_BIB = osp.join(ROOT, "pub.bib")
PUB_YML = osp.join(ROOT, "_data", "publications.yaml")
PUB_JSON = osp.join(ROOT, "_data", "publications.json")
PUB_JSON_WEB = osp.join(ROOT, "assets", "publications.json")
NOW = datetime.now()
NOW_DATE = NOW.year * 12 + NOW.month
MONTH_DICT = {
"jan": 1,
"feb": 2,
"mar": 3,
"apr": 4,
"may": 5,
"jun": 6,
"jul": 7,
"aug": 8,
"sep": 9,
"oct": 10,
"nov": 11,
"dec": 12,
}
CLEAN = ["tags", "author+an", "thumbnail"]
REQUIRE = [
"author",
"booktitle",
"title",
"year",
"abstract",
"eprint", # requirement
"code",
"video",
"month", # optional
*CLEAN, # supplementary
]
MATCH_DICT = {
"Text-Generation": [
"Text Generation",
"generation",
"generating",
"machine translation",
],
"Drug-Discovery": ["Drug Discovery", "drug", "protein"],
"Non-Autoregressive-Generation": [
"Non-Autoregressive Generation",
"non-autoregressive",
],
}
# "author+an", ,"student"
def get_month_num(month: str) -> int:
return MONTH_DICT.get(month, 1)
def valid_recent(year: int, month):
if isinstance(month, str):
try:
month = int(month)
except ValueError:
month = get_month_num(month)
paper_date = year * 12 + month
return (NOW_DATE - paper_date) < 24
def highlight_author(info: str, author: str):
if ";" in info:
parts = [
claim.strip().split("=") # id, type
for claim in info.split(";")
if not (claim.isspace() or len(claim) == 0)
]
else:
parts = [info.split("=")]
author_list = [au.strip() for au in author.split("and")]
for seq, type in parts:
_seq = int(seq) - 1
if type == "student":
pass # TODO: how to highlight student?
elif type == "highlight":
author_list[_seq] = f"<b class='highlight'>{author_list[_seq]}</b>"
if len(author_list) > 1:
author_list[-1] = f"and {author_list[-1]}"
return ", ".join(author_list)
def match_tag(entry: dict, tag: str):
if "title" in entry and entry["title"].find(tag) != -1:
return True
elif "abstract" in entry and entry["abstract"].find(tag) != -1:
return True
else:
return False
def tag_by_match(info_dict: dict, idx: int, entry: dict):
"""Search keyword in title and abstract"""
for key, val in MATCH_DICT.items():
if any(match_tag(entry, t) for t in val):
if key not in info_dict:
info_dict[key] = []
info_dict[key].append(idx)
writer = bibtexparser.bwriter.BibTexWriter()
def cleanup_bibtex(parser_entry: dict) -> str:
for key in CLEAN:
if key in parser_entry:
parser_entry.pop(key)
return writer._entry_to_bibtex(parser_entry)
def update_tags(
group: dict[str, list[int]], idx: int, entry: dict, enable_manual: bool = True
):
"""Update tags for each entry"""
# ignore recent papers
# if valid_recent(entry["year"], entry.get("month", 1)):
# if "recent" not in group:
# group["recent"] = []
# group["recent"].append(idx)
if "tags" in entry:
tag_in_entry = [t.strip() for t in entry["tags"].split(",")]
for tag in tag_in_entry:
if tag not in group:
group[tag] = []
group[tag].append(idx)
elif enable_manual:
tag_by_match(group, idx, entry)
def build_pubs(root: str, storage: str):
with open(root) as fd:
bib_db = bibtexparser.load(fd)
entries: list[dict[str, str | int]] = []
for entry in bib_db.entries:
bib = {key: entry[key] for key in REQUIRE if key in entry}
bib["bib"] = cleanup_bibtex(entry)
bib["year"] = int(bib["year"])
if "eprint" in bib:
bib["paper"] = bib.pop("eprint")
entries.append(bib)
tag_group: dict[str, list[int]] = {}
for idx, entry in enumerate(entries):
update_tags(tag_group, idx, entry, False)
entry["author"] = highlight_author(entry["author+an"], entry["author"])
with open(storage, "w") as fd:
result = {
"entries": entries,
"tags": tag_group,
}
# json.dump(result, fd)
yaml.safe_dump(result, fd, sort_keys=False)
if __name__ == "__main__":
build_pubs(PUB_BIB, PUB_YML)
# build_pubs(PUB_BIB, PUB_JSON)
# shutil.copyfile(PUB_JSON, PUB_JSON_WEB)